Monte Carlo simulation with "double" indicization

4 visualizaciones (últimos 30 días)
Uccio
Uccio el 21 de Jul. de 2019
Comentada: Uccio el 21 de Jul. de 2019
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 comentarios
John D'Errico
John D'Errico el 21 de Jul. de 2019
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.
Uccio
Uccio el 21 de Jul. de 2019
I get your point. Actually rn(t) cannot be a scalar (I cannot write rn=randn(1)) since rn(t) is a variable in a linear dynamic sthocastic model. At each point in time, I allow it to have a different realization.
Now my question is: cannot I run my model N times and then aggregate the results in order to obtain a density function for z and pi?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by