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

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

Introduction

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

Prototype

public RealVector add(RealVector v) throws DimensionMismatchException 

Source Link

Document

Compute the sum of this vector and v .

Usage

From source file:org.rhwlab.BHCnotused.GaussianGIWPrior.java

@Override
public RealVector getMean() {
    RealVector ret = new ArrayRealVector(m.getDimension());
    int n = 0;/* w w  w . j  a v a 2  s. c  o  m*/
    for (LabeledFieldVector v : data) {
        int label = v.getLabel();
        RealVector[] vs = source.getClusterVectors(label);
        for (int i = 0; i < vs.length; ++i) {
            ret = ret.add(vs[i]);
            ++n;
        }
        ret = ret.mapDivide(n);
    }
    return ret;
}

From source file:org.rhwlab.BHCnotused.StdGaussianWishartGaussian.java

public void init() {
    int n = data.size();
    int d = m.getDimension();
    double rP = r + n;
    double nuP = nu + n;

    RealMatrix C = new Array2DRowRealMatrix(d, d);
    RealVector X = new ArrayRealVector(d); // a vector of zeros    
    for (RealVector v : data) {
        X = X.add(v);
        RealMatrix v2 = v.outerProduct(v);
        C = C.add(v2);//from   w  w  w. j a v a  2s.c o  m
    }

    RealVector mP = (m.mapMultiply(r).add(X)).mapDivide(rP);
    RealMatrix Sp = C.add(S);

    RealMatrix rmmP = mP.outerProduct(mP).scalarMultiply(rP);
    Sp = Sp.add(rmm).subtract(rmmP);

    LUDecomposition ed = new LUDecomposition(Sp);
    double detSp = Math.pow(ed.getDeterminant(), nuP / 2.0);

    double gamma = 1.0;
    double gammaP = 1.0;
    for (int i = 1; i <= d; ++i) {
        gamma = gamma * Gamma.gamma((nu + 1 - i) / 2.0);
        gammaP = gammaP * Gamma.gamma((nuP + 1 - i) / 2.0);
    }

    double t1 = Math.pow(Math.PI, -n * d / 2.0);
    double t2 = Math.pow(r / rP, d / 2.0);
    double t3 = detS / detSp;

    double t4 = gammaP / gamma;
    likelihood = t1 * t2 * t3 * t4;
}

From source file:org.rhwlab.variationalbayesian.GaussianMixture.java

public void Mstep() {
    alphaBetaNu();/*ww w  . j  av a  2 s  .c  o  m*/
    // compute the means of the components
    for (int k = 0; k < K; ++k) {
        if (inModel[k]) {
            RealVector v1 = xBar[k].mapMultiply(N[k]);
            RealVector v2 = mu0.mapMultiply(beta0);
            RealVector v3 = v2.add(v1);
            m[k] = v3.mapDivide(beta[k]);
        }
    }

    // compute the precision matrices
    for (int k = 0; k < K; ++k) {
        if (inModel[k]) {
            RealVector del = xBar[k].subtract(mu0);
            RealMatrix del2 = del.outerProduct(del);
            double f = beta0 * N[k] / (beta0 + N[k]);
            RealMatrix mat = del2.scalarMultiply(f);
            RealMatrix NS = S[k].scalarMultiply(N[k]);
            Winverse[k] = W0inverse.add(NS).add(mat);
            LUDecomposition cd = new LUDecomposition(Winverse[k]);
            W[k] = cd.getSolver().getInverse();
            detWinv[k] = cd.getDeterminant();
            detW[k] = 1.0 / cd.getDeterminant();
        }
    }
    lambdaTilde();
}

From source file:org.rhwlab.variationalbayesian.SuperVoxelGaussianMixture.java

public void Mstep() {
    lnPi();/*from   w  ww .j a v a 2s .  c  om*/
    alphaBetaNuLambda();
    // compute the means of the components
    for (int k = 0; k < K; ++k) {
        RealVector v1 = xBar[k].mapMultiply(N[k]);
        RealVector v2 = mu0.mapMultiply(beta0);
        RealVector v3 = v2.add(v1);
        m[k] = v3.mapDivide(beta[k]);
    }

    // compute the precision matrices
    for (int k = 0; k < K; ++k) {
        RealVector del = xBar[k].subtract(mu0);
        RealMatrix del2 = del.outerProduct(del);
        double f = beta0 * N[k] / (beta0 + N[k]);
        RealMatrix mat = del2.scalarMultiply(f);
        RealMatrix NS = S[k].scalarMultiply(N[k]);
        RealMatrix Winverse = W0inverse.add(NS).add(mat);
        LUDecomposition cd = new LUDecomposition(Winverse);
        W[k] = cd.getSolver().getInverse();
        detW[k] = 1.0 / cd.getDeterminant();
        int auiosdfu = 0;
    }
}

