List of usage examples for org.apache.commons.math3.fitting GaussianCurveFitter create
public static GaussianCurveFitter create()
From source file:ardufocuser.starfocusing.Utils.java
public static double[] computeGaussianParams(int[][] image, int starCenterX, int starCenterY, int radius) { int min = computeMinMax(image, starCenterX - radius, starCenterY - radius, starCenterX + radius, starCenterY + radius)[0];/* www.j av a2 s.c om*/ WeightedObservedPoints obs = new WeightedObservedPoints(); for (int y = starCenterY - radius; y <= starCenterY + radius; y++) { for (int x = starCenterX - radius; x <= starCenterX + radius; x++) { double d = computeDistance(starCenterX, starCenterY, x, y); obs.add(d, image[x][y] - min); } } double[] parameters = GaussianCurveFitter.create() .withStartPoint(new double[] { image[starCenterX][starCenterY], 0.0, 1.0 }).fit(obs.toList()); return parameters; }
From source file:com.qtplaf.library.trading.data.indicators.GaussianSmoother.java
/** * Calculates the indicator data at the given index, for the list of indicator sources. * <p>/*from w ww .j ava 2s . c o m*/ * This indicator already calculated data is passed as a parameter because some indicators may need previous * calculated values or use them to improve calculation performance. * * @param index The data index. * @param indicatorSources The list of indicator sources. * @param indicatorData This indicator already calculated data. * @return The result data. */ @Override public Data calculate(int index, List<IndicatorSource> indicatorSources, DataList indicatorData) { // If index < 0 do nothing. if (index < 0) { return null; } // The unique data list and the index of the data. int period = getIndicatorInfo().getParameter("PERIOD").getValue().getInteger(); // If index < period, calculate the mean from scratch. if (index < period) { return getSource(index, indicatorSources); } // The Gaussian curve fitter and the Gaussian function. GaussianCurveFitter fitter = GaussianCurveFitter.create(); Gaussian.Parametric function = new Gaussian.Parametric(); // For every indicator source, build the list of observation points. int numIndexes = getNumIndexes(); double[] values = new double[numIndexes]; Arrays.fill(values, 0); int valueIndex = 0; for (IndicatorSource source : indicatorSources) { DataList dataList = source.getDataList(); List<Integer> indexes = source.getIndexes(); for (Integer dataIndex : indexes) { // The list of observations. WeightedObservedPoints obs = new WeightedObservedPoints(); int startIndex = index - period + 1; int endIndex = index; int x = 0; for (int i = startIndex; i <= endIndex; i++) { double y = dataList.get(i).getValue(dataIndex); obs.add(x, y); x++; } // Reduce last x to get the last coordinate applied. x--; // The parameters to apply to the function. double[] params = fitter.fit(obs.toList()); // The value. values[valueIndex] = function.value(x, params); valueIndex++; } } Data data = new Data(); data.setData(values); data.setTime(indicatorSources.get(0).getDataList().get(index).getTime()); return data; }
From source file:org.terracotta.statistics.derived.histogram.HistogramFittingTest.java
private double[] gaussianHistogramFit(long seed) { Random rndm = new Random(seed); Histogram hist = histogram(bias, bars, generate(rndm::nextGaussian).limit(100000)); return fit(hist, GaussianCurveFitter.create()); }
From source file:stats.GaussianFitMeanStdev.java
public void CalculateGlobalMeanStdev(ChrHistogramFactory gchisto, WindowPlan wins) { Long maxvalue = 0l;// www . jav a 2 s . co m int count = 0; for (String chr : wins.getChrList()) { if (!gchisto.hasChrHistogram(chr)) continue; for (Double d : gchisto.getChrHistogram(chr).retrieveRDBins()) { count++; if (d.longValue() > maxvalue) maxvalue = d.longValue(); } } if (count < 3 || maxvalue.intValue() < 3) { log.log(Level.SEVERE, "Error! Could not calculate Global Mean and Stdev! Count: " + count + " maxvalue: " + maxvalue); System.exit(-1); } Double[] bins = new Double[maxvalue.intValue() + 1]; java.util.Arrays.fill(bins, 0.0d); double sum = 0.0d; for (String chr : wins.getChrList()) { if (!gchisto.hasChrHistogram(chr)) continue; for (Double d : gchisto.getChrHistogram(chr).retrieveRDBins()) { sum += d; bins[d.intValue()] += 1; } } WeightedObservedPoints obs = new WeightedObservedPoints(); for (int i = 0; i < bins.length; i++) { obs.add(i, bins[i]); } double testmean = sum / count; double teststdev = 0.0d; for (String chr : wins.getChrList()) { for (Double d : gchisto.getChrHistogram(chr).retrieveRDBins()) { teststdev += (double) Math.pow(d - testmean, 2.0d); } } teststdev /= (double) (count - 1); teststdev = Math.sqrt(teststdev); double[] parameters; try { this.fitter = GaussianCurveFitter.create().withMaxIterations(50) .withStartPoint(new double[] { maxvalue * 0.4 / teststdev, testmean, teststdev * 0.5 }); parameters = fitter.fit(obs.toList()); } catch (TooManyIterationsException ex) { log.log(Level.WARNING, "Too many iterations! Using regular mean and stdev."); this.mean = testmean; this.stdev = teststdev; return; } Double mincut = parameters[1] - 2.0d * parameters[2]; Double maxcut = parameters[1] + 2.0d * parameters[2]; if (maxcut - mincut < 3 || mincut < 0 || maxcut < 0) { log.log(Level.WARNING, "Gaussian fitting calculation had " + mincut + " and " + maxcut + "! Not fitting values"); this.mean = parameters[1]; this.stdev = parameters[2]; return; } List<Double> tempvals = new ArrayList<>(); for (int i = mincut.intValue(); i < maxcut.intValue(); i++) { tempvals.add(bins[i]); } obs = new WeightedObservedPoints(); for (int i = mincut.intValue(); i < maxcut.intValue(); i++) { obs.add(i, bins[i]); } try { this.fitter = GaussianCurveFitter.create().withMaxIterations(50) .withStartPoint(new double[] { maxvalue * 0.4 / teststdev, testmean, teststdev * 0.5 }); } catch (TooManyIterationsException ex) { log.log(Level.WARNING, "Too many iterations! Using previously generated mean and stdev."); return; } double[] par = fitter.fit(obs.toList()); this.mean = par[1]; this.stdev = par[2]; }
From source file:stats.GaussianFitMeanStdev.java
public void CalculateMeanStdev(double[] values) { Long maxvalue = 0l;/* w w w . ja v a2 s .c om*/ if (values.length < 3) { log.log(Level.WARNING, "Mean/Stdev calculation had " + values.length + "initial observations! Doing simple estimate"); this.mean = StdevAvg.DoubleAvg(values); this.stdev = StdevAvg.stdevDBL(this.mean, values); return; } for (Double d : values) { if (Math.round(d) > maxvalue) maxvalue = d.longValue(); } if (maxvalue.intValue() <= 3) { log.log(Level.WARNING, "Chromosome had very little signal! Max signal value: " + maxvalue.intValue()); this.cantUse = true; return; } Double[] bins = new Double[maxvalue.intValue() + 1]; java.util.Arrays.fill(bins, 0.0d); for (Double d : values) { bins[d.intValue()] += 1; } double testmean = StdevAvg.DoubleAvg(values); double teststdev = StdevAvg.stdevDBL(testmean, values); log.log(Level.FINEST, "[GMSTDEV] testmean: " + testmean + " teststdev: " + teststdev); try { this.fitter = GaussianCurveFitter.create().withMaxIterations(50) .withStartPoint(new double[] { maxvalue * 0.4 / teststdev, testmean, teststdev * 0.5 }); } catch (TooManyIterationsException ex) { log.log(Level.WARNING, "Too many iterations for local fitter! Using regular mean and stdev."); this.mean = testmean; this.stdev = teststdev; return; } WeightedObservedPoints obs = new WeightedObservedPoints(); for (int i = 0; i < bins.length && i < testmean * 3; i++) { obs.add(i, bins[i]); } double[] parameters; try { parameters = fitter.fit(obs.toList()); } catch (TooManyIterationsException ex) { log.log(Level.WARNING, "Too many iterations for local fitter! Using regular mean and stdev."); this.mean = testmean; this.stdev = teststdev; return; } this.mean = parameters[1]; this.stdev = parameters[2]; Double mincut = parameters[1] - 2.0d * parameters[2]; Double maxcut = parameters[1] + 2.0d * parameters[2]; log.log(Level.FINEST, "[GMSTDEV] initial mean: " + this.mean + " initial stdev: " + this.stdev + " mincut, maxcut: ( " + mincut + "," + maxcut + " )"); if (maxcut - mincut < 3 || mincut < 0 || maxcut < 0) { log.log(Level.WARNING, "Mean/Stdev calculation had " + mincut + " and " + maxcut + "! Not cropping values"); return; } //java.util.Arrays.fill(bins, 0.0d); obs = new WeightedObservedPoints(); for (int i = mincut.intValue(); i < maxcut.intValue(); i++) { obs.add(i, bins[i]); } double[] par = fitter.fit(obs.toList()); this.mean = par[1]; this.stdev = par[2]; }