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