Search in sources :

Example 6 with GaussianRandomGenerator

use of org.apache.commons.math3.random.GaussianRandomGenerator 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 7 with GaussianRandomGenerator

use of org.apache.commons.math3.random.GaussianRandomGenerator 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 8 with GaussianRandomGenerator

use of org.apache.commons.math3.random.GaussianRandomGenerator 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 9 with GaussianRandomGenerator

use of org.apache.commons.math3.random.GaussianRandomGenerator in project metron by apache.

the class UniformSamplerTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    Random rng = new Random(0);
    GaussianRandomGenerator gen = new GaussianRandomGenerator(new MersenneTwister(0));
    for (int i = 0; i < SAMPLE_SIZE; ++i) {
        double us = 10 * rng.nextDouble();
        uniformSample.add(us);
        uniformStats.addValue(us);
        double gs = 10 * gen.nextNormalizedDouble();
        gaussianSample.add(gs);
        gaussianStats.addValue(gs);
    }
}
Also used : Random(java.util.Random) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) BeforeClass(org.junit.BeforeClass)

Aggregations

GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)9 MersenneTwister (org.apache.commons.math3.random.MersenneTwister)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)3 Random (java.util.Random)2 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)1 BeforeClass (org.junit.BeforeClass)1