/** * Compute the Mahalanobis distance from the centroid for a given vector. * * @param vec Vector * @return Mahalanobis distance */ public double mahalanobisDistance(Vector vec) { double[] difference = vec.minus(mean).getArrayRef(); double agg = 0.; for(int i = 0; i < difference.length; i++) { agg += difference[i] / variance * difference[i]; } return agg; }
/** * Compute the Mahalanobis distance from the centroid for a given vector. * * @param vec Vector * @return Mahalanobis distance */ public double mahalanobisDistance(Vector vec) { double[] difference = vec.minus(mean).getArrayRef(); double agg = 0.; for(int i = 0; i < variances.length; i++) { agg += difference[i] / variances[i] * difference[i]; } return agg; }
/** * Returns the distance of Matrix p from the hyperplane underlying this * solution. * * @param p a vector in the space underlying this solution * @return the distance of p from the hyperplane underlying this solution */ private double distance(Vector p) { // TODO: Is there a particular reason not to do this: // return p.minus(centroid).projection(weakEigenvectors).euclideanNorm(0); // V_affin = V + a // dist(p, V_affin) = d(p-a, V) = ||p - a - proj_V(p-a) || Vector p_minus_a = p.minus(centroid); Vector proj = p_minus_a.projection(strongEigenvectors); return p_minus_a.minus(proj).euclideanLength(); }
/** * Compute the Mahalanobis distance from the centroid for a given vector. * * @param vec Vector * @return Mahalanobis distance */ public double mahalanobisDistance(Vector vec) { if(invCovMatr != null) { return VMath.mahalanobisDistance(invCovMatr.getArrayRef(), vec.getArrayRef(), mref); } Vector difference = vec.minus(mean); return difference.transposeTimes(difference); }
/** * Split an existing centroid into two initial centers. * * @param parentCluster Existing cluster * @param relation Data relation * @return List of new centroids */ protected List<? extends NumberVector> splitCentroid(Cluster<? extends MeanModel> parentCluster, Relation<V> relation) { Vector parentCentroid = parentCluster.getModel().getMean(); // Compute size of cluster/region double radius = 0.; for(DBIDIter it = parentCluster.getIDs().iter(); it.valid(); it.advance()) { double d = getDistanceFunction().distance(relation.get(it), parentCentroid); radius = (d > radius) ? d : radius; } // Choose random vector Random random = rnd.getSingleThreadedRandom(); final int dim = RelationUtil.dimensionality(relation); Vector randomVector = VectorUtil.randomVector(Vector.FACTORY, dim, random).normalize(); randomVector.timesEquals((.4 + random.nextDouble() * .5) * radius); // Get the new centroids ArrayList<Vector> vecs = new ArrayList<>(2); vecs.add(parentCentroid.minus(randomVector)); vecs.add(randomVector.plusEquals(parentCentroid)); return vecs; }
Vector q = pc[k]; Vector ppq = pc[j].plus(q).timesEquals(MathUtil.SQRTHALF); Vector pmq = pc[j].minus(q).timesEquals(MathUtil.SQRTHALF); hull.add(ppq); hull.add(ppq.times(-1)); Vector ppqpr = ppq.plus(r).timesEquals(Math.sqrt(1 / 3.)); Vector pmqpr = pmq.plus(r).timesEquals(Math.sqrt(1 / 3.)); Vector ppqmr = ppq.minus(r).timesEquals(Math.sqrt(1 / 3.)); Vector pmqmr = pmq.minus(r).timesEquals(Math.sqrt(1 / 3.)); hull.add(ppqpr); hull.add(ppqpr.times(-1));
/** * Returns an orthonormalization of this matrix. * * @return the orthonormalized matrix */ public final Matrix orthonormalize() { Matrix v = copy(); // FIXME: optimize - excess copying! for(int i = 1; i < columndimension; i++) { final Vector u_i = getCol(i); final Vector sum = new Vector(elements.length); for(int j = 0; j < i; j++) { final Vector v_j = v.getCol(j); double scalar = u_i.transposeTimes(v_j) / v_j.transposeTimes(v_j); sum.plusTimesEquals(v_j, scalar); } final Vector v_i = u_i.minus(sum); v.setCol(i, v_i); } v.normalizeColumns(); return v; }
/** * Build a convex hull to approximate the sphere. * * @param pc Principal components * @return Polygon */ protected Polygon makeHull(Vector[] pc) { GrahamScanConvexHull2D hull = new GrahamScanConvexHull2D(); Vector diag = new Vector(0, 0); for(int j = 0; j < pc.length; j++) { hull.add(pc[j]); hull.add(pc[j].times(-1)); for(int k = j + 1; k < pc.length; k++) { Vector q = pc[k]; Vector ppq = pc[j].plus(q).timesEquals(MathUtil.SQRTHALF); Vector pmq = pc[j].minus(q).timesEquals(MathUtil.SQRTHALF); hull.add(ppq); hull.add(ppq.times(-1)); hull.add(pmq); hull.add(pmq.times(-1)); } diag.plusEquals(pc[j]); } diag.timesEquals(1.0 / Math.sqrt(pc.length)); hull.add(diag); hull.add(diag.times(-1)); return hull.getHull(); }
b = xx_inverse.timesTranspose(x).times(y); e = y.minus(x.times(b));