/** * Computes an estimate of the standard deviation of the parameters. The * returned values are the square root of the diagonal coefficients of the * covariance matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} * is the optimized value of the {@code i}-th parameter, and {@code C} is * the covariance matrix. * * @param params Model parameters. * @param covarianceSingularityThreshold Singularity threshold (see * {@link #computeCovariances(double[],double) computeCovariances}). * @return an estimate of the standard deviation of the optimized parameters * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed. */ public double[] computeSigma(double[] params, double covarianceSingularityThreshold) { final int nC = params.length; final double[] sig = new double[nC]; final double[][] cov = computeCovariances(params, covarianceSingularityThreshold); for (int i = 0; i < nC; ++i) { sig[i] = FastMath.sqrt(cov[i][i]); } return sig; }
/** * Computes the weighted Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W<sup>1/2</sup> J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); }
/** * Gets the root-mean-square (RMS) value. * * The RMS the root of the arithmetic mean of the square of all weighted * residuals. * This is related to the criterion that is minimized by the optimizer * as follows: If <em>c</em> if the criterion, and <em>n</em> is the * number of measurements, then the RMS is <em>sqrt (c/n)</em>. * * @return the RMS value. */ public double getRMS() { return FastMath.sqrt(getChiSquare() / getTargetSize()); }
/** * 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))); }
/** * Scans the list of (required and optional) optimization data that * characterize the problem. * If the weight matrix is specified, the {@link #weightMatrixSqrt} * field is recomputed. * * @param optData Optimization data. The following data will be looked for: * <ul> * <li>{@link Weight}</li> * </ul> */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } }
/** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target = getTarget(); if (objectiveValue.length != target.length) { throw new DimensionMismatchException(target.length, objectiveValue.length); } final double[] residuals = new double[target.length]; for (int i = 0; i < target.length; i++) { residuals[i] = target[i] - objectiveValue[i]; } return residuals; }
/** * Get the covariance matrix of the optimized parameters. * <br/> * Note that this operation involves the inversion of the * <code>J<sup>T</sup>J</code> matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param params Model parameters. * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). */ public double[][] computeCovariances(double[] params, double threshold) { // Set up the Jacobian. final RealMatrix j = computeWeightedJacobian(params); // Compute transpose(J)J. final RealMatrix jTj = j.transpose().multiply(j); // Compute the covariances matrix. final DecompositionSolver solver = new QRDecomposition(jTj, threshold).getSolver(); return solver.getInverse().getData(); }
/** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return Math.sqrt(r.dotProduct(getWeight().operate(r))); }
/** * Scans the list of (required and optional) optimization data that * characterize the problem. * If the weight matrix is specified, the {@link #weightMatrixSqrt} * field is recomputed. * * @param optData Optimization data. The following data will be looked for: * <ul> * <li>{@link Weight}</li> * </ul> */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } }
/** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target = getTarget(); if (objectiveValue.length != target.length) { throw new DimensionMismatchException(target.length, objectiveValue.length); } final double[] residuals = new double[target.length]; for (int i = 0; i < target.length; i++) { residuals[i] = target[i] - objectiveValue[i]; } return residuals; }
/** * Get the covariance matrix of the optimized parameters. * <br/> * Note that this operation involves the inversion of the * <code>J<sup>T</sup>J</code> matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param params Model parameters. * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). */ public double[][] computeCovariances(double[] params, double threshold) { // Set up the Jacobian. final RealMatrix j = computeWeightedJacobian(params); // Compute transpose(J)J. final RealMatrix jTj = j.transpose().multiply(j); // Compute the covariances matrix. final DecompositionSolver solver = new QRDecomposition(jTj, threshold).getSolver(); return solver.getInverse().getData(); }
/** * Gets the root-mean-square (RMS) value. * * The RMS the root of the arithmetic mean of the square of all weighted * residuals. * This is related to the criterion that is minimized by the optimizer * as follows: If <em>c</em> if the criterion, and <em>n</em> is the * number of measurements, then the RMS is <em>sqrt (c/n)</em>. * * @return the RMS value. */ public double getRMS() { return Math.sqrt(getChiSquare() / getTargetSize()); }
/** * 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 weighted Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W<sup>1/2</sup> J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); }
/** * Scans the list of (required and optional) optimization data that * characterize the problem. * If the weight matrix is specified, the {@link #weightMatrixSqrt} * field is recomputed. * * @param optData Optimization data. The following data will be looked for: * <ul> * <li>{@link Weight}</li> * </ul> */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } }
/** * Computes an estimate of the standard deviation of the parameters. The * returned values are the square root of the diagonal coefficients of the * covariance matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} * is the optimized value of the {@code i}-th parameter, and {@code C} is * the covariance matrix. * * @param params Model parameters. * @param covarianceSingularityThreshold Singularity threshold (see * {@link #computeCovariances(double[],double) computeCovariances}). * @return an estimate of the standard deviation of the optimized parameters * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed. */ public double[] computeSigma(double[] params, double covarianceSingularityThreshold) { final int nC = params.length; final double[] sig = new double[nC]; final double[][] cov = computeCovariances(params, covarianceSingularityThreshold); for (int i = 0; i < nC; ++i) { sig[i] = Math.sqrt(cov[i][i]); } return sig; }
/** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target = getTarget(); if (objectiveValue.length != target.length) { throw new DimensionMismatchException(target.length, objectiveValue.length); } final double[] residuals = new double[target.length]; for (int i = 0; i < target.length; i++) { residuals[i] = target[i] - objectiveValue[i]; } return residuals; }
/** * Get the covariance matrix of the optimized parameters. * <br/> * Note that this operation involves the inversion of the * <code>J<sup>T</sup>J</code> matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param params Model parameters. * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). */ public double[][] computeCovariances(double[] params, double threshold) { // Set up the Jacobian. final RealMatrix j = computeWeightedJacobian(params); // Compute transpose(J)J. final RealMatrix jTj = j.transpose().multiply(j); // Compute the covariances matrix. final DecompositionSolver solver = new QRDecomposition(jTj, threshold).getSolver(); return solver.getInverse().getData(); }
/** * Gets the root-mean-square (RMS) value. * * The RMS the root of the arithmetic mean of the square of all weighted * residuals. * This is related to the criterion that is minimized by the optimizer * as follows: If <em>c</em> if the criterion, and <em>n</em> is the * number of measurements, then the RMS is <em>sqrt (c/n)</em>. * * @return the RMS value. */ public double getRMS() { return FastMath.sqrt(getChiSquare() / getTargetSize()); }
/** * Computes the weighted Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W<sup>1/2</sup> J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); }