Skip to content

Utility Functions

George Fisher edited this page Feb 6, 2016 · 14 revisions
library(ustreasuries)
#> 
#> Attaching package: 'ustreasuries'
#> The following object is masked from 'package:stats':
#> 
#>     Gamma
  • CAGR Calculate Compound Annual Growth Rates
PV    <- 9000
FV    <- 13000
years <- 3

# geometric
# =========
(geometric  <- CAGR(9000, 13000, years, type="geometric"))
#> [1] 0.1304038
9000 * (1 + geometric) ** years
#> [1] 13000

# continuous
# ==========
(continuous <- CAGR(9000, 13000, years, type="continuous"))
#> [1] 0.1225749
9000 * exp(continuous * years)
#> [1] 13000
  • r_continuous Convert from discrete to continuous
discrete <- 0.04
freq     <- 2     # 4% compounded semi annually

(continuous <- r_continuous(discrete, freq))
#> [1] 0.03960525
  • r_discrete Convert from continuous to discrete
PV    <- 9000
FV    <- 13000
years <- 3
freq  <- 2   # compounding frequency = 2 => semi-annual

(continuous <- CAGR(PV, FV, years, type="continuous"))
#> [1] 0.1225749
(discrete   <- r_discrete(continuous, freq))
#> [1] 0.126409

PV * (1 + discrete / freq) ** (freq * years)
#> [1] 13000

PV * exp(continuous * years)
#> [1] 13000
  • discount_factor Calculate discount factor Z(t, T)
PV    <- 9000
FV    <- 13000
years <- 3
freq  <- 2   # compounding frequency = 2 => semi-annual

(continuous <- CAGR(PV, FV, years, type="continuous"))
#> [1] 0.1225749
(discrete   <- r_discrete(continuous, freq))
#> [1] 0.126409

(df_continuous <- discount_factor(continuous,  years))
#> [1] 0.6923077

(df_discrete   <- discount_factor(discrete,  years, freq))
#> [1] 0.6923077

FV * df_continuous
#> [1] 9000
FV * df_discrete
#> [1] 9000
  • CallParity Convert from a put-option price using put/call parity
  • PutParity Convert from a call-option price using put/call parity
# Hull 7th edition Ch 17 P 357
Stock    <- 49
Exercise <- 50
Time     <- 20/52
Interest <- 0.05
Yield    <- 0
sigma    <- 0.20

EC = EuroCall(Stock, Exercise, Time, Interest, Yield, sigma)
EP = EuroPut(Stock, Exercise, Time, Interest, Yield, sigma)

PC = CallParity(Stock, Exercise, Time, Interest, Yield, EP)
PP = PutParity(Stock, Exercise, Time, Interest, Yield, EC)

writeLines(paste0("European Call Price:\t", EC, "\n",
                  "Call Parity Price:\t\t", PC, "\n",
                  "Difference:\t\t\t\t", EC-PC, "\n\n",

                  "European Put Price:\t", EP, "\n",
                  "Put Parity Price:\t\t", PP, "\n",
                  "Difference:\t\t\t\t ", EP-PP))

#> European Call Price: 2.40053407821637
#> Call Parity Price:   2.40053407821637
#> Difference:         -7.105427357601e-15
#> 
#> European Put Price:  2.44818219622651
#> Put Parity Price:    2.4481821962265
#> Difference:          3.5527136788005e-15
  • RiskNeutralProb Binomial tree risk-neutral probability
Interest <- 0.05
Yield    <- 0.10
sigma    <- 0.20
deltaT   <- 5
RiskNeutralProb(Interest, Yield, sigma, deltaT)
#> [1] 0.1507705
  • ForwardPrice Forward price with or without income or yield
# Hull 7th edition Ch 5 P 103
# ===========================
Spot     <- 40
Time     <- 0.25
Interest <- 0.05
Yield    <- 0
Income   <- 0
ForwardPrice(Spot, Time, Interest, Yield, Income)
#> [1] 40.50314

# Hull 7th edition Ch 5 P 105
# ===========================
Spot     <- 900
Time     <- 0.75
Interest <- 0.04
Yield    <- 0
Income   <- 40 * exp(-0.03 * 4/12) # PV(40) = 39.60
ForwardPrice(Spot, Time, Interest, Yield, Income)
#> [1] 886.601

# Hull 7th edition Ch 5 P 107
# ===========================
Spot     <- 25
Time     <- 0.50
Interest <- 0.10

# convert 0.04 discrete to continuous
Yield_d  <- 0.04
Yield    <- r_continuous(Yield_d, 2)

Income   <- 0
ForwardPrice(Spot, Time, Interest, Yield, Income)
#> [1] 25.76645
  • ForwardRate Forward rate from Time1 to Time2 (discrete compounding)
  • IntrinsicValueCall The in-the-money portion of a call option's premium
# Investopia: Intrinsic Value
# http://www.investopedia.com/terms/i/intrinsicvalue.asp
Stock    <- 25 # S_0
Exercise <- 15 # K
IntrinsicValueCall(Stock, Exercise) # 10
#> [1] 10
  • IntrinsicValuePut The in-the-money portion of a put option's premium
# Investopia: Intrinsic Value
# http://www.investopedia.com/terms/i/intrinsicvalue.asp
Stock    <- 15 # S_0
Exercise <- 25 # K
IntrinsicValuePut(Stock, Exercise) # 10
#> [1] 10
  • InTheMoneyCall Is a call option in the money?
# http://www.call-options.com/in-the-money.html
Stock    <- 37.75     # S_0
Exercise <- 35        # K

InTheMoneyCall(Stock, Exercise)  # TRUE
#> [1] TRUE
  • InTheMoneyPut Is a put option in the money?
# http://www.call-options.com/in-the-money.html
Stock    <- 35     # S_0
Exercise <- 37.50  # K

InTheMoneyPut(Stock, Exercise)  # TRUE
#> [1] TRUE

See https://github.com/grfiv/BlackScholesMerton for these functions written in Python and Excel VBA
See also http://www.philadelphia-reflections.com/topic/230.htm