You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to do looping with over many variables ? (MONTE CARLO)
2 views (last 30 days)
Show older comments
Hey, very new to matlab. What command should I use to loop over many variables ? e.g. n=3,4,5,.....100. Thanks.
Accepted Answer
KSSV
on 19 Nov 2020
for n = 3:100
n
end
11 Comments
Jardine Shi
on 19 Nov 2020
(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
on 19 Nov 2020
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
on 19 Nov 2020
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
on 19 Nov 2020
%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
on 19 Nov 2020
%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
on 19 Nov 2020
ahhh I see, that was rather surprising. I thought it would be something very difficult. Thanks for your help!
Jardine Shi
on 19 Nov 2020
quick question here, how to set the range of a graph ? e.g. range for vertical & horizontal axis
Jardine Shi
on 19 Nov 2020
yeap, I just found it. I just need to put xlim or ylim on the last line hahaha
KSSV
on 19 Nov 2020
axis works for both the limits at once...._xlim_, ylim is to fix seperately.
And thanks is accepting/ voting the answer. :)
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)