R/euler_maruyama2.R

Defines functions euler_maruyama2

Documented in euler_maruyama2

#' Simulating Ito diffusions
#'
#' @param maturity timespan of simulation
#' @param mu drift coefficient function
#' @param volat volatility coefficient function
#' @param x0 initial position
#' @param n number of time sub-intervals in simulation
#' @param expIto boolean whether or not to simulate Ito diffusion directly or an exponential Ito diffusion, i.e. \eqn{S_0 e^{X_t}} where \eqn{X_t} is the Ito diffusion.
#' @param spot defaults to NULL if not using an exponential-Ito process, otherwise enter a spot price.
#'
#' @description {TO BE DEPRECATED-----Euler-Maruyama scheme for simulationg Ito diffusion.}
#' @details {Simple time-stepping scheme over each sub-interval. See details on e.g. wikipedia}
#' @return data.frame time series
euler_maruyama2 <- function(maturity, mu, volat, x0 = 0, n = 100, expIto = FALSE, spot = NULL)
{
  x <- matrix(0, nrow = n+1)
  x[1] <- x0

  k <- maturity/n
  tt <- seq(0, maturity, k)

  for(i in 2:(n+1))
  {
    x[i] <- x[i-1] +mu(x[i-1], tt[i-1])*k+volat(x[i-1], tt[i-1])*stats::rnorm(1, 0, sd = sqrt(k))
  }
  if(expIto)
  {
    if(!is.null(spot))
    {
      data.frame(t = tt, X = spot*exp(x))
    } else
    {
      stop("Spot is null but expIto = TRUE; please enter valid spot price")
    }

  } else
  {
    data.frame(t = tt, X = x)
  }

}
shill1729/FeynmanKacSolver documentation built on May 19, 2020, 8:23 p.m.