Monte Carlo simulation with "double" indicization
3 次查看(过去 30 天)
显示 更早的评论
Good evening everybody,
I would like run a Monte Carlo simulation with the following code. My problem is that I have to define kind of "double" loop, one for the time "t" and the other one for the "n" realization of the random variable rn. In the following my code WITHOUT Monte Carlo sampling (it works fine).
T=10000;
sigma=0.157;
k=0.3;
beta=0.99;
po=0.35;
phipi=1.5;
phiz=0;
B2=[sigma 1-beta*phipi; k*sigma k+beta*(sigma+phiz)]
B1=1/(sigma+phiz+k*phipi)
B=B1*B2
y=NaN(2,T)
y(1,1)=0.1
y(2,1)=0.2
kappa=[1/(sigma+phiz+phipi*k); k/(sigma+phiz+phipi*k)]
rn=randn(1,T)
a=NaN(2,T);
a(1,1)=1
a(2,1)=2
for t=2:T
y(:,t)=B*a(:,t-1)+kappa*rn(:,t);
a(:,t)=a(:,t-1)+t^(-1)*(y(:,t)-a(:,t-1));
end
stz=a(1,:)'
stpi=a(2,:)'
In the following instead "what I am trying to do". Bear in mind that I would like to obtain a multidimensional vector defined as y(2,T,N) in order to plot a "density function" of the realizations of the components of vector y (that is, the two variables z e pi).
T=100;
sigma=0.157;
k=0.3;
beta=0.99;
po=0.35;
phipi=1.5;
phiz=0;
B2=[sigma 1-beta*phipi; k*sigma k+beta*(sigma+phiz)]
B1=1/(sigma+phiz+k*phipi)
B=B1*B2
y=NaN(2,T)
y(1,1)=0.1
y(2,1)=0.2
kappa=[1/(sigma+phiz+phipi*k); k/(sigma+phiz+phipi*k)]
rn=randn(1,T)
a=NaN(2,T);
a(1,1)=1
a(2,1)=2
N=100
for n=1:N
rn=randn(1,N)
for t=2:T
y(:,t,n)=B*a(:,t-1)+kappa*rn(:,t);
a(:,t)=a(:,t-1)+t^(-1)*(y(:,t)-a(:,t-1));
end
end
Am I doing well? Another problem is that I cannot push the simulation above N=100 or T=100 (it is seems to me that N e T must agree, am I right?) otherwise it take too much time to compute.
What command I am supposed to use in Matlab to plot a density function of the MC simulation?
Thank you everybody
4 个评论
John D'Errico
2019-7-21
A bit confusing. You say this:
"In the following my code WITHOUT Monte Carlo sampling (it works fine)."
Yet then you have the line:
rn=randn(1,T)
Which makes this indeed a Monte Carlo sampling method.
By the way, learn to use semi-colons! If not, then you will dump out 10000 numbers to the screen every time you run this code.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Monte-Carlo 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!