# python – The code finds the std var for PEWMA

Can someone please share the python codes for forecasting time series data using PEWMA and NBAR, PAR, integer-valued autoregressive models
This is the code I have taken from the internet

``````from itertools import islice
from math import sqrt
from scipy.stats import norm

#pewna parameters
T = 30      # initialization period (in cycles)
beta = 0.5  # lower values make the algorithm behave more like regular EWMA
a = 0.99    # the maximum value of the EWMA a parameter, used for outliers
z = 3

#the PEWNA Model

# create a DataFrame for the run time variables we'll need to calculate
pewm = pd.DataFrame(index=df.index, columns=('Mean', 'Var', 'Std'), dtype=float)
pewm.iloc(0) = (df.iloc(0)(value_col), 0, 0)

t = 0

for _, row in islice(df.iterrows(), 1, None):
diff = row(value_col) - pewm.iloc(t).Mean # difference from moving average
p = norm.pdf(diff / pewm.iloc(t).Std) if pewm.iloc(t).Std != 0 else 0
a_t = a * (1 - beta * p) if t > T else 1 - 1/(t+1) # weight to give to this point
incr = (1 - a_t) * diff

# Update Mean, Var, Std
pewm.iloc(t+1).Mean = pewm.iloc(t).Mean + incr
pewm.iloc(t+1).Var = a_t * (pewm.iloc(t).Var + diff * incr)
pewm.iloc(t+1).Std = sqrt(pewm.iloc(t+1).Var)
t += 1
``````