Member
- Joined
- Feb 3, 2023
- Messages
- 70
- Thread Author
- #1
I'm using a for loop with if statements to create a column ['EWMA'] filled partially recoursively. For the min Date observed the value is computed using the SD of the sample while for all the others I recall the EWMA value of the previous day. The function seems to work but I don't know why the column is not added to the df (thus, df['EWMA'] doesn't exist)
Code:
import pandas as pd
import numpy as np
from numpy import log as ln
import statistics
df=pd.read_excel(r'C:\Users\cristinamariaromano\Downloads\RACE.TSLAMI.xlsx', parse_dates=['Date'])
#Returns
df['Returns RACE'] = ln(df['Close RACE'].div(df['Close RACE'].shift(3)))
df['Returns RACE'] = df['Returns RACE'].replace(np.nan, 0)
df['Returns TSLA'] = ln(df['Close TSLA'].div(df['Close TSLA'].shift(3)))
df['Returns TSLA'] = df['Returns TSLA'].replace(np.nan, 0)
lam=float(0.98)
SD_sample= float(statistics.stdev(df['Returns RACE']))
print(SD_sample)
min_Date=df['Date'].min()
for i in df['Date']:
if min_Date == True:
df['EWMA'] == ((lam*(SD_sample**2)+(1-lam)*(df['Returns RACE']**2)))**1/2
if min_Date == False:
df['EWMA'] == (lam*((df['EWMA'].shift(1))**2)+(1-lam)*(df['Returns RACE']**2))**1/2
print(df)