Skip to content

Commit 0fa33da

Browse files
authored
Merge pull request #1 from daifengqi/main
change argument names
2 parents 4240e2e + d542ff1 commit 0fa33da

12 files changed

+158
-130
lines changed

pkg/R/bachelier_impvol.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
#' @param spot current stock price
66
#' @param forward forward stock price
77
#' @param strike strike price
8-
#' @param t.exp time to expiry
9-
#' @param r interest rate
10-
#' @param div dividend rate
8+
#' @param texp time to expiry
9+
#' @param intr interest rate
10+
#' @param divr dividend rate
1111
#' @return implied vol
1212
#' @examples
1313
#' spot <- 100
1414
#' strike <- 100
15-
#' t.exp <- 1.2
15+
#' texp <- 1.2
1616
#' sigma <- 0.2
17-
#' r <- 0.05
17+
#' intr <- 0.05
1818
#' price <- 20
19-
#' vol <- FER::BachelierImpvol(price=price, spot=spot, strike=strike, t.exp=t.exp, r=r)
19+
#' vol <- FER::BachelierImpvol(price=price, spot=spot, strike=strike, texp=texp, intr=intr)
2020
#' @export
2121
BachelierImpvol <- function(
22-
type = "call", price, spot, forward = spot*exp((r-div)*t.exp),
23-
strike = forward, t.exp = 1, r = 0, div = 0
22+
type = "call", price, spot, forward = spot*exp((intr-divr)*texp),
23+
strike = forward, texp = 1, intr = 0, divr = 0
2424
){
2525

26-
price.forward = price * exp(r*t.exp)
26+
price.forward = price * exp(intr*texp)
2727

2828
if( type == "call" ) {
2929
price.straddle <- 2*price.forward - (forward - strike)
@@ -33,7 +33,7 @@ BachelierImpvol <- function(
3333
price.straddle <- price.forward
3434
}
3535

36-
#vectors a and b used for rational Chebyshev approximation
36+
# vectors a and b used for rational Chebyshev approximation
3737
a <- c(3.994961687345134e-1,
3838
2.100960795068497e1,
3939
4.980340217855084e1,
@@ -71,7 +71,7 @@ BachelierImpvol <- function(
7171

7272
#approximation of h(n)
7373
#implied volatility
74-
vol <- sqrt(pi*nu/(2*t.exp)) * price.straddle * (poly.a/poly.b)
74+
vol <- sqrt(pi*nu/(2*texp)) * price.straddle * (poly.a/poly.b)
7575

7676
return(vol)
7777
}

pkg/R/bachelier_price.R

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
#' @param price Price
55
#' @param spot current stock price
66
#' @param forward forward stock price
7-
#' @param t.exp time to expiry
8-
#' @param r interest rate
9-
#' @param div dividend rate
7+
#' @param texp time to expiry
8+
#' @param intr interest rate
9+
#' @param divr dividend rate
1010
#' @param sigma volatility
1111
#' @return option price
1212
#' @examples
1313
#' spot <- 100
1414
#' strike <- seq(80,125,5)
15-
#' t.exp <- 1.2
15+
#' texp <- 1.2
1616
#' sigma <- 0.2
17-
#' r <- 0.05
18-
#' price <- FER::BachelierPrice(spot=spot, t.exp = t.exp, sigma=sigma, strike=strike, r=r)
17+
#' intr <- 0.05
18+
#' price <- FER::BachelierPrice(spot=spot, texp = texp, sigma=sigma, strike=strike, intr=intr)
1919
#' @export
2020
BachelierPrice <- function(
21-
type = "call", spot, forward = spot*exp((r-div)*t.exp),
22-
strike = forward, t.exp = 1, r = 0, div = 0, sigma
21+
type = "call", spot, forward = spot*exp((intr-divr)*texp),
22+
strike = forward, texp = 1, intr = 0, divr = 0, sigma
2323
){
2424

2525
#------------------------------------------------
@@ -39,48 +39,48 @@ BachelierPrice <- function(
3939
#------------------------------------------------
4040
#------------------------------------------------
4141

42-
stdev <- sigma*sqrt(t.exp)
42+
stdev <- sigma*sqrt(texp)
4343
d1 <- (forward-strike) / stdev
4444
pnorm.d1 <- pnorm(d1) # normal CDF
4545
dnorm.d1 <- dnorm(d1) # normal PDF =exp(-d1*d1/2)/sqrt(2*pi)
46-
disc.factor <- exp(-r*t.exp)
46+
disc.factor <- exp(-intr*texp)
4747

4848
if(type=="call"){
4949
#Option Price
5050
price <- (forward-strike)*pnorm.d1 + stdev*dnorm.d1
5151
#Greeks
5252
delta <- pnorm.d1 #Delta
53-
gamma <- 1 / (sigma*t.exp)/sqrt(2*pi)*exp(-d1^2/2)#Gamma
54-
vega <- sqrt(t.exp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
53+
gamma <- 1 / (sigma*texp)/sqrt(2*pi)*exp(-d1^2/2)#Gamma
54+
vega <- sqrt(texp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
5555

5656
}else if (type=="put"){
5757
#Option Price
5858
price <- (strike-forward)*(1-pnorm.d1) + stdev*dnorm.d1
5959
#Greeks
6060
delta <- pnorm.d1 - 1 #Delta
61-
gamma <- 1 / (sigma*t.exp)/sqrt(2*pi)*exp(-d1^2/2)#Gamma
62-
vega <- sqrt(t.exp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
61+
gamma <- 1 / (sigma*texp)/sqrt(2*pi)*exp(-d1^2/2)#Gamma
62+
vega <- sqrt(texp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
6363

6464
} else if (type=="straddle"){
6565
#Straddle price
6666
price <- (forward-strike)*(2*pnorm.d1-1) + 2*stdev*dnorm.d1
6767
delta <- 2*pnorm.d1 - 1
68-
gamma <- 2 / (sigma*t.exp)/sqrt(2*pi)*exp(-d1^2/2)
69-
vega <- 2 * sqrt(t.exp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
68+
gamma <- 2 / (sigma*texp)/sqrt(2*pi)*exp(-d1^2/2)
69+
vega <- 2 * sqrt(texp)/sqrt(2*pi)*exp(-d1^2/2)#Vega
7070

7171
} else if (type== "digital call"){
7272
#Digital call price
7373
price <- pnorm(d1)
74-
delta <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma*sqrt(t))
75-
gamma <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma^2*t)
76-
vega <- -1 / sqrt(2*pi)*exp(-d1^2/2)*(spot-strike)/(sigma^2*sqrt(t))
74+
delta <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma*sqrt(texp))
75+
gamma <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma^2*texp)
76+
vega <- -1 / sqrt(2*pi)*exp(-d1^2/2)*(spot-strike)/(sigma^2*sqrt(texp))
7777

7878
} else if (type== "digital put"){
7979
#Digital put price
8080
price <- pnorm(-d1)
81-
delta <- -1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma*sqrt(t))
82-
gamma <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma^2*t)
83-
vega <- -1 / sqrt(2*pi)*exp(-d1^2/2)*(spot-strike)/(sigma^2*sqrt(t))
81+
delta <- -1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma*sqrt(texp))
82+
gamma <- 1 / sqrt(2*pi)*exp(-d1^2/2)/(sigma^2*texp)
83+
vega <- -1 / sqrt(2*pi)*exp(-d1^2/2)*(spot-strike)/(sigma^2*sqrt(texp))
8484
} else {
8585
cat("Error! Please input: call/put/straddle/digital call/digital put")
8686
}

pkg/R/blackscholes_impvol.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
#' @param spot current stock price
66
#' @param forward forward stock price
77
#' @param strike strike price
8-
#' @param t.exp time to expiry
9-
#' @param r interest rate
10-
#' @param div dividend rate
8+
#' @param texp time to expiry
9+
#' @param intr interest rate
10+
#' @param divr dividend rate
1111
#' @return Implied vol
1212
#' @examples
1313
#' spot <- 100
1414
#' strike <- 100
15-
#' t.exp <- 1.2
15+
#' texp <- 1.2
1616
#' sigma <- 0.2
17-
#' r <- 0.05
17+
#' intr <- 0.05
1818
#' price <- 20
19-
#' vol <- FER::BlackScholesImpvol(price=price, spot=spot, strike=strike, t.exp=t.exp, r=r)
19+
#' vol <- FER::BlackScholesImpvol(price=price, spot=spot, strike=strike, texp=texp, intr=intr)
2020
#' @export
2121
BlackScholesImpvol <- function(
22-
type = "call", price, spot, forward = spot*exp((r-div)*t.exp),
23-
strike = forward, t.exp = 1, r = 0, div = 0
22+
type = "call", price, spot, forward = spot*exp((intr-div)*texp),
23+
strike = forward, texp = 1, intr = 0, divr = 0
2424
){
25-
price.forward = price * exp( r*t.exp)
25+
price.forward = price * exp( r*texp)
2626

2727
n.price = length(price.forward)
2828
n.strike = length(strike)
@@ -40,7 +40,7 @@ BlackScholesImpvol <- function(
4040
# Be careful here.... Chekc how functional works in R.
4141
sub <- function(sigma){
4242
f <- CalcBsmPrice(
43-
type = type, forward = forward, strike = strike.vec[k], t.exp = t.exp, sigma = sigma
43+
type = type, forward = forward, strike = strike.vec[k], texp = texp, sigma = sigma
4444
)[1] - price.forward[k]
4545
return(f)
4646
}

pkg/R/blackscholes_price.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44
#' @param spot current stock price
55
#' @param forward forward stock price
66
#' @param strike strike price
7-
#' @param t.exp time to expiry
8-
#' @param r interest rate
9-
#' @param div dividend rate
7+
#' @param texp time to expiry
8+
#' @param intr interest rate
9+
#' @param divr dividend rate
1010
#' @param sigma volatility
1111
#' @return option price
1212
#' @examples
1313
#' spot <- 100
1414
#' strike <- seq(80,125,5)
15-
#' t.exp <- 1.2
15+
#' texp <- 1.2
1616
#' sigma <- 0.2
17-
#' r <- 0.05
18-
#' price <- FER::BlackScholesPrice(spot=spot, t.exp = t.exp, sigma=sigma, strike=strike, r=r)
17+
#' intr <- 0.05
18+
#' price <- FER::BlackScholesPrice(spot=spot, texp = texp, sigma=sigma, strike=strike, intr=intr)
1919
#' @export
2020
BlackScholesPrice <- function(
21-
type = "call", spot, forward = spot*exp((r-div)*t.exp),
22-
strike = forward, t.exp = 1, r = 0, div = 0, sigma
21+
type = "call", spot, forward = spot*exp((intr-divr)*texp),
22+
strike = forward, texp = 1, intr = 0, divr = 0, sigma
2323
){
24-
stdev <- sigma*sqrt(t.exp)
24+
stdev <- sigma*sqrt(texp)
2525

2626
# a trick to get the intrinsic value for negative or zero vol
2727
# also avoid NAN in case forward = strike
2828
stdev[stdev < 1e-32] <- 1e-32
2929

3030
d1 <- log(forward/strike)/stdev + 0.5*stdev
3131
d2 <- d1 - stdev
32-
disc.factor <- exp(-r*t.exp)
32+
disc.factor <- exp(-intr*texp)
3333

3434
pnorm.d1 <- pnorm(d1)
3535
pnorm.d2 <- pnorm(d2)

pkg/R/cev.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#' Constant Elasticity Of Variance (CEV) model option price
22
#'
3-
#' @param strike
4-
#' @param spot
5-
#' @param forward
6-
#' @param t.exp
7-
#' @param sigma
3+
#' @param strike strike price
4+
#' @param spot current stock price
5+
#' @param forward forward stock price
6+
#' @param texp time to expiry
7+
#' @param sigma volatility
88
#' @param beta
9-
#' @param r
10-
#' @param div
11-
#' @param type
9+
#' @param intr interest rate
10+
#' @param divr dividend rate
11+
#' @param type option type either 'call' or 'put'
1212
#'
13-
#' @return
13+
#' @return option price
1414
#' @export
1515
#'
1616
#' @examples
1717
CEV <- function(
18-
strike, spot, forward = spot*exp((r-div)*t.exp),
19-
t.exp=1, sigma=0.01, beta=0.5,
20-
r = 0, div = 0, type="call"
18+
strike, spot, forward = spot*exp((intr-divr)*texp),
19+
texp=1, sigma=0.01, beta=0.5,
20+
intr = 0, divr = 0, type="call"
2121
){
2222
betac <- 1 - beta
23-
scale <- (betac*sigma)^2*t.exp
23+
scale <- (betac*sigma)^2*texp
2424
strike_cov = strike^(2*betac) / scale # strike change of variable
2525
forward_cov = forward^(2*betac) / scale # forward change of variable
2626
df <- 1/betac
@@ -30,7 +30,7 @@ CEV <- function(
3030
term1 <- stats::pchisq(strike_cov, df=df+2, ncp=forward_cov, lower.tail=!iscall)
3131
term2 <- stats::pchisq(forward_cov, df=df, ncp=strike_cov, lower.tail=iscall)
3232

33-
price <- ifelse(iscall, 1, -1)*exp(-r*t.exp)*(forward*term1 - strike*term2)
33+
price <- ifelse(iscall, 1, -1)*exp(-intr*texp)*(forward*term1 - strike*term2)
3434

3535
return(price)
3636
}

pkg/R/sabr_hagan2002.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
#' Hagan approximation for the SABR model
22
#'
3-
#' @param strike
4-
#' @param spot
5-
#' @param forward
6-
#' @param t.exp
7-
#' @param sigma
3+
#' @param strike strike price
4+
#' @param spot current stock price
5+
#' @param forward forward stock price
6+
#' @param texp time to expiry
7+
#' @param sigma volatility
88
#' @param vov
99
#' @param rho
1010
#' @param beta
11-
#' @param r
12-
#' @param div
11+
#' @param intr interest rate
12+
#' @param divr dividend rate
1313
#' @param type 'BlackScholes', 'call', 'put'
1414
#'
1515
#' @return Black-Scholes volatility (type="BlackScholes") or option price ("call" or "put")
1616
#' @export
1717
#'
1818
#' @examples
1919
SabrHagan2002 <- function(
20-
strike, spot, forward = spot*exp((r-div)*t.exp),
21-
t.exp=1, sigma=0.01, vov=0, rho=0, beta=1,
22-
r = 0, div = 0, type="BlackScholes"
20+
strike, spot, forward = spot*exp((intr-divr)*texp),
21+
texp=1, sigma=0.01, vov=0, rho=0, beta=1,
22+
intr = 0, divr = 0, type="BlackScholes"
2323
){
2424
betac <- 1 - beta
2525
powFwdStrk <- (forward*strike)^(betac/2)
@@ -32,7 +32,7 @@ SabrHagan2002 <- function(
3232
pre2alp1 <- vov*rho*beta/4/powFwdStrk
3333
pre2alp2 <- betac^2/24/powFwdStrk^2
3434

35-
pre2 <- 1 + t.exp*( pre2alp0 + sigma*(pre2alp1 + pre2alp2*sigma) )
35+
pre2 <- 1 + texp*( pre2alp0 + sigma*(pre2alp1 + pre2alp2*sigma) )
3636

3737
zz <- powFwdStrk*logFwdStrk*vov/sigma # need to make sure sig > 0
3838
yy <- sqrt(1 + zz*(zz-2*rho))
@@ -51,8 +51,8 @@ SabrHagan2002 <- function(
5151
return(volBlks)
5252
} else if(type=="call" | type=="put") {
5353
p <- BlackScholesPrice(
54-
type=type, spot=spot, strike=strike, t.exp=t.exp, sigma=volBlks,
55-
r=r, div=div
54+
type=type, spot=spot, strike=strike, texp=texp, sigma=volBlks,
55+
intr=intr, divr=divr
5656
)
5757
return(p)
5858
}

pkg/man/BachelierImpvol.Rd

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)