0

I'm writing a script for the euler maruyama approximation of sdes. The code works, however, since 'rnorm' is present, i expect my output to vary. In MATLAB, there is the randn('state',100) that helps with this issue. Is there a way to stabilize the variable generator on R?

I've tried using the 'varbvs' package, but the same issue occured since it also produces random variables from a normal distribution.

(MATLAB)

function [St]=euler(N)

randn('state',100) % set the state of randn

T = 5;

dt = T/N;

gamma = 0.67/2;

mu = 0.1;

sigma = 1.2;

X= 7.753;

dW = zeros(1,N); % preallocate arrays ...

W = zeros(1,N); % for efficiency

dW(1) = sqrt(dt)*normrnd(0,1);

(R)

library(optimbase) #Package to create matrix of zeros

#M points-->(M-1) intervals. let N=(M-1)

N <- 1250

T <- 5

dt <- T/N

gamma <- 0.67/2

mu <- 0.1

sigma <- 1.2

S0 <- 7.753

dW <- zeros(1,N) 

W <- zeros(1,N) 

St <- zeros(1,N)


dW[1,1] <- sqrt(dt)*rnorm(1,0,1) #Generate random Z from normal. 

W[1,1] <- dW[1,1]


for (j in 2:N){

dW[1,j] <- sqrt(dt)*rnorm(1,0,1) 

W[1,j] <- W[1,j-1] + dW[1,j]

}


St<-zeros(1,N)

St[1,1]<- S0

for (j in 2:N){

St[1,j] <- (St[1,j-1])+mu*(St[1,j-1])*dt+

  sigma*(St[1,j-1])^gamma*(W[1,j]-W[1,j-1])

}
Strike=St[1,N]

print(Strike)

t<-seq(0,5,length.out = N)

plot(t,St,type="l",col="blue",main="Euler Maruyama Approximation for CEV")

I expect Strike=19.4258

Sotos
  • 51,121
  • 6
  • 32
  • 66
Catalina
  • 25
  • 5

0 Answers0