Valuation Classes
from dx import *
There are the following single risk factor valuation classes available:
valuation_mcs_european_single
valuation_mcs_american_single
r = constant_short_rate('r', 0.06)
me = market_environment('me', dt.datetime(2015, 1, 1))
me.add_constant('initial_value', 36.)
me.add_constant('volatility', 0.2)
me.add_constant('final_date', dt.datetime(2015, 12, 31))
me.add_constant('currency', 'EUR')
me.add_constant('frequency', 'W')
me.add_constant('paths', 25000)
me.add_curve('discount_curve', r)
gbm = geometric_brownian_motion('gbm', me)
me.add_constant('maturity', dt.datetime(2015, 12, 31))
# me.add_constant('strike', 40.)
call_eur = valuation_mcs_european_single('call_eur', gbm, me,
'np.maximum(maturity_value - 40, 0)')
call_eur.present_value()
call_eur.delta()
call_eur.vega()
%%time
s_list = np.arange(34., 46.1, 2.)
pv = []; de = []; ve = []
for s in s_list:
call_eur.update(s)
pv.append(call_eur.present_value())
de.append(call_eur.delta(.5))
ve.append(call_eur.vega(0.2))
%matplotlib inline
from dx_plot import *
plot_option_stats(s_list, pv, de, ve)
me.add_constant('initial_value', 36.)
# reset initial_value
put_ame = valuation_mcs_american_single('put_eur', gbm, me,
'np.maximum(40. - instrument_values, 0)')
put_ame.present_value()
put_ame.delta()
put_ame.vega()
%%time
s_list = np.arange(34., 46.1, 2.)
pv = []; de = []; ve = []
for s in s_list:
put_ame.update(s)
pv.append(put_ame.present_value())
de.append(put_ame.delta(.5))
ve.append(put_ame.vega(0.2))
plot_option_stats(s_list, pv, de, ve)
These are the classes for portfolio valuation:
derivatives_position
derivatives_portfolio
me.add_constant('model', 'gbm')
put = derivatives_position(
name='put',
quantity=1,
underlyings=['gbm'],
mar_env=me,
otype='American single',
payoff_func='np.maximum(40. - instrument_values, 0)')
put.get_info()
me_jump = market_environment('me_jump', dt.datetime(2015, 1, 1))
me_jump.add_environment(me)
me_jump.add_constant('lambda', 0.8)
me_jump.add_constant('mu', -0.8)
me_jump.add_constant('delta', 0.1)
me_jump.add_constant('model', 'jd')
call_jump = derivatives_position('call_jump', 3, ['jd'], me_jump, 'European single',
'np.maximum(maturity_value - 36., 0)')
underlyings = {'gbm': me, 'jd' : me_jump}
positions = {'put' : put, 'call_jump' : call_jump}
val_env = market_environment('general', dt.datetime(2015, 1, 1))
val_env.add_constant('frequency', 'M')
val_env.add_constant('paths', 50000)
val_env.add_constant('starting_date', val_env.pricing_date)
val_env.add_constant('final_date', val_env.pricing_date)
val_env.add_curve('discount_curve', r)
port = derivatives_portfolio('portfolio', positions, val_env, underlyings)
%%time
port.get_statistics()
port.get_positions()
correlations = [['gbm', 'jd', 0.9]]
port = derivatives_portfolio('portfolio', positions, val_env, underlyings,
correlations, fixed_seed=True)
port.get_statistics()
port.val_env.lists['cholesky_matrix']
path_no = 0
paths1 = port.underlying_objects['gbm'].get_instrument_values()[:, path_no]
paths2 = port.underlying_objects['jd'].get_instrument_values()[:, path_no]
plt.plot(port.time_grid, paths1, 'r', label='gbm')
plt.plot(port.time_grid, paths2, 'b', label='jd')
plt.gcf().autofmt_xdate()
plt.legend(loc=0); plt.grid(True)
# highly correlated underlyings -- until a jump occurs