Example usage for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector

List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector

Introduction

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

Prototype

public ArrayRealVector(double[] v1, double[] v2) 

Source Link

Document

Construct a vector by appending one vector to another vector.

Usage

From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurement.java

public MultivariateGaussianCriterionMeasurement(List<Alternative> alternatives) {
    this.alternatives = new ArrayList<Alternative>(alternatives);
    this.meanVector = new ArrayRealVector(alternatives.size(), 0.0);
    this.covarianceMatrix = MatrixUtils.createRealIdentityMatrix(alternatives.size());
}

From source file:edu.oregonstate.eecs.mcplan.ml.KMeans.java

private RealVector centerOfMass(final int c) {
    final RealVector com = new ArrayRealVector(m_, 0);
    int nelements = 0;
    for (int i = 0; i < n_; ++i) {
        if (c_[i] == c) {
            nelements += 1;//w  ww.j a  v a 2 s  .co  m
            com.combineToSelf(1.0, 1.0, data_[i]); // Add in-place
        }
    }
    assert (nelements > 0);
    com.mapDivideToSelf(nelements);
    return com;
}

From source file:iocomms.subpos.NonLinearLeastSquaresSolver.java

public Optimum solve(double[] target, double[] weights, double[] initialPoint, boolean debugInfo) {
    if (debugInfo) {
        System.out.println("Max Number of Iterations : " + MAXNUMBEROFITERATIONS);
    }/* ww w .j a  v  a 2  s.co  m*/

    LeastSquaresProblem leastSquaresProblem = LeastSquaresFactory.create(
            // function to be optimized
            function,
            // target values at optimal point in least square equation
            // (x0+xi)^2 + (y0+yi)^2 + ri^2 = target[i]
            new ArrayRealVector(target, false), new ArrayRealVector(initialPoint, false),
            new DiagonalMatrix(weights), null, MAXNUMBEROFITERATIONS, MAXNUMBEROFITERATIONS);

    return leastSquaresOptimizer.optimize(leastSquaresProblem);
}

From source file:edu.stanford.cfuller.imageanalysistools.fitting.BisquareLinearFit.java

/**
* Performs a robust least squares fit with bisquare weights to the supplied data.
* 
* @param indVarValues A RealVector containing the values of the independent variable.
* @param depVarValues A RealVector containing the values of the dependent variable.
* @return a RealVector containing two elements: the slope of the fit and the y-intercept of the fit.
*//*from   w  ww. jav  a  2s  .c o  m*/
public RealVector fit(RealVector indVarValues, RealVector depVarValues) {

    RealVector uniformWeights = new ArrayRealVector(indVarValues.getDimension(), 1.0);

    RealVector lastParams = new ArrayRealVector(2, Double.MAX_VALUE);

    RealVector currParams = wlsFit(indVarValues, depVarValues, uniformWeights);

    RealVector weights = uniformWeights;

    RealVector leverages = this.calculateLeverages(indVarValues);

    int c = 0;

    double norm_mult = 1.0;

    if (!this.noIntercept) {
        norm_mult = 2.0;
    }

    int maxiter = 10000;

    while (lastParams.subtract(currParams).getNorm() > CONV_NORM * norm_mult && c++ < maxiter) {

        lastParams = currParams;

        RealVector stdAdjR = this.calculateStandardizedAdjustedResiduals(indVarValues, depVarValues, leverages,
                currParams);

        weights = calculateBisquareWeights(stdAdjR);

        currParams = wlsFit(indVarValues, depVarValues, weights);

    }

    return currParams;

}

From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurementTest.java

@Test
public void testSetMeanVector() {
    RealVector mu1 = new ArrayRealVector(3, 0.0);
    RealVector mu2 = new ArrayRealVector(new double[] { 25.3, 2.1, -3 });
    JUnitUtil.testSetter(m, MultivariateGaussianCriterionMeasurement.PROPERTY_MEAN_VECTOR, mu1, mu2);
}

From source file:edu.oregonstate.eecs.mcplan.ml.SequentialProjectionHashLearner.java

