Search in sources :

Example 1 with MersenneTwister

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

the class OnlineStatisticsProviderTest method testNormallyDistributedRandomDataShiftedBackwards.

@Test
public void testNormallyDistributedRandomDataShiftedBackwards() {
    List<Double> values = new ArrayList<>();
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    for (int i = 0; i < 1000000; ++i) {
        double d = gaussian.nextNormalizedDouble() - 10;
        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 2 with MersenneTwister

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

the class OnlineStatisticsProviderTest method testNormallyDistributedRandomDataShifted.

@Test
public void testNormallyDistributedRandomDataShifted() {
    List<Double> values = new ArrayList<>();
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    for (int i = 0; i < 1000000; ++i) {
        double d = gaussian.nextNormalizedDouble() + 10;
        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 3 with MersenneTwister

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

the class StellarStatisticsFunctionsTest method testMergeProviders.

@Test
public void testMergeProviders() throws Exception {
    List<StatisticsProvider> providers = new ArrayList<>();
    /*
    Create 10 providers, each with a sample drawn from a gaussian distribution.
    Update the reference stats from commons math to ensure we are
     */
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(1L));
    SummaryStatistics sStatistics = new SummaryStatistics();
    DescriptiveStatistics dStatistics = new DescriptiveStatistics();
    for (int i = 0; i < 10; ++i) {
        List<Double> sample = new ArrayList<>();
        for (int j = 0; j < 100; ++j) {
            double s = gaussian.nextNormalizedDouble();
            sample.add(s);
            sStatistics.addValue(s);
            dStatistics.addValue(s);
        }
        StatisticsProvider provider = (StatisticsProvider) run("STATS_ADD(STATS_INIT(), " + Joiner.on(",").join(sample) + ")", new HashMap<>());
        providers.add(provider);
    }
    /*
    Merge the providers and validate
     */
    Map<String, Object> providerVariables = new HashMap<>();
    for (int i = 0; i < providers.size(); ++i) {
        providerVariables.put("provider_" + i, providers.get(i));
    }
    StatisticsProvider mergedProvider = (StatisticsProvider) run("STATS_MERGE([" + Joiner.on(",").join(providerVariables.keySet()) + "])", providerVariables);
    OnlineStatisticsProviderTest.validateStatisticsProvider(mergedProvider, sStatistics, dStatistics);
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) GaussianRandomGenerator(org.apache.commons.math3.random.GaussianRandomGenerator) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) Test(org.junit.Test)

Example 4 with MersenneTwister

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

the class MedianAbsoluteDeviationTest method testLongTailed.

@Test
public void testLongTailed() {
    TDistribution generator = new TDistribution(new MersenneTwister(0L), 100);
    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 = generator.sample();
        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) ArrayList(java.util.ArrayList) TDistribution(org.apache.commons.math3.distribution.TDistribution) MersenneTwister(org.apache.commons.math3.random.MersenneTwister) Test(org.junit.Test)

Example 5 with MersenneTwister

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

the class OnlineStatisticsProviderTest method testNormallyDistributedRandomData.

@Test
public void testNormallyDistributedRandomData() {
    List<Double> values = new ArrayList<>();
    GaussianRandomGenerator gaussian = new GaussianRandomGenerator(new MersenneTwister(0L));
    for (int i = 0; i < 1000000; ++i) {
        double d = gaussian.nextNormalizedDouble();
        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)

Aggregations

MersenneTwister (org.apache.commons.math3.random.MersenneTwister)10 GaussianRandomGenerator (org.apache.commons.math3.random.GaussianRandomGenerator)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)4 Random (java.util.Random)2 TDistribution (org.apache.commons.math3.distribution.TDistribution)1 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)1 BeforeClass (org.junit.BeforeClass)1