Example usage for org.apache.commons.math3.linear RealMatrix scalarAdd

List of usage examples for org.apache.commons.math3.linear RealMatrix scalarAdd

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix scalarAdd.

Prototype

RealMatrix scalarAdd(double d);

Source Link

Document

Returns the result of adding d to each entry of this .

Usage

From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java

/**
 * test sclarAdd/*from   w ww.j a v  a 2  s.c o  m*/
 */
@Test
public void testScalarAdd() {
    RealMatrix m = new BigSparseRealMatrix(testData);
    TestUtils.assertEquals("scalar add", new BigSparseRealMatrix(testDataPlus2), m.scalarAdd(2d),
            entryTolerance);
}

From source file:org.knime.al.util.noveltydetection.knfst.KNFST.java

private static RealMatrix centerKernelMatrix(final RealMatrix kernelMatrix) {
    // get size of kernelMatrix
    final int n = kernelMatrix.getRowDimension();

    // get mean values for each row/column
    final RealVector columnMeans = MatrixFunctions.columnMeans(kernelMatrix);
    final double matrixMean = MatrixFunctions.mean(kernelMatrix);

    RealMatrix centeredKernelMatrix = kernelMatrix.copy();

    for (int k = 0; k < n; k++) {
        centeredKernelMatrix.setRowVector(k, centeredKernelMatrix.getRowVector(k).subtract(columnMeans));
        centeredKernelMatrix.setColumnVector(k, centeredKernelMatrix.getColumnVector(k).subtract(columnMeans));
    }/*  w  ww.ja v a  2s.c  o m*/

    centeredKernelMatrix = centeredKernelMatrix.scalarAdd(matrixMean);

    return centeredKernelMatrix;
}

From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java

public static RealMatrix ones(final int rowCount, final int columnCount) {
    final RealMatrix ones = MatrixUtils.createRealMatrix(rowCount, columnCount);
    return ones.scalarAdd(1);
}

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;
            }/*  w  w w .j a  va2  s .  c o 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);
}