Bias Correction Implementation

Hello @ajaykumar3456,

Good job conducting your own experiment! I also get the same solution.

For completeness, I attach sample code and a plot that further emphasizes the importance of bias correction in your example:

import numpy as np
import matplotlib.pyplot as plt

temperatures = np.array([25, 28, 26, 22, 19, 23, 27])

beta = 0.9
v = np.zeros(len(temperatures) + 1)
v_corrected = np.copy(v)
for t, temperature in enumerate(temperatures, start=1):
    v[t] = beta * v[t - 1] + (1 - beta) * temperature
    v_corrected[t] = v[t] / (1 - beta ** t)

print('temperatures:', temperatures)
print('v:', v[1:])
print('v_corrected:', v_corrected[1:])

plt.plot(temperatures, label='temperature')
plt.plot(v[1:], label='v')
plt.plot(v_corrected[1:], label='v_corrected')
plt.title('Bias Correction in Exponentially Weighted Averages')
plt.legend()
plt.savefig('bias.png', dpi=300)
plt.show()

Output:

temperatures: [25 28 26 22 19 23 27]
v: [ 2.5        5.05       7.145      8.6305     9.66745   11.000705
 12.6006345]
v_corrected: [25.         26.57894737 26.36531365 25.09595813 23.60736002 23.47773706
 24.15288408]

3 Likes