Mean of 1000 for loop Euler-Maruyama simulations of SDE
Hello,
I’m experimenting with this code adapted from the paper "An Algorithmic Introduction to Numerical Simulation of Stochastic Differential Equations" by Desmond J. Higham. It runs a simulation of an SDE then plots the single simulation. I’m trying to make it so the program runs many such simulations, say 1000, and then plots their mean, so the graph should be more like a smooth line (I am trying to see if it is). Basically I want to take the for loop in this code, run it 1000 times, then get the mean of the 1000 results we get and plot its graph.
I’d appreciate some advice, I’m sure the solution is fairly simple but I haven’t been successful in my attempts.
%EM Euler-Maruyama method on linear SDE
% Discretized Brownian path over [0,1] has dt = 2^(-8).
% Euler-Maruyama uses timestep R*dt.
%randn("state",100)
Xzero = 0; % problem parameters
T = 1;
N = 2^8;
dt = 1/N;
r=1;
G=0.7;
e=0.5;
M=1000;
dW = sqrt(dt)*randn(M,N); % Brownian increments
W = cumsum(dW); % discretized Brownian path
R = 2; Dt = R*dt; L = N/R; % L EM steps of size Dt = R*dt
Xem = zeros(1,L); % preallocate for efficiency
Xtemp = Xzero;
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(j) = Xtemp;
end
plot([0:Dt:T],[Xzero,Xem],"r–*"), hold off
xlabel("t","FontSize",12)
ylabel("X","FontSize",16,"Rotation",0,"HorizontalAlignment","right")Hello,
I’m experimenting with this code adapted from the paper "An Algorithmic Introduction to Numerical Simulation of Stochastic Differential Equations" by Desmond J. Higham. It runs a simulation of an SDE then plots the single simulation. I’m trying to make it so the program runs many such simulations, say 1000, and then plots their mean, so the graph should be more like a smooth line (I am trying to see if it is). Basically I want to take the for loop in this code, run it 1000 times, then get the mean of the 1000 results we get and plot its graph.
I’d appreciate some advice, I’m sure the solution is fairly simple but I haven’t been successful in my attempts.
%EM Euler-Maruyama method on linear SDE
% Discretized Brownian path over [0,1] has dt = 2^(-8).
% Euler-Maruyama uses timestep R*dt.
%randn("state",100)
Xzero = 0; % problem parameters
T = 1;
N = 2^8;
dt = 1/N;
r=1;
G=0.7;
e=0.5;
M=1000;
dW = sqrt(dt)*randn(M,N); % Brownian increments
W = cumsum(dW); % discretized Brownian path
R = 2; Dt = R*dt; L = N/R; % L EM steps of size Dt = R*dt
Xem = zeros(1,L); % preallocate for efficiency
Xtemp = Xzero;
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(j) = Xtemp;
end
plot([0:Dt:T],[Xzero,Xem],"r–*"), hold off
xlabel("t","FontSize",12)
ylabel("X","FontSize",16,"Rotation",0,"HorizontalAlignment","right") Hello,
I’m experimenting with this code adapted from the paper "An Algorithmic Introduction to Numerical Simulation of Stochastic Differential Equations" by Desmond J. Higham. It runs a simulation of an SDE then plots the single simulation. I’m trying to make it so the program runs many such simulations, say 1000, and then plots their mean, so the graph should be more like a smooth line (I am trying to see if it is). Basically I want to take the for loop in this code, run it 1000 times, then get the mean of the 1000 results we get and plot its graph.
I’d appreciate some advice, I’m sure the solution is fairly simple but I haven’t been successful in my attempts.
%EM Euler-Maruyama method on linear SDE
% Discretized Brownian path over [0,1] has dt = 2^(-8).
% Euler-Maruyama uses timestep R*dt.
%randn("state",100)
Xzero = 0; % problem parameters
T = 1;
N = 2^8;
dt = 1/N;
r=1;
G=0.7;
e=0.5;
M=1000;
dW = sqrt(dt)*randn(M,N); % Brownian increments
W = cumsum(dW); % discretized Brownian path
R = 2; Dt = R*dt; L = N/R; % L EM steps of size Dt = R*dt
Xem = zeros(1,L); % preallocate for efficiency
Xtemp = Xzero;
for j = 1:L
Winc = sum(dW(R*(j-1)+1:R*j));
Xtemp = Dt*r*(G-Xtemp) + sqrt((e*Xtemp)*(1-Xtemp))*Winc;
Xem(j) = Xtemp;
end
plot([0:Dt:T],[Xzero,Xem],"r–*"), hold off
xlabel("t","FontSize",12)
ylabel("X","FontSize",16,"Rotation",0,"HorizontalAlignment","right") for loop, mean, graph MATLAB Answers — New Questions