Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

In code, the implementation of the alpha version of EWMA looks like this:def EWMA(past_value, current_value, alpha):return (1- alpha) * past_value + alpha * current_valueFor computing it over a series of values, given a period, we can define a functionlike this:def calc_ewma(values, period):alpha = 2 / (period + 1)result = []for v in values:try:prev_value = result[-1]except IndexError:prev_value = 0new_value = EWMA(prev_value, v, alpha)result.append(new_value)return np.array(result)In the try..except block, you can see that, if there is no previous value for theEWMA (as in the very first step), it assumes a previous value of zero.The way the EWMA is constructed has its issues—since it does not need to keeptrack of all the values inside its period, in its first steps, the "average" will be wayoff (or biased). For an alpha=0.1 (corresponding to the 19-periods average), thevery first "average" will be exactly the first value divided by ten.To address this issue, we can compute the bias-corrected EWMA:Equation 6.8 - Bias-corrected EWMAThe beta in the formula above is the same as before: 1 - alpha. In code, we canimplement the correction factor like this:Learning Rates | 459

def correction(averaged_value, beta, steps):return averaged_value / (1 - (beta ** steps))For computing the corrected EWMA over a series of values, we can use a functionlike this:def calc_corrected_ewma(values, period):ewma = calc_ewma(values, period)alpha = 2 / (period + 1)beta = 1 - alpharesult = []for step, v in enumerate(ewma):adj_value = correction(v, beta, step + 1)result.append(adj_value)return np.array(result)Let’s apply both EWMAs, together with a regular moving average, to a sequence oftemperature values to illustrate the differences:temperatures = np.array([5, 11, 15, 6, 5, 3, 3, 0, 0, 3, 4, 2, 1,-1, -2, 2, 2, -2, -1, -1, 3, 4, -1, 2, 6, 4, 9, 11, 9, -2])ma_vs_ewma(temperatures, periods=19)Figure 6.16 - Moving average vs EWMA460 | Chapter 6: Rock, Paper, Scissors

def correction(averaged_value, beta, steps):

return averaged_value / (1 - (beta ** steps))

For computing the corrected EWMA over a series of values, we can use a function

like this:

def calc_corrected_ewma(values, period):

ewma = calc_ewma(values, period)

alpha = 2 / (period + 1)

beta = 1 - alpha

result = []

for step, v in enumerate(ewma):

adj_value = correction(v, beta, step + 1)

result.append(adj_value)

return np.array(result)

Let’s apply both EWMAs, together with a regular moving average, to a sequence of

temperature values to illustrate the differences:

temperatures = np.array([5, 11, 15, 6, 5, 3, 3, 0, 0, 3, 4, 2, 1,

-1, -2, 2, 2, -2, -1, -1, 3, 4, -1, 2, 6, 4, 9, 11, 9, -2])

ma_vs_ewma(temperatures, periods=19)

Figure 6.16 - Moving average vs EWMA

460 | Chapter 6: Rock, Paper, Scissors

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!