From source file:shapeCompare.PairingTools.java

public static RealVector alignPointFromShape2toShape1(RealVector trans2toOrigin, RealVector trans,
        RealMatrix rot, RealVector point) {

    RealVector translatedToOriginPoint = point.add(trans2toOrigin.copy());
    RealVector rotatedPoint = rot.operate(translatedToOriginPoint);
    RealVector translatedBackPoint = rotatedPoint.subtract(trans2toOrigin);

    RealVector translatedToShape1 = translatedBackPoint.add(trans);

    return translatedToShape1;
}

From source file:tagger.MatrixPerceptron.java

final public RealVector getScore(int[] featureID) {
    RealVector score = createRealVector(new double[N_LABELS]);
    for (int i = 0; i < featureID.length; ++i)
        score = score.add(createRealVector(weight[featureID[i]]));
    return score;
}

From source file:tagger.Supertagger.java

private Hypothesis[] beamSearch(int t, Hypothesis[] beam, Sentence sent, int[][] featureID) {
    HypoComparator orderedHypoQueue = new HypoComparator(BEAM_WIDTH);

    int[] featureID_t = featureID[t];
    RealVector featureScore = perceptron.getScore(featureID_t);
    int[][] markovFeature = null;
    String[] markovStrFeature = null;
    if (IS_HASH)/*from w w w .j  av a2s  .  c  o m*/
        markovFeature = featurizer.getMarkovFeature(sent, t);
    else
        markovStrFeature = featurizer.getStrMarkovFeature(sent, t);

    for (int k = 0; k < BEAM_WIDTH; ++k) {
        Hypothesis hypo = beam[k];

        if (hypo == null)
            break;

        int[] markovFeatureID;
        if (IS_HASH)
            markovFeatureID = featurizer.getFeatureID(markovFeature, hypo.label, hypo.prevHypo.label);
        else
            markovFeatureID = featurizer.getFeatureID(markovStrFeature, hypo.label, hypo.prevHypo.label, false);
        RealVector markovFeatureScore = perceptron.getScore(markovFeatureID);
        double[] score = featureScore.add(markovFeatureScore).toArray();

        for (int n = 0; n < N_LABELS; ++n)
            orderedHypoQueue.addSort(n, score[n], featureID_t, markovFeatureID, hypo);
    }

    for (int k = 0; k < BEAM_WIDTH; ++k) {
        beam[k] = orderedHypoQueue.get(k);
        if (k == orderedHypoQueue.size() - 1)
            break;
    }

    return beam;
}

From source file:tagger.Supertagger.java

private Hypothesis[] beamSearch(int t, Hypothesis[] beam, Sentence sent) {
    HypoComparator orderedHypoQueue = new HypoComparator(BEAM_WIDTH);

    int[] featureID_t;
    if (IS_HASH)/*from w  ww  .  j  a  v a  2  s  .c o m*/
        featureID_t = featurizer.getFeatureID(featurizer.getFeature(sent, t));
    else
        featureID_t = featurizer.getFeatureID(featurizer.getStrFeature(sent, t), false);

    int[][] markovFeature = null;
    String[] markovStrFeature = null;
    if (IS_HASH)
        markovFeature = featurizer.getMarkovFeature(sent, t);
    else
        markovStrFeature = featurizer.getStrMarkovFeature(sent, t);

    RealVector featureScore = getScore(featureID_t);

    for (int k = 0; k < BEAM_WIDTH; ++k) {
        Hypothesis hypo = beam[k];

        if (hypo == null)
            break;

        int[] markovFeatureID;
        if (IS_HASH)
            markovFeatureID = featurizer.getFeatureID(markovFeature, hypo.label, hypo.prevHypo.label);
        else
            markovFeatureID = featurizer.getFeatureID(markovStrFeature, hypo.label, hypo.prevHypo.label, true);
        RealVector markovFeatureScore = getScore(markovFeatureID);
        double[] score = featureScore.add(markovFeatureScore).toArray();

        for (int n = 0; n < N_LABELS; ++n)
            orderedHypoQueue.addSort(n, score[n], featureID_t, markovFeatureID, hypo);
    }

    for (int k = 0; k < BEAM_WIDTH; ++k) {
        beam[k] = orderedHypoQueue.get(k);
        if (k == orderedHypoQueue.size() - 1)
            break;
    }

    return beam;
}

From source file:tagger.Supertagger.java

private RealVector getScore(int[] featureID) {
    RealVector score = createRealVector(new double[N_LABELS]);
    for (int i = 0; i < featureID.length; ++i)
        score = score.add(createRealVector(weight[featureID[i]]));
    return score;
}