org.apache.commons.math3.fitting.GaussianCurveFitter Java Examples

The following examples show how to use org.apache.commons.math3.fitting.GaussianCurveFitter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GaussFitEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length >= 3) {
    throw new IOException("gaussfit function takes a maximum of 2 arguments.");
  }

  Object first = objects[0];

  double[] x = null;
  double[] y = null;

  if(objects.length == 1) {
    //Only the y values passed

    y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    x = new double[y.length];
    for(int i=0; i<y.length; i++) {
      x[i] = i;
    }

  } else if(objects.length == 2) {
    // x and y passed
    Object second = objects[1];
    x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();


  }

  GaussianCurveFitter curveFitter = GaussianCurveFitter.create();

  WeightedObservedPoints points = new WeightedObservedPoints();
  for(int i=0; i<x.length; i++) {
    points.add(x[i], y[i]);
  }

  List<WeightedObservedPoint> pointList = points.toList();

  double[] guess = new GaussianCurveFitter.ParameterGuesser(pointList).guess();
  curveFitter = curveFitter.withStartPoint(guess);

  double[] coef = curveFitter.fit(pointList);
  Gaussian gaussian = new Gaussian(coef[0], coef[1], coef[2]);
  List list = new ArrayList();
  for(double xvalue : x) {
    double yvalue= gaussian.value(xvalue);
    list.add(yvalue);
  }

  return new VectorFunction(gaussian, list);
}