Home > Modeling Tips > Running Mean and Standard Deviation

Running Mean and Standard Deviation

This is an update to post published on August 31, 2009.  The attached model was updated to find negative means and an alternate method was included at the end.

I am frequently asked which built-in function gives either the running mean or running standard deviation of a model variable.  Unfortunately, there is no such built-in at this time (no, that is not what MEAN() does).

Luckily, however, we can replicate the behavior we desire from built-in functions by creating a reusable module.  I can create a module that calculates a running average and a running standard deviation from any model variable.

When building a reusable module component, it is important to carefully define what the input to the module will be (i.e., what are the parameters to the built-in function) and what the output of the module will be (i.e., what is the result or return value of the built-in function).  In this particular case, the input will be the variable whose running average or running standard deviation we wish to find.  There are two outputs:  the running average and the running standard deviation.  Note we do not have to use both outputs all the time.

Thus, our new module can be used as shown below:

image

Note the name of the module was chosen to give a meaningful context to the running mean and standard deviation variables, which have fixed names defined inside the reusable module.  As in this example, it is always a good idea to give the module outputs general names that make sense when qualified by a context (the module name).

The reusable module itself was built and tested in iThink, and can also be used in STELLA.  The input parameter was given an equation to allow the model to be completely tested and debugged before being reused.  The model appears below and can be downloaded by clicking here.

image

Note the input to the module is named value.  After importing the module, this will need to be assigned to the variable in question, Cash in the above example.  This can be done from outside the module by right-clicking on Cash and choosing “Module->Assign to”, or right-clicking on value and choosing “Module->Assign Input to”.  The outputs can be assigned in a similar way, or the Ghost tool can be used.

This method, while relatively easy to understand, does accurately compute the standard deviation when the mean of the running sum of squares is close in magnitude to the running mean squared.  An alternate method that does not suffer this problem was developed by Welford in 1962 and is implemented in the model that can be downloaded by clicking here.

Finally, I am including a simple reusable module that finds the maximum value of a model variable across the entire run of a simulation.  It can be downloaded by clicking here.  It uses a stock to hold the maximum value seen so far, and takes advantage of the fact that uniflows cannot be negative.  It is used the same way as the running mean and standard deviation module, but only has one output called maximum.

For more information about modules, consult the iThink and STELLA help files.  These on-line resources are also available:

Using Modules Webinar

Module FAQs

If you enjoyed this post, make sure you subscribe to my RSS feed!
  • Test, test test

  • Marion G.

    Tenuously related Question: We are modeling Nitrogen in a wetland ecosystem over five years, with different hydrologic inflows and outflows, and one flow within the system is nitirification, flow from NH4 to NO3.
    That is more generally:

    STOCK —FLOW—>STOCK

    Of interest is the maximum value of the flow over the entire five year period, while there may or may not be various local maximum of flow. Is there an easy, non-fiddly graph way of pulling that maximum value? Without a converter referencing itself, or a way for a converter to 'hold' its value, I'm having issues.

    Thanks much for any help!

  • kchichakly

    I have updated this article and included a module for you at the end that will find the maximum value across the simulation. You need to keep the maximum value in a stock. Stocks are the only things that “hold values” from one DT to the next. This should do the trick!

  • kchichakly

    I have updated this article and included a module for you at the end that will find the maximum value across the simulation. You need to keep the maximum value in a stock. Stocks are the only things that “hold values” from one DT to the next. This should do the trick!

  • Edroxuk

    hello what does ^ mean in stella

    • Npierson

      The carat symbol means raising a number to a certain power. 5^3 means 5 to the 3rd power (125)