0
$\begingroup$

I am trying to do a Least Squares Monte Carlo in R. I don't know if it is the right place to post this, but I am out of options. I don't understand the following lines of the script.

mean = (log(s0)+k*log(s.t[1:n]))/(K+1)
vol = (k*dt/(K+1))^0.5*z`

Why is (K+1) used? Is it supposed to be k instand of K?

The full script is the following:

  lsm = function(n = 50000, d = 50, S0 = 38, K = 40, sigma = 0.2
               , r = 0.06, T = 1)
{
  # S0 = initial asset price
  # K = strike price
  # r = risk-free interest rate
  # sigma = volatility
  # T = maturity time
  # n = Number of paths simulated
  # d = Number of time steps in the simulation
  s0 = S0/K
  dt = T/d
  z = rnorm(n)
  s.t = s0*exp((r - 0.5*sigma^2)*T + sigma*z*(T^0.5))
  s.t[(n+1):(2*n)] = s0*exp((r - 0.5*sigma^2)*T - sigma*z*(T^0.5))
  CC = pmax(1-s.t, 0)
  payoffeu = exp(-r*T)*(CC[1:n]+CC[(n+1):(2*n)])/2*K
  euprice = mean(payoffeu)

  for(k in (d-1):1)
  {
    z = rnorm(n)
    mean = (log(s0)+k*log(s.t[1:n]))/(K+1)
    vol = (k*dt/(K+1))^0.5*z
    s.t.1 = exp(mean+sigma*vol)
    mean = (log(s0)+k*log(s.t[(n+1):(2*n)]))/(k+1)
    s.t.1[(n+1):(2*n)] = exp(mean-sigma*vol)
    CE = pmax(1-s.t.1, 0)
    idx = (1:(2*n))[CE > 0]
    discountedCC = CC[idx]*exp(-r*dt)
    basis1 = s.t.1[idx] 
    basis2 = (s.t.1[idx])^2 
    p = lm(discountedCC ~ basis1 + basis2)$coefficients
    estimatedCC = p[1] + p[2]*basis1 + p[3]*basis2
    EF = rep(0, 2*n)
    EF[idx] = (CE[idx] > estimatedCC)
    CC = (EF == 0)* CC * exp(-r*dt) + (EF == 1)*CE
    s.t = s.t.1
  }

  payoff = exp(-r*dt)*(CC[1:n]+CC[(n+1):(2*n)])/2
  usprice = mean(payoff*K)
  error = 1.96*sd(payoff*K)/sqrt(n)
  earlyex = usprice - euprice
  data.frame(usprice, error, euprice)
}

lsm()
$\endgroup$
1
$\begingroup$

I emailed the author of the R script. His answer was that the "K"s in

mean = (log(s0)+k*log(s.t[1:n]))/(K+1) vol = (k*dt/(K+1))^0.5*z s.t.1 = exp(mean+sigma*vol) mean = (log(s0)+k*log(s.t[(n+1):(2*n)]))/(k+1)

should all the "k"s. It makes more sense.

Corrected:

mean = (log(s0)+k*log(s.t[1:n]))/(k+1) vol = (k*dt/(k+1))^0.5*z s.t.1 = exp(mean+sigma*vol) mean = (log(s0)+k*log(s.t[(n+1):(2*n)]))/(k+1)

$\endgroup$
  • $\begingroup$ ROFL. So the published code is wrong. Did Shariq Mohammed get an A in the course? $\endgroup$ – Alex C Apr 12 '18 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.