from dx import *
import seaborn as sns; sns.set()

"""
Explanation: <img src="http://hilpisch.com/tpq_logo.png" alt="The Python Quants" width="45%" align="right" border="4">
Single-Risk Derivatives Valuation
This part introduces into the modeling and valuation of derivatives instruments (contingent claims) based on a single risk factor (e.g. a stock price, stock index level or interest rate). It also shows how to model and value portfolios composed of such instruments.
End of explanation
"""


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)

"""
Explanation: The following single risk factor valuation classes are available:

valuation_mcs_european_single for derivatives with European exercise
valuation_mcs_american_single for derivatives with American/Bermudan exercise

Modeling the Risk Factor
Before moving on to the valuation classes, we need to model an instantiate an underlying risk factor, in this case a geometric_brownian_motion object. Background information is provided in the respective part of the documentation about model classes.
End of explanation
"""


me.add_constant('maturity', dt.datetime(2015, 12, 31))
me.add_constant('strike', 40.)

"""
Explanation: valuation_mcs_european_single
The first instrument we value is a European call option written on the single relevant risk factor as embodied by the gbm model object. To this end, we add a maturity date to the market environment and a strike price.
End of explanation
"""


call_eur = valuation_mcs_european_single(
                name='call_eur',
                underlying=gbm,
                mar_env=me,
                payoff_func='np.maximum(maturity_value - strike, 0)')

"""
Explanation: To instantiate a the valuation_mcs_european_single class, the following information/data is to be provided:

name as a string object
instance of a model class
market environment
payoff of the instrument a string object and containing "regular" Python/NumPy code
End of explanation
"""


payoff = 'np.maximum(np.minimum(maturity_value) * 2 - 50, 0)'

"""
Explanation: In this case, the payoff is that of a regular, plain vanilla European call option. If $T$ is the maturity date, $S_T$ the value of the relevant risk factor at that date and $K$ the strike price, the payoff $h_T$ at maturity of such an option is given by
$$
h_T = \max[S_T - K, 0]
$$
maturity_value represents the value vector of the risk factor at maturity. Any other "sensible" payoff definition is possible. For instance, the following works as well:
End of explanation
"""


call_eur.present_value()

"""
Explanation: Other standardized payoff elemenets include mean_value, max_value and min_value representing maturity value vectors with the pathwise means, maxima and minima. Using these payoff elements allows the easy definition of options with Asian features.
Having instantiated the valuation class, the present_value method returns the present value Monte Carlo estimator for the call option.
End of explanation
"""


call_eur.delta()

call_eur.vega()

"""
Explanation: Similarly, the delta and vega methods return the delta and the vega of the option, estimated numerically by a forward difference scheme and Monte Carlo simulation.
End of explanation
"""


%%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

"""
Explanation: This approach allows to work with such a valuation object similar to an analytical valuation formula like the one of Black-Scholes-Merton (1973). For example, you can estimate and plot present values, deltas and vegas for a range of different initial values of the risk factor.
End of explanation
"""


plot_option_stats(s_list, pv, de, ve)

"""
Explanation: There is a little plot helper function available to plot these statistics conveniently.
End of explanation
"""


me.add_constant('initial_value', 36.)
  # reset initial_value

put_ame = valuation_mcs_american_single(
                name='put_eur',
                underlying=gbm,
                mar_env=me,
                payoff_func='np.maximum(strike - instrument_values, 0)')

"""
Explanation: valuation_mcs_american_single
The modeling and valuation of derivatives with American/Bermudan exercise is almost completely the same as in the more simple case of European exercise.
End of explanation
"""


put_ame.present_value()

"""
Explanation: The only difference to consider here is that for American options where exercise can take place at any time before maturity, the inner value of the option (payoff of immediate exercise) is relevant over the whole set of dates. Therefore, maturity_value needs to be replaced by instrument_values in the definition of the payoff function.
End of explanation
"""


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)

"""
Explanation: Since DX Analytics relies on Monte Carlo simulation and other numerical methods, the calculation of the delta and vega of such an option is identical to the European exercise case.
End of explanation
"""


me.add_constant('model', 'gbm')

"""
Explanation: Portfolio Valuation
In general, market players (asset managers, investment banks, hedge funds, insurance companies, etc.) have to value not only single derivatvies instruments but rather portfolios composed of several derivatives instruments. A consistent derivatives portfolio valuation is particularly important when there are multiple derivatives written on the same risk factor and/or correlations between different risk factors.
These are the classes availble for a consistent portfolio valuation:

derivatives_position to model a portfolio position
derivatives_portfolio to model a derivatives portfolio

derivatives_position
We work with the market_environment object from before and add information about the risk factor model we are using.
End of explanation
"""


