/** {@inheritDoc} */ @Override protected void remove(Record r) { assertModifiable(); if(size == 0) { throw new IllegalArgumentException("The cluster is empty."); } size--; RealVector rv = DataframeMatrix.parseRecord(r, featureIds); //update cluster clusterParameters xi_sum=xi_sum.subtract(rv); xi_square_sum=xi_square_sum.subtract(rv.outerProduct(rv)); updateClusterParameters(); }
/** Same as checkMmul, but for matrix subtraction */ public static boolean checkSubtract(INDArray first, INDArray second, double maxRelativeDifference, double minAbsDifference) { RealMatrix rmFirst = convertToApacheMatrix(first); RealMatrix rmSecond = convertToApacheMatrix(second); INDArray result = first.sub(second); RealMatrix rmResult = rmFirst.subtract(rmSecond); if (!checkShape(rmResult, result)) return false; boolean ok = checkEntries(rmResult, result, maxRelativeDifference, minAbsDifference); if (!ok) { INDArray onCopies = Shape.toOffsetZeroCopy(first).sub(Shape.toOffsetZeroCopy(second)); printFailureDetails(first, second, rmResult, result, onCopies, "sub"); } return ok; }
/** * Update of the evolution paths ps and pc. * * @param zmean Weighted row matrix of the gaussian random numbers generating * the current offspring. * @param xold xmean matrix of the previous generation. * @return hsig flag indicating a small correction. */ private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) { ps = ps.scalarMultiply(1 - cs).add( B.multiply(zmean).scalarMultiply(FastMath.sqrt(cs * (2 - cs) * mueff))); normps = ps.getFrobeniusNorm(); final boolean hsig = normps / FastMath.sqrt(1 - FastMath.pow(1 - cs, 2 * iterations)) / chiN < 1.4 + 2 / ((double) dimension + 1); pc = pc.scalarMultiply(1 - cc); if (hsig) { pc = pc.add(xmean.subtract(xold).scalarMultiply(FastMath.sqrt(cc * (2 - cc) * mueff) / sigma)); } return hsig; }
/** * Update of the evolution paths ps and pc. * * @param zmean Weighted row matrix of the gaussian random numbers generating * the current offspring. * @param xold xmean matrix of the previous generation. * @return hsig flag indicating a small correction. */ private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) { ps = ps.scalarMultiply(1 - cs).add( B.multiply(zmean).scalarMultiply( FastMath.sqrt(cs * (2 - cs) * mueff))); normps = ps.getFrobeniusNorm(); final boolean hsig = normps / FastMath.sqrt(1 - FastMath.pow(1 - cs, 2 * iterations)) / chiN < 1.4 + 2 / ((double) dimension + 1); pc = pc.scalarMultiply(1 - cc); if (hsig) { pc = pc.add(xmean.subtract(xold).scalarMultiply(FastMath.sqrt(cc * (2 - cc) * mueff) / sigma)); } return hsig; }
/** {@inheritDoc} */ @Override protected void updateClusterParameters() { assertModifiable(); //fetch hyperparameters int kappa_n = kappa0 + size; int nu = nu0 + size; RealVector mu = xi_sum.mapDivide(size); RealVector mu_mu0 = mu.subtract(mu0); RealMatrix C = xi_square_sum.subtract( ( mu.outerProduct(mu) ).scalarMultiply(size) ); RealMatrix psi = psi0.add( C.add( ( mu_mu0.outerProduct(mu_mu0) ).scalarMultiply(kappa0*size/(double)kappa_n) ));// mean = ( mu0.mapMultiply(kappa0) ).add( mu.mapMultiply(size) ).mapDivide(kappa_n); synchronized(this) { covariance = psi.scalarMultiply( (kappa_n+1.0)/(kappa_n*(nu - dimensions + 1.0)) ); cache_covariance_determinant = null; cache_covariance_inverse = null; } meanError = calculateMeanError(psi, kappa_n, nu); meanDf = nu-dimensions+1; } }
errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
double negccov = 0; if (ccov1 + ccovmu > 0) { final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu)) .scalarMultiply(1 / sigma); // mu difference vectors final RealMatrix roneu = pc.multiply(pc.transpose()) .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))) .subtract(Cneg.scalarMultiply(negccov)); } else {
final RealMatrix tmp1 = a.subtract(b.multiply(dInv).multiply(c)); final SingularValueDecomposition tmp1Dec = new SingularValueDecomposition(tmp1); final DecompositionSolver tmp1Solver = tmp1Dec.getSolver(); final RealMatrix tmp2 = d.subtract(c.multiply(aInv).multiply(b)); final SingularValueDecomposition tmp2Dec = new SingularValueDecomposition(tmp2); final DecompositionSolver tmp2Solver = tmp2Dec.getSolver();
double negccov = 0; if (ccov1 + ccovmu > 0) { final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu)) .scalarMultiply(1 / sigma); // mu difference vectors final RealMatrix roneu = pc.multiply(pc.transpose()) .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))) .subtract(Cneg.scalarMultiply(negccov)); } else {
private double computeS(double mu) { double SPenalty = spenalty * mu; double[][] penalizedS = softThreshold(X.subtract(L).getData(), SPenalty); S = MatrixUtils.createRealMatrix(penalizedS); return l1norm(penalizedS) * SPenalty; }
private double computeS(double mu) { double SPenalty = spenalty * mu; double[][] penalizedS = softThreshold(X.subtract(L).getData(), SPenalty); S = MatrixUtils.createRealMatrix(penalizedS); return l1norm(penalizedS) * SPenalty; }
private double computeS(double mu) { double SPenalty = spenalty * mu; double[][] penalizedS = softThreshold(X.subtract(L).getData(), SPenalty); S = MatrixUtils.createRealMatrix(penalizedS); return l1norm(penalizedS) * SPenalty; }
@Override public double getScoreForResponse(SurveyResponse surveyResponse) throws SurveyException { RealMatrix obs = new BlockRealMatrix(new double[][]{surveyResponse.getPoint()}); RealMatrix diff = obs.subtract(new BlockRealMatrix(new double[][]{this.means.toArray()})); RealMatrix inner = diff.multiply(covarianceMatrix).multiply(diff.transpose()); assert inner.getColumnDimension() == 1 : "Expecting one column"; assert inner.getRowDimension() == 1 : "Expecting one row"; return Math.sqrt(inner.getEntry(0,0)); }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }
private double computeL(double mu) { double LPenalty = lpenalty * mu; SingularValueDecomposition svd = new SingularValueDecomposition(X.subtract(S)); double[] penalizedD = softThreshold(svd.getSingularValues(), LPenalty); RealMatrix D_matrix = MatrixUtils.createRealDiagonalMatrix(penalizedD); L = svd.getU().multiply(D_matrix).multiply(svd.getVT()); return sum(penalizedD) * LPenalty; }