Example usage for org.apache.commons.math3.fitting GaussianCurveFitter fit

List of usage examples for org.apache.commons.math3.fitting GaussianCurveFitter fit

Introduction

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

Prototype

public double[] fit(Collection<WeightedObservedPoint> points) 

Source Link

Document

Fits a curve.

Usage

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>/*  w w  w.jav a2 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;
}