Example usage for org.apache.commons.math3.linear RealVector addToEntry

List of usage examples for org.apache.commons.math3.linear RealVector addToEntry

Introduction

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

Prototype

public void addToEntry(int index, double increment) throws OutOfRangeException 

Source Link

Document

Change an entry at the specified index.

Usage

From source file:edu.byu.nlp.stats.DirichletMLEOptimizableTest.java

/**
 * Computes a Newton-Raphson update in-place to alpha.
 *//*w w w.j  av  a 2  s .c  o m*/
private static RealVector newtonRaphsonUpdate(final double[][] data, double[] alpha) {
    // We'll compute the gold-standard value the "long" way (taking the inverse of the Hessian)
    RealMatrix hessian = new Array2DRowRealMatrix(alpha.length, alpha.length);
    for (int r = 0; r < hessian.getRowDimension(); r++) {
        for (int c = 0; c < hessian.getColumnDimension(); c++) {
            hessian.addToEntry(r, c, data.length * Gamma.trigamma(DoubleArrays.sum(alpha)));
            if (r == c) {
                hessian.addToEntry(r, c, -data.length * Gamma.trigamma(alpha[r]));
            }
        }
    }
    RealVector derivative = new ArrayRealVector(alpha.length);
    for (int k = 0; k < alpha.length; k++) {
        derivative.setEntry(k,
                data.length * (Gamma.digamma(DoubleArrays.sum(alpha)) - Gamma.digamma(alpha[k])));
        for (double[] theta : data) {
            derivative.addToEntry(k, theta[k]);
        }
    }

    RealMatrix hessianInverse = new LUDecomposition(hessian).getSolver().getInverse();
    RealVector negDiff = hessianInverse.preMultiply(derivative);
    negDiff.mapMultiplyToSelf(-1.0);

    RealVector expected = new ArrayRealVector(alpha, true);
    return expected.add(negDiff);
}

From source file:com.mothsoft.alexis.engine.textual.DocumentFeatures.java

private void increment(RealVector vector, Integer id, int increment) {
    vector.addToEntry(id, increment);
}

From source file:com.analog.lyric.dimple.test.solvers.sumproduct.TestSampledFactors.java

/**
 * Adapted from MATLAB test4 in tests/algoGaussian/testSampledFactors.m
 *//* w ww  .  j a  v  a  2s  .  c  o m*/
@Test
public void sampledComplexProduct() {
    // NOTE: test may fail if seed is changed! We keep the number of samples down so that the test doesn't
    // take too long. Increasing the samples produces better results.

    testRand.setSeed(42);

    try (CurrentModel cur = using(new FactorGraph())) {
        final Complex a = complex("a");
        final Complex b = complex("b");
        final Complex c = product(a, b);

        double[] aMean = new double[] { 10, 10 };
        RealMatrix aCovariance = randCovariance(2);
        a.setPrior(new MultivariateNormal(aMean, aCovariance.getData()));

        double[] bMean = new double[] { -20, 20 };
        RealMatrix bCovariance = randCovariance(2);
        b.setPrior(new MultivariateNormalParameters(bMean, bCovariance.getData()));

        GaussianRandomGenerator normalGenerator = new GaussianRandomGenerator(testRand);
        CorrelatedRandomVectorGenerator aGenerator = new CorrelatedRandomVectorGenerator(aMean, aCovariance,
                1e-12, normalGenerator);
        CorrelatedRandomVectorGenerator bGenerator = new CorrelatedRandomVectorGenerator(bMean, bCovariance,
                1e-12, normalGenerator);

        StorelessCovariance expectedCov = new StorelessCovariance(2);

        final int nSamples = 10000;

        RealVector expectedMean = MatrixUtils.createRealVector(new double[2]);
        double[] cSample = new double[2];

        for (int i = 0; i < nSamples; ++i) {
            double[] aSample = aGenerator.nextVector();
            double[] bSample = bGenerator.nextVector();

            // Compute complex product
            cSample[0] = aSample[0] * bSample[0] - aSample[1] * bSample[1];
            cSample[1] = aSample[0] * bSample[1] + aSample[1] * bSample[0];

            expectedMean.addToEntry(0, cSample[0]);
            expectedMean.addToEntry(1, cSample[1]);

            expectedCov.increment(cSample);
        }

        expectedMean.mapDivideToSelf(nSamples); // normalize

        SumProductSolverGraph sfg = requireNonNull(cur.graph.setSolverFactory(new SumProductSolver()));
        sfg.setOption(GibbsOptions.numSamples, nSamples);

        sfg.solve();

        MultivariateNormalParameters cBelief = requireNonNull(c.getBelief());

        RealVector observedMean = MatrixUtils.createRealVector(cBelief.getMean());
        double scaledMeanDistance = expectedMean.getDistance(observedMean) / expectedMean.getNorm();

        //         System.out.format("expectedMean = %s\n", expectedMean);
        //         System.out.format("observedMean = %s\n", observedMean);
        //         System.out.println(scaledMeanDistance);

        assertEquals(0.0, scaledMeanDistance, .02);

        RealMatrix expectedCovariance = expectedCov.getCovarianceMatrix();
        RealMatrix observedCovariance = MatrixUtils.createRealMatrix(cBelief.getCovariance());
        RealMatrix diffCovariance = expectedCovariance.subtract(observedCovariance);

        double scaledCovarianceDistance = diffCovariance.getNorm() / expectedCovariance.getNorm();

        //         System.out.println(expectedCovariance);
        //         System.out.println(expectedCovariance.getNorm());
        //         System.out.println(diffCovariance);
        //         System.out.println(diffCovariance.getNorm());
        //         System.out.println(diffCovariance.getNorm() / expectedCovariance.getNorm());

        assertEquals(0.0, scaledCovarianceDistance, .2);
    }
}

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

