Search in sources :

Example 81 with DoubleVector

use of de.lmu.ifi.dbs.elki.data.DoubleVector in project elki by elki-project.

the class SpatialPrimitiveDistanceFunctionTest method testSpatialDistanceConsistencyPositive.

@Test
public void testSpatialDistanceConsistencyPositive() {
    final Random rnd = new Random(1);
    final int dim = 7;
    final int iters = 10000;
    List<SpatialPrimitiveDistanceFunction<? super NumberVector>> dists = new ArrayList<>();
    dists.add(EuclideanDistanceFunction.STATIC);
    dists.add(ManhattanDistanceFunction.STATIC);
    dists.add(MaximumDistanceFunction.STATIC);
    dists.add(MinimumDistanceFunction.STATIC);
    dists.add(new LPNormDistanceFunction(3));
    dists.add(new LPNormDistanceFunction(.5));
    dists.add(CanberraDistanceFunction.STATIC);
    dists.add(HistogramIntersectionDistanceFunction.STATIC);
    dists.add(SquaredEuclideanDistanceFunction.STATIC);
    dists.add(ArcCosineDistanceFunction.STATIC);
    dists.add(CosineDistanceFunction.STATIC);
    double[] d1 = new double[dim];
    double[] d2 = new double[dim];
    double[] d3 = new double[dim];
    double[] d4 = new double[dim];
    DoubleVector v1 = DoubleVector.wrap(d1);
    ModifiableHyperBoundingBox mbr = new ModifiableHyperBoundingBox(d2, d3);
    DoubleVector v2 = DoubleVector.wrap(d4);
    for (int i = 0; i < iters; i++) {
        for (int d = 0; d < dim; d++) {
            d1[d] = rnd.nextDouble() * 2E4;
            d2[d] = rnd.nextDouble() * 2E4;
            d3[d] = rnd.nextDouble() * 2E4;
            if (d2[d] > d3[d]) {
                double t = d2[d];
                d2[d] = d3[d];
                d3[d] = t;
            }
            double m = rnd.nextDouble();
            d4[d] = m * d2[d] + (1 - m) * d3[d];
        }
        for (SpatialPrimitiveDistanceFunction<? super NumberVector> dis : dists) {
            compareDistances(v1, mbr, v2, dis);
        }
    }
}
Also used : LPNormDistanceFunction(de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.LPNormDistanceFunction) Random(java.util.Random) NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) ArrayList(java.util.ArrayList) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) ModifiableHyperBoundingBox(de.lmu.ifi.dbs.elki.data.ModifiableHyperBoundingBox) Test(org.junit.Test)

Example 82 with DoubleVector

use of de.lmu.ifi.dbs.elki.data.DoubleVector in project elki by elki-project.

the class TriangularDistanceFunctionTest method testTriganular.

@Test
public void testTriganular() {
    DoubleVector v0 = DoubleVector.wrap(new double[] { 0.8, 0.1, 0.1 });
    DoubleVector v1 = DoubleVector.wrap(new double[] { 0.1, 0.8, 0.1 });
    DoubleVector v2 = DoubleVector.wrap(new double[] { 0.1, 0.1, 0.8 });
    DoubleVector v3 = DoubleVector.wrap(new double[] { 1. / 3, 1. / 3, 1. / 3 });
    DoubleVector v4 = DoubleVector.wrap(new double[] { 0.6, 0.2, 0.2 });
    DoubleVector[] vecs = { v0, v1, v2, v3, v4 };
    // Manual computation of correct distances:
    double d10 = .49 / .9 + .49 / .9 + 0.;
    double d30 = // 
    (.8 - 1. / 3) * (.8 - 1. / 3) / (.8 + 1. / 3) + 2 * (.1 - 1. / 3) * (.1 - 1. / 3) / (.1 + 1. / 3);
    double d40 = .04 / 1.4 + .01 / .3 + .01 / .3;
    double d41 = .25 / 0.7 + .36 / 1. + .01 / .3;
    double d43 = // 
    (.6 - 1. / 3) * (.6 - 1. / 3) / (.6 + 1. / 3) + 2 * (.2 - 1. / 3) * (.2 - 1. / 3) / (.2 + 1. / 3);
    double[][] distances = { // 
    { 0., d10, d10, d30, d40 }, // 
    { d10, 0., d10, d30, d41 }, // 
    { d10, d10, 0., d30, d41 }, // 
    { d30, d30, d30, 0., d43 }, // 
    { d40, d41, d41, d43, 0. } };
    PrimitiveDistanceFunction<NumberVector> df = TriangularDistanceFunction.STATIC;
    for (int i = 0; i < vecs.length; i++) {
        for (int j = 0; j < vecs.length; j++) {
            assertEquals("Distance " + i + "," + j + " incorrect.", Math.sqrt(distances[i][j]), df.distance(vecs[i], vecs[j]), 1e-15);
        }
    }
}
Also used : NumberVector(de.lmu.ifi.dbs.elki.data.NumberVector) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) Test(org.junit.Test)

Example 83 with DoubleVector

