26medias / timeseries-analysis

NPM Package - Timeseries analysis, noise removal, stats, ...
237 stars 48 forks source link

Getting forecasted point #3

Closed quentin-sommer closed 8 years ago

quentin-sommer commented 8 years ago

Hi, I'm having trouble forecasting using this example of the docs:

let bestSettings = t.regression_forecast_optimize();
      t.sliding_regression_forecast({
        sample: bestSettings.sample,
        degree: bestSettings.degree,
        method: bestSettings.method
      });

How can I easily get the length + 1 point in a var ?

Thanks!

26medias commented 8 years ago

Sorry for the late reply, I only noticed the issue when receiving the notification that you closed it.

regression_forecast_optimize() will returns the best settings for the autoregression. For example: { MSE: 0.05086675645862624, method: 'ARMaxEntropy', degree: 4, sample: 20 }

To forecast a point, you then need to apply the auto-regression:

// Calculate the AR coefficients of the 10 previous points
var coeffs = t.ARMaxEntropy({
    data:   t.data.slice(0,20) // regression_forecast_optimize() give you the number of sample to use
});

// Output the coefficients to the console
console.log(coeffs);

// Calculate the forecasted value of the 21th datapoint using the AR coefficients:
var forecast    = 0;    // Init the value at 0.
for (var i=0;i<coeffs.length;i++) { // Loop through the coefficients
    forecast -= t.data[20-i][1]*coeffs[i];
    // Explanation for that line:
    // t.data contains the current dataset, which is in the format [ [date, value], [date,value], ... ]
    // For each coefficient, we substract from "forecast" the value of the "N - x" datapoint's value, multiplicated by the coefficient, where N is the last known datapoint value, and x is the coefficient's index.
}

console.log("forecast",forecast);
// Output: 92.7237232432106

sliding_regression_forecast() is creating a sliding window on a dataset, forecasting the next value so that the accuracy can be compared once the forecast and the actual data are charted. It's not really meant to forecast the next point, it's more for validation of a model. If you were using it to forecast the next point, then that point would be the last data in the dataset, but you'd have calculated the forecast for all the previous points also, which is a waste of processing power.

quentin-sommer commented 8 years ago

Thanks!