List of usage examples for org.apache.commons.math3.fitting WeightedObservedPoints add
public void add(double weight, double x, double y)
From source file:org.autobet.ai.TeamRatersStatsApproximation.java
private Polynomial approximate(TeamRaterStats teamRaterStats, GameResult gameResult) { Map<Integer, RateStats> stats = teamRaterStats.getHomeStats(); List<Integer> rates = teamRaterStats.getRates(); final WeightedObservedPoints obs = new WeightedObservedPoints(); for (int rate : rates) { RateStats rateStats = stats.get(rate); int stat = rateStats.get(gameResult); int count = rateStats.getCount(); obs.add(count, rate, (double) stat / count); }//from w w w. ja v a 2 s . c o m return new Polynomial(fitter.fit(obs.toList())); }
From source file:org.micromanager.asidispim.fit.Fitter.java
/** * Utility to facilitate fitting data plotted in JFreeChart * Provide data in JFReeChart format (XYSeries), and retrieve univariate * function parameters that best fit (using least squares) the data. All data * points will be weighted equally.//from w ww .j av a2 s .c om * * TODO: investigate whether weighting (possibly automatic weighting) can * improve accuracy * * @param data xy series in JFReeChart format * @param type one of the Fitter.FunctionType predefined functions * @param guess initial guess for the fit. The number and meaning of these parameters depends on the FunctionType. Implemented: Gaussian: 0: Normalization, 1: Mean 2: Sigma * @return array with parameters, whose meaning depends on the FunctionType. * Use the function getXYSeries to retrieve the XYDataset predicted * by this fit */ public static double[] fit(XYSeries data, FunctionType type, double[] guess) { if (type == FunctionType.NoFit) { return null; } // create the commons math data object from the JFreeChart data object final WeightedObservedPoints obs = new WeightedObservedPoints(); for (int i = 0; i < data.getItemCount(); i++) { obs.add(1.0, data.getX(i).doubleValue(), data.getY(i).doubleValue()); } double[] result = null; switch (type) { case Pol1: final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(1); result = fitter1.fit(obs.toList()); break; case Pol2: final PolynomialCurveFitter fitter2 = PolynomialCurveFitter.create(2); result = fitter2.fit(obs.toList()); break; case Pol3: final PolynomialCurveFitter fitter3 = PolynomialCurveFitter.create(3); result = fitter3.fit(obs.toList()); break; case Gaussian: final GaussianWithOffsetCurveFitter gf = GaussianWithOffsetCurveFitter.create(); if (guess != null) { gf.withStartPoint(guess); } result = gf.fit(obs.toList()); default: break; } return result; }
From source file:org.micromanager.saim.fit.Fitter.java
/** * Utility to facilitate fitting data plotted in JFreeChart * Provide data in JFReeChart format (XYSeries), and retrieve univariate * function parameters that best fit (using least squares) the data. All data * points will be weighted equally./* www . j ava 2 s . c om*/ * * Various weightmethods are implemented and can be selected using the * weightMethods parameter. * * @param data xy series in JFReeChart format * @param type one of the Fitter.FunctionType predefined functions * @param guess initial guess for the fit. The number and meaning of these parameters depends on the FunctionType. Implemented: Gaussian: 0: Normalization, 1: Mean 2: Sigma * @param weightMethod One of the methods in the WeightMethod enum * @return array with parameters, whose meaning depends on the FunctionType. * Use the function getXYSeries to retrieve the XYDataset predicted * by this fit */ public static double[] fit(XYSeries data, FunctionType type, double[] guess, WeightMethod weightMethod) { if (type == FunctionType.NoFit) { return null; } // create the commons math data object from the JFreeChart data object final WeightedObservedPoints obs = new WeightedObservedPoints(); // range is used in weigt calculations double range = data.getMaxY() - data.getMinY(); for (int i = 0; i < data.getItemCount(); i++) { // add weight based on y intensity and selected weight method double weight = 1.0; // used in Equal method if (weightMethod != WeightMethod.Equal) { double valueMinusMin = data.getY(i).doubleValue() - data.getMinY(); weight = valueMinusMin / range; switch (weightMethod) { case Equal: break; // weight is already linear case Quadratic: weight *= weight; break; case Top50Linear: if (valueMinusMin < (0.5 * range)) weight = 0.0; break; case Top80Linear: if (valueMinusMin < (0.8 * range)) weight = 0.0; break; } } obs.add(weight, data.getX(i).doubleValue(), data.getY(i).doubleValue()); } // Carry out the actual fit double[] result = null; switch (type) { case Pol1: final PolynomialCurveFitter fitter1 = PolynomialCurveFitter.create(1); result = fitter1.fit(obs.toList()); break; case Pol2: final PolynomialCurveFitter fitter2 = PolynomialCurveFitter.create(2); result = fitter2.fit(obs.toList()); break; case Pol3: final PolynomialCurveFitter fitter3 = PolynomialCurveFitter.create(3); result = fitter3.fit(obs.toList()); break; case Gaussian: GaussianWithOffsetCurveFitter gf = GaussianWithOffsetCurveFitter.create(); gf = gf.withMaxIterations(50); if (guess != null) { gf.withStartPoint(guess); } result = gf.fit(obs.toList()); } return result; }
From source file:org.specvis.logic.Functions.java
/** * Get fitting coefficients for N degree polynomial. * @param x//from www. j a v a2 s .c o m * @param y * @param degree * @param reverseCoefficients * @return Fitting coefficients. */ public double[] fitPolynomialToData(double[] x, double[] y, int degree, boolean reverseCoefficients) { WeightedObservedPoints obs = new WeightedObservedPoints(); for (int i = 0; i < x.length; i++) { obs.add(1, x[i], y[i]); } PolynomialCurveFitter fitter = PolynomialCurveFitter.create(degree); double[] coefficients = fitter.fit(obs.toList()); if (reverseCoefficients) { return reverseDoublePrimitiveArray(coefficients); } else { return coefficients; } }