Search in sources :

Example 31 with Gaussian

use of org.apache.commons.math3.analysis.function.Gaussian in project metron by apache.

the class OnlineStatisticsProviderTest method testNormallyDistributedRandomDataSkewed.

@Test
public void testNormallyDistributedRandomDataSkewed() {
    List<Double> values = new ArrayList<>();
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    for (int i = 0; i < 1000000; ++i) {
        double d = (gaussian.nextNormalizedDouble() + 10000) / 1000;
        values.add(d);
    }
    validateEquality(values);
}
Also used : GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) ArrayList(java.util.ArrayList) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) Test(org.junit.Test)

Example 32 with Gaussian

use of org.apache.commons.math3.analysis.function.Gaussian in project metron by apache.

the class StatisticalBinningPerformanceDriver method main.

public static void main(String... argv) {
    DescriptiveStatistics perfStats = new DescriptiveStatistics();
    OnlineStatisticsProvider statsProvider = new OnlineStatisticsProvider();
    List<Double> values = new ArrayList<>();
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    for (int i = 0; i < NUM_DATA_POINTS; ++i) {
        // get the data point out of the [0,1] range
        double d = 1000 * gaussian.nextNormalizedDouble();
        values.add(d);
        statsProvider.addValue(d);
    }
    for (int perfRun = 0; perfRun < NUM_RUNS; ++perfRun) {
        StellarStatisticsFunctions.StatsBin bin = new StellarStatisticsFunctions.StatsBin();
        long start = System.currentTimeMillis();
        Random r = new Random(0);
        for (int i = 0; i < TRIALS_PER_RUN; ++i) {
            // grab a random value and fuzz it a bit so we make sure there's no cheating via caching in t-digest.
            bin.apply(ImmutableList.of(statsProvider, values.get(r.nextInt(values.size())) - 3.5, PERCENTILES));
        }
        perfStats.addValue(System.currentTimeMillis() - start);
    }
    System.out.println("Min/25th/50th/75th/Max Milliseconds: " + perfStats.getMin() + " / " + perfStats.getPercentile(25) + " / " + perfStats.getPercentile(50) + " / " + perfStats.getPercentile(75) + " / " + perfStats.getMax());
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) ArrayList(java.util.ArrayList) Random(java.util.Random) MersenneTwister(org.apache.commons.math3.random.MersenneTwister)

Example 33 with Gaussian

use of org.apache.commons.math3.analysis.function.Gaussian in project metron by apache.

the class MedianAbsoluteDeviationTest method test.

@Test
public void test() {
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    DescriptiveStatistics stats = new DescriptiveStatistics();
    List<MedianAbsoluteDeviationFunctions.State> states = new ArrayList<>();
    MedianAbsoluteDeviationFunctions.State currentState = null;
    // initialize the state
    currentState = (MedianAbsoluteDeviationFunctions.State) run("OUTLIER_MAD_STATE_MERGE(states, NULL)", ImmutableMap.of("states", states));
    for (int i = 0, j = 0; i < 10000; ++i, ++j) {
        Double d = gaussian.nextNormalizedDouble();
        stats.addValue(d);
        run("OUTLIER_MAD_ADD(currentState, data)", ImmutableMap.of("currentState", currentState, "data", d));
        if (j >= 1000) {
            j = 0;
            List<MedianAbsoluteDeviationFunctions.State> stateWindow = new ArrayList<>();
            for (int stateIndex = Math.max(0, states.size() - 5); stateIndex < states.size(); ++stateIndex) {
                stateWindow.add(states.get(stateIndex));
            }
            currentState = (MedianAbsoluteDeviationFunctions.State) run("OUTLIER_MAD_STATE_MERGE(states, currentState)", ImmutableMap.of("states", stateWindow, "currentState", currentState));
        }
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState", currentState, "value", stats.getMin()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being a minimum.", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState", currentState, "value", stats.getMax()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being a maximum", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState", currentState, "value", stats.getMean() + 4 * stats.getStandardDeviation()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being 4 std deviations away from the mean", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState", currentState, "value", stats.getMean() - 4 * stats.getStandardDeviation()));
        Assert.assertTrue("Score: " + score + " is not an outlier despite being 4 std deviations away from the mean", score > 3.5);
    }
    {
        Double score = (Double) run("OUTLIER_MAD_SCORE(currentState, value)", ImmutableMap.of("currentState", currentState, "value", stats.getMean()));
        Assert.assertFalse("Score: " + score + " is an outlier despite being the mean", score > 3.5);
    }
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) ArrayList(java.util.ArrayList) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) Test(org.junit.Test)

Example 34 with Gaussian

use of org.apache.commons.math3.analysis.function.Gaussian in project pyramid by cheng-li.

