# Ticket #7197: trac_7197_basic_stats_part5.patch

File trac_7197_basic_stats_part5.patch, 1.9 KB (added by amhou, 12 years ago)
• ## sage/stats/all.py

```# HG changeset patch
# User Andrew Hou <amhou@uw.edu>
# Date 1258052851 28800
# Node ID c7e86a36719bc08e47f716ca04c85d825e7e04d0
# Parent  bbf5f88f04e9bd4aefd7d16e09b72b2328d6ce96
Moving average function added

diff -r bbf5f88f04e9 -r c7e86a36719b sage/stats/all.py```
 a from r import (ttest) from basic_stats import (mean, mode, std, variance, median) from basic_stats import (mean, mode, std, variance, median, moving_average) import hmm.all as hmm
• ## sage/stats/basic_stats.py

`diff -r bbf5f88f04e9 -r c7e86a36719b sage/stats/basic_stats.py`
 a upper = values[len(values)/2] return (lower + upper)/ZZ(2) def moving_average(v, bins=1): """ Provides the moving average of a list The moving average of a list is often used to smooth out noisy data. Given a selected number of bins, the original list will be cut up into that number of bins. Then, the mean of each bin is calculated, and appended into a new list. If 'v' is empty, we define the entries of the moving average to be NaN. INPUT: - v -- a list - bins -- number of bins, default set to 1 OUTPUT: - a list EXAMPLES:: sage: moving_average([1..10],4) [3/2, 4, 13/2, 9] sage: moving_average([]) [NaN] sage: moving_average([pi, e, I, sqrt(2), 3/5], 2) [1/2*pi + 1/2*e, 1/3*sqrt(2) + 1/3*I + 1/5] """ x = [] j = 0 bin_size = len(v)/bins for i in range(bins): tmp = [] for k in range(j,j+bin_size): tmp.append(v[k]) x.append(mean(tmp)) j += bin_size return x