Python for Finance: Analyze Big Financial Data

(Elle) #1

European Exercise


The first case to which we want to specialize the generic valuation class is European


exercise. To this end, consider the following simplified recipe to generate a Monte Carlo


estimator for an option value:


1 . Simulate the relevant underlying risk factor S under the risk-neutral measure I times


to come up with as many simulated values of the underlying at the maturity of the


option T — i.e.,


2 . Calculate the payoff hT of the option at maturity for every simulated value of the


underlying — i.e.,


3 . Derive the Monte Carlo estimator for the option’s present value as


The Valuation Class


Example 17-2 shows the class implementing the present_value method based on this


recipe. In addition, it contains the method generate_payoff to generate the simulated


paths and the payoff of the option given the simulated paths. This, of course, builds the


very basis for the Monte Carlo estimator.


Example 17-2. Valuation class for European exercise



DX Library Valuation


valuation_mcs_european.py



import numpy as np


from valuation_class import valuation_class


class valuation_mcs_european(valuation_class):
”’ Class to value European options with arbitrary payoff
by single-factor Monte Carlo simulation.


            Methods
=======
generate_payoff :
returns payoffs given the paths and the payoff function
present_value :
returns present value (Monte Carlo estimator)
”’

def generate_payoff(self, fixed_seed=False):
”’
Parameters
==========
fixed_seed : Boolean
use same/fixed seed for valuation
”’
try:


strike defined?


strike = self.strike
except:
pass
paths = self.underlying.get_instrument_values(fixed_seed=fixed_seed)
time_grid = self.underlying.time_grid
try:
time_index = np.where(time_grid == self.maturity)[ 0 ]
time_index = int(time_index)
except:
print “Maturity date not in time grid of underlying.”

Free download pdf