Example usage for org.apache.commons.math3.fitting WeightedObservedPoints toList

List of usage examples for org.apache.commons.math3.fitting WeightedObservedPoints toList

Introduction

In this page you can find the example usage for org.apache.commons.math3.fitting WeightedObservedPoints toList.

Prototype

public List<WeightedObservedPoint> toList() 

Source Link

Document

Gets a snapshot of the observed points.

Usage

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  . ja  va 2  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:cn.edu.xmu.tidems.control.support.TideMSUtil.java

public static double[] polynomialFit(final double[] x, final double[] y, final int order) {

    if ((x == null) || (x.length == 0) || (y == null) || (y.length == 0)) {
        throw new IllegalArgumentException("Input arrays x/y  can't be null or empty.");
    }/*  w  ww .  j a  va  2s  .c  o  m*/
    if (x.length != y.length) {
        throw new IllegalArgumentException("The length of arrays x, y must be equal.");
    }
    if (order < 1) {
        throw new IllegalArgumentException("Order must be greater than 0.");
    }
    if (x.length <= order) {
        throw new IllegalArgumentException("The length of array must be greater than order.");
    }
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        obs.add(x[i], y[i]);
    }
    final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(order);
    final double[] coeff = fitter.fit(obs.toList());
    return coeff;
}

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];/*from   w  w  w. j a v  a 2  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:heizung.Heizkurve.java

public static PolynomialFunction getPolynomialFit(List<Point> pList) {
    PolynomialFunction result = null;//from   www . j  a  v a2s .c o  m
    if (pList == null) {
        return result;
    }
    try {

        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (Point p : pList) {
            obs.add(p.getX(), p.getY());
        }

        final ParametricUnivariateFunction function = new PolynomialFunction.Parametric();
        // Start fit from initial guesses that are far from the optimal
        // values.
        // final SimpleCurveFitter fitter =
        // SimpleCurveFitter.create(function,
        // new double[] { -1e20, 3e15, -5e25 });
        final SimpleCurveFitter fitter = SimpleCurveFitter.create(function,
                new double[] { -2e20, 1e15, -1e25 });
        // 2e2 ist 2*10^2 = 2*100
        final double[] best = fitter.fit(obs.toList());
        // System.out.println("Parameters: " + best.length);
        // funktion ausgeben
        result = new PolynomialFunction(best);
    } catch (Exception e) {
        // e.printStackTrace();
        System.out.println("PolynomialFunction: " + e);
    }
    return result;
}

From source file:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries//from  w w w.  ja v a2 s .c o m
 * 
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYDataset data, int series, double gMin, double gMax) {
    // gaussian fit
    WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < data.getItemCount(series); i++) {
        double x = data.getXValue(series, i);
        if (x >= gMin && x <= gMax)
            obs.add(x, data.getYValue(series, i));
    }
    return fitter.fit(obs.toList());
}

From source file:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries/*from w  ww  .ja v  a2s.  co  m*/
 * 
 * @param series the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYSeries series, double gMin, double gMax) {
    // gaussian fit
    WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < series.getItemCount(); i++) {
        double x = series.getX(i).doubleValue();
        if (x >= gMin && x <= gMax)
            obs.add(x, series.getY(i).doubleValue());
    }

    return fitter.fit(obs.toList());
}

From source file:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries//from  w  w  w . java  2s  .  c  om
 * 
 * @param data the data
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(List<DataPoint> data, double gMin, double gMax) {
    // gaussian fit
    WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < data.size(); i++) {
        double x = data.get(i).getMZ();
        if (x >= gMin && x <= gMax)
            obs.add(x, data.get(i).getIntensity());
    }
    try {
        return fitter.fit(obs.toList());
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Cannot fit Gaussian from {} to {}", gMin, gMax, e);
        return null;
    }
}

From source file:edu.harvard.we99.services.MathServiceImpl.java

/**
 *
 * @param points Required to be n arrays of size 2.
 *//*from  w  w w.  ja  v  a2 s. c o  m*/
@Override
public double[] fitCurve(double[][] points) {

    if (points.length == 0) {
        throw new WebApplicationException(Response.status(400).build());
    }

    // Create weighted points.
    WeightedObservedPoints obs = new WeightedObservedPoints();
    for (double[] point : points) {
        if (point.length != 2) {
            throw new WebApplicationException(Response.status(400).build());
        }
        obs.add(point[0], point[1]);
    }
    return fitter.fit(obs.toList());

}

From source file:eu.tango.energymodeller.energypredictor.CpuOnlyPolynomialEnergyPredictor.java

/**
 * This calculates the mathematical function that predicts the power
 * consumption given the cpu utilisation.
 *
 * @param host The host to get the function for
 * @return The mathematical function that predicts the power consumption
 * given the cpu utilisation.//from w ww. ja v  a  2s  . c om
 */
private PredictorFunction<PolynomialFunction> retrieveModel(Host host) {
    PredictorFunction<PolynomialFunction> answer;
    if (modelCache.containsKey(host)) {
        /**
         * A small cache avoids recalculating the regression so often.
         */
        return modelCache.get(host);
    }
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (HostEnergyCalibrationData data : host.getCalibrationData()) {
        points.add(data.getCpuUsage(), data.getWattsUsed());
    }
    PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
    final double[] best = fitter.fit(points.toList());
    PolynomialFunction function = new PolynomialFunction(best);
    double sse = getSumOfSquareError(function, points.toList());
    double rmse = getRootMeanSquareError(sse, points.toList().size());
    answer = new PredictorFunction<>(function, sse, rmse);
    modelCache.put(host, answer);
    return answer;
}

From source file:eu.tango.energymodeller.energypredictor.CpuAndAcceleratorEnergyPredictor.java

/**
 * This calculates the mathematical function that predicts the power
 * consumption given the cpu utilisation.
 *
 * @param host The host to get the function for
 * @return The mathematical function that predicts the power consumption
 * given the cpu utilisation.//  w  w  w. ja  va  2  s .  co  m
 */
private PredictorFunction<PolynomialFunction> retrieveCpuModel(Host host) {
    PredictorFunction<PolynomialFunction> answer;
    if (modelCache.containsKey(host)) {
        /**
         * A small cache avoids recalculating the regression so often.
         */
        return modelCache.get(host);
    }
    WeightedObservedPoints points = new WeightedObservedPoints();
    for (HostEnergyCalibrationData data : host.getCalibrationData()) {
        points.add(data.getCpuUsage(), data.getWattsUsed());
    }
    PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
    final double[] best = fitter.fit(points.toList());
    PolynomialFunction function = new PolynomialFunction(best);
    double sse = getSumOfSquareError(function, points.toList());
    double rmse = getRootMeanSquareError(sse, points.toList().size());
    answer = new PredictorFunction<>(function, sse, rmse);
    modelCache.put(host, answer);
    return answer;
}