/** {@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); final double[] startPoint = initialGuess != null ? initialGuess : // Compute estimation. new ParameterGuesser(observations).guess(); // Return a new least squares problem set up to fit a Gaussian curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(startPoint). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
/** * Constructs instance with the specified observed points. * * @param observations Observed points from which to guess the * parameters of the Gaussian. * @throws NullArgumentException if {@code observations} is * {@code null}. * @throws NumberIsTooSmallException if there are less than 3 * observations. */ public ParameterGuesser(Collection<WeightedObservedPoint> observations) { if (observations == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (observations.size() < 3) { throw new NumberIsTooSmallException(observations.size(), 3, true); } final List<WeightedObservedPoint> sorted = sortObservations(observations); final double[] params = basicGuess(sorted.toArray(new WeightedObservedPoint[0])); norm = params[0]; mean = params[1]; sigma = params[2]; }
final WeightedObservedPoint p1 = points[i]; final WeightedObservedPoint p2 = points[i + idxStep]; if (isBetween(y, p1.getY(), p2.getY())) { if (idxStep < 0) { return new WeightedObservedPoint[] { p2, p1 };
final WeightedObservedPoint p1 = points[i]; final WeightedObservedPoint p2 = points[i + idxStep]; if (isBetween(y, p1.getY(), p2.getY())) { if (idxStep < 0) { return new WeightedObservedPoint[] { p2, p1 };
double[] guess = new GaussianCurveFitter.ParameterGuesser(pointList).guess(); curveFitter = curveFitter.withStartPoint(guess);
= getInterpolationPointsForY(points, startIdx, idxStep, y); final WeightedObservedPoint p1 = twoPoints[0]; final WeightedObservedPoint p2 = twoPoints[1];
= getInterpolationPointsForY(points, startIdx, idxStep, y); final WeightedObservedPoint p1 = twoPoints[0]; final WeightedObservedPoint p2 = twoPoints[1];
/** {@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); final double[] startPoint = initialGuess != null ? initialGuess : // Compute estimation. new ParameterGuesser(observations).guess(); // Return a new least squares problem set up to fit a Gaussian curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(startPoint). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
/** {@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); final double[] startPoint = initialGuess != null ? initialGuess : // Compute estimation. new ParameterGuesser(observations).guess(); // Return a new least squares problem set up to fit a Gaussian curve to the // observed points. return new LeastSquaresBuilder(). maxEvaluations(Integer.MAX_VALUE). maxIterations(maxIter). start(startPoint). target(target). weight(new DiagonalMatrix(weights)). model(model.getModelFunction(), model.getModelFunctionJacobian()). build(); }
/** * Guesses the parameters based on the specified observed points. * * @param points Observed points, sorted. * @return the guessed parameters (normalization factor, mean and * sigma). */ private double[] basicGuess(WeightedObservedPoint[] points) { final int maxYIdx = findMaxY(points); final double n = points[maxYIdx].getY(); final double m = points[maxYIdx].getX(); double fwhmApprox; try { final double halfY = n + ((m - n) / 2); final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY); final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY); fwhmApprox = fwhmX2 - fwhmX1; } catch (OutOfRangeException e) { // TODO: Exceptions should not be used for flow control. fwhmApprox = points[points.length - 1].getX() - points[0].getX(); } final double s = fwhmApprox / (2 * Math.sqrt(2 * Math.log(2))); return new double[] { n, m, s }; }
/** * Guesses the parameters based on the specified observed points. * * @param points Observed points, sorted. * @return the guessed parameters (normalization factor, mean and * sigma). */ private double[] basicGuess(WeightedObservedPoint[] points) { final int maxYIdx = findMaxY(points); final double n = points[maxYIdx].getY(); final double m = points[maxYIdx].getX(); double fwhmApprox; try { final double halfY = n + ((m - n) / 2); final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY); final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY); fwhmApprox = fwhmX2 - fwhmX1; } catch (OutOfRangeException e) { // TODO: Exceptions should not be used for flow control. fwhmApprox = points[points.length - 1].getX() - points[0].getX(); } final double s = fwhmApprox / (2 * FastMath.sqrt(2 * FastMath.log(2))); return new double[] { n, m, s }; }
/** * Constructs instance with the specified observed points. * * @param observations Observed points from which to guess the * parameters of the Gaussian. * @throws NullArgumentException if {@code observations} is * {@code null}. * @throws NumberIsTooSmallException if there are less than 3 * observations. */ public ParameterGuesser(Collection<WeightedObservedPoint> observations) { if (observations == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (observations.size() < 3) { throw new NumberIsTooSmallException(observations.size(), 3, true); } final List<WeightedObservedPoint> sorted = sortObservations(observations); final double[] params = basicGuess(sorted.toArray(new WeightedObservedPoint[0])); norm = params[0]; mean = params[1]; sigma = params[2]; }
/** * Constructs instance with the specified observed points. * * @param observations Observed points from which to guess the * parameters of the Gaussian. * @throws NullArgumentException if {@code observations} is * {@code null}. * @throws NumberIsTooSmallException if there are less than 3 * observations. */ public ParameterGuesser(Collection<WeightedObservedPoint> observations) { if (observations == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (observations.size() < 3) { throw new NumberIsTooSmallException(observations.size(), 3, true); } final List<WeightedObservedPoint> sorted = sortObservations(observations); final double[] params = basicGuess(sorted.toArray(new WeightedObservedPoint[0])); norm = params[0]; mean = params[1]; sigma = params[2]; }
final WeightedObservedPoint p1 = points[i]; final WeightedObservedPoint p2 = points[i + idxStep]; if (isBetween(y, p1.getY(), p2.getY())) { if (idxStep < 0) { return new WeightedObservedPoint[] { p2, p1 };
= getInterpolationPointsForY(points, startIdx, idxStep, y); final WeightedObservedPoint p1 = twoPoints[0]; final WeightedObservedPoint p2 = twoPoints[1];
/** * Guesses the parameters based on the specified observed points. * * @param points Observed points, sorted. * @return the guessed parameters (normalization factor, mean and * sigma). */ private double[] basicGuess(WeightedObservedPoint[] points) { final int maxYIdx = findMaxY(points); final double n = points[maxYIdx].getY(); final double m = points[maxYIdx].getX(); double fwhmApprox; try { final double halfY = n + ((m - n) / 2); final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY); final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY); fwhmApprox = fwhmX2 - fwhmX1; } catch (OutOfRangeException e) { // TODO: Exceptions should not be used for flow control. fwhmApprox = points[points.length - 1].getX() - points[0].getX(); } final double s = fwhmApprox / (2 * FastMath.sqrt(2 * FastMath.log(2))); return new double[] { n, m, s }; }