/** Add an observed weighted (x,y) point to the sample. * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); }
public static WeightedObservedPoint[] clone( WeightedObservedPoint[] points) { WeightedObservedPoint[] ret = new WeightedObservedPoint[points.length]; for (int i = 0; i < points.length; i++) { ret[i] = new WeightedObservedPoint(points[i].getWeight(), points[i].getX(), points[i].getY()); } return ret; }
/** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; }
/** * @param f function to fit. * @param observations Observations. */ public TheoreticalValuesFunction(final ParametricUnivariateFunction f, final Collection<WeightedObservedPoint> observations) { this.f = f; final int len = observations.size(); this.points = new double[len]; int i = 0; for (WeightedObservedPoint obs : observations) { this.points[i++] = obs.getX(); } }
/** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; }
/** {@inheritDoc} */ @Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) { // Prepare least-squares problem. final int len = observations.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int count = 0; for (WeightedObservedPoint obs : observations) { target[count] = obs.getY(); weights[count] = obs.getWeight(); ++count; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(function, observations); // Create an optimizer for fitting the curve to the observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(initialGuess). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); } }
/** * Adds a point to the sample. * * @param weight Weight of the observed point. * @param x Abscissa of the point. * @param y Observed value at {@code x}. After fitting we should * have {@code f(x)} as close as possible to this value. * * @see #add(double, double) * @see #add(WeightedObservedPoint) * @see #toList() */ public void add(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); }
/** * Estimate a first guess of the phase. * * @param observations Observations, sorted w.r.t. abscissa. * @return the guessed phase. */ private double guessPhi(WeightedObservedPoint[] observations) { // initialize the means double fcMean = 0; double fsMean = 0; double currentX = observations[0].getX(); double currentY = observations[0].getY(); for (int i = 1; i < observations.length; ++i) { // one step forward final double previousX = currentX; final double previousY = currentY; currentX = observations[i].getX(); currentY = observations[i].getY(); final double currentYPrime = (currentY - previousY) / (currentX - previousX); double omegaX = omega * currentX; double cosine = FastMath.cos(omegaX); double sine = FastMath.sin(omegaX); fcMean += omega * currentY * cosine - currentYPrime * sine; fsMean += omega * currentY * sine + currentYPrime * cosine; } return FastMath.atan2(-fsMean, fcMean); } }
/** {@inheritDoc} */ @Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) { // Prepare least-squares problem. final int len = observations.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int i = 0; for (WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations); if (initialGuess == null) { throw new MathInternalError(); } // Return a new least squares problem set up to fit a polynomial curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(initialGuess). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
/** Add an observed weighted (x,y) point to the sample. * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); }