Python for Finance: Analyze Big Financial Data

(Elle) #1

Generic Simulation Class


Object-oriented modeling — as introduced in Chapter 13 — allows inheritance of


attributes and methods. This is what we want to make use of when building our simulation


classes: we start with a generic simulation class containing those attributes and methods


that all other simulation classes share.


To begin with, it is noteworthy that we instantiate an object of any simulation class by


“only” providing three attributes:


name

A string object as a name for the model simulation object


mar_env

An instance of the market_environment class


corr

A flag (bool) indicating whether the object is correlated or not


This again illustrates the role of a market environment: to provide in a single step all data


and objects required for simulation and valuation. The methods of the generic class are:


generate_time_grid

This method generates the time grid of relevant dates used for the simulation; this


task is the same for every simulation class.


get_instrument_values

Every simulation class has to return the ndarray object with the simulated instrument


values (e.g., simulated stock prices, commodities prices, volatilities).


Example 16-2 presents such a generic model simulation class. The methods make use of


other methods that the model-tailored classes will provide, like self.generate_paths. All


details in this regard will become clear when we have the full picture of a specialized,


nongeneric simulation class.


Example 16-2. Generic financial model simulation class



DX Library Simulation


simulation_class.py



import numpy as np
import pandas as pd


class simulation_class(object):
”’ Providing base methods for simulation classes.


            Attributes
==========
name : string
name of the object
mar_env : instance of market_environment
market environment data for simulation
corr : Boolean
True if correlated with other model object

Methods
=======
Free download pdf