use of de.lmu.ifi.dbs.elki.data.DoubleVector in project elki by elki-project.

the class WeightedDiscreteUncertainifier method newFeatureVector.

@Override
public <A> WeightedDiscreteUncertainObject newFeatureVector(Random rand, A array, NumberArrayAdapter<?, A> adapter) {
    UncertainObject uo = inner.newFeatureVector(rand, array, adapter);
    final int distributionSize = rand.nextInt((maxQuant - minQuant) + 1) + minQuant;
    DoubleVector[] samples = new DoubleVector[distributionSize];
    double[] weights = new double[distributionSize];
    double wsum = 0.;
    for (int i = 0; i < distributionSize; i++) {
        samples[i] = uo.drawSample(rand);
        double w = rand.nextDouble();
        while (w <= 0.) {
            // Avoid zero weights.
            w = rand.nextDouble();
        }
        weights[i] = w;
        wsum += w;
    }
    // Normalize to a total weight of 1:
    assert (wsum > 0.);
    for (int i = 0; i < distributionSize; i++) {
        weights[i] /= wsum;
    }
    return new WeightedDiscreteUncertainObject(samples, weights);
}
Also used : DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) WeightedDiscreteUncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.WeightedDiscreteUncertainObject) UncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.UncertainObject) WeightedDiscreteUncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.WeightedDiscreteUncertainObject)

Example 84 with DoubleVector

use of de.lmu.ifi.dbs.elki.data.DoubleVector in project elki by elki-project.

the class UnweightedDiscreteUncertainifier method newFeatureVector.

@Override
public <A> UnweightedDiscreteUncertainObject newFeatureVector(Random rand, A array, NumberArrayAdapter<?, A> adapter) {
    UncertainObject uo = inner.newFeatureVector(rand, array, adapter);
    final int distributionSize = rand.nextInt((maxQuant - minQuant) + 1) + minQuant;
    DoubleVector[] samples = new DoubleVector[distributionSize];
    for (int i = 0; i < distributionSize; i++) {
        samples[i] = uo.drawSample(rand);
    }
    return new UnweightedDiscreteUncertainObject(samples);
}
Also used : UnweightedDiscreteUncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.UnweightedDiscreteUncertainObject) DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector) UncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.UncertainObject) UnweightedDiscreteUncertainObject(de.lmu.ifi.dbs.elki.data.uncertain.UnweightedDiscreteUncertainObject)

Example 85 with DoubleVector

use of de.lmu.ifi.dbs.elki.data.DoubleVector in project elki by elki-project.

the class KMeansElkan method recomputeSeperation.

/**
 * Recompute the separation of cluster means.
 *
 * @param means Means
 * @param sep Output array of separation
 * @param cdist Center-to-Center distances
 */
private void recomputeSeperation(double[][] means, double[] sep, double[][] cdist) {
    final int k = means.length;
    assert (sep.length == k);
    boolean issquared = distanceFunction.isSquared();
    Arrays.fill(sep, Double.POSITIVE_INFINITY);
    for (int i = 1; i < k; i++) {
        DoubleVector mi = DoubleVector.wrap(means[i]);
        for (int j = 0; j < i; j++) {
            double d = distanceFunction.distance(mi, DoubleVector.wrap(means[j]));
            d = issquared ? FastMath.sqrt(d) : d;
            d *= .5;
            cdist[i][j] = d;
            cdist[j][i] = d;
            sep[i] = (d < sep[i]) ? d : sep[i];
            sep[j] = (d < sep[j]) ? d : sep[j];
        }
    }
}
Also used : DoubleVector(de.lmu.ifi.dbs.elki.data.DoubleVector)

Aggregations

DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)147 Test (org.junit.Test)112 Database (de.lmu.ifi.dbs.elki.database.Database)85 ELKIBuilder (de.lmu.ifi.dbs.elki.utilities.ELKIBuilder)75 AbstractClusterAlgorithmTest (de.lmu.ifi.dbs.elki.algorithm.clustering.AbstractClusterAlgorithmTest)50 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)26 AbstractDataSourceTest (de.lmu.ifi.dbs.elki.datasource.AbstractDataSourceTest)24 OutlierResult (de.lmu.ifi.dbs.elki.result.outlier.OutlierResult)22 AbstractOutlierAlgorithmTest (de.lmu.ifi.dbs.elki.algorithm.outlier.AbstractOutlierAlgorithmTest)16 ArrayList (java.util.ArrayList)14 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)12 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)11 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)10 VectorFieldTypeInformation (de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation)9 ListParameterization (de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.ListParameterization)9 Random (java.util.Random)9 AbstractSimpleAlgorithmTest (de.lmu.ifi.dbs.elki.algorithm.AbstractSimpleAlgorithmTest)8 Model (de.lmu.ifi.dbs.elki.data.model.Model)8 LinearScanDistanceKNNQuery (de.lmu.ifi.dbs.elki.database.query.knn.LinearScanDistanceKNNQuery)8 MedoidModel (de.lmu.ifi.dbs.elki.data.model.MedoidModel)7