put = derivatives_position(
                name='put',  # name of position
                quantity=1,  # number of instruments
                underlyings=['gbm'],  # relevant risk factors
                mar_env=me,  # market environment
                otype='American single',  # the option type
                payoff_func='np.maximum(40. - instrument_values, 0)')
                    # the payoff funtion

"""
Explanation: A derivatives position consists of "data only" and not instantiated model or valuation objects. The necessary model and valuation objects are instantiated during the portfolio valuation.
End of explanation
"""


put.get_info()

"""
Explanation: The method get_info prints an overview of the all relevant information stored for the respective derivatives_position object.
End of explanation
"""


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')

"""
Explanation: derivatives_portfolio
The derivatives_portfolio class implements the core portfolio valuation tasks. This sub-section illustrates to cases, one with uncorrelated underlyings and another one with correlated underlyings
Uncorrelated Underlyings
The first example is based on a portfolio with two single-risk factor instruments on two different risk factors which are not correlated. In addition to the gbm object, we define a jump_diffusion object.
End of explanation
"""


call_jump = derivatives_position(
                name='call_jump',
                quantity=3,
                underlyings=['jd'],
                mar_env=me_jump,
                otype='European single',
                payoff_func='np.maximum(maturity_value - 36., 0)')

"""
Explanation: Based on this new risk factor model object, a European call option is defined.
End of explanation
"""


risk_factors = {'gbm': me, 'jd' : me_jump}
positions = {'put' : put, 'call_jump' : call_jump}

"""
Explanation: Our relevant market now takes on the following form (defined a dictionary objects):
End of explanation
"""


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)

"""
Explanation: To instantiate the derivatives_portfolio class, a valuation environment (instance of market_environment class) is needed.
End of explanation
"""


port = derivatives_portfolio(
            name='portfolio',  # name 
            positions=positions,  # derivatives positions
            val_env=val_env,  # valuation environment
            risk_factors=risk_factors, # relevant risk factors
            correlations=False,  # correlation between risk factors
            fixed_seed=False,  # fixed seed for randon number generation
            parallel=False)  # parallel valuation of portfolio positions

"""
Explanation: For the instantiation, we pass all the elements to the portfolio class.
End of explanation
"""


%%time
stats = port.get_statistics()

stats

"""
Explanation: Once instantiated, the method get_statistics provides major portfolio statistics like position values, position deltas ans position vegas.
End of explanation
"""


stats[['pos_value', 'pos_delta', 'pos_vega']].sum()

"""
Explanation: The method returns a standard pandas DataFrame object with which you can work as you are used to.
End of explanation
"""


%time port.get_values()

"""
Explanation: The metod get_values only calculates the present values of the derivatives instruments and positions and is therefore a bit less compute and time intensive.
End of explanation
"""


port.get_positions()

"""
Explanation: The method get_positions provides detailed information about the single derivatives positions of the derivatives_portfolio object.
End of explanation
"""


correlations = [['gbm', 'jd', 0.9]]

"""
Explanation: Correlated Underlyings
The second example case is exactly the same but now with a highly positive correlation between the two relevant risk factors. Correlations are to be provided as a list  of list objects using the risk factor model names to reference them.
End of explanation
"""


port = derivatives_portfolio(
            name='portfolio',
            positions=positions,
            val_env=val_env,
            risk_factors=risk_factors,
            correlations=correlations,
            fixed_seed=True,
            parallel=False)

port.get_statistics()

"""
Explanation: Except from now passing this new object, the application and usage remains the same.
End of explanation
"""


port.val_env.lists['cholesky_matrix']

"""
Explanation: The Cholesky matrix has been added to the valuation environment (which gets passed to the risk factor model objects).
End of explanation
"""


path_no = 0
paths1 = port.underlying_objects['gbm'].get_instrument_values()[:, path_no]
paths2 = port.underlying_objects['jd'].get_instrument_values()[:, path_no]

"""
Explanation: Let us pick two specific simulated paths, one for each risk factor, and let us visualize these.
End of explanation
"""


plt.figure(figsize=(10, 6))
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
# -- with a large jump for one risk factor

"""
Explanation: The plot illustrates that the two paths are indeed highly positively correlated. However, in this case a large jump occurs for the jump_diffusion object.
End of explanation
"""


