org.apache.commons.math3.analysis.interpolation.LoessInterpolator Java Examples

The following examples show how to use org.apache.commons.math3.analysis.interpolation.LoessInterpolator. 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: LoessEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object... objects) throws IOException{

  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) {
    Object second = objects[1];
    x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
  }

  LoessInterpolator interpolator = new LoessInterpolator(bandwidth, robustIterations);
  double[] smooth = interpolator.smooth(x, y);

  List<Number> list = new ArrayList<>();
  for(double yvalue : smooth) {
    list.add(yvalue);
  }

  PolynomialSplineFunction spline = interpolator.interpolate(x, y);

  VectorFunction vec = new VectorFunction(spline, list);
  vec.addToContext("x", x);
  vec.addToContext("y", y);

  return vec;
}
 
Example #2
Source File: PeakCountingOrderDetector.java    From symmetry with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
	public int calculateOrder(AFPChain afpChain, Atom[] ca) throws RefinerFailedException {

		try {

			RotationAxis axis = new RotationAxis(afpChain);
			logger.info("Calculating rotation samples");
			Pair<double[],double[]> pair = RotationOrderDetector.sampleRotations(ca, axis, degreeSampling);
			logger.info("Smoothing with LOESS");
			LoessInterpolator loess = new LoessInterpolator(bandwidth, robustnessIterations, loessAccuracy);

			double[] smoothed = loess.smooth(pair.getKey(), pair.getValue());
			logger.info("Counting Peaks");
			
			int nPeaks = countPeaks(smoothed, epsilon * Math.PI/180);
			logger.info("Found {} peaks",nPeaks);
			
			/*
			 *  TODO Currently this isn't likely to handle order=1 well,
			 *  since C1 cases can easily have, say, exactly 5 peaks.
			 *  We will need to combine this with some smarter fitting method.
			 */

			return nPeaks; // for now
			
//			return nPeaks>maxOrder? 1 : nPeaks;

		} catch (Exception e) {
			throw new RefinerFailedException(e);
		}

	}
 
Example #3
Source File: StlDecomposition.java    From stl-java with Apache License 2.0 4 votes vote down vote up
private TimesAndValues padEdges(double[] ts, double[] xs) {
  // Find step between times
  double step = Math.abs(ts[1] - ts[0]);
  // Times (assuming uniform
  double[] paddedTimes = new double[ts.length + 2];
  System.arraycopy(ts, 0, paddedTimes, 1, ts.length);
  paddedTimes[0] = paddedTimes[1] - step;
  paddedTimes[paddedTimes.length - 1] = paddedTimes[paddedTimes.length - 2] + step;

  // Series
  double[] paddedSeries = new double[xs.length + 2];
  System.arraycopy(xs, 0, paddedSeries, 1, xs.length);

  // Use Loess at ends to pad
  // n.b. For some reason, this can result in NaN values - perhaps similar to
  // https://issues.apache.org/jira/browse/MATH-296. If we see NaN, just "extrapolate" by copying
  // the end points :(

  double left = paddedSeries[1];
  double right = paddedSeries[paddedSeries.length - 2];

  double bandwidth = 0.3;
  if (ts.length * bandwidth > 2) {
    PolynomialSplineFunction loess = new LoessInterpolator(bandwidth, 2).interpolate(ts, xs);

    double loessLeft = loess.value(ts[0]);
    if (!Double.isNaN(loessLeft)) {
      left = loessLeft;
    }

    double loessRight = loess.value(ts[ts.length - 1]);
    if (!Double.isNaN(loessRight)) {
      right = loessRight;
    }
  }

  paddedSeries[0] = left;
  paddedSeries[paddedSeries.length - 1] = right;

  return new TimesAndValues(paddedTimes, paddedSeries);
}
 
Example #4
Source File: StlDecomposition.java    From stl-java with Apache License 2.0 3 votes vote down vote up
/**
 * Performs weighted Loess smoothing on a series.
 *
 * <p>
 *   Does not assume contiguous time.
 * </p>
 *
 * @param times
 *  The times.
 * @param series
 *  The time series data.
 * @param bandwidth
 *  The amount of neighbor points to consider for each point in Loess.
 * @param weights
 *  The weights to use for smoothing, if null, equal weights are assumed.
 * @return
 *  Loess-smoothed series.
 */
private double[] loessSmooth(double[] times,
                             double[] series,
                             double bandwidth,
                             double[] weights) {
  if (weights == null) {
    return new LoessInterpolator(
        bandwidth,
        config.getLoessRobustnessIterations()).smooth(times, series);
  } else {
    return new LoessInterpolator(
        bandwidth,
        config.getLoessRobustnessIterations()).smooth(times, series, weights);
  }
}