Correcting Time-Indexed Terms in a Least Squares Monte Carlo Script
Summary
The document discusses a coding question about simulating asset prices in a Least Squares Monte Carlo (LSM) method. The question focuses on formulas used to calculate the conditional log-price mean and volatility at an earlier time step, and asks why the strike variable appears in their denominators.
The response reports that the script’s author confirmed those terms should use the loop index k instead of the strike K. It presents corrected expressions, making the time-step dependence consistent across the mean and volatility calculations. This is a targeted correction rather than a full explanation of LSM, the derivation of the formulas, or a complete review of the simulation and exercise logic. The exchange offers no numerical validation or comparison of resulting option prices, so it clarifies the apparent variable-name error without establishing that the rest of the implementation is correct.
Key ideas
- The script models an American option using a Least Squares Monte Carlo procedure.
- The mean and volatility calculations depend on the current time-step index.
- The author confirms that the denominators should use k rather than the strike variable K.
- The exchange corrects a specific coding error but does not validate the full implementation.
Tags
Full text
# R script for Leasts Square Monte Carlo. How to explain vol and mean?
# R script for Leasts Square Monte Carlo. How to explain vol and mean?
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()
```
## Answer by Jeremi (score 1)
https://quant.stackexchange.com/a/39223
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)`Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.