現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)