Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Holt's Exponential Smoothing
- * @returns {object} Object containing the forecast points, the residuals, the sum of squares of the residuals and the factor
- */
- ssci.fore.holt = function(){
- var data = [];
- var dataArray = [];
- var numPoints = 0;
- var output = [];
- var resids = [];
- var sumsq = 0;
- var factor = 0.3;
- var trend = 0.3;
- var x_conv = function(d){ return d[0]; };
- var y_conv = function(d){ return d[1]; };
- var l=[];
- var t=[];
- var funcs_T = {
- '1': t1,
- '2': t2,
- '3': t3,
- '4': t4
- };
- var funcT = '1'; //Function to use to calculate the starting value of
- /**
- * Initial average difference between first three pairs of points
- */
- function t1(){
- return (1/3)*(dataArray[1][1]-dataArray[0][1])+(dataArray[2][1]-dataArray[1][1])+(dataArray[3][1]-dataArray[2][1]);
- }
- /**
- * Calculate trend for entire series and multiply by average distance between points
- */
- function t2(){
- return ssci.reg.polyBig(dataArray,1).constants[1] * ((dataArray[numPoints-1][0]-dataArray[0][0])/(numPoints-1));
- }
- /**
- * Trend for first to second point
- */
- function t3(){
- return dataArray[1][1]-dataArray[0][1];
- }
- /**
- * Trend between first and last point
- */
- function t4(){
- return (dataArray[numPoints-1][1]-dataArray[0][1])/(numPoints-1);
- }
- function retVar(){
- var i;
- //Clear output array - needed to stop output growing when function called repeatedly
- output = [];
- //Create array of data using accessors
- dataArray = data.map( function(d){
- return [x_conv(d), y_conv(d)];
- });
- numPoints = dataArray.length;
- //Push first value to dataArray
- output.push(dataArray[0]);
- //Generate starting value for l - first value of dataArray
- if(l.length===0){
- l.push(dataArray[0][1]);
- }
- //Generate starting value for t - initial average difference between first three pairs of points
- if(t.length===0){
- t.push(funcs_T[funcT]);
- }
- //Calculate new values for level, trend and forecast
- for(i=1;i<(numPoints);i++){
- l.push(factor*dataArray[i][1]+(1-factor)*(l[i-1]+t[i-1]));
- t.push(trend*(l[i]-l[i-1])+(1-trend)*t[i-1]);
- //Create forecasts - current forecast is based on last periods estimates of l(evel) and t(rend)
- output.push([dataArray[i][0], l[i-1]+t[i-1]]);
- }
- //Calculate residuals
- sumsq=0;
- for(i=1;i<numPoints;i++){
- resids.push(dataArray[i][1]-output[i][1]);
- sumsq += Math.pow(dataArray[i][1]-output[i][1],2);
- }
- }
- /**
- * Get or set the initial value for the level
- * @param {number} [value] - The value for the level
- * @returns Either the value for the level or the enclosing object
- */
- retVar.initialLevel = function(value){
- if(!arguments.length){ return l[0]; }
- l = [];
- l.push(value);
- return retVar;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement