List of usage examples for org.apache.commons.math3.fitting WeightedObservedPoint getY
public double getY()
From source file:com.cloudera.hts.utils.math.MyFunc2.java
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> points) { final int len = points.size(); final double[] target = new double[len]; final double[] weights = new double[len]; final double[] initialGuess = { 1.0, 1.0, 1.0 }; int i = 0;/* ww w .j a v a 2 s .com*/ for (WeightedObservedPoint point : points) { target[i] = point.getY(); weights[i] = point.getWeight(); i += 1; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( new MyFunc(), points); return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE) .start(initialGuess).target(target).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); }
From source file:edu.ucsf.valelab.saim.calculations.SaimFunctionFitter.java
@Override protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> points) { final int len = points.size(); final double[] target = new double[len]; final double[] weights = new double[len]; int i = 0;/*from w w w . ja va 2s .co m*/ for (WeightedObservedPoint point : points) { target[i] = point.getY(); weights[i] = point.getWeight(); i += 1; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( saimFunction_, points); ConvergenceChecker<PointVectorValuePair> checker = new SimpleVectorValueChecker(1.0e-6, 1.0e-10); // this parameter validator appears to have the same effect // as using the SaimFunctionFitterWithBounds double[] lowerBounds = { 0.0, 0.0, 0.0 }; double[] upperBounds = { 64000, 64000, 1000 }; ParameterValidator spv = new SaimParameterValidator(lowerBounds, upperBounds); return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(maxIterations_) .lazyEvaluation(true). //checker(checker). start(guess_).target(target).parameterValidator(spv).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); }
From source file:edu.washington.gs.skyline.model.quantification.RegressionFit.java
public Double computeRSquared(CalibrationCurve curve, List<WeightedObservedPoint> points) { SummaryStatistics yValues = new SummaryStatistics(); SummaryStatistics residuals = new SummaryStatistics(); for (WeightedObservedPoint point : points) { Double yFitted = curve.getY(point.getX()); if (yFitted == null) { continue; }/*from ww w . j a v a2 s. com*/ yValues.addValue(point.getY()); residuals.addValue(point.getY() - yFitted); } if (0 == residuals.getN()) { return null; } double yMean = yValues.getMean(); double totalSumOfSquares = points.stream().mapToDouble(p -> (p.getY() - yMean) * (p.getY() - yMean)).sum(); double sumOfSquaresOfResiduals = residuals.getSumsq(); double rSquared = 1 - sumOfSquaresOfResiduals / totalSumOfSquares; return rSquared; }
From source file:eu.tango.energymodeller.energypredictor.CpuAndAcceleratorEnergyPredictor.java
/** * This performs a calculation to determine how close the fit is for a given * model.//from w w w. j a va 2s. c om * * @param function The PolynomialFunction to assess * @param observed The actual set of observed points * @return The sum of the square error. */ private double getSumOfSquareError(PolynomialFunction function, List<WeightedObservedPoint> observed) { double answer = 0; for (WeightedObservedPoint current : observed) { double error = current.getY() - function.value(current.getX()); answer = answer + (error * error); } return answer; }
From source file:eu.tango.energymodeller.energypredictor.CpuAndBiModalAcceleratorEnergyPredictor.java
/** * This performs a calculation to determine how close the fit is for a given * model./*from ww w . ja v a2s . c om*/ * * @param function The PolynomialFunction to assess * @param observed The actual set of observed points * @return The sum of the square error. */ private double getSumOfSquareError(GroupingFunction function, List<WeightedObservedPoint> observed) { double answer = 0; for (WeightedObservedPoint current : observed) { double error = current.getY() - function.value(current.getX()); answer = answer + (error * error); } return answer; }
From source file:edu.ucsf.valelab.saim.calculations.SaimErrorFunction.java
/** * For each observedPoint.getX calculates the predicted intensity * Returns the sum of absolute errors//from w ww . j a va 2s . c om * @param point {A, B, h} * @return sum of absolute errors */ @Override public double value(double[] point) { if (point.length != 3) { throw new DimensionMismatchException(point.length, 3); } double A = point[0]; double B = point[1]; double h = point[2]; double error = 0.0; for (WeightedObservedPoint observedPoint : observedPoints_) { double angle = observedPoint.getX(); Complex rTE = fresnelTE_.get(angle); double phaseDiff = SaimCalc.PhaseDiff(data_.wavelength_, angle, data_.nSample_, h); double c = rTE.getReal(); double d = rTE.getImaginary(); double val = 1 + 2 * c * Math.cos(phaseDiff) - 2 * d * Math.sin(phaseDiff) + c * c + d * d; error += Math.abs(A * val + B - observedPoint.getY()); } return error; }
From source file:be.ugent.maf.cellmissy.analysis.doseresponse.impl.SigmoidFitterImpl.java
@Override public void fitBotTopConstrain(List<DoseResponsePair> dataToFit, SigmoidFittingResultsHolder resultsHolder, Double bottomConstrain, Double topConstrain, int standardHillslope) { final Double bottom = bottomConstrain; final Double top = topConstrain; //initial parameter values for fitting: middle x and standard hillslope double[] xValues = AnalysisUtils.generateXValues(dataToFit); double[] yValues = AnalysisUtils.generateYValues(dataToFit); double initialLogEC50; double maxX = xValues[0]; double minX = xValues[0]; for (int i = 0; i < xValues.length; i++) { if (xValues[i] < minX) { minX = xValues[i];/*from www . j a v a 2 s .c om*/ } else if (xValues[i] > maxX) { maxX = xValues[i]; } } initialLogEC50 = (maxX + minX) / 2; final double[] initialGuesses = new double[] { initialLogEC50, standardHillslope }; //add all datapoint to collection with standard weight 1.0 Collection<WeightedObservedPoint> observations = new ArrayList<>(); for (int i = 0; i < xValues.length; i++) { observations.add(new WeightedObservedPoint(1.0, xValues[i], yValues[i])); } final ParametricUnivariateFunction function = new ParametricUnivariateFunction() { /** * @param conc The concentration of the drug, log transformed * @param paramaters The fitted parameters (bottom, top, logEC50 and * hillslope) * @return The velocity */ @Override public double value(double conc, double[] parameters) { double logEC50 = parameters[0]; double hillslope = parameters[1]; return (bottom + (top - bottom) / (1 + Math.pow(10, (logEC50 - conc) * hillslope))); } @Override public double[] gradient(double conc, double[] parameters) { double logEC50 = parameters[0]; double hillslope = parameters[1]; return new double[] { (hillslope * Math.log(10) * Math.pow(10, hillslope * (logEC50 + conc)) * (bottom - top)) / (Math.pow(Math.pow(10, hillslope * conc) + Math.pow(10, hillslope * logEC50), 2)), (Math.log(10) * (logEC50 - conc) * (bottom - top) * Math.pow(10, (logEC50 + conc) * hillslope)) / Math.pow((Math.pow(10, logEC50 * hillslope) + Math.pow(10, hillslope * conc)), 2) }; } }; //set up the fitter with the observations and function created above DoseResponseAbstractCurveFitter fitter = new DoseResponseAbstractCurveFitter() { @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 (final WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( function, observations); // build a new least squares problem set up to fit a secular and harmonic curve to the observed points return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE) .start(initialGuesses).target(target).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); } }; OptimumImpl bestFit = fitter.performRegression(observations); //get best-fit parameters double[] params = bestFit.getPoint().toArray(); double logEC50 = params[0]; double hillslope = params[1]; //set the values in the fitting results holder resultsHolder.setBottom(bottom); resultsHolder.setTop(top); resultsHolder.setLogEC50(logEC50); resultsHolder.setHillslope(hillslope); //bottom and top parameter were constrained List<String> constrainedParam = new ArrayList<>(); constrainedParam.add("bottom"); constrainedParam.add("top"); resultsHolder.setConstrainedParameters(constrainedParam); resultsHolder.setCovariances(bestFit.getCovariances(0).getData()); }
From source file:be.ugent.maf.cellmissy.analysis.doseresponse.impl.SigmoidFitterImpl.java
@Override public void fitTopConstrain(List<DoseResponsePair> dataToFit, SigmoidFittingResultsHolder resultsHolder, Double topConstrain, int standardHillslope) { final Double top = topConstrain; //initial parameter values for fitting: lowest y, middle x and standard hillslope double[] yValues = AnalysisUtils.generateYValues(dataToFit); double[] xValues = AnalysisUtils.generateXValues(dataToFit); double initialBottom = yValues[0]; double initialLogEC50; double maxX = xValues[0]; double minX = xValues[0]; for (int i = 0; i < yValues.length; i++) { if (yValues[i] < initialBottom) { initialBottom = yValues[i];/*from ww w . j a v a2 s . co m*/ } if (xValues[i] < minX) { minX = xValues[i]; } else if (xValues[i] > maxX) { maxX = xValues[i]; } } initialLogEC50 = (maxX + minX) / 2; final double[] initialGuesses = new double[] { initialBottom, initialLogEC50, standardHillslope }; //add all datapoint to collection with standard weight 1.0 Collection<WeightedObservedPoint> observations = new ArrayList<>(); for (int i = 0; i < xValues.length; i++) { observations.add(new WeightedObservedPoint(1.0, xValues[i], yValues[i])); } final ParametricUnivariateFunction function = new ParametricUnivariateFunction() { /** * @param conc The concentration of the drug, log transformed * @param paramaters The fitted parameters (bottom, logEC50 and * hillslope) * @return The velocity */ @Override public double value(double conc, double[] parameters) { double bottom = parameters[0]; double logEC50 = parameters[1]; double hillslope = parameters[2]; return (bottom + (top - bottom) / (1 + Math.pow(10, (logEC50 - conc) * hillslope))); } @Override public double[] gradient(double conc, double[] parameters) { double bottom = parameters[0]; double logEC50 = parameters[1]; double hillslope = parameters[2]; return new double[] { 1 - (1 / ((Math.pow(10, (logEC50 - conc) * hillslope)) + 1)), (hillslope * Math.log(10) * Math.pow(10, hillslope * (logEC50 + conc)) * (bottom - top)) / (Math.pow(Math.pow(10, hillslope * conc) + Math.pow(10, hillslope * logEC50), 2)), (Math.log(10) * (logEC50 - conc) * (bottom - top) * Math.pow(10, (logEC50 + conc) * hillslope)) / Math.pow((Math.pow(10, logEC50 * hillslope) + Math.pow(10, hillslope * conc)), 2) }; } }; //set up the fitter with the observations and function created above DoseResponseAbstractCurveFitter fitter = new DoseResponseAbstractCurveFitter() { @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 (final WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( function, observations); // build a new least squares problem set up to fit a secular and harmonic curve to the observed points return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE) .start(initialGuesses).target(target).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); } }; OptimumImpl bestFit = fitter.performRegression(observations); double[] params = bestFit.getPoint().toArray(); double bottom = params[0]; double logEC50 = params[1]; double hillslope = params[2]; resultsHolder.setBottom(bottom); resultsHolder.setTop(top); resultsHolder.setLogEC50(logEC50); resultsHolder.setHillslope(hillslope); //top parameter was constrained List<String> constrainedParam = new ArrayList<>(); constrainedParam.add("top"); resultsHolder.setConstrainedParameters(constrainedParam); resultsHolder.setCovariances(bestFit.getCovariances(0).getData()); }
From source file:be.ugent.maf.cellmissy.analysis.doseresponse.impl.SigmoidFitterImpl.java
@Override public void fitBotConstrain(List<DoseResponsePair> dataToFit, SigmoidFittingResultsHolder resultsHolder, Double bottomConstrain, int standardHillslope) { final Double bottom = bottomConstrain; //initial parameter values for fitting: highest y, middle x and standard hillslope double[] yValues = AnalysisUtils.generateYValues(dataToFit); double[] xValues = AnalysisUtils.generateXValues(dataToFit); double initialTop = yValues[0]; double initialLogEC50; double maxX = xValues[0]; double minX = xValues[0]; for (int i = 0; i < yValues.length; i++) { if (yValues[i] > initialTop) { initialTop = yValues[i];//ww w. j a v a 2s . com } if (xValues[i] < minX) { minX = xValues[i]; } else if (xValues[i] > maxX) { maxX = xValues[i]; } } initialLogEC50 = (maxX + minX) / 2; final double[] initialGuesses = new double[] { initialTop, initialLogEC50, standardHillslope }; //add all datapoint to collection with standard weight 1.0 Collection<WeightedObservedPoint> observations = new ArrayList<>(); for (int i = 0; i < xValues.length; i++) { observations.add(new WeightedObservedPoint(1.0, xValues[i], yValues[i])); } final ParametricUnivariateFunction function = new ParametricUnivariateFunction() { /** * @param conc The concentration of the drug, log transformed * @param paramaters The fitted parameters (top, logEC50 and * hillslope) * @return The velocity */ @Override public double value(double conc, double[] parameters) { double top = parameters[0]; double logEC50 = parameters[1]; double hillslope = parameters[2]; return (bottom + (top - bottom) / (1 + Math.pow(10, (logEC50 - conc) * hillslope))); } @Override public double[] gradient(double conc, double[] parameters) { double top = parameters[0]; double logEC50 = parameters[1]; double hillslope = parameters[2]; return new double[] { 1 / ((Math.pow(10, (logEC50 - conc) * hillslope)) + 1), (hillslope * Math.log(10) * Math.pow(10, hillslope * (logEC50 + conc)) * (bottom - top)) / (Math.pow(Math.pow(10, hillslope * conc) + Math.pow(10, hillslope * logEC50), 2)), (Math.log(10) * (logEC50 - conc) * (bottom - top) * Math.pow(10, (logEC50 + conc) * hillslope)) / Math.pow((Math.pow(10, logEC50 * hillslope) + Math.pow(10, hillslope * conc)), 2) }; } }; //set up the fitter with the observations and function created above DoseResponseAbstractCurveFitter fitter = new DoseResponseAbstractCurveFitter() { @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 (final WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( function, observations); // build a new least squares problem set up to fit a secular and harmonic curve to the observed points return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE) .start(initialGuesses).target(target).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); } }; OptimumImpl bestFit = fitter.performRegression(observations); double[] params = bestFit.getPoint().toArray(); double top = params[0]; double logEC50 = params[1]; double hillslope = params[2]; resultsHolder.setBottom(bottom); resultsHolder.setTop(top); resultsHolder.setLogEC50(logEC50); resultsHolder.setHillslope(hillslope); //bottom parameter was constrained List<String> constrainedParam = new ArrayList<>(); constrainedParam.add("bottom"); resultsHolder.setConstrainedParameters(constrainedParam); resultsHolder.setCovariances(bestFit.getCovariances(0).getData()); }
From source file:be.ugent.maf.cellmissy.analysis.doseresponse.impl.SigmoidFitterImpl.java
@Override public void fitNoConstrain(List<DoseResponsePair> dataToFit, SigmoidFittingResultsHolder resultsHolder, int standardHillslope) { //initial parameter values for fitting: lowest y, highest y, middle x and standard hillslope double[] yValues = AnalysisUtils.generateYValues(dataToFit); double[] xValues = AnalysisUtils.generateXValues(dataToFit); double initialTop = yValues[0]; double initialBottom = yValues[0]; double initialLogEC50; double maxX = xValues[0]; double minX = xValues[0]; for (int i = 0; i < yValues.length; i++) { if (yValues[i] < initialBottom) { initialBottom = yValues[i];/*from w w w . jav a 2 s .c o m*/ } else if (yValues[i] > initialTop) { initialTop = yValues[i]; } if (xValues[i] < minX) { minX = xValues[i]; } else if (xValues[i] > maxX) { maxX = xValues[i]; } } initialLogEC50 = (maxX + minX) / 2; final double[] initialGuesses = new double[] { initialBottom, initialTop, initialLogEC50, standardHillslope }; //add all datapoint to collection with standard weight 1.0 Collection<WeightedObservedPoint> observations = new ArrayList<>(); for (int i = 0; i < xValues.length; i++) { observations.add(new WeightedObservedPoint(1.0, xValues[i], yValues[i])); } final ParametricUnivariateFunction function = new ParametricUnivariateFunction() { /** * @param conc The concentration of the drug, log transformed * @param paramaters The fitted parameters (bottom, top, logEC50 and * hillslope) * @return The velocity */ @Override public double value(double conc, double[] parameters) { double bottom = parameters[0]; double top = parameters[1]; double logEC50 = parameters[2]; double hillslope = parameters[3]; return (bottom + (top - bottom) / (1 + Math.pow(10, (logEC50 - conc) * hillslope))); } @Override public double[] gradient(double conc, double[] parameters) { double bottom = parameters[0]; double top = parameters[1]; double logEC50 = parameters[2]; double hillslope = parameters[3]; return new double[] { 1 - (1 / ((Math.pow(10, (logEC50 - conc) * hillslope)) + 1)), 1 / ((Math.pow(10, (logEC50 - conc) * hillslope)) + 1), (hillslope * Math.log(10) * Math.pow(10, hillslope * (logEC50 + conc)) * (bottom - top)) / (Math.pow(Math.pow(10, hillslope * conc) + Math.pow(10, hillslope * logEC50), 2)), (Math.log(10) * (logEC50 - conc) * (bottom - top) * Math.pow(10, (logEC50 + conc) * hillslope)) / Math.pow((Math.pow(10, logEC50 * hillslope) + Math.pow(10, hillslope * conc)), 2) }; } }; //set up the fitter with the observations and function created above DoseResponseAbstractCurveFitter fitter = new DoseResponseAbstractCurveFitter() { @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 (final WeightedObservedPoint obs : observations) { target[i] = obs.getY(); weights[i] = obs.getWeight(); ++i; } final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction( function, observations); // build a new least squares problem set up to fit a secular and harmonic curve to the observed points return new LeastSquaresBuilder().maxEvaluations(Integer.MAX_VALUE).maxIterations(Integer.MAX_VALUE) .start(initialGuesses).target(target).weight(new DiagonalMatrix(weights)) .model(model.getModelFunction(), model.getModelFunctionJacobian()).build(); } }; OptimumImpl bestFit = fitter.performRegression(observations); //get the best-fit parameters double[] params = bestFit.getPoint().toArray(); double bottom = params[0]; double top = params[1]; double logEC50 = params[2]; double hillslope = params[3]; //set the fields of the fitting results holder resultsHolder.setBottom(bottom); resultsHolder.setTop(top); resultsHolder.setLogEC50(logEC50); resultsHolder.setHillslope(hillslope); //no parameters were constrained resultsHolder.setConstrainedParameters(new ArrayList<String>()); //TEST: what is the effect of the singularity threshold argument? resultsHolder.setCovariances(bestFit.getCovariances(0).getData()); }