Example usage for org.apache.commons.math3.distribution TDistribution sample

List of usage examples for org.apache.commons.math3.distribution TDistribution sample

Introduction

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

Prototype

public double sample() 

Source Link

Document

The default implementation uses the <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.

Usage

From source file:org.apache.metron.statistics.outlier.MedianAbsoluteDeviationTest.java

@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);// w  ww  . ja  va  2  s . co  m
        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);
    }
}