EDIT SUMMARY So I have: p1 dataframe with the prices and the returns of n-financial instrument of the last 5 years:
Date a_price b_price a_ret b_ret
0 2018-04-13 6.335 5.114 0.0047 0.01
.
.
.
1272 2023-04-13 11.525 5.708 0.0039 -0.04
Then I have another dataframe seed, which contains only one date and the Standard deviations of the same n-instruments
Date a_SD b_SD
63 2023-04-13 0.019 0.017
In p1 I need to create n-new columns EWMA as follows: for the oldest date in p1 (2018-04-13), a_EWMA = X*(seed[a_SD]**2) + (1-X) *(p1[a_Ret]*2) for any other date a_EWMA = X(p1.loc[i-1,a_EWMA]**2) + (1-X) *(p1[a_Ret]**2).
The desired output is p1 as follows:
Date a_price b_price a_ret b_ret a_EWMA b_EWMA
0 2018-04-13 6.335 5.114 0.0047 0.01 ... ...
.
.
.
1272 2023-04-13 11.525 5.708 0.0039 -0.04 ... ...
Since I need to automatize, I'm trying to use f'{col} to match the 2 dataframes but I think I'm not doing it correctly. I'm doing it with the below function but it gives me all None results and worst it's creating too many columns while it should create only n-more (2 in this example)
def EWMA(x):
for i in p1.index:
if p1.loc[i,'Date'] == min_Date and '_SD' in seed.columns:
return math.sqrt((lam*(seed[[f'{col}_SD'for col in seed.columns]])**2) + (1-lam) * (p1.loc[i,[f'{col}_Ret' for col in p1.columns]]**2))
elif p1.loc[i,'Date'] != min_Date and '_SD' in seed.columns:
return math.sqrt(lam*((p1.loc[i-1,[f'{col}_EWMA'for col in p1.columns]])**2)+(1-lam)*(p1.loc[i,[f'{col}_Ret'for col in p1.columns]]**2)) if f'_Ret' in p1.columns:
p1[[f'{col}_EWMA'for col in p1.columns]] = p1.apply(EWMA)