Example usage for org.apache.commons.math3.distribution NormalDistribution probability

List of usage examples for org.apache.commons.math3.distribution NormalDistribution probability

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution NormalDistribution probability.

Prototype

@Override
public double probability(double x0, double x1) throws NumberIsTooLargeException 

Source Link

Usage

From source file:cloudnet.examples.elasticity.bn.DistrHelper.java

private static double[] getWorkloadDistrByAverageValue(double time) {

    double[] result = new double[WorkloadLevel.AllWithOverusage().length];
    int index = 0;
    double levelStep = (100.0 / WorkloadLevel.All().length);

    // get percent
    double percent = (100 * time) / (double) MaxSlaViolationTime;
    //specify distr
    NormalDistribution d = new NormalDistribution(percent / 2, percent / 6);

    // get probabilites
    double[] probabilities = new double[WorkloadLevel.AllWithOverusage().length];
    double maxp = 0.0d;
    for (int l = 0; l < WorkloadLevel.AllWithOverusage().length - 1; l++) {
        double p = d.probability(l * levelStep, (l + 1) * levelStep);
        probabilities[l] = p;/*from  ww  w .  j a  va2  s. c  o m*/
        maxp = FastMath.max(maxp, p);
    }
    probabilities[WorkloadLevel.AllWithOverusage().length - 1] = d
            .probability(WorkloadLevel.AllWithOverusage().length * levelStep, Double.POSITIVE_INFINITY);

    if (maxp != 0.0d) {

        // and insert their normalized values into result array 
        double sum = 1.0d;
        for (int l = 0; l < probabilities.length - 1; l++) {
            double newValue = FastMath.min(sum, probabilities[l] / maxp);
            result[index++] = newValue;
            sum -= newValue;
        }
        result[index++] = sum;
        Ensure.GreaterThanOrEquals(sum, 0d, "sum of probabilities");
    } else {
        // if no max probability found, just put 1.0 for the closest level
        // let say, if percent = 42 and there are 10 levels, levelIndex = would be 4
        int levelIndex = (int) FastMath.floor(percent / levelStep);
        if (levelIndex > WorkloadLevel.All().length) {
            levelIndex = WorkloadLevel.AllWithOverusage().length - 1;
        }
        Ensure.GreaterThanOrEquals(levelIndex, 0, "levelIndex");
        for (int l = 0; l < WorkloadLevel.AllWithOverusage().length; l++) {
            if (l == levelIndex) {
                result[index++] = 1.0d;
            } else {
                result[index++] = 0.0d;
            }
        }
    }

    return result;
}

From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java

@VisibleForTesting
List<DataTreeNode> modelFitAnomalyDetection(long targetEpoch, int numObservations, boolean doubleToLongBits,
        boolean raw, double percentile, int minMeasurement) {
    int measurement;
    int count = 0;
    int min = Integer.MAX_VALUE;

    if (targetEpoch < 0) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (numObservations <= 0) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (reservoir == null) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (targetEpoch < minEpoch) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (targetEpoch >= minEpoch + reservoir.length) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    } else if (numObservations > (reservoir.length - 1)) {
        return makeDefaultNodes(raw, targetEpoch, numObservations);
    }//from   w w w . ja  v  a  2  s .  c om

    /**
     * Fitting to a geometric distribution uses the mean value of the sample.
     *
     * Fitting to a normal distribution uses the Apache Commons Math implementation.
     */
    double mean = 0.0;
    double m2 = 0.0;
    double stddev;
    double gaussianNegative = -1.0;
    Map<Integer, Integer> frequencies = new HashMap<>();
    double threshold;
    double measurePercentile = -100.0;

    int index = reservoir.length - 1;
    long currentEpoch = minEpoch + index;

    while (currentEpoch != targetEpoch) {
        index--;
        currentEpoch--;
    }

    measurement = reservoir[index--];
    currentEpoch--;

    while (count < numObservations && index >= 0) {
        int value = reservoir[index--];
        if (value < min) {
            min = value;
        }
        updateFrequencies(frequencies, value);
        count++;
        double delta = value - mean;
        mean += delta / count;
        m2 += delta * (value - mean);
    }

    while (count < numObservations) {
        int value = 0;
        if (value < min) {
            min = value;
        }
        updateFrequencies(frequencies, value);
        count++;
        double delta = value - mean;
        mean += delta / count;
        m2 += delta * (value - mean);
    }

    if (count < 2) {
        stddev = 0.0;
    } else {
        stddev = Math.sqrt(m2 / count);
    }

    int mode = -1;
    int modeCount = -1;

    for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) {
        int key = entry.getKey();
        int value = entry.getValue();
        if (value > modeCount || (value == modeCount && key > mode)) {
            mode = key;
            modeCount = value;
        }
    }

    if (mean > 0.0 && stddev > 0.0) {
        gaussianNegative = gaussianNegativeProbability(mean, stddev);
    }

    if (mean == 0.0) {
        threshold = 0.0;
    } else if (stddev == 0.0) {
        threshold = mean;
    } else if (mean > 1.0) {
        NormalDistribution distribution = new NormalDistribution(mean, stddev);
        double badProbability = distribution.cumulativeProbability(0.0);
        double goodProbability = badProbability + (1.0 - badProbability) * (percentile / 100.0);
        threshold = distribution.inverseCumulativeProbability(goodProbability);
        measurePercentile = distribution.probability(0.0, measurement) / (1.0 - badProbability) * 100.0;
    } else {
        double p = 1.0 / (1.0 + mean);
        GeometricDistribution distribution = new GeometricDistribution(p);
        threshold = distribution.inverseCumulativeProbability(percentile / 100.0);
        measurePercentile = distribution.cumulativeProbability(measurement) * 100.0;
    }

    List<DataTreeNode> result = new ArrayList<>();
    VirtualTreeNode vchild, vparent;

    if (measurement >= minMeasurement && (measurement > threshold || percentile == 0.0)) {
        vchild = new VirtualTreeNode("gaussianNegative", doubleToLong(gaussianNegative, doubleToLongBits));
        vparent = new VirtualTreeNode("percentile", doubleToLong(measurePercentile, doubleToLongBits),
                generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("mode", mode, generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("stddev", doubleToLong(stddev, doubleToLongBits),
                generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("mean", doubleToLong(mean, doubleToLongBits),
                generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("measurement", measurement, generateSingletonArray(vchild));
        vchild = vparent;
        vparent = new VirtualTreeNode("delta", doubleToLong(measurement - threshold, doubleToLongBits),
                generateSingletonArray(vchild));
        result.add(vparent);
        if (raw) {
            addRawObservations(result, targetEpoch, numObservations);
        }
    } else {
        makeDefaultNodes(raw, targetEpoch, numObservations);
    }
    return result;
}