Learn R Programming

Description And Installation

ragtop prices equity derivatives using variants of the famous Black-Scholes model, with special attention paid to the case of American and European exercise options and to convertible bonds. To install the development version, use the command

devtools::install_github('brianboonstra/ragtop')

Usage

Basic Usage

You can price american and european exercise options, either individually, or in groups. In the simplest case that looks like this for European exercise

blackscholes(c(CALL, PUT), S0=100, K=c(100,110), time=0.77, r = 0.06, vola=0.20)
#> $Price
#> [1] 9.326839 9.963285
#> 
#> $Delta
#> [1]  0.6372053 -0.5761608
#> 
#> $Vega
#> [1] 32.91568 34.36717

and like this for American exercise

american(PUT, S0=100, K=c(100,110), time=0.77, const_short_rate = 0.06, const_volatility=0.20)
#> A100_281_0 A110_281_0 
#>    5.24386   11.27715

Including Term Structures

There are zillions of implementations of the Black-Scholes formula out there, and quite a few simple trees as well. One thing that makes ragtop a bit more useful than most other packages is that it treats dividends and term structures without too much pain. Assume we have some nontrivial term structures and dividends

## Dividends
divs = data.frame(time=seq(from=0.11, to=2, by=0.25),
                  fixed=seq(1.5, 1, length.out=8),
                  proportional = seq(1, 1.5, length.out=8))

## Interest rates
disct_fcn = ragtop::spot_to_df_fcn(data.frame(time=c(1, 5, 10), 
                                              rate=c(0.01, 0.02, 0.035)))

## Default intensity
disc_factor_fcn = function(T, t, ...) {
  exp(-0.03 * (T - t)) }
surv_prob_fcn = function(T, t, ...) {
  exp(-0.07 * (T - t)) }

## Variance cumulation / volatility term structure
vc = variance_cumulation_from_vols(
   data.frame(time=c(0.1,2,3),
              volatility=c(0.2,0.5,1.2)))
paste0("Cumulated variance to 18 months is ", vc(1.5, 0))
[1] "Cumulated variance to 18 months is 0.369473684210526"

then we can price vanilla options

black_scholes_on_term_structures(
   callput=TSLAMarket$options[500,'callput'], 
   S0=TSLAMarket$S0, 
   K=TSLAMarket$options[500,'K'], 
   discount_factor_fcn=disct_fcn, 
   time=TSLAMarket$options[500,'time'], 
   variance_cumulation_fcn=vc,
   dividends=divs)
$Price
[1] 62.55998

$Delta
[1] 0.7977684

$Vega
[1] 52.21925

American exercise options

american(
    callput = TSLAMarket$options[400,'callput'], 
    S0 = TSLAMarket$S0, 
    K=TSLAMarket$options[400,'K'], 
    discount_factor_fcn=disct_fcn, 
    time = TSLAMarket$options[400,'time'],
    survival_probability_fcn=surv_prob_fcn,
    variance_cumulation_fcn=vc,
    dividends=divs)
A360_137_2 
  2.894296 

We can also find volatilities of European exercise options

implied_volatility_with_term_struct(
    option_price=19, callput = PUT, 
    S0 = 185.17,K=182.50, 
    discount_factor_fcn=disct_fcn, 
    time = 1.12,
    survival_probability_fcn=surv_prob_fcn,
    dividends=divs)
[1] 0.1133976

as well as American exercise options

american_implied_volatility(
    option_price=19, callput = PUT, 
    S0 = 185.17,K=182.50, 
    discount_factor_fcn=disct_fcn, 
    time = 1.12,
    survival_probability_fcn=surv_prob_fcn,
    dividends=divs)
[1] 0.113407

More Sophisticated Calibration

You can also find more complete calibration routines in ragtop. See the vignette or the documentation for fit_variance_cumulation and fit_to_option_market.

Technical Documentation

The source for the technical paper is in this repository. You can also find the pdf here

Copy Link

Version

Install

install.packages('ragtop')

Monthly Downloads

306

Version

1.1.1

License

GPL (>= 2)

Maintainer

Brian Boonstra

Last Published

March 3rd, 2020

