Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagate calculation result

Tags:

python

numpy

I'd like to initialize an array b similar to this, but faster:

a = [0.53, 0.66, 0.064, 0.94, 0.44]
b = [0.0]*5
x = 14.3
for i in range(5):
    x = b[i] = a[i]*0.1 + x*0.9

Is there something in numpy for that purpose?

like image 394
Jonas Byström Avatar asked Jan 01 '23 23:01

Jonas Byström


2 Answers

An ugly but vectorized numpy solution:

import numpy as np
a = np.array([0.53, 0.66, 0.064, 0.94, 0.44])
x = 14.3
idx = np.arange(a.size)
0.9 ** idx * (0.1 * (a * 0.9 ** (-idx)).cumsum() + x * 0.9)
# array([12.923    , 11.6967   , 10.53343  ,  9.574087 ,  8.6606783])

Result from for loop:

a = [0.53, 0.66, 0.064, 0.94, 0.44]
b = [0.0]*5
x = 14.3
for i in range(5):
    x = b[i] = a[i]*0.1 + x*0.9

b
#[12.923000000000002,
# 11.696700000000003,
# 10.533430000000003,
# 9.574087000000002,
# 8.660678300000002]

This is because:

enter image description here

And components in the result can be vectorized correspondingly, note the sum of a terms in the inner parenthesis is vectorized as cumsum.

like image 50
Psidom Avatar answered Jan 04 '23 13:01

Psidom


Maybe we can break down and find the correlation between each steps

***x0=x0***

***x1=a[0]*0.1 + x0*0.9***

***x2=a[1]*0.1 + x1*0.9=a[1]*0.1 + (a[0]*0.1 + x0*0.9)*0.9***

So xn=an*0.1+an-1*0.1*0.9+...+a0*0.1*0.9**n-1+x0*0.9**n

n=np.array(0.9)**np.arange(len(a))


sum(a[::-1]*n)*0.1+x*(0.9**(len(a)))
Out[338]: 8.6606783

Update output array

np.diag(np.fliplr(((a*n[::-1])*0.1).cumsum()/n[:,None]+x*(0.9**(np.arange(1,len(a)+1)))))[::-1]
Out[472]: array([12.923    , 11.6967   , 10.53343  ,  9.574087 ,  8.6606783])
like image 35
BENY Avatar answered Jan 04 '23 12:01

BENY