How to code the 3 sigma method

187 ビュー (過去 30 日間)
Enrico Scupola
Enrico Scupola 2017 年 3 月 2 日
回答済み: Sindhu Priya 2017 年 3 月 8 日
Hi guys, I need to code the 3 sigma method in Matlab and I don't know how to do it. Basically the idea is to create a range from mean + 3 sigma(standard deviation) to mean - 3 sigma. Then I need to construct a dummy variable (not sure about that) to see how many times the SP500 returns are outside this range basically and then compute the probability of having this values in the whole set of observations. The aim is to compute the empirical probability of having booms and busts (extrema) and comparing this probability with the one coming from a Normal distribution (theoretical probability).
Your help would be very appreciated. Thanks.
Guys I tried this code but I got an error in the dummy variable ... r_logActual are the log returns of 6 assets ( matrix).
for x=1:6
upperbound(x,:) = mean(r_logActual(:,x))+3*std(r_logActual(:,x));
lowerbound(x,:) = mean(r_logActual(:,x))-3*std(r_logActual(:,x));
end
for n = 1:6
if r_logActual(:,n) <= upperbound(n:1) & r_logActual(:,n) >= lowerbound(n:1)
emp_prob(x)=1;
else
emp_prob(x)=0;
end
end

回答 (1 件)

Sindhu Priya
Sindhu Priya 2017 年 3 月 8 日
Hi Enrico,
There is a small mistake in the logic where you compare the upperbound and lowerbound with the actual values.
upperbound(n:1) & lowerbound(n:1) will return a 0X1 column vector, it might be the line which is causing the error.
The corrected code is as follows,
for x=1:6
upperbound(x,:) = mean(r_logActual(:,x))+3*std(r_logActual(:,x));
lowerbound(x,:) = mean(r_logActual(:,x))-3*std(r_logActual(:,x));
end
for n = 1:6
if r_logActual(:,n) <= upperbound(n,1) & r_logActual(:,n) >= lowerbound(n,1)
emp_prob(x)=1;
else
emp_prob(x)=0;
end
end
Hope it will resolve the issue.

Community Treasure Hunt

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

Start Hunting!

Translated by