這次是比較簡單的蒙地卡羅演算法選擇權定價,學期快進入尾聲了有點不知所措啊~
一、問題描述:American puts pricing using least-squares Monte Carlo method
Write a least-squares Monte Carlo program to price American puts.
Output:
- Price and
- Standard derivation(sample)
Inputs:
- S (stock price at time 0),
- X (strike price),
- T (maturity in years),
- s (%) (annual volatility),
- r (%) (continuously compounded annual interest rate),
- n (number of periods), and
- k (number of simulation paths).
For example, when
- S = 101, X = 105, T = 1 (years), s = 15%, r = 2%, n = 50, and k = 100,000,
the price is about 7.3642 and the standard derivation is about 0.9975.
二、執行方式與執行結果
直接使用 R 直譯器執行 LSMC_implement.R 即可
- LSMC_implement.R : 本次實作的主程式,可於參數區塊調整測試參數,答案(price, sd)將以標準輸出印於terminal
- LSMC_package.R : 第三方套件 LSMonteCarlo,用於驗證答案(需要安裝 LSMonteCarlo 套件)
- textbook_example.txt : 投影片講義之測試資料 http://www.csie.ntu.edu.tw/~lyuu/finance1.html
使用題目測試資料跑出來的結果如下:(目前還不知為何sd不太準)
用 IDE Rstudio 直接執行之畫面:
三、設計與實作
1. Create two table : Stock Price Path Table and Stock Path Table
Generate the multivariate normal distribution for the Monte Carlo pricing of multivariate derivatives. using the formula below :
then we can generate a stock price paths table : StockPath[k,n]
and initial a cash flow table like this :
R code
# Create Stock Price Path Table StockPath <- matrix(NA, nrow=k, ncol=n) for(i in 1:k) { front_para <- (r - s^2/2) * deltaT back_para <- s * (sqrt(deltaT)) * rnorm(n, mean=0, sd=1) StockPath[i,] <- S * exp(cumsum(front_para+back_para)) } StockPath[ StockPath>X ] <- 0 # Create Cash Flow Table CashFlow <- matrix(0, nrow=k, ncol=n) CashFlow[,n] <- ifelse(StockPath[,n]!=0, X-StockPath[,n], 0)
2. Backward pricing with regression
將 Cash Flow Table 從最後一期一直推回第一期
- Select in-the-money path : The cash flows at each year are the exercise value if the put is in the money.
- Regression : Only in-the-money paths will be used in the regression
- Compare : Compare Exercise value and Continuation value
R code
# Backward pricing with regression for(m in (n-1):1){ sel_path <- which(StockPath[,m] > 0) x_reg <- StockPath[sel_path,m] y_reg <- vector(mode = "numeric", length = length(sel_path)) for(i in 1:length(sel_path)){ d <- min(which(CashFlow[ sel_path[i], ] > 0)) - m y_reg[i] <- CashFlow[ sel_path[i], m+d] * discount_fac^d } contiuation <- predict( lm(y_reg ~ x_reg + I(x_reg^2)), x=x_reg) exercise <- X - StockPath[which(StockPath[,m]>0), m] CashFlow[sel_path,m] <- ifelse(exercise > contiuation, exercise, 0 ) }
3. Discount to calculate the price
最後,把 CahFlow表格的各個路徑的最後保留的值折現即可得到選擇權價格。
四、結語:善用現成的工具
其實蒙地卡羅方法的選擇權定價在R中有第三方的套件可以直接使用,在實際應用的場合就不用「重造輪子」囉!
R code : using LSMonteCarlo package
library("LSMonteCarlo") AmerPutLSM() put<-AmerPutLSM(Spot = 101, Strike= 105, mT = 1, sigma = 0.15, r = 0.02, m = 50, n = 100000) summary(put)
References
Y.-D. Lyuu - Principles of Financial Computing
http://www.csie.ntu.edu.tw/~lyuu/finance1.html