Skip to content
Rolling average, Ro...
 
Notifications
Clear all

Rolling average, Rolling Sdeviation, etc.

1 Posts
1 Users
0 Reactions
1,158 Views
Avatar
Posts: 26
Moderator
Topic starter
(@drice)
Member
Joined: 3 years ago

A function that maps a 1-D array to a scalar is called an array-reducing function. Examples are Sum, Average, SDeviation, Product, Min, Max, etc.  Occasionally I get asked the question: How do I compute the rolling metric (e.g., the rolling average or rolling sdeviation), in which you want the array reducing function to be applied to the most recent «windowSize» values (i.e., applied to a window).

Here is a User-Defined Function that can be used with any array reducing function, including your own UDFs, given that they have the form
Function F( x : [I] ; I : Index ). 

Function Rolling( metric : Function( z:[I] ; I : Index ) ;
                               x : [T] ; 
                               T : Index ; 
                               windowSize : positive atom ) ::= 

LocalIndex Window := 1-windowSize .. 0; 
metric( X[@T=@T+Window], Window )

You could use this to compute the rolling Sdeviation of revenue over the previous 12 months using:

Rolling( SDeviation, Revenue, Time, 12 )

Rolling has a time-complexity of O( N * windowSize * s(windowSize) ), where N is the length of your array's index (e.g., Time), and s(k) is the complexity of computing the metric (which is often O(k)).  

You can use the Rolling function to compute these:

Rolling( Sum, Revenue, Time, 12 )

Rolling( Average, Revenue, Time, 12 )

Rolling( Kurtosis, Revenue, Time, 12 )

Rolling( Max, Revenue, Time, 12 )

and others. However, a rolling Sum and rolling Ave can be easily computed in O(n) time, which can be noticeably faster.  For that, you can add this UDF:

Function RollingAve( X : Array[T] ; 
                                     T : Index ; 
                                     windowSize : positive atom ) :=
Local cum := cumulate(X,T) Do
   (cum - cum[@T=@T-windowSize,defVal:0]) / min([@T,windowSize])

For RollingSum, use the same definition but just remove the denominator.

 

Share:

Download the free edition of Analytica

The free version of Analytica lets you create and edit models with up to 101 variables, which is pretty substantial since each variable can be a multidimensional array. It also lets you run larger modes in ‘browse mode.’ Learn more about the free edition.

While Analytica doesn’t run on macOS, it does work with Parallels or VMWare through Windows.


    Analytica Cubes Pattern

    Download the free edition of Analytica

    The free version of Analytica lets you create and edit models with up to 101 variables, which is pretty substantial since each variable can be a multidimensional array. It also lets you run larger modes in ‘browse mode.’ Learn more about the free edition.

    While Analytica doesn’t run on macOS, it does work with Parallels or VMWare through Windows.


      Analytica Cubes Pattern