## July 20, 2016

### Computing a Moving Average

Posted by Rick Kossik

GoldSim allows you to compute and display average outputs of any variable you are simulating.  For example, if you were using a daily timestep, you could compute monthly or annual values using a feature in GoldSim called reporting periods. These are defined in the Simulation Settings.

In some cases, however, you may want to compute a different kind of average – a moving average. For example, at any point in the simulation, you might want to reference the average value of a variable over the previous 10 days. GoldSim provides an easy way to do this.

Why would you want to compute a moving average?  As an example, consider a case where you are simulating a system that constantly adjusts (reacts to) one variable (e.g., a pumping rate from a pond) based on the value of some other varialble (a flow rate into the pond). If the flow rate was highly variable, you probably would not want to adjust the pumping rate based on the current (instantanteous value) of the flow rate (as the pumping rate would then also vary widely), but instead on the average value of that flow rate over some previous period (which will smooth the variations in the flow rate).

Mathematically, a moving average of X over some averaging time ATime is simply computed as follows:

That is, it is a time integral. GoldSim Stock elements (Reservoirs and Integrators) solve time integrals. Integrators are simplified versions of Reservoirs (although as we shall see, they provide some specialized features that Reservoirs do not).  In particular, Integrators provide the (optional) ability to compute a moving average.

An Integrator element looks lime this:

As you can see, the Integrator is a simplified version of the Reservoir:

•  Both require an Initial Value.
• The Integrator combines the Reservoir’s Addition Rate and Withdrawal Rate into a single Rate of Change.
• The Integrator does not have a Lower Bound or an Upper Bound.

We can use one of the Integrator’s advanced capabilities to compute a moving average.
We can access these capabilities by pressing the More button:

As can be seen, the Integrator provides the ability to compute up to three moving averages.

In the example below, we are averaging a stochastic signal (a streamflow), and computing a 30 day moving average and a 1 year moving average:

This adds two secondary outputs the the Integrator (Average.Month and Average.Year).

In this case, if we run the model and plot the results (the original signal and the two moving averages), it looks like this:

As can be seen, the moving average smooths the variations in the flow rate, and because it is backward-looking, shifts the peaks to the right (i.e., delays them).