# Source code for naginterfaces.library.examples.stat.moving_average_ex

```#!/usr/bin/env python3
"``naginterfaces.library.stat.moving_average`` Python Example."

# pylint: disable=invalid-name,too-many-locals

from naginterfaces.library import stat

[docs]def main():
"""
Example for :func:`naginterfaces.library.stat.moving_average`.

Calculate the mean and, optionally, the standard deviation using a
rolling window for an arbitrary sized data stream.

>>> main()
naginterfaces.library.stat.moving_average Python Example Results.
Spencer's 15-point moving average for the change in rate of the
Earth's rotation between 1821 and 1850.
Interval         Mean        Std. Dev.
--------------------------------------
[  1, 15]       -427.6           -
[  2, 16]       -332.5           -
[  3, 17]       -337.1           -
[  4, 18]       -438.2           -
[  5, 19]       -604.4           -
[  6, 20]       -789.4           -
[  7, 21]       -935.4           -
[  8, 22]       -990.6           -
[  9, 23]       -927.1           -
[ 10, 24]       -752.1           -
[ 11, 25]       -501.3           -
[ 12, 26]       -227.2           -
[ 13, 27]         23.2           -
[ 14, 28]        236.2           -
[ 15, 29]        422.4           -
[ 16, 30]        604.2           -
Total number of observations: 30
Length of window: 15
"""

print('naginterfaces.library.stat.moving_average Python Example Results.')
print('Spencer\'s 15-point moving average for the change in rate of the')
print('Earth\'s rotation between 1821 and 1850.')

# The type of weighting to use: each position in the window has its own
# weight.
iwt = 2
wt = [
-3.0, -6.0, -5.0, 3.0, 21.0, 46.0, 67.0,
74.0, 67.0, 46.0, 21.0, 3.0, -5.0, -6.0, -3.0
]
# The length of the rolling window:
m = 15
# We would not like to compute standard deviations:
wantsd = False

# Define the blocks of data:
x_full = [
[-2170.0, -1770.0, -1660.0, -1360.0, -1100.0],
[
-950.0, -640.0, -370.0, -140.0, -250.0, -510.0, -620.0,
-730.0, -880.0, -1130.0
],
[
-1200.0, -830.0, -330.0, -190.0, 210.0, 170.0, 440.0, 440.0,
780.0, 880.0, 1220.0, 1260.0, 1140.0, 850.0, 640.0
],
]

# Communication object:
comm = {}

'Interval         Mean        Std. Dev.',
]

# No observations have been processed so far:
pn = 0

for x in x_full:

# Calculate the statistics for this block:
pn, rmean, rsd = stat.moving_average(
m, x,
iwt=iwt, wt=wt, pn=pn, wantsd=wantsd, comm=comm
)

# The number of results printed so far:
offset = max(0, pn - len(x) - m + 1)

# Display the results for this block of data:
for obs_i, obs_x in enumerate(rmean):
interval_a = obs_i + 1 + offset
interval_b = interval_a + m - 1
interval = '[{:3d},{:3d}]'.format(interval_a, interval_b)
print_row = interval + ' '*3 + '{:10.1f}'.format(obs_x) + ' '*3 + (
'{:10.1f}'.format(rsd[obs_i]) if wantsd else ' '*8 + '-'
)
print(print_row)

print('Total number of observations: {:d}'.format(pn))
print('Length of window: {:d}'.format(m))

if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)
```