1

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]]
Gerry
  • 606
  • 6
  • 16
  • 6
    @Jared `_` is a [great name](https://stackoverflow.com/a/5893946/770830) for a variable that's not used. – bereal Jul 23 '21 at 12:19
  • Is `b` the same size as `a`? If so, how is `b(0, j)` defined? – Seon Jul 23 '21 at 12:21
  • @Seon good point. I guess in this case `b(0, j)` is `nan`. Think of this as exponentially weighted moving average where `k` is decay rate – Gerry Jul 23 '21 at 12:22

0 Answers0