Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * Holt's Exponential Smoothing
  4.  * @returns {object} Object containing the forecast points, the residuals, the sum of squares of the residuals and the factor
  5.  */
  6. ssci.fore.holt = function(){
  7.     var data = [];
  8.     var dataArray = [];
  9.     var numPoints = 0;
  10.     var output = [];
  11.     var resids = [];
  12.     var sumsq  = 0;
  13.     var factor = 0.3;
  14.     var trend  = 0.3;
  15.     var x_conv = function(d){ return d[0]; };
  16.     var y_conv = function(d){ return d[1]; };
  17.     var l=[];
  18.     var t=[];
  19.     var funcs_T = {
  20.         '1': t1,
  21.         '2': t2,
  22.         '3': t3,
  23.         '4': t4
  24.     };
  25.     var funcT = '1';    //Function to use to calculate the starting value of
  26.  
  27.     /**
  28.      * Initial average difference between first three pairs of points
  29.      */
  30.     function t1(){
  31.         return (1/3)*(dataArray[1][1]-dataArray[0][1])+(dataArray[2][1]-dataArray[1][1])+(dataArray[3][1]-dataArray[2][1]);
  32.     }
  33.     /**
  34.      * Calculate trend for entire series and multiply by average distance between points
  35.      */
  36.     function t2(){
  37.         return ssci.reg.polyBig(dataArray,1).constants[1] * ((dataArray[numPoints-1][0]-dataArray[0][0])/(numPoints-1));
  38.     }
  39.     /**
  40.      * Trend for first to second point
  41.      */
  42.     function t3(){
  43.         return dataArray[1][1]-dataArray[0][1];
  44.     }
  45.     /**
  46.      * Trend between first and last point
  47.      */
  48.     function t4(){
  49.         return (dataArray[numPoints-1][1]-dataArray[0][1])/(numPoints-1);
  50.     }
  51.    
  52.     function retVar(){
  53.         var i;
  54.        
  55.         //Clear output array - needed to stop output growing when function called repeatedly
  56.         output = [];
  57.        
  58.         //Create array of data using accessors
  59.         dataArray = data.map( function(d){
  60.             return [x_conv(d), y_conv(d)];
  61.         });
  62.         numPoints = dataArray.length;
  63.        
  64.         //Push first value to dataArray
  65.         output.push(dataArray[0]);
  66.        
  67.         //Generate starting value for l - first value of dataArray
  68.         if(l.length===0){
  69.             l.push(dataArray[0][1]);
  70.         }
  71.        
  72.         //Generate starting value for t - initial average difference between first three pairs of points
  73.         if(t.length===0){
  74.             t.push(funcs_T[funcT]);
  75.         }
  76.        
  77.         //Calculate new values for level, trend and forecast
  78.         for(i=1;i<(numPoints);i++){
  79.             l.push(factor*dataArray[i][1]+(1-factor)*(l[i-1]+t[i-1]));
  80.             t.push(trend*(l[i]-l[i-1])+(1-trend)*t[i-1]);
  81.             //Create forecasts - current forecast is based on last periods estimates of l(evel) and t(rend)
  82.             output.push([dataArray[i][0], l[i-1]+t[i-1]]);
  83.         }
  84.        
  85.         //Calculate residuals
  86.         sumsq=0;
  87.         for(i=1;i<numPoints;i++){
  88.             resids.push(dataArray[i][1]-output[i][1]);
  89.             sumsq += Math.pow(dataArray[i][1]-output[i][1],2);
  90.         }
  91.        
  92.     }
  93.    
  94.     /**
  95.      * Get or set the initial value for the level
  96.      * @param {number} [value] - The value for the level
  97.      * @returns Either the value for the level or the enclosing object
  98.      */
  99.     retVar.initialLevel = function(value){
  100.         if(!arguments.length){ return l[0]; }
  101.         l = [];
  102.        
  103.         l.push(value);
  104.        
  105.         return retVar;
  106.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement