程序代写 GR5260 Class5 – Jupyter Notebook

2/19/22, 3:33 PM GR5260 Class5 – Jupyter Notebook
# class definition
class MyClass(object):
x = 13 # class attribute (‘static’, not truly static) # constructor

Copyright By PowCoder代写 加微信 powcoder

def __init__(self, y):
print(‘__init_ is called’, y) self.y = y # class attribute
def method1(self, a):
print(‘method1 is called by ‘, a)
__init_ is called 1.2
__init_ is called 2
# instance of MyClass
c = MyClass(1.2)
c.y, c.x, MyClass.x
(1.2, 13, 13)
method1 is called by me
c.method1(‘me’)
c.x, MyClass.x
# encapsulation: private vs public (vs protected) # private: __xxx
#class MyClass(object):
class MyClass:
__x = 13 # private class attribute (only accessible within this class) x1 = 23
# constructor
def __init__(self, y, w=1.0):
print(‘__init_ is called’, y)
self.y = y # class attribute
self.__w = w
self.__w__ = w + y # private but accessible to public
def __init__(self):
print(‘__init_ is called’)
def method1(self, a):
print(‘method1 is called by ‘, a)
def __method1(self, a): # private method print(‘__method1 is called by ‘, a)
c1 = MyClass(2,3)
localhost:8888/notebooks/2022_GR5260/GR5260 Class5.ipynb

2/19/22, 3:33 PM GR5260 Class5 – Jupyter Notebook
__init_ is called 1
__init_ is called 5
#c1.__method(‘me’) error
c1._MyClass__x
a = MyClass(1,4)
b = MyClass(5,6)
MyClass.x1 = 30
a.x1, b.x1
# inheritance
class MySubClass(MyClass):
def __init__(self, y, w, z):
super().__init__(y, w) self.z = z
c2 = MySubClass(1,2,3)
__init_ is called 1
# Example: define classes for different trade types
# base: equity trade position
localhost:8888/notebooks/2022_GR5260/GR5260 Class5.ipynb

2/19/22, 3:33 PM GR5260 Class5 – Jupyter Notebook
import datetime as dt import numpy as np
class Market:
def __init__(self, date, spot_price, vol=0.0, zero_rate=0.0, div_rate=0.0):
self.date = date self.spot_price = spot_price self.vol = vol self.zero_rate = zero_rate self.div_rate = div_rate
class Trade:
def __init__(self, name, side, quantity): # more attributes
self.name = name
self.side = side # long: +1, short: -1 self.quantity = quantity
def value(self, mkt):
if not isinstance(mkt, Market):
raise TypeError(‘Input must be an instance of Market’) return self.side*self.quantity*mkt.spot_price
def delta(self, mkt):
if not isinstance(mkt, Market):
raise TypeError(‘Input must be an instance of Market’) return self.side*self.quantity
def scen_values(self, mkt, spot_prices):
vals = self.side*self.quantity*spot_prices return vals
# class exercise: define a subclass EuropeanCall(Trade) for European call option on e
from scipy import stats
# from class notes 3
def BS_option_price(S0, K, T, r, q, v):
# can add checks on inputs
d1 = (np.log(S0/K) + (r – q + v**2 /2)*T) / (v*np.sqrt(T))
d2 = d1 – v*np.sqrt(T)
price = S0*np.exp(-q*T)*stats.norm.cdf(d1) – K*np.exp(-r*T)*stats.norm.cdf(d2) return price
localhost:8888/notebooks/2022_GR5260/GR5260 Class5.ipynb

2/19/22, 3:33 PM GR5260 Class5 – Jupyter Notebook
class EuropeanCall(Trade):
def __init__(self, name, side, quantity, strike, expiry_date):
Trade.__init__(self, name, side, quantity) self.strike = strike
self.expiry_date = expiry_date
def value(self, mkt):
t = (self.expiry_date – mkt.date).days / 365
val = BS_option_price(mkt.spot_price, self.strike, t,
mkt.zero_rate, mkt.div_rate, mkt.vol) return self.side*self.quantity*val
def delta(self, mkt):
if not isinstance(mkt, Market):
raise TypeError(‘Input must be an instance of Market’) t = (self.expiry_date – mkt.date).days / 365
d1 = (np.log(mkt.spot_price/self.strike) +
(mkt.zero_rate – mkt.div_rate + mkt.vol**2 /2)*t) / (mkt.vol*np.sqrt(t) return self.side*self.quantity*stats.norm.cdf(d1)
def scen_values(self, mkt, spot_prices):
t = (self.expiry_date – mkt.date).days / 365
vals = BS_option_price(spot_prices, self.strike, t,
mkt.zero_rate, mkt.div_rate, mkt.vol) return self.side*self.quantity*vals
localhost:8888/notebooks/2022_GR5260/GR5260 Class5.ipynb

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com