For (n,m)
(n>=1e6, m>=10
) python array a
, I want to derive a new array b
by applying following recursive operation to it:
b(i, j) = nan if i=0
= (1 - k) * a(i-1, j) + k * a(i, j) if i=1
= (1 - k) * b(i-1, j) + k * a(i, j) if i>1
where k
is a fixed parameter such that 0 < k < 1
Note: The size of the array so speed (optimized/vectorized computation) is of importance so as to be able to run full simulation that uses many such operations.
Sample a
can be generated as follows:
import numpy as np
a = np.random.normal(0,1,10) # For m=10
for _ in range(1,1e6): # For n=1e6
a = np.c_[a, np.random.normal(0,1,10)]
a = a.transpose()
Sample output based on small sample input a
, n=m=3
and k=0.5
(note the last row). Basically this is computing exponentially decaying mean:
a = [[1, 2, 3],
[10, 20, 30],
[100, 200, 300]]
a = np.array(a)
# Expected solution for b is following:
b = [[nan, nan, nan],
[5.5, 11, 16.5],
[52.75, 105.5, 158.25]] # (100 + 5.5)/2 = 52.75, (200 + 11)/2 = 105.5, ...
# And below is not expected outcome (last row is not correct here)
b = [[nan, nan, nan],
[5.5, 11, 16.5],
[55, 110, 165]]