R package for risk neutral economic scenarios generation with a g2++ model
esg2 only relies on R default libraries. The package
devtools is required in order to install it directly from its GitHub repository.
The package is available from its GitHub repository.
The G2++ model implemented in this package is described by the following equation:
The Black & Scholes model implemented for related assets follows the classical model, with a time-dependant interest rate.
The script file test_esg2.R contains all the usefull commands in order to run an end to end example with this package.
All the functions of the package are documented through R's
Zero Coupon Curve
The Zero Cupon curve is the basic brick for rate projection.
Import the curve
A zero coupon rate curve is imported through the function
curvezc, which create a Zero Coupon Curve object from the rates given in standard unit.
Three discounting methods are available:
Only rates are given in the function. Maturities are assumed to go continuously from 1 to the length of the curve.
For example, importing a 10 periods curve is done with the command:
?curvezc rates <- c(-0.00316,-0.00269,-0.00203,-0.00122,-0.00022, 0.00092,0.00215,0.00342,0.00465,0.00581) curve <- curvezc(method = "continuous", rates = rates )
It is possible to display, print or plot a curve object.
curve print(curve) plot(curve)
The plot gives the zero coupon rate for each maturity.
The rate model uses swaptions for its calibration. Those instrument are stocked as a table in an object of class Swaptions.
The swaptions initiator needs a curve object for the pricing of the swaptions, along with a list of maturities, tenors, and volatilities.
Two pricing method are available:
- normal: Bachelier pricing;
- lognormal: Black & Scholes Pricing.
The pricing model also use the frequency as the number of payments for each period.
?swaptions maturities = c(2,3,4,5) tenors = c(1,1,1,1) vols = c(0.016735,0.009841,0.007156,0.005425) swaptions <- swaptions("normal", curve, maturities, tenors, vols, 2)
It is possible to display, print or plot the volatility surface of a swaptions object. The
swaptions print(swaptions) plot(swaptions)
Correlations needs to be handled between at least the two components of the rate model, and for associated assets using the projected rates in a Black & Scholes context.
We thus need to generate W_1 and W_2 processes with a rho correlation, and take into account the correlations between the other Wiener's process.
genW is thus implemented in order to generate as many correlated processes as needed:
This function takes as argument the correlation matrix between the different process, the horizon for projection and the number of desired simulations.
In order to get the calibrated parameters of the rate model, this function should be run after the calibration process.
?genW correl <- cbind(c(1,g2model@rho, 0.25),c(g2model@rho,1, 0), c(0.25, 0, 1)) W <- genW(correl, g2model@nsimul, g2model@horizon)
The rate model is initialized via a zero coupon curve, a projection horizon and a number of simulation.
It is possible to directly pass the parameters of the model as arguments in order to skip the calibration step.
?g2 g2model <- g2(curve, horizon = 50, nsimul = 1000)
Calibration is performed on the model over a panel of swaptions defined in a Swaption object.
The optimized function for the process is a trade off between the differences between theoretical and observed prices and the probability to get negative rates. The optimal parameters are found with a Nelder-Mead algorithm followed by a Newton-Rhaphson method.
?calibrate g2model <- calibrate(g2model, swaptions, maxIter = 100)
Projection and Visualization
Once calibrated, the model is projected via the function
It is possible to pass Wx and Wy as optional arguments, that corresponds to W_1 and W_2 in the model equation and are generated to be correlated between themselves and with other assets. If not given, those two processes are computed with the rho parameter of the model, without taking into account any external process.
?project g2model <- project(g2model, Wx = W[,,1], Wy = W[,,2])
It is possible to display, print, and plot the model. For the later case, 100 scenarios are displayed, along with the mean trajectory and the standard deviation around the mean.
g2model print(g2model) plot(g2model)
Once the projection are realized, it is possible to get the deflator or the projected zero-coupon prices table at a time t.
# Get deflator table ?deflator def <- deflator(g2model) # Get zero coupon table at time 10 ?zctable zc10 <- zctable(g2model, 10)
The deflator test is also implemented in the package. This test aims to verify that the means of the deflator values match the zero coupon curve for every maturity.
test_deflator plot the zero coupon curve against the mean of the deflator for each maturity, along with a table displaying the absolute differences in points.
Black & Scholes Model
The library allows to use a Black & Scholes model for asset projection with the projected rate model.
Initializing a Black & Scholes modelized asset requires:
- a calibrated and projected rate model;
- an initial value for the projection;
- a volatility for the asset;
- a dividend rate, as the proportion of the value of the asset that is paid each year as dividends or rent;
- the correlation with the rate model;
- the generated distribution for W_s.
The last value is optional. If not provided, a distribution is computed with the given correlation.
?bs action <- bs(g2model, s0 = 100, vol = 0.2, div = 0.02, rho = -0.5, W = W[,,3])
Projection and Visualization
All the traditional display functions are available, and the trajectories can be extracted with the function
?traj trajAction <- traj(action) action print(action) plot(action)
The martingality test is implemented.
The means of the discounted value of the asset is ploted with the 95% confidence interval of its values.
- Arnaud Buzzi - esg2 package - View on GitHub
This project is licensed under GPL Licensing.