public static RealVector rowSums(final RealMatrix matrix) {
    final RealVector rowSums = MatrixUtils.createRealVector(new double[matrix.getRowDimension()]);
    for (int r = 0; r < matrix.getRowDimension(); r++) {
        final double[] row = matrix.getRow(r);
        for (final double cell : row) {
            rowSums.addToEntry(r, cell);
        }/*from   w w w.ja v  a 2  s .c  o  m*/
    }
    return rowSums;
}

From source file:org.lambda3.indra.util.VectorIterator.java

public RealVector readSparseVector(int dimensions) throws IOException {
    RealVector vector = new OpenMapRealVector(dimensions);

    int size = input.readInt();
    for (int i = 0; i < size; i++) {
        vector.addToEntry(input.readInt(), (double) input.readFloat());
    }/*from ww w .  j a  v a  2s  .co  m*/

    return vector;
}

From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java

/**
 * Do a single feature iteration./*www.j  a v  a2 s. c  o m*/
 *
 *
 *
 * @param estimates The estimates.
 * @param ratings   The ratings to train on.
 * @param userFeatureVector The user column vector for the current feature.
 * @param itemFeatureVector The item column vector for the current feature.
 * @param trail The sum of the remaining user-item-feature values.
 * @return The RMSE of the feature iteration.
 */
protected double doFeatureIteration(TrainingEstimator estimates, List<RatingMatrixEntry> ratings,
        RealVector userFeatureVector, RealVector itemFeatureVector, double trail) {
    // We'll create a fresh updater for each feature iteration
    // Not much overhead, and prevents needing another parameter
    FunkSVDUpdater updater = rule.createUpdater();

    for (RatingMatrixEntry r : ratings) {
        final int uidx = r.getUserIndex();
        final int iidx = r.getItemIndex();

        updater.prepare(0, r.getValue(), estimates.get(r), userFeatureVector.getEntry(uidx),
                itemFeatureVector.getEntry(iidx), trail);

        // Step 3: Update feature values
        userFeatureVector.addToEntry(uidx, updater.getUserFeatureUpdate());
        itemFeatureVector.addToEntry(iidx, updater.getItemFeatureUpdate());
    }

    return updater.getRMSE();
}

From source file:org.lenskit.mf.funksvd.FunkSVDModelProvider.java

/**
 * Do a single feature iteration./*from w w w  .java2s  .c  o  m*/
 *
 *
 *
 * @param estimates The estimates.
 * @param ratings   The ratings to train on.
 * @param userFeatureVector The user column vector for the current feature.
 * @param itemFeatureVector The item column vector for the current feature.
 * @param trail The sum of the remaining user-item-feature values.
 * @return The RMSE of the feature iteration.
 */
