이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
how to do looping with over many variables ? (MONTE CARLO)
조회 수: 2 (최근 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일
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/419363/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/419368/image.png)
(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개)
참고 항목
태그
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 (한국어)