how do i create this loop in matlab?

i want to create this loop in matlab, this is a python code :
mvols = np.zeros((len(df['NASDAQ']),len(risks)))
for j in range(len(risks)):
mvols[0:windowsize,j] = np.std(np.array(df[risks[j]])[0:windowsize],ddof=1)
for i in range(windowsize-1,len(df['NASDAQ'])-1):
mvols[i+1,j] = np.sqrt(l*mvols[i,j]**2+(1-l)*df.iloc[i][risks[j]]**2)
for j in range(len(risks)):
the variable risks is a matrix of indices which are all the same length :
risks = ['NASDAQ','AEXUSD','DAXUSD','Nikkei','EURIBOR']
matlab gives me an error because i want to calculate the varaible mvols(i+1,j) which is one time period ahead and its past, mvols(i,j) is in the formula , i have no idea how to code this in matlab, does anyone have any idea?
thanks in advance

6 Comments

What Matlab code is your best try?
j=1;
suma=0
for n=length(logNASDAQ):1:1
vol(j,n) = sqrt(l.*vol(j-1,n).^2+(1-l)*risks.^2)
suma=vol(j,n)-suma;
vol=1-suma
n=n+1;
end
this one, and it doesnt work
I have no clue what you're trying to achieve, so it is difficult to help.
I do notice that you have some vector logNASDAQ which you want to loop over (but don't use in the loop). This loop will only run if that vector is of size 1, as otherwise the definition is invalid. If you want to loop backwards, you must specify a negative step size, e.g. numel(logNASDAQ):-1:1.
You also don't need to change the value of your loop variable, as that change will be overwritten when the loop restarts. The Matlab IDE will warn you about this. In this short code snippet I have no fewer than 5 warnings from mlint. Your should try to fix those as well.
i was trying to solve for this value
mvols[i+1,j] = np.sqrt(l*mvols[i,j]**2+(1-l)*df.iloc[i][risks[j]]**2)
to calculate mvols[i+1,j] which is a vector and in its formula np.sqrt(l*mvols[i,j]**2+(1-l)*df.iloc[i][risks[j]]**2) the past value of mvols[i+1,j] is included whihch is mvols[i,j], and im not sure how i can write a code to calculate it
From my limited knowledge of Python I suspect that line of code becomes this in Matlab:
mvols(i+1,j) = sqrt(l*mvols(i,j)^2+(1-l)*risks{j}(i)^2);
I had to guess df.iloc[i][risks[j]].
Using the previous element of a vector is easy as that, but depending on what data you have stored in each variable, converting the rest to Matlab might be a bit more tricky.
thank you

Sign in to comment.

Answers (0)

Asked:

on 23 Apr 2021

Commented:

on 23 Apr 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!