Last updated: 2023-03-16.
tf_quant_finance.models.hjm.swaption_price#
Calculates the price of European swaptions using the HJM model.
tf_quant_finance.models.hjm.swaption_price(
*, expiries, fixed_leg_payment_times, fixed_leg_daycount_fractions,
fixed_leg_coupon, reference_rate_fn, num_hjm_factors, mean_reversion,
volatility, times=None, time_step=None, num_time_steps=None, curve_times=None,
corr_matrix=None, notional=None, is_payer_swaption=None,
valuation_method=tf_quant_finance.models.ValuationMethod.MONTE_CARLO,
num_samples=1, random_type=None, seed=None, skip=0,
time_step_finite_difference=None, num_time_steps_finite_difference=None,
num_grid_points_finite_difference=101, dtype=None, name=None
)
A European Swaption is a contract that gives the holder an option to enter a swap contract at a future date at a prespecified fixed rate. A swaption that grants the holder the right to pay fixed rate and receive floating rate is called a payer swaption while the swaption that grants the holder the right to receive fixed and pay floating payments is called the receiver swaption. Typically the start date (or the inception date) of the swap coincides with the expiry of the swaption. Mid-curve swaptions are currently not supported (b/160061740).
This implementation uses the HJM model to numerically value European swaptions. For more information on the formulation of the HJM model, see quasi_gaussian_hjm.py.
Example#
import numpy as np
import tensorflow as tf
import tf_quant_finance as tff
dtype = tf.float64
# Price 1y x 1y swaption with quarterly payments using Monte Carlo
# simulations.
expiries = np.array([1.0])
fixed_leg_payment_times = np.array([1.25, 1.5, 1.75, 2.0])
fixed_leg_daycount_fractions = 0.25 * np.ones_like(fixed_leg_payment_times)
fixed_leg_coupon = 0.011 * np.ones_like(fixed_leg_payment_times)
zero_rate_fn = lambda x: 0.01 * tf.ones_like(x, dtype=dtype)
mean_reversion = [0.03]
volatility = [0.02]
price = tff.models.hjm.swaption_price(
expiries=expiries,
fixed_leg_payment_times=fixed_leg_payment_times,
fixed_leg_daycount_fractions=fixed_leg_daycount_fractions,
fixed_leg_coupon=fixed_leg_coupon,
reference_rate_fn=zero_rate_fn,
notional=100.,
num_hjm_factors=1,
mean_reversion=mean_reversion,
volatility=volatility,
valuation_method=tff.model.ValuationMethod.MONTE_CARLO,
num_samples=500000,
time_step=0.1,
random_type=tff.math.random.RandomType.STATELESS_ANTITHETIC,
seed=[1, 2])
# Expected value: [[0.716]]
References:#
[1]: D. Brigo, F. Mercurio. Interest Rate Models-Theory and Practice. Second Edition. 2007. Section 6.7, page 237.
Args:#
expiries: A realTensorof any shape and dtype. The time to expiration of the swaptions. The shape of this input along with the batch shape of the HJM model determines the number (and shape) of swaptions to be priced and the shape of the output. If the batch shape of HJM models ismodel_batch_shape, then the leading dimensions ofexpiriesmust be broadcastable tomodel_batch_shape. For example, if the rank ofmodel_batch_shapeisnand the rank ofexpiries.shapeism, thenm>=nand the leadingndimensions ofexpiries.shapemust be broadcastable tomodel_batch_shape.fixed_leg_payment_times: A realTensorof the same dtype asexpiries. The payment times for each payment in the fixed leg. The shape of this input should beexpiries.shape + [n]wherendenotes the number of fixed payments in each leg. Thefixed_leg_payment_timesshould be greater-than or equal-to the corresponding expiries.fixed_leg_daycount_fractions: A realTensorof the same dtype and compatible shape asfixed_leg_payment_times. The daycount fractions for each payment in the fixed leg.fixed_leg_coupon: A realTensorof the same dtype and compatible shape asfixed_leg_payment_times. The fixed rate for each payment in the fixed leg.reference_rate_fn: A Python callable that accepts expiry time as a realTensorand returns aTensorof shapemodel_batch_shape + input_shape. Returns the continuously compounded zero rate at the present time for the input expiry time.num_hjm_factors: A Python scalar which corresponds to the number of factors in the batch of HJM models to be used for pricing.mean_reversion: A real positiveTensorof shapemodel_batch_shape + [num_hjm_factors]. Corresponds to the mean reversion rate of each factor in the batch.volatility: A real positiveTensorof the samedtypeand shape asmean_reversionor a callable with the following properties: (a) The callable should accept a scalarTensortand aTensorr(t)of shapebatch_shape + [num_samples]and returns aTensorof shape compatible withbatch_shape + [num_samples, dim]. The variabletstands for time andr(t)is the short rate at timet. The function returns instantaneous volatilitysigma(t) = sigma(t, r(t)). Whenvolatilityis specified as a realTensor, each factor is assumed to have a constant instantaneous volatility and the model is effectively a Gaussian HJM model. Corresponds to the instantaneous volatility of each factor.times: An optional rank 1Tensorof increasing positive real values. The times at which Monte Carlo simulations are performed. Relevant when swaption valuation is done using Monte Calro simulations. Default value:Nonein which case simulation times are computed based on eithertime_stepornum_time_stepsinputs.time_step: Optional scalar realTensor. Maximal distance between time grid points in Euler scheme. Relevant when Euler scheme is used for simulation. This input ornum_time_stepsare required when valuation method is Monte Carlo. Default Value:None.num_time_steps: An optional scalar integerTensor- a total number of time steps during Monte Carlo simulations. The maximal distance betwen points in grid is bounded bytimes[-1] / (num_time_steps - times.shape[0]). Either this ortime_stepshould be supplied when the valuation method is Monte Carlo. Default value:None.curve_times: An optional rank 1Tensorof positive and increasing real values. The maturities at which spot discount curve is computed during simulations. Default value:Nonein which casecurve_timesis computed based on swaption expiries andfixed_leg_payments_timesinputs.corr_matrix: ATensorof shape[num_hjm_factors, num_hjm_factors]and the samedtypeasmean_reversion. Specifies the correlation between HJM factors. Default value:Nonein which case the factors are assumed to be uncorrelated.notional: An optionalTensorof same dtype and compatible shape asstrikesspecifying the notional amount for the underlying swaps. Default value: None in which case the notional is set to 1.is_payer_swaption: A booleanTensorof a shape compatible withexpiries. Indicates whether the swaption is a payer (if True) or a receiver (if False) swaption. If not supplied, payer swaptions are assumed.valuation_method: An enum of typeValuationMethodspecifying the method to be used for swaption valuation. Currently the valuation is supported usingMONTE_CARLOandFINITE_DIFFERENCEmethods. Valuation using finite difference is only supported for Gaussian HJM models, i.e. for models with constant mean-reversion rate and time-dependent volatility. Default value:ValuationMethod.MONTE_CARLO, in which case swaption valuation is done using Monte Carlo simulations.num_samples: Positive scalarint32Tensor. The number of simulation paths during Monte-Carlo valuation. This input is ignored during analytic valuation. Default value: The default value is 1.random_type: Enum value ofRandomType. The type of (quasi)-random number generator to use to generate the simulation paths. This input is relevant only for Monte-Carlo valuation and ignored during analytic valuation. Default value:Nonewhich maps to the standard pseudo-random numbers.seed: Seed for the random number generator. The seed is only relevant ifrandom_typeis one of[STATELESS, PSEUDO, HALTON_RANDOMIZED, PSEUDO_ANTITHETIC, STATELESS_ANTITHETIC]. ForPSEUDO,PSEUDO_ANTITHETICandHALTON_RANDOMIZEDthe seed should be an Python integer. ForSTATELESSandSTATELESS_ANTITHETICmust be supplied as an integerTensorof shape[2]. This input is relevant only for Monte-Carlo valuation and ignored during analytic valuation. Default value:Nonewhich means no seed is set.skip:int320-dTensor. The number of initial points of the Sobol or Halton sequence to skip. Used only whenrandom_typeis ‘SOBOL’, ‘HALTON’, or ‘HALTON_RANDOMIZED’, otherwise ignored. Default value:0.time_step_finite_difference: Optional scalar realTensor. Spacing between time grid points in finite difference discretization. This input is only relevant for valuation using finite difference. Default value:None. Ifnum_time_steps_finite_differenceis also unspecified then atime_stepcorresponding to 100 discretization steps is used.num_time_steps_finite_difference: Optional scalar realTensor. Number of time grid points in finite difference discretization. This input is only relevant for valuation using finite difference. Default value:None. Iftime_step_finite_differenceis also unspecified, then 100 time steps are used.num_grid_points_finite_difference: Optional scalar realTensor. Number of spatial grid points per dimension. Currently, we construct an uniform grid for spatial discretization. This input is only relevant for valuation using finite difference. Default value: 101.dtype: The default dtype to use when converting values toTensors. Default value:Nonewhich means that default dtypes inferred by TensorFlow are used.name: Python string. The name to give to the ops created by this function. Default value:Nonewhich maps to the default namehjm_swaption_price.
Returns:#
A Tensor of real dtype and shape derived from model_batch_shape and
expiries.shape containing the computed swaption prices. The shape of the
output is as follows:
If the
model_batch_shapeis [], then the shape of the output is expiries.shapeOtherwise, the shape of the output is
model_batch_shape + expiries.shape[model_batch_shape.rank:]For swaptions that have reset in the past (expiries<0), the function sets the corresponding option prices to 0.0.