List of usage examples for org.apache.commons.math3.linear RealVector ebeDivide
@Deprecated public abstract RealVector ebeDivide(RealVector v) throws DimensionMismatchException;
From source file:edu.stanford.cfuller.colocalization3d.correction.Correction.java
/** * Applies an existing correction to a single x-y position in the Image plane. * * @param x The x-position at which to apply the correction. * @param y The y-position at which to apply the correction. * @return A RealVector containing 3 elements-- the magnitude of the correction in the x, y, and z dimensions, in that order. *//*from w w w .j a va2 s. co m*/ public RealVector correctPosition(double x, double y) throws UnableToCorrectException { RealVector corrections = new ArrayRealVector(3, 0.0); RealVector distsToCentroids = this.getPositionsForCorrection().getColumnVector(0).mapSubtract(x) .mapToSelf(new Power(2)); distsToCentroids = distsToCentroids .add(this.getPositionsForCorrection().getColumnVector(1).mapSubtract(y).mapToSelf(new Power(2))); distsToCentroids.mapToSelf(new Sqrt()); RealVector distRatio = distsToCentroids.ebeDivide(this.getDistanceCutoffs()); RealVector distRatioBin = new ArrayRealVector(distRatio.getDimension(), 0.0); for (int i = 0; i < distRatio.getDimension(); i++) { if (distRatio.getEntry(i) <= 1) distRatioBin.setEntry(i, 1.0); } RealVector weights = distRatio.map(new Power(2.0)).mapMultiplyToSelf(-3).mapAddToSelf(1) .add(distRatio.map(new Power(3.0)).mapMultiplyToSelf(2)); weights = weights.ebeMultiply(distRatioBin); double sumWeights = 0; int countWeights = 0; for (int i = 0; i < weights.getDimension(); i++) { if (weights.getEntry(i) > 0) { sumWeights += weights.getEntry(i); countWeights++; } } if (countWeights == 0) { // this means there were no points in the correction dataset near the position being corrected. throw (new UnableToCorrectException( "Incomplete coverage in correction dataset at (x,y) = (" + x + ", " + y + ").")); } RealMatrix cX = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealMatrix cY = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealMatrix cZ = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealVector xVec = new ArrayRealVector(countWeights, 0.0); RealVector yVec = new ArrayRealVector(countWeights, 0.0); RealVector keptWeights = new ArrayRealVector(countWeights, 0.0); int keptCounter = 0; for (int i = 0; i < weights.getDimension(); i++) { if (weights.getEntry(i) > 0) { cX.setRowVector(keptCounter, this.getCorrectionX().getRowVector(i)); cY.setRowVector(keptCounter, this.getCorrectionY().getRowVector(i)); cZ.setRowVector(keptCounter, this.getCorrectionZ().getRowVector(i)); xVec.setEntry(keptCounter, x - this.getPositionsForCorrection().getEntry(i, 0)); yVec.setEntry(keptCounter, y - this.getPositionsForCorrection().getEntry(i, 1)); keptWeights.setEntry(keptCounter, weights.getEntry(i)); keptCounter++; } } double xCorr = 0; double yCorr = 0; double zCorr = 0; RealMatrix allCorrectionParameters = new Array2DRowRealMatrix(countWeights, numberOfCorrectionParameters); RealVector ones = new ArrayRealVector(countWeights, 1.0); allCorrectionParameters.setColumnVector(0, ones); allCorrectionParameters.setColumnVector(1, xVec); allCorrectionParameters.setColumnVector(2, yVec); allCorrectionParameters.setColumnVector(3, xVec.map(new Power(2))); allCorrectionParameters.setColumnVector(4, yVec.map(new Power(2))); allCorrectionParameters.setColumnVector(5, xVec.ebeMultiply(yVec)); for (int i = 0; i < countWeights; i++) { xCorr += allCorrectionParameters.getRowVector(i).dotProduct(cX.getRowVector(i)) * keptWeights.getEntry(i); yCorr += allCorrectionParameters.getRowVector(i).dotProduct(cY.getRowVector(i)) * keptWeights.getEntry(i); zCorr += allCorrectionParameters.getRowVector(i).dotProduct(cZ.getRowVector(i)) * keptWeights.getEntry(i); } xCorr /= sumWeights; yCorr /= sumWeights; zCorr /= sumWeights; corrections.setEntry(0, xCorr); corrections.setEntry(1, yCorr); corrections.setEntry(2, zCorr); return corrections; }
From source file:org.lenskit.pf.HPFModelProvider.java
@Override public HPFModel get() { final int userNum = ratings.getUserIndex().size(); final int itemNum = ratings.getItemIndex().size(); final int featureCount = hyperParameters.getFeatureCount(); final double a = hyperParameters.getUserWeightShpPrior(); final double aPrime = hyperParameters.getUserActivityShpPrior(); final double bPrime = hyperParameters.getUserActivityPriorMean(); final double c = hyperParameters.getItemWeightShpPrior(); final double cPrime = hyperParameters.getItemActivityShpPrior(); final double dPrime = hyperParameters.getItemActivityPriorMean(); final double kappaShpU = aPrime + featureCount * a; final double tauShpI = cPrime + featureCount * c; RealMatrix gammaShp = MatrixUtils.createRealMatrix(userNum, featureCount); RealMatrix gammaRte = MatrixUtils.createRealMatrix(userNum, featureCount); RealVector kappaShp = MatrixUtils.createRealVector(new double[userNum]); RealVector kappaRte = MatrixUtils.createRealVector(new double[userNum]); RealMatrix lambdaShp = MatrixUtils.createRealMatrix(itemNum, featureCount); RealMatrix lambdaRte = MatrixUtils.createRealMatrix(itemNum, featureCount); RealVector tauShp = MatrixUtils.createRealVector(new double[itemNum]); RealVector tauRte = MatrixUtils.createRealVector(new double[itemNum]); RealMatrix gammaShpNext = MatrixUtils.createRealMatrix(userNum, featureCount); RealMatrix lambdaShpNext = MatrixUtils.createRealMatrix(itemNum, featureCount); gammaShpNext = gammaShpNext.scalarAdd(a); lambdaShpNext = lambdaShpNext.scalarAdd(c); RealVector phiUI = MatrixUtils.createRealVector(new double[featureCount]); initialize(gammaShp, gammaRte, kappaRte, kappaShp, lambdaShp, lambdaRte, tauRte, tauShp); logger.info("initialization finished"); final List<RatingMatrixEntry> train = ratings.getTrainRatings(); final List<RatingMatrixEntry> validation = ratings.getValidationRatings(); double avgPLLPre = Double.MAX_VALUE; double avgPLLCurr = 0.0; double diffPLL = 1.0; int iterCount = 1; while (iterCount < maxIterCount && diffPLL > threshold) { // update phi Iterator<RatingMatrixEntry> allUIPairs = train.iterator(); while (allUIPairs.hasNext()) { RatingMatrixEntry entry = allUIPairs.next(); int item = entry.getItemIndex(); int user = entry.getUserIndex(); double ratingUI = entry.getValue(); if (ratingUI <= 0) { continue; }//from w ww . j av a2 s . co m for (int k = 0; k < featureCount; k++) { double gammaShpUK = gammaShp.getEntry(user, k); double gammaRteUK = gammaRte.getEntry(user, k); double lambdaShpIK = lambdaShp.getEntry(item, k); double lambdaRteIK = lambdaRte.getEntry(item, k); double phiUIK = Gamma.digamma(gammaShpUK) - Math.log(gammaRteUK) + Gamma.digamma(lambdaShpIK) - Math.log(lambdaRteIK); phiUI.setEntry(k, phiUIK); } logNormalize(phiUI); if (ratingUI > 1) { phiUI.mapMultiplyToSelf(ratingUI); } for (int k = 0; k < featureCount; k++) { double value = phiUI.getEntry(k); gammaShpNext.addToEntry(user, k, value); lambdaShpNext.addToEntry(item, k, value); } } logger.info("iteration {} first phrase update finished", iterCount); RealVector gammaRteSecondTerm = MatrixUtils.createRealVector(new double[featureCount]); for (int k = 0; k < featureCount; k++) { double gammaRteUK = 0.0; for (int item = 0; item < itemNum; item++) { gammaRteUK += lambdaShp.getEntry(item, k) / lambdaRte.getEntry(item, k); } gammaRteSecondTerm.setEntry(k, gammaRteUK); } // update user parameters double kappaRteFirstTerm = aPrime / bPrime; for (int user = 0; user < userNum; user++) { double gammaRteUKFirstTerm = kappaShp.getEntry(user) / kappaRte.getEntry(user); double kappaRteU = 0.0; for (int k = 0; k < featureCount; k++) { double gammaShpUK = gammaShpNext.getEntry(user, k); gammaShp.setEntry(user, k, gammaShpUK); gammaShpNext.setEntry(user, k, a); double gammaRteUK = gammaRteSecondTerm.getEntry(k); gammaRteUK += gammaRteUKFirstTerm; gammaRte.setEntry(user, k, gammaRteUK); kappaRteU += gammaShpUK / gammaRteUK; } kappaRteU += kappaRteFirstTerm; kappaRte.setEntry(user, kappaRteU); } logger.info("iteration {} second phrase update finished", iterCount); RealVector lambdaRteSecondTerm = MatrixUtils.createRealVector(new double[featureCount]); for (int k = 0; k < featureCount; k++) { double lambdaRteIK = 0.0; for (int user = 0; user < userNum; user++) { lambdaRteIK += gammaShp.getEntry(user, k) / gammaRte.getEntry(user, k); } lambdaRteSecondTerm.setEntry(k, lambdaRteIK); } // update item parameters double tauRteFirstTerm = cPrime / dPrime; for (int item = 0; item < itemNum; item++) { double lambdaRteFirstTerm = tauShp.getEntry(item) / tauRte.getEntry(item); double tauRteI = 0.0; for (int k = 0; k < featureCount; k++) { double lambdaShpIK = lambdaShpNext.getEntry(item, k); lambdaShp.setEntry(item, k, lambdaShpIK); lambdaShpNext.setEntry(item, k, c); double lambdaRteIK = lambdaRteSecondTerm.getEntry(k); // plus first term lambdaRteIK += lambdaRteFirstTerm; lambdaRte.setEntry(item, k, lambdaRteIK); // update tauRteI second term tauRteI += lambdaShpIK / lambdaRteIK; } tauRteI += tauRteFirstTerm; tauRte.setEntry(item, tauRteI); } logger.info("iteration {} third phrase update finished", iterCount); // compute average predictive log likelihood of validation data per {@code iterationfrequency} iterations if (iterCount == 1) { for (int user = 0; user < userNum; user++) { kappaShp.setEntry(user, kappaShpU); } for (int item = 0; item < itemNum; item++) { tauShp.setEntry(item, tauShpI); } } if ((iterCount % iterationFrequency) == 0) { Iterator<RatingMatrixEntry> valIter = validation.iterator(); avgPLLCurr = 0.0; while (valIter.hasNext()) { RatingMatrixEntry ratingEntry = valIter.next(); int user = ratingEntry.getUserIndex(); int item = ratingEntry.getItemIndex(); double rating = ratingEntry.getValue(); double eThetaBeta = 0.0; for (int k = 0; k < featureCount; k++) { double eThetaUK = gammaShp.getEntry(user, k) / gammaRte.getEntry(user, k); double eBetaIK = lambdaShp.getEntry(item, k) / lambdaRte.getEntry(item, k); eThetaBeta += eThetaUK * eBetaIK; } double pLL = 0.0; if (isProbPredition) { pLL = (rating == 0) ? (-eThetaBeta) : Math.log(1 - Math.exp(-eThetaBeta)); } else { pLL = rating * Math.log(eThetaBeta) - eThetaBeta - Gamma.logGamma(rating + 1); } avgPLLCurr += pLL; } avgPLLCurr = avgPLLCurr / validation.size(); diffPLL = Math.abs((avgPLLCurr - avgPLLPre) / avgPLLPre); avgPLLPre = avgPLLCurr; logger.info("iteration {} with current average predictive log likelihood {} and the change is {}", iterCount, avgPLLCurr, diffPLL); } iterCount++; } // construct feature matrix used by HPFModel RealMatrix eTheta = MatrixUtils.createRealMatrix(userNum, featureCount); RealMatrix eBeta = MatrixUtils.createRealMatrix(itemNum, featureCount); for (int user = 0; user < userNum; user++) { RealVector gammaShpU = gammaShp.getRowVector(user); RealVector gammaRteU = gammaRte.getRowVector(user); RealVector eThetaU = gammaShpU.ebeDivide(gammaRteU); eTheta.setRowVector(user, eThetaU); logger.info("Training user {} features finished", user); } for (int item = 0; item < itemNum; item++) { RealVector lambdaShpI = lambdaShp.getRowVector(item); RealVector lambdaRteI = lambdaRte.getRowVector(item); RealVector eBetaI = lambdaShpI.ebeDivide(lambdaRteI); eBeta.setRowVector(item, eBetaI); logger.info("Training item {} features finished", item); } KeyIndex uidx = ratings.getUserIndex(); KeyIndex iidx = ratings.getItemIndex(); return new HPFModel(eTheta, eBeta, uidx, iidx); }