26medias / timeseries-analysis

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

Forecasting Data #10

Open srinivasyl opened 6 years ago

srinivasyl commented 6 years ago

I've a data set of 120 items i need to forecast next item or next 5 items how to do it.

I've tried below example `var forecastDatapoint = 121;

var coeffs = t.ARMaxEntropy({
    data:   t.data.slice(0,120)
});
console.log(coeffs);

var forecast    = 0;    // Init the value at 0.
for (var i=0;i<coeffs.length;i++) { // Loop through the coefficients
    forecast -= t.data[10-i][1]*coeffs[i];
}
console.log("forecast",forecast);`

I have noticed that you've no where used forecastDatapoint this variable in above example so how exactly you're calculating 121 (11 in example) data point also tired the sliding_regression_forecast but it failed to show red dot indicate at which point the forecast starts. can you help in forecasting next 5 data points?

26medias commented 6 years ago

// Calculate the AR coefficients of the 120 previous points
var coeffs = t.ARMaxEntropy({
    data:   t.data.slice(0,120) // regression_forecast_optimize() give you the number of sample to use
});
// You might want to use only the last 20 points maybe to have a forecast that considers only the latest movements and not the entire history? It'd work a lot better for real-life data:
var coeffs = t.ARMaxEntropy({
    data:   t.data.slice(t.data.length-20)
});

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

// Calculate the forecasted value of the 21st 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);

// Now push that number to t.data and do the same thing again:
// Get the last 20 points (so the 19 latest of the point from the previous data + the forecast we just found making the 20th point)
t.data.push([new Date(), forecast]);
var coeffs = t.ARMaxEntropy({
    data:   t.data.slice(t.data.length-20)
});
//...

If you look at the code of , this is what it does:

If you use sliding_regression_forecast(), make sure that your sample size if only a few points, not the entire size of your data.

dushyantbangal commented 6 years ago

@26medias if I am collecting temperature data every 10 minutes. So I'll have 144 records for the whole day. If I need to forecast the next day's data, won't I need to use the whole dataset in order for it to understand the pattern?

26medias commented 6 years ago

In that case, yes, it makes sense

If it still doesn't work, can you send a demo of your code on https://plnkr.co/ or another JS editor?