protected double doFeatureIteration(TrainingEstimator estimates, List<RatingMatrixEntry> ratings,
        RealVector userFeatureVector, RealVector itemFeatureVector, double trail) {
    // We'll create a fresh updater for each feature iteration
    // Not much overhead, and prevents needing another parameter
    FunkSVDTrainingUpdater updater = rule.createUpdater();

    double acc_ud = 0;
    double acc_id = 0;

    for (RatingMatrixEntry r : ratings) {
        final int uidx = r.getUserIndex();
        final int iidx = r.getItemIndex();

        updater.prepare(0, r.getValue(), estimates.get(r), userFeatureVector.getEntry(uidx),
                itemFeatureVector.getEntry(iidx), trail);

        // Step 3: Update feature values
        double ud = updater.getUserFeatureUpdate();
        acc_ud += ud * ud;
        userFeatureVector.addToEntry(uidx, ud);
        double id = updater.getItemFeatureUpdate();
        acc_id += id * id;
        itemFeatureVector.addToEntry(iidx, id);
    }

    logger.trace("finished iteration with |du|={}, |di|={}", Math.sqrt(acc_ud), Math.sqrt(acc_id));
    return updater.getRMSE();
}

From source file:scorePairing.ScorePairingWithStaticMethods.java

public static ResultFromScorePairing computeCost(PairingAndNullSpaces currentNewPairingAndNewNullSpaces,
        Map<Integer, PointWithPropertiesIfc> queryShape, Map<Integer, PointWithPropertiesIfc> hitShape,
        AlgoParameters algoParameters) {

    int countOfPairedPoint = currentNewPairingAndNewNullSpaces.getPairing().size();

    double[][] matrixPointsModelDouble = new double[3][countOfPairedPoint];
    double[][] matrixPointsCandidateDouble = new double[3][countOfPairedPoint];

    RealMatrix matrixPointsModel = new Array2DRowRealMatrix(matrixPointsModelDouble);
    RealMatrix matrixPointsCandidate = new Array2DRowRealMatrix(matrixPointsCandidateDouble);

    int currentPoint = 0;

    RealVector sumVector1 = new ArrayRealVector(new double[3]);
    RealVector sumVector2 = new ArrayRealVector(new double[3]);

    sumVector1.setEntry(0, 0.0);/*from  w  w w . j  a va2  s  .  c  o  m*/
    sumVector1.setEntry(1, 0.0);
    sumVector1.setEntry(2, 0.0);
    sumVector2.setEntry(0, 0.0);
    sumVector2.setEntry(1, 0.0);
    sumVector2.setEntry(2, 0.0);

    for (Map.Entry<Integer, Integer> entry : currentNewPairingAndNewNullSpaces.getPairing().entrySet()) {

        Integer point1id = Integer.valueOf(entry.getKey().intValue());
        Integer point2id = Integer.valueOf(entry.getValue().intValue());

        // the 2 points could be defined as static be static
        PointWithPropertiesIfc point1 = queryShape.get(point1id);
        PointWithPropertiesIfc point2 = hitShape.get(point2id);

        // matrix for procrustes
        matrixPointsModel.setEntry(0, currentPoint, point1.getCoords().getCoords()[0]);
        matrixPointsModel.setEntry(1, currentPoint, point1.getCoords().getCoords()[1]);
        matrixPointsModel.setEntry(2, currentPoint, point1.getCoords().getCoords()[2]);

        matrixPointsCandidate.setEntry(0, currentPoint, point2.getCoords().getCoords()[0]);
        matrixPointsCandidate.setEntry(1, currentPoint, point2.getCoords().getCoords()[1]);
        matrixPointsCandidate.setEntry(2, currentPoint, point2.getCoords().getCoords()[2]);

        // barycenter computation
        sumVector1.addToEntry(0, point1.getCoords().getCoords()[0]);
        sumVector1.addToEntry(1, point1.getCoords().getCoords()[1]);
        sumVector1.addToEntry(2, point1.getCoords().getCoords()[2]);

        sumVector2.addToEntry(0, point2.getCoords().getCoords()[0]);
        sumVector2.addToEntry(1, point2.getCoords().getCoords()[1]);
        sumVector2.addToEntry(2, point2.getCoords().getCoords()[2]);
        currentPoint += 1;

    }

    RealVector barycenterVector1 = sumVector1.mapDivide((double) currentPoint);
    RealVector barycenterVector2 = sumVector2.mapDivide((double) currentPoint);

    RealVector translationVectorToTranslateShape2ToOrigin = barycenterVector2.mapMultiply(-1.0);
    RealVector translationVectorToTranslateShape2ToShape1 = barycenterVector1.subtract(barycenterVector2);

    translateBarycenterListOfPointToOrigin(matrixPointsModel, barycenterVector1);
    translateBarycenterListOfPointToOrigin(matrixPointsCandidate, barycenterVector2);

    ProcrustesAnalysisIfc procrustesAnalysis = null;
    try {
        procrustesAnalysis = algoParameters.procrustesAnalysisBuffer.get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    procrustesAnalysis.run(matrixPointsModel, matrixPointsCandidate);

    RealMatrix rotationMatrixToRotateShape2ToShape1 = procrustesAnalysis.getRotationMatrix();

    try {
        algoParameters.procrustesAnalysisBuffer.put(procrustesAnalysis);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // not used: we dont really care how accurate is the overlap of points in pairs as it is within expected range because of the
    // fact that I extend hits with a distance criteria
    double distanceResidual = procrustesAnalysis.getResidual();
    // no need to use hit coverage as already used as a filter

    // no need to use the distance to outside as it is used as a filter

    // handling of probability to have a small contribution of if
    float toleranceElectronProbability = 0.2f;

    // use only properties for cost
    double costCharge = 0.0;
    double costHydrophobicity = 0.0;
    double costHbondDonnor = 0.0;
    double costHbondAcceptor = 0.0;
    double costDehydron = 0.0;
    double costAromaticRing = 0.0;
    double costProbabilityDiff = 0.0;

    for (Map.Entry<Integer, Integer> entry : currentNewPairingAndNewNullSpaces.getPairing().entrySet()) {

        Integer idFromMap1 = entry.getKey();
        Integer idFromMap2 = entry.getValue();
        PointWithPropertiesIfc point1 = queryShape.get(idFromMap1);
        PointWithPropertiesIfc point2 = hitShape.get(idFromMap2);

        float probabilityOfPoint1 = point1.getElectronProbability();
        float probabilityOfPoint2 = point2.getElectronProbability();
        float probabilityDiff = Math.abs(probabilityOfPoint1 - probabilityOfPoint2);
        if (probabilityDiff < toleranceElectronProbability) {
            probabilityDiff = 0.0f;
        } else {
            probabilityDiff -= toleranceElectronProbability;
        }
        costProbabilityDiff += probabilityDiff;

        // handle multi properties

        // Hydrophobe and Aromatic
        Float hydrophobicityOfPointFromMap1 = point1.get(PropertyName.Hydrophobicity);
        Float hydrophobicityOfPointFromMap2 = point2.get(PropertyName.Hydrophobicity);
        Float aromaticRingOfPointFromMap1 = point1.get(PropertyName.AromaticRing);
        Float aromaticRingOfPointFromMap2 = point2.get(PropertyName.AromaticRing);

        // if aromatic it is hydrophobe as well by definition
        if (aromaticRingOfPointFromMap1 != null && hydrophobicityOfPointFromMap1 == null) {
            System.out.println(aromaticRingOfPointFromMap1 != null && hydrophobicityOfPointFromMap1 == null);
        }
        if (aromaticRingOfPointFromMap2 != null && hydrophobicityOfPointFromMap2 == null) {
            System.out.println(aromaticRingOfPointFromMap2 != null && hydrophobicityOfPointFromMap2 == null);
        }
        // if one hydrophobe and not the other one
        // then cost hydrophobe
        if (hydrophobicityOfPointFromMap1 != null && hydrophobicityOfPointFromMap2 == null) {
            costHydrophobicity += returnCost(hydrophobicityOfPointFromMap1, hydrophobicityOfPointFromMap2);
        }
        if (hydrophobicityOfPointFromMap2 != null && hydrophobicityOfPointFromMap1 == null) {
            costHydrophobicity += returnCost(hydrophobicityOfPointFromMap1, hydrophobicityOfPointFromMap2);
        }
        // so a difference of hydrophobe to aromatic is neglected !!!

        Float chargeOfPointFromMap1 = point1.get(PropertyName.FormalCharge);
        Float chargeOfPointFromMap2 = point2.get(PropertyName.FormalCharge);
        costCharge += returnCost(chargeOfPointFromMap1, chargeOfPointFromMap2);

        Float hBondDonnorOfPointFromMap1 = point1.get(PropertyName.HbondDonnor);
        Float hBondDonnorOfPointFromMap2 = point2.get(PropertyName.HbondDonnor);
        Float hBondAcceptorOfPointFromMap1 = point1.get(PropertyName.HbondAcceptor);
        Float hBondAcceptorOfPointFromMap2 = point2.get(PropertyName.HbondAcceptor);

        costHbondDonnor += returnCost(hBondDonnorOfPointFromMap1, hBondDonnorOfPointFromMap2);
        costHbondAcceptor += returnCost(hBondAcceptorOfPointFromMap1, hBondAcceptorOfPointFromMap2);

        Float dehydronOfPointFromMap1 = point1.get(PropertyName.Dehydron);
        Float dehydronOfPointFromMap2 = point2.get(PropertyName.Dehydron);
        costDehydron += returnCost(dehydronOfPointFromMap1, dehydronOfPointFromMap2);

    }

    int size = currentNewPairingAndNewNullSpaces.getPairing().size();

    costCharge = costCharge / size;
    costHbondDonnor = costHbondDonnor / size;
    costHbondAcceptor = costHbondAcceptor / size;
    costDehydron = costDehydron / size;
    costHydrophobicity = costHydrophobicity / size;
    costAromaticRing = costAromaticRing / size;
    costProbabilityDiff = costProbabilityDiff / size;

    double costOnPairs = algoParameters.getWEIGHT_DIFFERENCE_IN_CHARGES_BETWEEN_PAIRED_POINTS() * costCharge
            + algoParameters.getWEIGHT_DIFFERENCE_IN_HYDROPHOBICITY_BETWEEN_PAIRED_POINTS() * costHydrophobicity
            + algoParameters.getWEIGHT_HBOND_DONNOR() * costHbondDonnor
            + algoParameters.getWEIGHT_HBOND_ACCEPTOR() * costHbondAcceptor
            + algoParameters.getWEIGHT_DEHYDRON() * costDehydron
            + algoParameters.getWEIGHT_DIFFERENCE_AROMATICRING() * costAromaticRing
            + algoParameters.getWEIGHT_DIFFERENCE_IN_PROBABILITIES_IN_PAIRED_POINTS() * costProbabilityDiff;

    //double costOnUnpairedPoints = computeRatioFromUnpairedPointsHitToAllPointsHits(currentNewPairingAndNewNullSpaces, hitShape);
    //double newFinalCost = costOnPairs + algoParameters.getWEIGHT_UNPAIRED_POINT_IN_SMALLEST_MAP() * costOnUnpairedPoints;

    double sumWeight = algoParameters.getWEIGHT_DIFFERENCE_IN_CHARGES_BETWEEN_PAIRED_POINTS()
            + algoParameters.getWEIGHT_DIFFERENCE_IN_HYDROPHOBICITY_BETWEEN_PAIRED_POINTS()
            + algoParameters.getWEIGHT_HBOND_DONNOR() + algoParameters.getWEIGHT_HBOND_ACCEPTOR()
            + algoParameters.getWEIGHT_DEHYDRON() + algoParameters.getWEIGHT_DIFFERENCE_AROMATICRING()
            + algoParameters.getWEIGHT_DIFFERENCE_IN_PROBABILITIES_IN_PAIRED_POINTS();

    double finalCost = costOnPairs / sumWeight;

    ResultFromScorePairing resultFromScorePairing = new ResultFromScorePairing(finalCost,
            rotationMatrixToRotateShape2ToShape1, translationVectorToTranslateShape2ToShape1,
            translationVectorToTranslateShape2ToOrigin, distanceResidual);
    return resultFromScorePairing;

}