-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunning_stats.py
29 lines (25 loc) · 1017 Bytes
/
running_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
from numpy.lib.stride_tricks import as_strided
def running_average_simple(values, window=10):
if (window>=values.shape[0]):
return np.array([np.nan])
result = np.zeros(values.shape[0] - window + 1)
for i in range(len(result)):
result[i] = np.nanmean(values[i:i + window,:])
return result
def running_variance_simple(values, window=10):
if (window>=values.shape[0]):
return np.array([np.nan])
result = np.zeros(values.shape[0] - window + 1)
for i in range(len(result)):
result[i] = np.nanvar(values[i:i + window,:])
return result
def running_z_simple(values, means, window=10):
if (window>=values.shape[0]):
return np.array([np.nan])
nReps = values.shape[1]
result = np.zeros(values.shape[0] - window + 1)
assert len(means) == len(result)
for i in range(len(result)):
result[i] = np.nansum(values[i:i + window,:])*means[i]/(np.count_nonzero(~np.isnan(values[i:i + window,:])))
return result