0
decay = 0.1
data = np.array([100,200,300,400,500])
y = np.zeros(len(data))

for i in range(len(data)):
    if i == 0:
        y[i] = (1.0 - decay) * data[i]

    else:
        y[i] = (1.0 - decay) * data[i] + (decay * y[i - 1])
        
y

Output:

array([ 90.   , 189.   , 288.9  , 388.89 , 488.889])

Now this I'm cut shorting by indexing

decay = 0.1
data = np.array([100,200,300,400,500])
data = (1.0 - decay) * data
data[1:] = data[1:] + decay * data[0:-1]
data

Output:

array([ 90., 189., 288., 387., 486.])

I feel here I'm doing some mistake or missing float values or not getting cumulative value as both the output aren't matching, the first output is the correct one, what is the mistake here?

Akilesh
  • 413
  • 3
  • 11
  • Isn't this identical to your last question? https://stackoverflow.com/questions/69031699/calculation-on-my-for-loop-and-want-to-do-it-without-for-loop-using-some-functio – user202729 Sep 03 '21 at 10:45
  • I see, you're asking what's the mistake. You can work out the math by hand and see that it doesn't match – user202729 Sep 03 '21 at 10:48
  • For later indices in your array you are missing comulative terms. Those are small fractions of the cumsum of your array, because you multiply a number less than one (`decay = .1` -> for the second element `.1*.1 = .01`) with itself multiple times. [Kyle Parsons answer](https://stackoverflow.com/a/69032484/14277722) is still your best option to vectorize. – Michael Szczesny Sep 03 '21 at 11:00
  • These are the terms you are missing: 288.9 = `288` + .01 * `90`, 388.89 = `387` + .01 * `189`, 488.889 = `486` + .01 * (`288` + .01 * `90`) – Michael Szczesny Sep 03 '21 at 11:37
  • @MichaelSzczesny Yeah Kyle's answer is the best option but looking for something simple to understand too. I have found the terms missing but am unable to achieve how do I get those missing values like .01 *90, .01*189 etc. – Akilesh Sep 05 '21 at 14:29

0 Answers0