From a risk management perspective it is important to know how sensitive derivatives portfolios are with regard to certain parameter values (market quotes, model assumptions, etc.). This part illustrates how to generate certain risk reports for derivatives_portfolio
objects.
In [1]:
import dx
import datetime as dt
import time
import numpy as np
The example is based on two risk factors, both modeled as geometric Brownian motions.
In [2]:
# constant short rate
r = dx.constant_short_rate('r', 0.01)
In [3]:
# market environment
me_gbm_1 = dx.market_environment('gbm_1', dt.datetime(2015, 1, 1))
In [4]:
# geometric Brownian motion
me_gbm_1.add_constant('initial_value', 40.)
me_gbm_1.add_constant('volatility', 0.2)
me_gbm_1.add_constant('currency', 'EUR')
me_gbm_1.add_constant('model', 'gbm')
In [5]:
me_gbm_2 = dx.market_environment('gbm_2', me_gbm_1.pricing_date)
In [6]:
# valuation environment
val_env = dx.market_environment('val_env', dt.datetime(2015, 1, 1))
val_env.add_constant('paths', 25000)
# 25,000 paths
val_env.add_constant('frequency', 'W')
# weekly frequency
val_env.add_curve('discount_curve', r)
val_env.add_constant('starting_date', dt.datetime(2015, 1, 1))
val_env.add_constant('final_date', dt.datetime(2015, 12, 31))
In [7]:
# add valuation environment to market environments
me_gbm_1.add_environment(val_env)
me_gbm_2.add_environment(me_gbm_1)
me_gbm_2.add_constant('initial_value', 40.)
me_gbm_2.add_constant('volatility', 0.5)
# higher volatility
In [8]:
risk_factors = {'gbm_1' : me_gbm_1, 'gbm_2' : me_gbm_2}
# market with two risk factors
We are going to model total of 6 derivatives positions.
All derivatives instruments (positions) share the same market_environment
object.
In [9]:
# market environment for the options
me_option = dx.market_environment('put', dt.datetime(2015, 1, 1))
me_option.add_constant('maturity', dt.datetime(2015, 12, 31))
me_option.add_constant('currency', 'EUR')
me_option.add_environment(val_env)
Two different kinds of derivatives make up the portfolio---an American put option and a European maximum call option. Both types of derivatives populate three positions, respectively.
In [10]:
positions = {}
half = 3 # 2 times that many options
for i in range(half):
name = 'am_put_pos_%s' %i # same name for position key and name
positions[name] = dx.derivatives_position(
name=name,
quantity=1,
underlyings=['gbm_1'],
mar_env=me_option,
otype='American single',
payoff_func='np.maximum(instrument_values - 40., 0)')
multi_payoff = "np.maximum(np.maximum(maturity_value['gbm_1'], maturity_value['gbm_2']) - 40., 0)"
for i in range(half, 2 * half):
name = 'multi_pos_%s' %i # same name for position key and name
positions[name] = dx.derivatives_position(
name=name,
quantity=1,
underlyings=['gbm_1', 'gbm_2'],
mar_env=me_option,
otype='European multi',
payoff_func=multi_payoff)
The instantiation of the derivatives_portfolio
object is as usual.
In [11]:
portfolio = dx.derivatives_portfolio(
name='portfolio',
positions=positions,
val_env=val_env,
risk_factors=risk_factors,
correlations=None,
parallel=False)
In [12]:
%time res = portfolio.get_values(fixed_seed=True)
Here, the value estimates from the Monte Carlo simulation and valuation.
In [13]:
res
Out[13]:
Portfolio risk reports are meant to provide a broad overview of how sensitive the value of a portfolio is with regard to the value of certain input parameters (market data, model parameters). While Greeks provide the same information with regard to marginal changes in the input paramters, risk reports provide a wider range input-output (parameter-portfolio value) combinations.
First, consider the portfolio from before, i.e. without correlation.
In [14]:
portfolio.val_env.get_list('cholesky_matrix')
Out[14]:
Calling the method get_port_risk
and providing a key for the respetive Greek yields sensitivities with regard to all risk factors (here: gbm_1
and gbm_2
).
In [15]:
%%time
vegas, benchvalue = portfolio.get_port_risk(Greek='Vega',
fixed_seed=True)
The return object is a pandas Panel
object.
In [16]:
vegas
Out[16]:
Using the helper funtion risk_report
allows the easy, readable printout of the results, i.e. the portfolio volatility sensitivities. In this case you can see that, for example, the increase in the first risk fator's (gbm_1
) volatility by 10% leads to a portfolio value increase bya bit less than 1 currency unit. Decreasing the same input parameter by 10% reduces the portfolio value by a bit less than 1 currency unit.
In [17]:
dx.risk_report(vegas)
Of course, you can generate the same risk report for the portfolio initial value sensitivities.
In [18]:
%time deltas, benchvalue = portfolio.get_port_risk(Greek='Delta', fixed_seed=True)
For example, increasing the initial value of the first risk factor (gbm_1
) by 10% increases the portfolio value by about 11 currency units.
In [19]:
dx.risk_report(deltas)
In [20]:
dx.risk_report(deltas.ix[:, :, 'value'] - benchvalue)
Consider now a highly negative correlation case.
In [21]:
correlations = [['gbm_1', 'gbm_2', -0.9]]
In [22]:
portfolio = dx.derivatives_portfolio(
'portfolio', positions, val_env,
risk_factors, correlations, parallel=False)
In [23]:
portfolio.val_env.get_list('cholesky_matrix')
Out[23]:
Since the value of the European maximum call option is dependent on the risk factor correlation you see a significant change in this derivative's value estimate.
In [24]:
%time portfolio.get_values(fixed_seed=True)
Out[24]:
Via the step
parameter, you can influence the granularity of the risk report.
In [25]:
%%time
deltas, benchvalue = portfolio.get_port_risk(Greek='Delta',
fixed_seed=True,
step=0.05)
In this case, an increase in the intial value of the first risk factor (gbm_1
) by 10% leads to a much higher increase
in the portfolio value of about 15 currency units.
In [26]:
dx.risk_report(deltas)
In [27]:
dx.risk_report(deltas.ix[:, :, 'value'] - benchvalue)
Copyright, License & Disclaimer
© Dr. Yves J. Hilpisch | The Python Quants GmbH
DX Analytics (the "dx library") is licensed under the GNU Affero General Public License version 3 or later (see http://www.gnu.org/licenses/).
DX Analytics comes with no representations or warranties, to the extent permitted by applicable law.
http://tpq.io | team@tpq.io | http://twitter.com/dyjh
Quant Platform | http://quant-platform.com
Derivatives Analytics with Python (Wiley Finance) | http://derivatives-analytics-with-python.com
Python for Finance (O'Reilly) | http://python-for-finance.com