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