List of usage examples for org.apache.commons.math3.fitting WeightedObservedPoint WeightedObservedPoint
public WeightedObservedPoint(final double weight, final double x, final double y)
From source file:com.cloudera.hts.utils.math.PolyFit.java
public static void main(String[] args) { double[] x = { -5.0, -4.0, -3.0, -2.0, 0, 2, 3, 4 }; double[] y = { 21, 13, 7, 3, 1, 7, 13, 21 }; WeightedObservedPoints obs = new WeightedObservedPoints(); // Add points here; for instance, int i = 0;/*from w w w. j a v a2 s .c o m*/ for (double xc : x) { WeightedObservedPoint point = new WeightedObservedPoint(xc, y[i], 1.0); obs.add(xc, y[i]); System.out.println(xc + " " + y[i]); i++; } // Instantiate a third-degree polynomial fitter. PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2); // Retrieve fitted parameters (coefficients of the polynomial function). final double[] coeff = fitter.fit(obs.toList()); System.out.println(Arrays.toString(coeff)); }
From source file:net.liuxuan.temp.mathtest.java
public static void main(String[] args) { final CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer()); fitter.addObservedPoint(-1.00, 2.021170021833143); fitter.addObservedPoint(-0.99, 2.221135431136975); fitter.addObservedPoint(-0.98, 2.09985277659314); fitter.addObservedPoint(-0.97, 2.0211192647627025); // ... Lots of lines omitted ... fitter.addObservedPoint(0.99, -2.4345814727089854); // The degree of the polynomial is deduced from the length of the array containing // the initial guess for the coefficients of the polynomial. final double[] init = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 // Compute optimal coefficients. final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init); // Construct the polynomial that best fits the data. final PolynomialFunction fitted = new PolynomialFunction(best); System.out.println(fitted.value(-0.995)); ;/*from w w w .jav a 2 s.co m*/ System.out.println(fitted.value(0.995)); ; System.out.println(fitted.toString()); ; System.out.println("============================================================="); PolynomialCurveFitter pcf = PolynomialCurveFitter.create(3); WeightedObservedPoints s; List<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); points.add(new WeightedObservedPoint(1, -1.00, 2.021170021833143)); points.add(new WeightedObservedPoint(1, -0.99, 2.221135431136975)); points.add(new WeightedObservedPoint(1, -0.98, 2.09985277659314)); points.add(new WeightedObservedPoint(1, -0.97, 2.0211192647627025)); points.add(new WeightedObservedPoint(1, 0.99, 2.4345814727089854)); double a[] = pcf.fit(points); for (int i = 0; i < a.length; i++) { double d = a[i]; System.out.println(d); } System.out.println(compute(a, -0.995)); System.out.println(compute(a, 0.99)); System.out.println(compute(a, 0.995)); }
From source file:com.cloudera.hts.utils.math.MyFunc2.java
public static void main(String[] args) { double[] x = { -5.0, -4.0, -3.0, -2.0, 0, 2, 3, 4 }; double[] y = { 21, 13, 7, 3, 1, 7, 13, 21 }; FunctionFitTest fitter = new FunctionFitTest(); ArrayList<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); // Add points here; for instance, int i = 0;//from w w w.j a va2 s . c om for (double xc : x) { if (i < 1) { WeightedObservedPoint point = new WeightedObservedPoint(xc, y[i], 1.0); points.add(point); System.out.println(xc + " " + y[i]); } i++; } final double coeffs[] = fitter.fit(points); System.out.println(Arrays.toString(coeffs)); }
From source file:edu.ucsf.valelab.saim.calculations.TestSaimFitter.java
public void test() throws Exception { double wavelength = 488.0; double nSample = 1.36; double dOx = 500.0; double A = 100.0; double B = 100.0; double h = 16.0; double fractionMaxError = 0.0000001; final int nrTries = 10; // make a collection of "observed" points ArrayList<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); for (int i = -50; i <= 50; i += 2) { double angle = Math.toRadians(i); double I = A * SaimCalc.fieldStrength(wavelength, angle, nSample, dOx, h) + B; WeightedObservedPoint point = new WeightedObservedPoint(1.0, angle, I); points.add(point);/* w ww . j a v a2s . com*/ } // create the fitter SaimFunctionFitter sff = new SaimFunctionFitter(wavelength, dOx, nSample, false); double[] values = new double[] { A, B, h }; // test by varying the input for height for (int i = 0; i < nrTries; i++) { double[] guess = values.clone(); guess[0] = 50.0; guess[1] = 150.0; guess[2] = guess[2] + (guess[2] * (Math.random() - 0.5) * 10); if (guess[2] < 1.0) guess[2] = 1.0; for (int j = 0; j < guess.length; j++) { System.out.println("Guess: " + j + " " + guess[j]); } sff.setGuess(guess); final double[] coefficients = sff.fit(points); for (int j = 0; j < coefficients.length; j++) { System.out .println("coefficient " + j + ", expected: " + values[j] + ", found: " + coefficients[j]); assertEquals(values[j], coefficients[j], values[j] * fractionMaxError); } System.out.println("Value was calculated: " + sff.getCalcCount() + " times"); sff.resetCalcCount(); } // test by adding noise to the input // make a collection of "observed" points final double noiseFactor = 0.1; // 10% noise fractionMaxError = noiseFactor; double[] guess = values.clone(); guess[0] = 50.0; guess[1] = 150.0; guess[2] = 50.0; for (int j = 0; j < guess.length; j++) { System.out.println("Guess: " + j + " " + guess[j]); } for (int i = 0; i < nrTries; i++) { ArrayList<WeightedObservedPoint> noisyPoints = new ArrayList<WeightedObservedPoint>(); for (int j = -50; j <= 50; j += 2) { double angle = Math.toRadians(j); double I = A * SaimCalc.fieldStrength(wavelength, angle, nSample, dOx, h) + B; WeightedObservedPoint noisyPoint = new WeightedObservedPoint(1.0, angle, I + I * (Math.random() - 0.5) * noiseFactor); noisyPoints.add(noisyPoint); } sff.setGuess(guess); final double[] coefficients = sff.fit(noisyPoints); for (int j = 0; j < coefficients.length; j++) { System.out .println("coefficient " + j + ", expected: " + values[j] + ", found: " + coefficients[j]); assertEquals(values[j], coefficients[j], values[j] * fractionMaxError); } System.out.println("Value was calculated: " + sff.getCalcCount() + " times"); sff.resetCalcCount(); } }
From source file:edu.ucsf.valelab.saim.calculations.TestSaimErrorFunction.java
public void test() throws Exception { SaimData data = new SaimData(); data.wavelength_ = 488.0;// w ww .j a v a 2 s . c om data.nSample_ = 1.36; data.dOx_ = 500.0; data.A_ = 1000.0; data.B_ = 5000.0; data.heights_ = new double[] { 75.0 }; double maxError = 0.0000000001; // make a collection of "observed" points ArrayList<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); for (int i = -50; i <= 50; i += 1) { double angle = Math.toRadians(i); double I = data.A_ * SaimCalc.fieldStrength(data.wavelength_, angle, data.nSample_, data.dOx_, data.heights_[0]) + data.B_; WeightedObservedPoint point = new WeightedObservedPoint(1.0, angle, I); points.add(point); } // calculate the error with these ideal points (shoudle be 0) SaimErrorFunction sef = new SaimErrorFunction(data, points); double[] parameters = { data.A_, data.B_, data.heights_[0] }; double error = sef.value(parameters); System.out.println("SaimError error: " + error); assertEquals(0.0, error, maxError); // now add 1 to all the data points. Error should be number of data points ArrayList<WeightedObservedPoint> newPoints = new ArrayList<WeightedObservedPoint>(); for (WeightedObservedPoint point : points) { newPoints.add(new WeightedObservedPoint(1.0, point.getX(), point.getY() + 1.0)); } SaimErrorFunction sef2 = new SaimErrorFunction(data, newPoints); error = sef2.value(parameters); System.out.println("SaimError error: " + error); assertEquals(points.size(), error, maxError); }
From source file:edu.ucsf.valelab.saim.calculations.TestSaimErrorFunctionFitter.java
public void test() throws Exception { SaimData data = new SaimData(); data.wavelength_ = 488.0;/*from ww w .ja v a 2 s .c o m*/ data.nSample_ = 1.36; data.dOx_ = 500.0; data.A_ = 1000.0; data.B_ = 5000.0; data.heights_ = new double[] { 75.0 }; double maxError = 0.00000000001; int nrRepeats = 5; // make a collection of "observed" points ArrayList<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); for (int i = -50; i <= 50; i += 1) { double angle = Math.toRadians(i); double I = data.A_ * SaimCalc.fieldStrength(data.wavelength_, angle, data.nSample_, data.dOx_, data.heights_[0]) + data.B_; WeightedObservedPoint point = new WeightedObservedPoint(1.0, angle, I); points.add(point); } System.out.println("BOBYQA Test results (TestSaimErrorFunctionFitter:"); System.out.println("Goal: " + data.A_ + ", " + data.B_ + ", " + data.heights_[0]); for (int i = 0; i < nrRepeats; i++) { SaimErrorFunctionFitter sef = new SaimErrorFunctionFitter(data); double[] guess = { Math.random() * 4000.0, Math.random() * 10000.0, Math.random() * 300.0 }; sef.setGuess(guess); double[] result = sef.fit(points); System.out.println("Guess: " + guess[0] + ", " + guess[1] + ", " + guess[2]); System.out.println("Result: " + result[0] + ", " + result[1] + ", " + result[2]); } }
From source file:edu.ucsf.valelab.saim.data.IntensityData.java
/** * Output as needed by apache commons Math library for fitting * Currently assigns the weight 1.0 to all points * @return list of weightedObservedPoints for fitting *///w w w .j a v a2 s . c om public ArrayList<WeightedObservedPoint> getWeightedObservedPoints() { ArrayList<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>(); for (IntensityDataItem item : data_) { points.add(new WeightedObservedPoint(1.0, item.getAngleRadians(), item.getIntensity())); } return points; }
From source file:edu.washington.gs.skyline.model.quantification.CalibrationCurveDataSet.java
public WeightedObservedPoint getWeightedPoint(double x, double y) { return new WeightedObservedPoint(getRegressionWeighting().getWeighting(x, y), x, y); }
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 ww w . ja v a 2 s . c om*/ } 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()); }
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];//w ww. j a v a 2 s . c o 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[] { 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()); }