use of de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution in project elki by elki-project.
the class BestFitEstimatorTest method testNormalDistribution.
@Test
public void testNormalDistribution() {
BestFitEstimator est = init();
Random r = new Random(0L);
double[] data = new double[10000];
for (int i = 0; i < data.length; i++) {
data[i] = 2 + 3 * r.nextGaussian();
}
Distribution edist = est.estimate(data, DoubleArrayAdapter.STATIC);
assertEquals("Wrong class of distribution", NormalDistribution.class, edist.getClass());
NormalDistribution good = (NormalDistribution) edist;
assertEquals("Mean not as expected from trimmed estimator.", 2., good.getMean(), 2e-2);
assertEquals("Stddev not as expected from trimmed estimator.", 3., good.getStddev(), 4e-2);
}
use of de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution in project elki by elki-project.
the class WinsorizingEstimatorTest method testNormalDistribution.
@Test
public void testNormalDistribution() {
final double trim = .01;
NormalMOMEstimator mom = NormalMOMEstimator.STATIC;
// We could instantiate directly, but we also want to cover the
// parameterizer class.
ListParameterization config = new ListParameterization();
config.addParameter(WinsorizingEstimator.Parameterizer.INNER_ID, mom);
config.addParameter(WinsorizingEstimator.Parameterizer.WINSORIZE_ID, trim);
WinsorizingEstimator<NormalDistribution> est = ClassGenericsUtil.parameterizeOrAbort(WinsorizingEstimator.class, config);
Random r = new Random(0L);
double[] data = new double[10000];
final int corrupt = (int) Math.floor(data.length * trim * .5);
for (int i = 0; i < data.length; i++) {
data[i] = i < corrupt ? 1e10 : r.nextGaussian();
}
NormalDistribution bad = mom.estimate(data, DoubleArrayAdapter.STATIC);
NormalDistribution good = est.estimate(data, DoubleArrayAdapter.STATIC);
assertEquals("Mean not as expected from naive estimator.", 5e7, bad.getMean(), 1e-2);
assertEquals("Stddev not as expected from naive estimator.", 7e8, bad.getStddev(), 1e7);
assertEquals("Mean not as expected from trimmed estimator.", 0, good.getMean(), 2e-2);
assertEquals("Stddev not as expected from trimmed estimator.", 1.0, good.getStddev(), 2e-2);
}
use of de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution in project elki by elki-project.
the class NormalMADEstimatorTest method testEstimator.
@Test
public void testEstimator() {
final NormalMADEstimator est = instantiate(NormalMADEstimator.class, NormalDistribution.class);
load("norm.ascii.gz");
double[] data = this.data.get("random_01_01");
NormalDistribution dist = est.estimate(data, DoubleArrayAdapter.STATIC);
assertStat("mean", dist.getMean(), 0.1, -0.012695629177971263);
assertStat("stddev", dist.getStddev(), 0.1, -0.026518777589684295);
data = this.data.get("random_0_1");
dist = est.estimate(data, DoubleArrayAdapter.STATIC);
assertStat("mean", dist.getMean(), 0., 0.01835053032901817);
assertStat("stddev", dist.getStddev(), 1., -0.0826324972827217);
data = this.data.get("random_1_3");
dist = est.estimate(data, DoubleArrayAdapter.STATIC);
assertStat("mean", dist.getMean(), 1., -0.6071640630952582);
assertStat("stddev", dist.getStddev(), 3., 0.46390113946568645);
}
use of de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution in project elki by elki-project.
the class TrimmedEstimatorTest method testNormalDistribution.
@Test
public void testNormalDistribution() {
final double trim = .01;
NormalMOMEstimator mom = NormalMOMEstimator.STATIC;
// We could instantiate directly, but we also want to cover the
// parameterizer class.
ListParameterization config = new ListParameterization();
config.addParameter(TrimmedEstimator.Parameterizer.INNER_ID, mom);
config.addParameter(TrimmedEstimator.Parameterizer.TRIM_ID, trim);
TrimmedEstimator<NormalDistribution> est = ClassGenericsUtil.parameterizeOrAbort(TrimmedEstimator.class, config);
Random r = new Random(0L);
double[] data = new double[10000];
final int corrupt = (int) Math.floor(data.length * trim * .5);
for (int i = 0; i < data.length; i++) {
data[i] = i < corrupt ? 1e10 : r.nextGaussian();
}
NormalDistribution bad = mom.estimate(data, DoubleArrayAdapter.STATIC);
NormalDistribution good = est.estimate(data, DoubleArrayAdapter.STATIC);
assertEquals("Mean not as expected from naive estimator.", 5e7, bad.getMean(), 1e-2);
assertEquals("Stddev not as expected from naive estimator.", 7e8, bad.getStddev(), 1e7);
assertEquals("Mean not as expected from trimmed estimator.", 0, good.getMean(), 1e-2);
assertEquals("Stddev not as expected from trimmed estimator.", 1.0, good.getStddev(), 3e-2);
}
use of de.lmu.ifi.dbs.elki.math.statistics.distribution.NormalDistribution in project elki by elki-project.
the class ReplaceNaNWithRandomFilterTest method parameters.
/**
* Test with standard normal distribution as parameter.
*/
@Test
public void parameters() {
String filename = UNITTEST + "nan-test-1.csv";
ReplaceNaNWithRandomFilter filter = //
new ELKIBuilder<>(ReplaceNaNWithRandomFilter.class).with(//
ReplaceNaNWithRandomFilter.Parameterizer.REPLACEMENT_DISTRIBUTION, new NormalDistribution(0, 1, new Random(0L))).build();
MultipleObjectsBundle filteredBundle = readBundle(filename, filter);
// Load the test data again without a filter.
MultipleObjectsBundle unfilteredBundle = readBundle(filename);
// Ensure the first column are the vectors.
assertTrue("Test file not as expected", TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(filteredBundle.meta(0)));
assertTrue("Test file not as expected", TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(unfilteredBundle.meta(0)));
// This cast is now safe (vector field):
int dimFiltered = ((FieldTypeInformation) unfilteredBundle.meta(0)).getDimensionality();
int dimUnfiltered = ((FieldTypeInformation) unfilteredBundle.meta(0)).getDimensionality();
assertEquals("Dimensionality expected equal", dimFiltered, dimUnfiltered);
// Note the indices of the NaN(s) in the data.
List<IntegerVector> NaNs = new ArrayList<IntegerVector>();
for (int row = 0; row < unfilteredBundle.dataLength(); row++) {
Object obj = unfilteredBundle.data(row, 0);
assertEquals("Unexpected data type", DoubleVector.class, obj.getClass());
DoubleVector d = (DoubleVector) obj;
for (int col = 0; col < dimUnfiltered; col++) {
final double v = d.doubleValue(col);
if (Double.isNaN(v)) {
NaNs.add(new IntegerVector(new int[] { row, col }));
}
}
}
// Verify that at least a single NaN exists in the unfiltered bundle.
assertTrue("NaN expected in unfiltered data", NaNs.size() > 0);
for (IntegerVector iv : NaNs) {
Object obj = filteredBundle.data(iv.intValue(0), 0);
assertEquals("Unexpected data type", DoubleVector.class, obj.getClass());
DoubleVector d = (DoubleVector) obj;
final double v = d.doubleValue(iv.intValue(1));
assertFalse("NaN not expected", Double.isNaN(v));
}
}
Aggregations