##
set.seed(123)
SimpleEulerApproximation = function(T,x,a,b,delta){
numberofSteps = T/delta;
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,1)
Y[1] = x;
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
for (j in 2:numberofSteps){
Y[j] = Y[j-1] + a*Y[j-1]*delta + b*Y[j-1]*rnorm(1,0,sqrt(delta));
}
##plot(TimeSteps,Y, type = "l")
}
SimpleEulerApproximation(1,20,-0.01,0.25,0.001)
set.seed(123)
MultipleEulerApproximation = function(T,x,a,b,delta,numberofTrajectories){
numberofSteps = round(T/delta);
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,rep(numberofTrajectories))
Y = data.matrix(Y)
for (i in 1:numberofTrajectories){
Y[,i] = SimpleEulerApproximation(T,x,a,b,delta);
}
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
AverageTrajectory = rep(numberofSteps,1)
for (i in 1:numberofSteps){
AverageTrajectory[i] = mean(Y[i,])
}
##plot(TimeSteps,AverageTrajectory)
}
MultipleEulerApproximation(1,52,0.12,0.30,0.0001,10000)
MonteCarloSimulation = function(T,x,r,sigma,K,delta,numberofTrajectories){
Y = MultipleEulerApproximation(T,x,r,sigma,delta,numberofTrajectories);
lastStep = round(T/delta);
max(Y[lastStep,]-K,0);
size(Y)
price = 1/numberofTrajectories * sum(max(Y[lastStep,]-K,0))*exp(-r*T)
}
MonteCarloSimulation(0.25,52,0.12,0.3,50,0.0001,10000)
When I run the code for multipleEulerApproximation, I get replacement has length 0 error. Can someone help me with this? Much Appreciated. The first one is simple Euler Approximation for stochastic differential equation dXt = −0.1Xtdt + 0.25XtdBt, X0 = 20 over the time interval [0, 1] with time step size ∆ = 0.001. The second chunk of code is for multipleeulerapproximation that is where the error. The third-chunk is for calculating European call option price using projections.