/**
 * @param X Each column is an unlabeled data point. Will be modified to have 0 mean.
 * @param XL Each column is a labeled data point. Will be modified to have 0 mean.
 * @param S S_ij = 1 if XL.get(i) similar to XL.get(j), -1 if not similar, 0 if unknown
 * @param K Number of hash bits/*from   ww  w.  j av a  2 s .  c  om*/
 * @param eta Regularization parameter
 * @param alpha Learning rate (it's more like a boosting weight)
 */
public SequentialProjectionHashLearner(final RealMatrix X, final RealMatrix XL, final RealMatrix S, final int K,
        final double eta, final double alpha) {
    assert (K <= 64); // Because we're going to hash into a long

    this.X = X;
    this.XL = XL;
    this.S = S;
    this.K = K;
    this.eta = eta;
    this.alpha = alpha;

    Xi_ = X;
    Si_ = S;

    final VectorMeanVarianceAccumulator mu = new VectorMeanVarianceAccumulator(X.getRowDimension());
    for (int j = 0; j < X.getColumnDimension(); ++j) {
        mu.add(X.getColumn(j));
    }
    b = new ArrayRealVector(mu.mean(), false);

    for (int i = 0; i < X.getRowDimension(); ++i) {
        for (int j = 0; j < X.getColumnDimension(); ++j) {
            final double x = X.getEntry(i, j);
            X.setEntry(i, j, x / b.getEntry(i));
        }
    }

    for (int i = 0; i < XL.getRowDimension(); ++i) {
        for (int j = 0; j < XL.getColumnDimension(); ++j) {
            final double x = XL.getEntry(i, j);
            XL.setEntry(i, j, x / b.getEntry(i));
        }
    }

    XLt = XL.transpose();
}

From source file:edu.stanford.cfuller.colocalization3d.fitting.P3DFitter.java

/**
 * Fits the distances between the two channels of a set of objects to a p3d distribution.
 * /*from   w ww .  j  a va  2s .co  m*/
 * @param objects the ImageObjects whose distances will be fit
 * @param diffs a RealVector containing the scalar distances between the channels of the ImageObjects, in the same order.
 * 
 * @return a RealVector containing the parameters for the distribution fit: first the mean parameter, second the standard deviation parameter
 */
public RealVector fit(List<ImageObject> objects, RealVector diffs) {

    P3dObjectiveFunction of = new P3dObjectiveFunction();

    of.setR(diffs);

    final double tol = 1e-12;

    NelderMeadMinimizer nmm = new NelderMeadMinimizer(tol);

    double initialMean = diffs.getL1Norm() / diffs.getDimension();

    double initialWidth = diffs.mapSubtract(initialMean)
            .map(new org.apache.commons.math3.analysis.function.Power(2)).getL1Norm() / diffs.getDimension();

    initialWidth = Math.sqrt(initialWidth);

    RealVector startingPoint = new ArrayRealVector(2, 0.0);

    startingPoint.setEntry(0, initialMean);
    startingPoint.setEntry(1, initialWidth);

    RealVector parametersMin = null;

    if (this.parameters.hasKey(ROBUST_P3D_FIT_PARAM)) {

        double cutoff = this.parameters.getDoubleValueForKey(ROBUST_P3D_FIT_PARAM);

        of.setMinProb(cutoff);

    }

    return nmm.optimize(of, startingPoint);

}

From source file:edu.utexas.cs.tactex.ChargeEstimatorTest.java

@Test
public void testEstimateCharge() {

    // test estimating one charge: estimateCharge()
    ArrayRealVector customerEnergy = new ArrayRealVector(7 * 24, 1.6);
    TariffSpecification spec1 = new TariffSpecification(brokerContext.getBroker(), PowerType.CONSUMPTION);
    spec1.addRate(new Rate().withValue(-1.0));
    TestHelperUtils.addToRepo(spec1, tariffRepo, timeService);

    double expected = 7 * 24 * 1.6 * -1.0;
    double actual = chargeEstimatorDefault.estimateCharge(customerEnergy, spec1);
    assertEquals("fixed rate", expected, actual, 1e-6);

    // test estimating several charges: estimateTariffCharges()
    TariffSpecification spec2 = new TariffSpecification(brokerContext.getBroker(), PowerType.CONSUMPTION);
    spec2.addRate(new Rate().withValue(-2.0));
    TestHelperUtils.addToRepo(spec2, tariffRepo, timeService);

    List<TariffSpecification> tariffSpecs = new ArrayList<TariffSpecification>();
    tariffSpecs.add(spec1);/*from w  w w .j  av  a  2s  .  co m*/
    tariffSpecs.add(spec2);

    CustomerInfo customer1 = new CustomerInfo("Austin", 1);
    CustomerInfo customer2 = new CustomerInfo("Dallas", 1);
    CustomerInfo customer3 = new CustomerInfo("Houston", 1);
    CustomerInfo customer4 = new CustomerInfo("NYC", 2);
    List<CustomerInfo> customers = new ArrayList<CustomerInfo>();
    customers.add(customer1);
    customers.add(customer2);
    customers.add(customer3);
    customers.add(customer4);

    ArrayRealVector energy1 = new ArrayRealVector(7 * 24, 1.0);
    ArrayRealVector energy2 = new ArrayRealVector(7 * 24, 2.0);
    ArrayRealVector energy3 = new ArrayRealVector(7 * 24, 3.0);
    ArrayRealVector energy4 = new ArrayRealVector(7 * 24, 4.0);
    HashMap<CustomerInfo, HashMap<TariffSpecification, ShiftedEnergyData>> customer2energy = new HashMap<CustomerInfo, HashMap<TariffSpecification, ShiftedEnergyData>>();
    customer2energy.put(customer1, new HashMap<TariffSpecification, ShiftedEnergyData>());
    customer2energy.put(customer2, new HashMap<TariffSpecification, ShiftedEnergyData>());
    customer2energy.put(customer3, new HashMap<TariffSpecification, ShiftedEnergyData>());
    customer2energy.put(customer4, new HashMap<TariffSpecification, ShiftedEnergyData>());
    customer2energy.get(customer1).put(spec1, new ShiftedEnergyData(energy1, 0.0));
    customer2energy.get(customer2).put(spec1, new ShiftedEnergyData(energy2, 0.0));
    customer2energy.get(customer3).put(spec1, new ShiftedEnergyData(energy3, 0.0));
    customer2energy.get(customer4).put(spec1, new ShiftedEnergyData(energy4, 0.0));
    customer2energy.get(customer1).put(spec2, new ShiftedEnergyData(energy1, 0.0));
    customer2energy.get(customer2).put(spec2, new ShiftedEnergyData(energy2, 0.0));
    customer2energy.get(customer3).put(spec2, new ShiftedEnergyData(energy3, 0.0));
    customer2energy.get(customer4).put(spec2, new ShiftedEnergyData(energy4, 0.0));

    HashMap<CustomerInfo, HashMap<TariffSpecification, Double>> estimatedCharges = chargeEstimatorDefault
            .estimateRelevantTariffCharges(tariffSpecs, customer2energy);

    expected = 7 * 24 * 1.0 * -1.0;
    actual = estimatedCharges.get(customer1).get(spec1);
    assertEquals("fixed rate, customer1, spec1", expected, actual, 1e-6);

    expected = 7 * 24 * 2.0 * -1.0;
    actual = estimatedCharges.get(customer2).get(spec1);
    assertEquals("fixed rate, customer2, spec1", expected, actual, 1e-6);

    expected = 7 * 24 * 3.0 * -1.0;
    actual = estimatedCharges.get(customer3).get(spec1);
    assertEquals("fixed rate, customer3, spec1", expected, actual, 1e-6);

    // charge computation is for 1 customer - therefore should ignore population number (here 2)
    expected = 7 * 24 * 4.0 * -1.0;
    actual = estimatedCharges.get(customer4).get(spec1);
    assertEquals("fixed rate, customer4, spec1", expected, actual, 1e-6);

    expected = 7 * 24 * 1.0 * -2.0;
    actual = estimatedCharges.get(customer1).get(spec2);
    assertEquals("fixed rate, customer1, spec2", expected, actual, 1e-6);

    expected = 7 * 24 * 2.0 * -2.0;
    actual = estimatedCharges.get(customer2).get(spec2);
    assertEquals("fixed rate, customer2, spec2", expected, actual, 1e-6);

    expected = 7 * 24 * 3.0 * -2.0;
    actual = estimatedCharges.get(customer3).get(spec2);
    assertEquals("fixed rate, customer3, spec2", expected, actual, 1e-6);

    // charge computation is for 1 customer - therefore should ignore population number (here 2)
    expected = 7 * 24 * 4.0 * -2.0;
    actual = estimatedCharges.get(customer4).get(spec2);
    assertEquals("fixed rate, customer4, spec2", expected, actual, 1e-6);

}

From source file:edu.duke.cs.osprey.tupexp.IterativeCGTupleFitter.java

RealVector applyAtA(RealVector rv) {
    //first apply A
    double Arv[] = calcFitVals(rv);

    //then apply A^T to Arv
    double ans[] = new double[numTup];
    for (int s = 0; s < numSamp; s++) {
        if (isSampleRestrained(s)) {
            ArrayList<Integer> sampTup = tupIndMat.calcSampleTuples(samples.get(s));
            for (int t : sampTup)
                ans[t] += Arv[s];//from w ww . ja  va  2  s  .c  o  m
        }
    }

    //damping
    if (curCoeffs != null) {//not first iteration
        for (int t = 0; t < numTup; t++)
            ans[t] += damperLambda * rv.getEntry(t);
    }

    return new ArrayRealVector(ans, false);//make RealVector without copying ans
}

From source file:fi.smaa.jsmaa.model.MultivariateGaussianCriterionMeasurementTest.java

@Test(expected = IllegalArgumentException.class)
public void testSetWrongSizeMeanVector() {
    m.setMeanVector(new ArrayRealVector(4, 0.0));
}