use of de.lmu.ifi.dbs.elki.math.MeanVariance in project elki by elki-project.
the class GlobalPrincipalComponentAnalysisTransformTest method defaultParameters.
/**
* Test with default parameters.
*/
@Test
public void defaultParameters() {
String filename = UNITTEST + "transformation-test-1.csv";
GlobalPrincipalComponentAnalysisTransform<DoubleVector> filter = new ELKIBuilder<GlobalPrincipalComponentAnalysisTransform<DoubleVector>>(GlobalPrincipalComponentAnalysisTransform.class).build();
MultipleObjectsBundle bundle = readBundle(filename, filter);
int dim = getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
// We verify that the result has mean 0 and variance 1 in each column.
// We also expect that covariances of any two columns are 0.
CovarianceMatrix cm = new CovarianceMatrix(dim);
MeanVariance[] mvs = MeanVariance.newArray(dim);
for (int row = 0; row < bundle.dataLength(); row++) {
DoubleVector d = get(bundle, row, 0, DoubleVector.class);
cm.put(d);
for (int col = 0; col < dim; col++) {
final double v = d.doubleValue(col);
if (v > Double.NEGATIVE_INFINITY && v < Double.POSITIVE_INFINITY) {
mvs[col].put(v);
}
}
}
double[][] ncm = cm.destroyToPopulationMatrix();
for (int col = 0; col < dim; col++) {
for (int row = 0; row < dim; row++) {
assertEquals("Unexpected covariance", col == row ? 1. : 0., ncm[row][col], 1e-15);
}
assertEquals("Mean not as expected", 0., mvs[col].getMean(), 1e-15);
assertEquals("Variance not as expected", 1., mvs[col].getNaiveVariance(), 1e-15);
}
}
use of de.lmu.ifi.dbs.elki.math.MeanVariance in project elki by elki-project.
the class InstanceMeanVarianceNormalizationTest method defaultParameters.
/**
* Test with default parameters.
*/
@Test
public void defaultParameters() {
String filename = UNITTEST + "normalization-test-1.csv";
InstanceMeanVarianceNormalization<DoubleVector> filter = new ELKIBuilder<>(InstanceMeanVarianceNormalization.class).build();
MultipleObjectsBundle bundle = readBundle(filename, filter);
int dim = getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
// Verify that the resulting data has mean 0 and variance 1 in each row.
MeanVariance mvs = new MeanVariance();
for (int row = 0; row < bundle.dataLength(); row++) {
mvs.reset();
DoubleVector d = get(bundle, row, 0, DoubleVector.class);
for (int col = 0; col < dim; col++) {
final double v = d.doubleValue(col);
if (v > Double.NEGATIVE_INFINITY && v < Double.POSITIVE_INFINITY) {
mvs.put(v);
}
}
assertEquals("Mean is not 0", 0., mvs.getMean(), 1e-14);
assertEquals("Variance is not 1", 1., mvs.getNaiveVariance(), 1e-14);
}
}
use of de.lmu.ifi.dbs.elki.math.MeanVariance in project elki by elki-project.
the class KNNJoinTest method doKNNJoin.
/**
* Actual test routine.
*
* @param inputparams
*/
void doKNNJoin(ListParameterization inputparams) {
Database db = AbstractSimpleAlgorithmTest.makeSimpleDatabase(dataset, shoulds, inputparams);
Relation<NumberVector> relation = db.getRelation(TypeUtil.NUMBER_VECTOR_FIELD);
// Euclidean
{
KNNJoin<DoubleVector, ?, ?> knnjoin = new KNNJoin<DoubleVector, RStarTreeNode, SpatialEntry>(EuclideanDistanceFunction.STATIC, 2);
Relation<KNNList> result = knnjoin.run(db);
MeanVariance meansize = new MeanVariance();
for (DBIDIter id = relation.getDBIDs().iter(); id.valid(); id.advance()) {
meansize.put(result.get(id).size());
}
org.junit.Assert.assertEquals("Euclidean mean 2NN set size", mean2nnEuclid, meansize.getMean(), 0.00001);
org.junit.Assert.assertEquals("Euclidean variance 2NN", var2nnEuclid, meansize.getSampleVariance(), 0.00001);
}
// Manhattan
{
KNNJoin<DoubleVector, ?, ?> knnjoin = new KNNJoin<DoubleVector, RStarTreeNode, SpatialEntry>(ManhattanDistanceFunction.STATIC, 2);
Relation<KNNList> result = knnjoin.run(db);
MeanVariance meansize = new MeanVariance();
for (DBIDIter id = relation.getDBIDs().iter(); id.valid(); id.advance()) {
meansize.put(result.get(id).size());
}
org.junit.Assert.assertEquals("Manhattan mean 2NN", mean2nnManhattan, meansize.getMean(), 0.00001);
org.junit.Assert.assertEquals("Manhattan variance 2NN", var2nnManhattan, meansize.getSampleVariance(), 0.00001);
}
}
use of de.lmu.ifi.dbs.elki.math.MeanVariance in project elki by elki-project.
the class KNNJoinTest method testLinearScan.
@Test
public void testLinearScan() {
Database db = AbstractSimpleAlgorithmTest.makeSimpleDatabase(dataset, shoulds);
Relation<NumberVector> relation = db.getRelation(TypeUtil.NUMBER_VECTOR_FIELD);
// Euclidean
{
DistanceQuery<NumberVector> dq = db.getDistanceQuery(relation, EuclideanDistanceFunction.STATIC);
KNNQuery<NumberVector> knnq = QueryUtil.getLinearScanKNNQuery(dq);
MeanVariance meansize = new MeanVariance();
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
meansize.put(knnq.getKNNForDBID(iditer, 2).size());
}
org.junit.Assert.assertEquals("Euclidean mean 2NN", mean2nnEuclid, meansize.getMean(), 0.00001);
org.junit.Assert.assertEquals("Euclidean variance 2NN", var2nnEuclid, meansize.getSampleVariance(), 0.00001);
}
// Manhattan
{
DistanceQuery<NumberVector> dq = db.getDistanceQuery(relation, ManhattanDistanceFunction.STATIC);
KNNQuery<NumberVector> knnq = QueryUtil.getLinearScanKNNQuery(dq);
MeanVariance meansize = new MeanVariance();
for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) {
meansize.put(knnq.getKNNForDBID(iditer, 2).size());
}
org.junit.Assert.assertEquals("Manhattan mean 2NN", mean2nnManhattan, meansize.getMean(), 0.00001);
org.junit.Assert.assertEquals("Manhattan variance 2NN", var2nnManhattan, meansize.getSampleVariance(), 0.00001);
}
}
use of de.lmu.ifi.dbs.elki.math.MeanVariance in project elki by elki-project.
the class AttributeWiseVarianceNormalizationTest method defaultParameters.
/**
* Test with default parameters.
*/
@Test
public void defaultParameters() {
String filename = UNITTEST + "normalization-test-1.csv";
AttributeWiseVarianceNormalization<DoubleVector> filter = new ELKIBuilder<AttributeWiseVarianceNormalization<DoubleVector>>(AttributeWiseVarianceNormalization.class).build();
MultipleObjectsBundle bundle = readBundle(filename, filter);
int dim = getFieldDimensionality(bundle, 0, TypeUtil.NUMBER_VECTOR_FIELD);
// We verify that the resulting data has mean 0 and variance 1 in each
// column:
MeanVariance[] mvs = MeanVariance.newArray(dim);
for (int row = 0; row < bundle.dataLength(); row++) {
DoubleVector d = get(bundle, row, 0, DoubleVector.class);
for (int col = 0; col < dim; col++) {
final double v = d.doubleValue(col);
if (v > Double.NEGATIVE_INFINITY && v < Double.POSITIVE_INFINITY) {
mvs[col].put(v);
}
}
}
for (int col = 0; col < dim; col++) {
assertEquals("Mean not as expected", 0., mvs[col].getMean(), 1e-14);
assertEquals("Variance not as expected", 1., mvs[col].getNaiveVariance(), 1e-14);
}
}
Aggregations