the class MultiLabelSynthesizer method gaussianNoise.

/**
     * 2 labels, 3 features, multi-variate gaussian noise
     * y0: w=(0,1,0)
     * y1: w=(1,0,0)
     * y2: w=(0,0,1)
     * @return
     */
public static MultiLabelClfDataSet gaussianNoise(int numData) {
    int numClass = 3;
    int numFeature = 3;
    MultiLabelClfDataSet dataSet = MLClfDataSetBuilder.getBuilder().numFeatures(numFeature).numClasses(numClass).numDataPoints(numData).build();
    // generate weights
    Vector[] weights = new Vector[numClass];
    for (int k = 0; k < numClass; k++) {
        Vector vector = new DenseVector(numFeature);
        weights[k] = vector;
    }
    weights[0].set(1, 1);
    weights[1].set(0, 1);
    weights[2].set(2, 1);
    // generate features
    for (int i = 0; i < numData; i++) {
        for (int j = 0; j < numFeature; j++) {
            dataSet.setFeatureValue(i, j, Sampling.doubleUniform(-1, 1));
        }
    }
    double[] means = new double[numClass];
    double[][] covars = new double[numClass][numClass];
    covars[0][0] = 0.5;
    covars[0][1] = 0.02;
    covars[1][0] = 0.02;
    covars[0][2] = -0.03;
    covars[2][0] = -0.03;
    covars[1][1] = 0.2;
    covars[1][2] = -0.03;
    covars[2][1] = -0.03;
    covars[2][2] = 0.3;
    MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(means, covars);
    // assign labels
    int numFlipped = 0;
    for (int i = 0; i < numData; i++) {
        double[] noises = distribution.sample();
        for (int k = 0; k < numClass; k++) {
            double dot = weights[k].dot(dataSet.getRow(i));
            double score = dot + noises[k];
            if (score >= 0) {
                dataSet.addLabel(i, k);
            }
            if (dot * score < 0) {
                numFlipped += 1;
            }
        }
    }
    System.out.println("number of flipped bits = " + numFlipped);
    return dataSet;
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) MultiLabelClfDataSet(edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet) DenseVector(org.apache.mahout.math.DenseVector) MultivariateNormalDistribution(org.apache.commons.math3.distribution.MultivariateNormalDistribution)

Example 35 with Gaussian

use of org.apache.commons.math3.analysis.function.Gaussian in project vcell by virtualcell.

the class BrownianDynamics2DSolver method bleachGuassian0.

private long bleachGuassian0(double muX, double muY, double psfVar, double bleachFactor_K, double deltaT) {
    // 
    // distribute each particle using a normalized Gaussian point spread function with variance psfVar.
    // we add the fluorescence to the image (can be called multiple times to accumulate fluorescence).
    // 
    long bleachCount = 0;
    double DISTANCE_6_SIGMA = Math.sqrt(psfVar) * 6;
    for (int p = 0; p < numParticles; p++) {
        if (bFluorescent[p]) {
            double pX = particleX[p] - muX;
            double pY = particleY[p] - muY;
            double radius2 = pX * pX + pY * pY;
            if (radius2 < DISTANCE_6_SIGMA) {
                double particleFluorRate = bleachFactor_K * deltaT * FastMath.exp(-radius2 / psfVar);
                PoissonDistribution poisson = new PoissonDistribution(simulationRng, particleFluorRate, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
                if (poisson.sample() >= 1) {
                    // if bleaching event happened
                    bFluorescent[p] = false;
                    bleachCount++;
                }
            }
        }
    }
    return bleachCount;
}
Also used : PoissonDistribution(org.apache.commons.math3.distribution.PoissonDistribution)

Aggregations

ArrayList (java.util.ArrayList)9 GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)8 MersenneTwister (org.apache.commons.math3.random.MersenneTwister)8 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)7 Test (org.junit.Test)7 WeightedObservedPoint (org.apache.commons.math3.fitting.WeightedObservedPoint)6 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)6 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)4 PointValuePair (org.apache.commons.math3.optim.PointValuePair)4 Mean (org.apache.commons.math3.stat.descriptive.moment.Mean)4 BaseTest (org.broadinstitute.hellbender.utils.test.BaseTest)4 Test (org.testng.annotations.Test)4 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)3 Plot2 (ij.gui.Plot2)3 Random (java.util.Random)3 NormalDistribution (org.apache.commons.math3.distribution.NormalDistribution)3 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)3 Well19937c (org.apache.commons.math3.random.Well19937c)3 ClusterPoint (gdsc.core.clustering.ClusterPoint)2 Gaussian2DFunction (gdsc.smlm.function.gaussian.Gaussian2DFunction)2