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

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

Introduction

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

Prototype

WeightedObservedPoints

Source Link

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  .j  a v  a  2  s.co  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:heizung.Heizkurve.java

public static PolynomialFunction getPolynomialFit(List<Point> pList) {
    PolynomialFunction result = null;//from  w w w  .  j a  v  a  2 s.  co 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:edu.harvard.we99.services.MathServiceImpl.java

/**
 *
 * @param points Required to be n arrays of size 2.
 *//* w  ww.  jav 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:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries//from  ww w.  j  a v  a  2  s . c o m
 * 
 * @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: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   www.  java2 s.co  m

    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: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 w  w  .j  av  a  2 s  . com*/
    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:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries/*from   w w w  .  jav  a  2 s  . c  om*/
 * 
 * @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:com.qtplaf.library.trading.data.indicators.GaussianSmoother.java

/**
 * Calculates the indicator data at the given index, for the list of indicator sources.
 * <p>/*from  ww w .  j ava2 s  . 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:net.sf.mzmine.chartbasics.HistogramChartFactory.java

/**
 * Performs Gaussian fit on XYSeries//from w  w w. java2  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: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  .  j a  va  2  s .co m
 */
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;
}