how to do looping with over many variables ? (MONTE CARLO)
1 次查看(过去 30 天)
显示 更早的评论
Jardine Shi
2020-11-19
Hey, very new to matlab. What command should I use to loop over many variables ? e.g. n=3,4,5,.....100. Thanks.
采纳的回答
KSSV
2020-11-19
for n = 3:100
n
end
11 个评论
Jardine Shi
2020-11-19
(its a typo should be 3,4,....100)
Here's the first part that I did, can you specify to repeat this using n=3,4,....100. where exactly should I put
n =3:100 ?
Trying it on the sample size will give me "Error using randn, size inputs must be scalar"
KSSV
2020-11-19
What ever the code you have....make it into a function or run it nside the loop.
for n = 3:100
%% Do what you want
%% Copy your code here
end
When you give n = 3:100 in a loop, the loop index is n, it keep changing by 1 from 3 to 100.
% check your self
for n = 3:100
n % see what is printed
end
Jardine Shi
2020-11-19
Oh, so I'd have to generate both rejt_n &rejN_n as functions of n ? How do I generate them into functions of n ?
Jardine Shi
2020-11-19
%HERE YOU GO :)
%QUESTION 1
rng(480058999)
%A Monte Carlo Simulation with n = 2 and 10,000 repetitions
%=============================================================================================================================
%i)random sample size n by sampling from N(mu,sample variance)
%with mu = 0 and sample variance = 1
%sample size
n=2
%independent draws given N(0,1)
X = randn(n,1);
%sample mean
Xbar = sum(X)/n
%sigma-hat squared
X2bar = sum(X.^2)/n;
sigmahat2 = X2bar-Xbar^2
%sample variance
S2= (n/(n-1))*sigmahat2
%continuation
%setting the number of repetitions
M=10000
%Creating the Mx1 vectors to save the realized sample stats
RealizedXbar=NaN(M,1);
realizedsigmahat2 = NaN(M,1);
RealizedS2 = NaN(M,1);
%looping the repetitions
for m= 1:M
%generating the random sample
X = randn(n,1);
%computing the sample stats
Xbar = sum(X)/n;
X2bar = sum(X.^2)/n;
sigmahat2 = X2bar-Xbar^2;
S2 = (n/(n-1))*sigmahat2;
%lastly, saving the sample stats
RealizedXbar(m,1) = Xbar;
Realizedsigmahat2(m,1) = sigmahat2;
RealizedS2(m,1) = S2;
end
%=============================================================================================================================
%ii) t-stat for testing null hypothesis Ho:mu = 0
tstat= RealizedXbar./sqrt(RealizedS2/n)
%=============================================================================================================================
%iii) Record whether the abs value of t-stat exceeds cn-1(0.05)
%recall n = 2, so n-1 = 1
alpha=0.05;
c=tinv(1-alpha/2,n-1);
%proportion
rejt_of_n=sum(tstat>c | tstat<-c, "all")/M
%=============================================================================================================================
%iv) Record whether the abs value of t-stat exceeds c(0.05)
c=norminv(0.975);
%proportion
rejN_of_n=sum(tstat>c | tstat<-c, "all")/M
%from the value given, 0.2904, it shows that it is not approximating well
%=============================================================================================================================
%B repeat part a using n=3,4.....100. Graph both rejt(n) & rejN(n)
%as functions of n. Set the vertical axis values in both graphs to range
%from 0-3
KSSV
2020-11-19
%HERE YOU GO :)
%QUESTION 1
rng(480058999)
%A Monte Carlo Simulation with n = 2 and 10,000 repetitions
%=============================================================================================================================
%i)random sample size n by sampling from N(mu,sample variance)
%with mu = 0 and sample variance = 1
%sample size
for n = 2:100
%independent draws given N(0,1)
X = randn(n,1);
%sample mean
Xbar = sum(X)/n
%sigma-hat squared
X2bar = sum(X.^2)/n;
sigmahat2 = X2bar-Xbar^2
%sample variance
S2= (n/(n-1))*sigmahat2
%continuation
%setting the number of repetitions
M=10000
%Creating the Mx1 vectors to save the realized sample stats
RealizedXbar=NaN(M,1);
realizedsigmahat2 = NaN(M,1);
RealizedS2 = NaN(M,1);
%looping the repetitions
for m= 1:M
%generating the random sample
X = randn(n,1);
%computing the sample stats
Xbar = sum(X)/n;
X2bar = sum(X.^2)/n;
sigmahat2 = X2bar-Xbar^2;
S2 = (n/(n-1))*sigmahat2;
%lastly, saving the sample stats
RealizedXbar(m,1) = Xbar;
Realizedsigmahat2(m,1) = sigmahat2;
RealizedS2(m,1) = S2;
end
%=============================================================================================================================
%ii) t-stat for testing null hypothesis Ho:mu = 0
tstat= RealizedXbar./sqrt(RealizedS2/n)
%=============================================================================================================================
%iii) Record whether the abs value of t-stat exceeds cn-1(0.05)
%recall n = 2, so n-1 = 1
alpha=0.05;
c=tinv(1-alpha/2,n-1);
%proportion
rejt_of_n=sum(tstat>c | tstat<-c, "all")/M
%=============================================================================================================================
%iv) Record whether the abs value of t-stat exceeds c(0.05)
c=norminv(0.975);
%proportion
rejN_of_n=sum(tstat>c | tstat<-c, "all")/M
%from the value given, 0.2904, it shows that it is not approximating well
%=============================================================================================================================
%B repeat part a using n=3,4.....100. Graph both rejt(n) & rejN(n)
%as functions of n. Set the vertical axis values in both graphs to range
%from 0-3
end
Jardine Shi
2020-11-19
ahhh I see, that was rather surprising. I thought it would be something very difficult. Thanks for your help!
Jardine Shi
2020-11-19
quick question here, how to set the range of a graph ? e.g. range for vertical & horizontal axis
Jardine Shi
2020-11-19
yeap, I just found it. I just need to put xlim or ylim on the last line hahaha
KSSV
2020-11-19
axis works for both the limits at once...._xlim_, ylim is to fix seperately.
And thanks is accepting/ voting the answer. :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)