Python for Finance: Analyze Big Financial Data

(Elle) #1

Derivatives Portfolios


From a portfolio perspective, a “relevant market” is mainly composed of the relevant risk


factors (underlyings) and their correlations, as well as the derivatives and derivatives


positions, respectively, to be valued. Theoretically, we are now dealing with a general


market model ℳ as defined in Chapter 15, and applying the Fundamental Theorem of


Asset Pricing (with its corollaries) to it.


[ 80 ]

The Class


A somewhat complex Python class implementing a portfolio valuation based on the


Fundamental Theorem of Asset Pricing — taking into account multiple relevant risk


factors and multiple derivatives positions — is presented as Example 18-2. The class is


rather comprehensively documented inline, especially during passages that implement


functionality specific to the purpose at hand.


Example 18-2. A class to value a derivatives portfolio



DX Library Portfolio


derivatives_portfolio.py



import numpy as np
import pandas as pd


from dx_valuation import *


models available for risk factor modeling


models = {‘gbm’ : geometric_brownian_motion,
‘jd’ : jump_diffusion,
‘srd’ : square_root_diffusion}


allowed exercise types


otypes = {‘European’ : valuation_mcs_european,
‘American’ : valuation_mcs_american}


class derivatives_portfolio(object):
”’ Class for building portfolios of derivatives positions.


            Attributes
==========
name : str
name of the object
positions : dict
dictionary of positions (instances of derivatives_position class)
val_env : market_environment
market environment for the valuation
assets : dict
dictionary of market environments for the assets
correlations : list
correlations between assets
fixed_seed : Boolean
flag for fixed rng seed

Methods
=======
get_positions :
prints information about the single portfolio positions
get_statistics :
returns a pandas DataFrame object with portfolio statistics
”’

def init(self, name, positions, val_env, assets,
correlations=None, fixed_seed=False):
self.name = name
self.positions = positions
self.val_env = val_env

Free download pdf