Example usage for org.apache.commons.math3.distribution GeometricDistribution GeometricDistribution

List of usage examples for org.apache.commons.math3.distribution GeometricDistribution GeometricDistribution

Introduction

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

Prototype

public GeometricDistribution(double p) 

Source Link

Document

Create a geometric distribution with the given probability of success.

Usage

From source file:jasima.core.random.discrete.IntGeometric.java

public IntGeometric(double probOfSuccess) {
    super();
    setDistribution(new GeometricDistribution(probOfSuccess));
}

From source file:jasima.core.random.discrete.IntGeometric.java

/**
 * Sets the probability of success in a single trial.
 * //  ww w  .  j a va  2  s.c o m
 * @param probOfSuccess
 *            The success probability.
 * @throws OutOfRangeException
 *             If the supplied probability if not in the interval
 *             {@code (0,1]}.
 */
public void setProbabilityOfSuccess(double probOfSuccess) throws OutOfRangeException {
    setDistribution(new GeometricDistribution(probOfSuccess));
}

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);
    }/*w w  w  .j av a2s.c  o m*/

    /**
     * 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;
}

From source file:org.apache.solr.client.solrj.io.eval.GeometricDistributionEvaluator.java

@Override
public Object doWork(Object first) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }/*from   w  ww. ja va  2  s  . c o m*/

    Number prob = (Number) first;

    return new GeometricDistribution(prob.doubleValue());
}

From source file:org.talend.dataquality.duplicating.DistributionFactory.java

public static AbstractIntegerDistribution createDistribution(String name, double expectation) {
    AbstractIntegerDistribution distro = null;
    if (expectation < 2) {
        throw new IllegalArgumentException("The expectation value must be greater than or equals to 2"); //$NON-NLS-1$
    }/*  www  . ja v a  2 s .  co  m*/
    if (name.equals("BERNOULLI")) { //$NON-NLS-1$
        distro = new BinomialDistribution((int) ((expectation - 2) * 2), 0.5);
    } else if (name.equals("GEOMETRIC")) { //$NON-NLS-1$
        distro = new GeometricDistribution(1 / (expectation - 1));
    } else if (name.equals("POISSON")) { //$NON-NLS-1$
        distro = new PoissonDistribution(expectation - 2 + java.lang.Double.MIN_VALUE);
    }
    return distro;
}