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);
}
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());
}
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);
}
}
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);
}
}
Aggregations