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:
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.
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: