/** * Solve the linear equation A × X = B in least square sense. * <p> * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. * </p> * @param b Right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. */ public RealVector solve(final RealVector b) { return pseudoInverse.operate(b); }
/** {@inheritDoc} */ public RealVector getResiduals() { return this.weightSqrt.operate(this.unweighted.getResiduals()); }
/** {@inheritDoc} */ public RealVector solve(RealVector b) { return p.operate(upper.solve(b)); }
/** {@inheritDoc} */ @Override public double[] sample() { final int dim = getDimension(); final double[] normalVals = new double[dim]; for (int i = 0; i < dim; i++) { normalVals[i] = random.nextGaussian(); } final double[] vals = samplingMatrix.operate(normalVals); for (int i = 0; i < dim; i++) { vals[i] += means[i]; } return vals; }
/** * Calculates the residuals of multiple linear regression in matrix * notation. * * <pre> * u = y - X * b * </pre> * * @return The residuals [n,1] matrix */ protected RealVector calculateResiduals() { RealVector b = calculateBeta(); return yVector.subtract(xMatrix.operate(b)); }
/** * {@inheritDoc} */ public double[] estimateResiduals() { RealVector b = calculateBeta(); RealVector e = yVector.subtract(xMatrix.operate(b)); return e.toArray(); }
for (final double yi : scale.operate(residuals)) { sumSquares += yi * yi;
for (final double yi : scale.operate(residuals)) { sumSquares += yi * yi;
/** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); }
/** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) * @since 3.1 */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); }
private double weightedResidual(final PointVectorValuePair pv) { final RealVector v = new ArrayRealVector(pv.getValueRef(), false); final RealVector r = target.subtract(v); return r.dotProduct(weight.operate(r)); } };
/** * Update the residuals array and cost function value. * @throws DimensionMismatchException if the dimension does not match the * problem dimension. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @deprecated As of 3.1. Please use {@link #computeResiduals(double[])}, * {@link #computeObjectiveValue(double[])}, {@link #computeCost(double[])} * and {@link #setCost(double)} instead. */ @Deprecated protected void updateResidualsAndCost() { objective = computeObjectiveValue(point); final double[] res = computeResiduals(objective); // Compute cost. cost = computeCost(res); // Compute weighted residuals. final ArrayRealVector residuals = new ArrayRealVector(res); weightedResiduals = weightMatrixSqrt.operate(residuals).toArray(); }
RealVector Y = X.operate(coefficients); for(Map.Entry<Integer, Record> e : newData.entries()) { Integer rId = e.getKey();
/** * 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()); }
/** * Calculates the estimated variance of the error term using the formula * <pre> * Var(u) = Tr(u' Omega^-1 u)/(n-k) * </pre> * where n and k are the row and column dimensions of the design * matrix X. * * @return error variance * @since 2.2 */ @Override protected double calculateErrorVariance() { RealVector residuals = calculateResiduals(); double t = residuals.dotProduct(getOmegaInverse().operate(residuals)); return t / (getX().getRowDimension() - getX().getColumnDimension()); }
/** * Calculates beta by GLS. * <pre> * b=(X' Omega^-1 X)^-1X'Omega^-1 y * </pre> * @return beta */ @Override protected RealVector calculateBeta() { RealMatrix OI = getOmegaInverse(); RealMatrix XT = getX().transpose(); RealMatrix XTOIX = XT.multiply(OI).multiply(getX()); RealMatrix inverse = new LUDecomposition(XTOIX).getSolver().getInverse(); return inverse.multiply(XT).multiply(OI).operate(getY()); }
weightedResidual = weightMatrixSqrt.operate(currentResiduals); for (int i = 0; i < nR; i++) { qtf[i] = weightedResidual[i];
weightedResidual = weightMatrixSqrt.operate(currentResiduals); for (int i = 0; i < nR; i++) { qtf[i] = weightedResidual[i];