Functions in ragtop (1.1.1)

adjust_for_dividends

Find the sum of time-adjusted dividend values and adjust grid prices according to their size in the given interval
Quandl_df_fcn_UST_raw

Get a US Treasury curve discount factor function
fit_to_option_market_df

Calibrate volatilities and equity-linked default intensity making many assumptions
equivalent_jump_vola_to_bs

Find jump process volatility with a given default risk from a straight Black-Scholes volatility
CALL

Constant CALL for defining option contracts
ragtop

Pricing schemes for derivatives using equity-linked default intensity
accelerated_coupon_value

Present value of coupons according to an acceleration schedule
ZeroCouponBond-class

A simple contract paying the notional amount at the maturity
equivalent_bs_vola_to_jump

Find straight Black-Scholes volatility equivalent to jump process with a given default risk
TIME_RESOLUTION_SIGNIF_DIGITS

Constant to define when times are considered so close to each other that they should be treated as simultaneous, in terms of significant digits
TIME_RESOLUTION_FACTOR

Constant to define when times are considered so close to each other that they should be treated as simultaneous
american

Price one or more american-exercise options
coupon_value_at_exercise

Present value of coupons according to an acceleration schedule
black_scholes_on_term_structures

Black-Scholes pricing of european-exercise options with term structure arguments
value_from_prior_coupons

Present value of past coupons paid
fit_to_option_market

Calibrate volatilities and equity-linked default intensity
find_present_value

Use a model to estimate the present value of financial derivatives
implied_volatility

Implied volatility of european-exercise option under Black-Scholes or a jump-process extension
detail_from_AnnivDates

Convert output of BondValuation::AnnivDates to inputd for Bond
TSLAMarket

Market information snapshot for TSLA options
take_implicit_timestep

Backwardate grid values one timestep
implied_volatilities_with_rates_struct

Find the implied volatility of european-exercise options with a term structure of interest rates
implied_volatilities

Implied volatilities of european-exercise options under Black-Scholes or a jump-process extension
form_present_value_grid

Use a model to estimate the present value of financial derivatives on a grid of initial underlying values
is.blank

Return TRUE if the argument is empty, NULL or NA
spot_to_df_fcn

Create a discount factor function from a yield curve
fit_variance_cumulation

Fit piecewise constant volatilities to a set of equity options
implied_jump_process_volatility

Implied volatility of any instrument
american_implied_volatility

Implied volatility of an american option with equity-independent term structures
iterate_grid_from_timestep

Iterate over a set of timesteps to integrate the pricing differential equation
shift_for_dividends

Shift a set of grid values for dividends paid, using spline interpolation
blackscholes

Vectorized Black-Scholes pricing of european-exercise options
time_adj_dividends

Find the sum of time-adjusted dividend values
integrate_pde

Numerically integrate the pricing differential equation
construct_implicit_grid_structure

Structure of implicit numerical integration grid
infer_conforming_time_grid

A time grid with extra times inserted for coupons, calls and puts
penalty_with_intensity_link

Helper function (volatility-normalized pricing error) for calibration of equity-linked default intensity
price_with_intensity_link

Helper function (instrument pricing) for calibration of equity-linked default intensity
variance_cumulation_from_vols

Create a variance cumulation function from a volatility term structure
control_variate_pairs

Form instrument objects for vanilla options
timestep_instruments

Take an implicit timestep for all the given instruments
construct_tridiagonals

Matrix entries for implicit numerical differentiation using Neumann boundary conditions
implied_volatility_with_term_struct

Find the implied volatility of a european-exercise option with term structures
AmericanOption-class

A standard option contract allowing for early exercise at the choice of the option holder
CouponBond-class

Standard corporate or government bond
ConvertibleBond-class

Convertible bond with exercise into stock
CallableBond-class

Callable (and putable) corporate or government bond.
EquityOption-class

An option contract with call or put terms
Quandl_df_fcn_UST

Get a US Treasury curve discount factor function
EuropeanOption-class

A standard option contract
PUT

Constant PUT for defining option contracts
GridPricedInstrument-class

Representation of financial instrument amenable to grid pricing schemes