Java Code Examples for org.apache.commons.math3.linear.RealVector#getDimension()
The following examples show how to use
org.apache.commons.math3.linear.RealVector#getDimension() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KalmanFilter.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Predict the internal state estimation one time step ahead. * * @param u * the control vector * @throws DimensionMismatchException * if the dimension of the control vector does not match */ public void predict(final RealVector u) throws DimensionMismatchException { // sanity checks if (u != null && u.getDimension() != controlMatrix.getColumnDimension()) { throw new DimensionMismatchException(u.getDimension(), controlMatrix.getColumnDimension()); } // project the state estimation ahead (a priori state) // xHat(k)- = A * xHat(k-1) + B * u(k-1) stateEstimation = transitionMatrix.operate(stateEstimation); // add control input if it is available if (u != null) { stateEstimation = stateEstimation.add(controlMatrix.operate(u)); } // project the error covariance ahead // P(k)- = A * P(k-1) * A' + Q errorCovariance = transitionMatrix.multiply(errorCovariance) .multiply(transitionMatrixT) .add(processModel.getProcessNoise()); }
Example 2
Source File: MultivariateTDistribution.java From macrobase with Apache License 2.0 | 6 votes |
public MultivariateTDistribution(RealVector mean, RealMatrix covarianceMatrix, double degreesOfFreedom) { this.mean = mean; if (mean.getDimension() > 1) { this.precisionMatrix = MatrixUtils.blockInverse(covarianceMatrix, (-1 + covarianceMatrix.getColumnDimension()) / 2); } else { this.precisionMatrix = MatrixUtils.createRealIdentityMatrix(1).scalarMultiply(1. / covarianceMatrix.getEntry(0, 0)); } this.dof = degreesOfFreedom; this.D = mean.getDimension(); double determinant = new LUDecomposition(covarianceMatrix).getDeterminant(); this.multiplier = Math.exp(Gamma.logGamma(0.5 * (D + dof)) - Gamma.logGamma(0.5 * dof)) / Math.pow(Math.PI * dof, 0.5 * D) / Math.pow(determinant, 0.5); }
Example 3
Source File: EvaluationTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testComputeSigma() throws IOException { final StatisticalReferenceDataset dataset = StatisticalReferenceDatasetFactory.createKirby2(); final LeastSquaresProblem lsp = builder(dataset).build(); final double[] expected = dataset.getParametersStandardDeviations(); final Evaluation evaluation = lsp.evaluate(lsp.getStart()); final double cost = evaluation.getCost(); final RealVector sig = evaluation.getSigma(1e-14); final int dof = lsp.getObservationSize() - lsp.getParameterSize(); for (int i = 0; i < sig.getDimension(); i++) { final double actual = FastMath.sqrt(cost * cost / dof) * sig.getEntry(i); Assert.assertEquals(dataset.getName() + ", parameter #" + i, expected[i], actual, 1e-6 * expected[i]); } }
Example 4
Source File: KalmanFilter.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Predict the internal state estimation one time step ahead. * * @param u * the control vector * @throws DimensionMismatchException * if the dimension of the control vector does not match */ public void predict(final RealVector u) throws DimensionMismatchException { // sanity checks if (u != null && u.getDimension() != controlMatrix.getColumnDimension()) { throw new DimensionMismatchException(u.getDimension(), controlMatrix.getColumnDimension()); } // project the state estimation ahead (a priori state) // xHat(k)- = A * xHat(k-1) + B * u(k-1) stateEstimation = transitionMatrix.operate(stateEstimation); // add control input if it is available if (u != null) { stateEstimation = stateEstimation.add(controlMatrix.operate(u)); } // project the error covariance ahead // P(k)- = A * P(k-1) * A' + Q errorCovariance = transitionMatrix.multiply(errorCovariance) .multiply(transitionMatrixT) .add(processModel.getProcessNoise()); }
Example 5
Source File: KalmanFilter.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Predict the internal state estimation one time step ahead. * * @param u * the control vector * @throws DimensionMismatchException * if the dimension of the control vector does not match */ public void predict(final RealVector u) throws DimensionMismatchException { // sanity checks if (u != null && u.getDimension() != controlMatrix.getColumnDimension()) { throw new DimensionMismatchException(u.getDimension(), controlMatrix.getColumnDimension()); } // project the state estimation ahead (a priori state) // xHat(k)- = A * xHat(k-1) + B * u(k-1) stateEstimation = transitionMatrix.operate(stateEstimation); // add control input if it is available if (u != null) { stateEstimation = stateEstimation.add(controlMatrix.operate(u)); } // project the error covariance ahead // P(k)- = A * P(k-1) * A' + Q errorCovariance = transitionMatrix.multiply(errorCovariance) .multiply(transitionMatrixT) .add(processModel.getProcessNoise()); }
Example 6
Source File: KalmanFilter.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Correct the current state estimate with an actual measurement. * * @param z * the measurement vector * @throws NullArgumentException * if the measurement vector is {@code null} * @throws DimensionMismatchException * if the dimension of the measurement vector does not fit * @throws SingularMatrixException * if the covariance matrix could not be inverted */ public void correct(final RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException { // sanity checks MathUtils.checkNotNull(z); if (z.getDimension() != measurementMatrix.getRowDimension()) { throw new DimensionMismatchException(z.getDimension(), measurementMatrix.getRowDimension()); } // S = H * P(k) - * H' + R RealMatrix s = measurementMatrix.multiply(errorCovariance) .multiply(measurementMatrixT) .add(measurementModel.getMeasurementNoise()); // invert S // as the error covariance matrix is a symmetric positive // semi-definite matrix, we can use the cholesky decomposition DecompositionSolver solver = new CholeskyDecomposition(s).getSolver(); RealMatrix invertedS = solver.getInverse(); // Inn = z(k) - H * xHat(k)- RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation)); // calculate gain matrix // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1 // K(k) = P(k)- * H' * S^-1 RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS); // update estimate with measurement z(k) // xHat(k) = xHat(k)- + K * Inn stateEstimation = stateEstimation.add(kalmanGain.operate(innovation)); // update covariance of prediction error // P(k) = (I - K * H) * P(k)- RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension()); errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance); }
Example 7
Source File: CityBlockRelatednessFunction.java From Indra with MIT License | 5 votes |
@Override public double sim(RealVector r1, RealVector r2, boolean sparse) { if (r1.getDimension() != r2.getDimension()) { return 0; } double sum = 0.0; for (int i = 0; i < r1.getDimension(); ++i) { sum += Math.abs((r1.getEntry(i) - r2.getEntry(i))); } double result = 1 / (1 + (sum == Double.NaN ? 0 : sum)); return Math.abs(result); }
Example 8
Source File: AbstractLeastSquaresOptimizerAbstractTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void doTestStRD(final StatisticalReferenceDataset dataset, final LeastSquaresOptimizer optimizer, final double errParams, final double errParamsSd) { final Optimum optimum = optimizer.optimize(builder(dataset).build()); final RealVector actual = optimum.getPoint(); for (int i = 0; i < actual.getDimension(); i++) { double expected = dataset.getParameter(i); double delta = FastMath.abs(errParams * expected); Assert.assertEquals(dataset.getName() + ", param #" + i, expected, actual.getEntry(i), delta); } }
Example 9
Source File: KalmanFilter.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Correct the current state estimate with an actual measurement. * * @param z * the measurement vector * @throws NullArgumentException * if the measurement vector is {@code null} * @throws DimensionMismatchException * if the dimension of the measurement vector does not fit * @throws SingularMatrixException * if the covariance matrix could not be inverted */ public void correct(final RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException { // sanity checks MathUtils.checkNotNull(z); if (z.getDimension() != measurementMatrix.getRowDimension()) { throw new DimensionMismatchException(z.getDimension(), measurementMatrix.getRowDimension()); } // S = H * P(k) - * H' + R RealMatrix s = measurementMatrix.multiply(errorCovariance) .multiply(measurementMatrixT) .add(measurementModel.getMeasurementNoise()); // invert S // as the error covariance matrix is a symmetric positive // semi-definite matrix, we can use the cholesky decomposition DecompositionSolver solver = new CholeskyDecomposition(s).getSolver(); RealMatrix invertedS = solver.getInverse(); // Inn = z(k) - H * xHat(k)- RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation)); // calculate gain matrix // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1 // K(k) = P(k)- * H' * S^-1 RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS); // update estimate with measurement z(k) // xHat(k) = xHat(k)- + K * Inn stateEstimation = stateEstimation.add(kalmanGain.operate(innovation)); // update covariance of prediction error // P(k) = (I - K * H) * P(k)- RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension()); errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance); }
Example 10
Source File: Solver.java From oryx with Apache License 2.0 | 5 votes |
public float[] solveFToF(float[] b) { RealVector bVec = new ArrayRealVector(b.length); for (int i = 0; i < b.length; i++) { bVec.setEntry(i, b[i]); } RealVector resultVec = solver.solve(bVec); float[] result = new float[resultVec.getDimension()]; for (int i = 0; i < result.length; i++) { result[i] = (float) resultVec.getEntry(i); } return result; }
Example 11
Source File: LeastSquaresFactory.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Create an {@link Evaluation} with no weights. * * @param model the model function * @param target the observed values * @param point the abscissa */ private LazyUnweightedEvaluation(final ValueAndJacobianFunction model, final RealVector target, final RealVector point) { super(target.getDimension()); // Safe to cast as long as we control usage of this class. this.model = model; this.point = point; this.target = target; }
Example 12
Source File: KnnModelTrigger.java From samantha with MIT License | 5 votes |
private void getNeighbors(Object2DoubleMap<String> item2score, IndexedVectorModel knnModel, String key, double weight) { if (knnModel.hasKey(key)) { RealVector sims = knnModel.getKeyVector(key); for (int i=0; i<sims.getDimension(); i+=2) { int idx = (int)sims.getEntry(i); double sim = sims.getEntry(i+1); String recItem = knnModel.getKeyByIndex(idx); double oldVal = item2score.getOrDefault(recItem, 0.0); item2score.put(recItem, weight * sim + oldVal); } } }
Example 13
Source File: KnnModelTrigger.java From samantha with MIT License | 5 votes |
private void getNeighbors(ObjectSet<String> items, IndexedVectorModel knnModel, String key) { if (knnModel.hasKey(key)) { RealVector sims = knnModel.getKeyVector(key); for (int i=0; i<sims.getDimension(); i+=2) { int idx = (int)sims.getEntry(i); String recItem = knnModel.getKeyByIndex(idx); items.add(recItem); } } }
Example 14
Source File: TestUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Asserts that all entries of the specified vectors are equal to within a * positive {@code delta}. * * @param message the identifying message for the assertion error (can be * {@code null}) * @param expected expected value * @param actual actual value * @param delta the maximum difference between the entries of the expected * and actual vectors for which both entries are still considered equal */ public static void assertEquals(final String message, final RealVector expected, final RealVector actual, final double delta) { final String msgAndSep = message.equals("") ? "" : message + ", "; Assert.assertEquals(msgAndSep + "dimension", expected.getDimension(), actual.getDimension()); final int dim = expected.getDimension(); for (int i = 0; i < dim; i++) { Assert.assertEquals(msgAndSep + "entry #" + i, expected.getEntry(i), actual.getEntry(i), delta); } }
Example 15
Source File: AbstractLeastSquaresOptimizerAbstractTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void doTestStRD(final StatisticalReferenceDataset dataset, final LeastSquaresOptimizer optimizer, final double errParams, final double errParamsSd) { final Optimum optimum = optimizer.optimize(builder(dataset).build()); final RealVector actual = optimum.getPoint(); for (int i = 0; i < actual.getDimension(); i++) { double expected = dataset.getParameter(i); double delta = FastMath.abs(errParams * expected); Assert.assertEquals(dataset.getName() + ", param #" + i, expected, actual.getEntry(i), delta); } }
Example 16
Source File: FlatRealTestHMM.java From gatk-protected with BSD 3-Clause "New" or "Revised" License | 5 votes |
public FlatRealTestHMM(final RealVector priors, final RealMatrix transitions) { this.priors = priors; this.transitions = transitions; Utils.validateArg(priors.getDimension() == this.transitions.getColumnDimension(), "bad dimensions"); Utils.validateArg(priors.getDimension() == this.transitions.getRowDimension(), "bad dimensions"); numStates = priors.getDimension(); }
Example 17
Source File: VoxConstrCache.java From OSPREY3 with GNU General Public License v2.0 | 5 votes |
private static int getVoxConstrDOFNum(RealVector coeff){ //return what dof is constrained if this is a vox constr; else return -1 int ans = -1; for(int dof=0; dof<coeff.getDimension(); dof++){ double val = coeff.getEntry(dof); if(Math.abs(val)>1e-10){//nonzero if(ans!=-1)//this is not the first constrained dof. Must not be a voxel constraint. return -1; if(Math.abs(val-1)>1e-10)//coeff 1 expected for vox constr return -1; ans = dof; } } return ans; }
Example 18
Source File: ContextualOutlierDetectorTest.java From macrobase with Apache License 2.0 | 5 votes |
private Datum makeDatum(MacroBaseConf conf, List<Integer> attributes, RealVector metrics, List<Integer> contextualDiscreteAttributes, RealVector contextualDoubleAttributes) { Datum ret = new Datum(attributes, metrics); for(int i = 0; i < contextualDiscreteAttributes.size(); ++i) { ret.attributes().add(conf.getEncoder().getIntegerEncoding(i, String.valueOf(contextualDiscreteAttributes.get(i)))); } int consumed = contextualDiscreteAttributes.size(); for(int i = consumed; i < consumed + contextualDoubleAttributes.getDimension(); ++i) { ret.attributes().add(conf.getEncoder().getIntegerEncoding(i, String.valueOf(contextualDoubleAttributes.getEntry( i-consumed)))); } return ret; }
Example 19
Source File: TestUtils.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Asserts that all entries of the specified vectors are equal to within a * positive {@code delta}. * * @param message the identifying message for the assertion error (can be * {@code null}) * @param expected expected value * @param actual actual value * @param delta the maximum difference between the entries of the expected * and actual vectors for which both entries are still considered equal */ public static void assertEquals(final String message, final RealVector expected, final RealVector actual, final double delta) { final String msgAndSep = message.equals("") ? "" : message + ", "; Assert.assertEquals(msgAndSep + "dimension", expected.getDimension(), actual.getDimension()); final int dim = expected.getDimension(); for (int i = 0; i < dim; i++) { Assert.assertEquals(msgAndSep + "entry #" + i, expected.getEntry(i), actual.getEntry(i), delta); } }
Example 20
Source File: EvaluationTestValidation.java From astor with GNU General Public License v2.0 | 4 votes |
/** * @return the normalized chi-square. */ private double getChi2N(LeastSquaresProblem lsp, RealVector params) { final double cost = lsp.evaluate(params).getCost(); return cost * cost / (lsp.getObservationSize() - params.getDimension()); }