Assignment 2 Computational Part
Assignment 2 Computational Part
pd.set_option('display.max_columns', 500)
%matplotlib inline
data_path = os.getcwd()+'\\'
files = os.listdir(data_path)
data_path
#files
In [10]: name='^GSPTSE'
tickers=[name]
folder= data_path
nosuccess=[]
for ticker in tickers:
print('Working on: ', ticker)
try:
dfprice=Ticker(ticker)
df=dfprice.history(period='5y',interval='1d')
thefile=folder+ticker+'.csv'
df.to_csv(thefile)
print(ticker+' has been saved to: '+folder)
except FileNotFoundError:
nosuccess.append(ticker)
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 1/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
2019-
0 ^GSPTSE 16347.299805 16409.099609 16308.700195 16379.900391 168389300 1
10-09
2019-
1 ^GSPTSE 16375.400391 16444.400391 16358.099609 16422.699219 186014600 1
10-10
2019-
2 ^GSPTSE 16487.199219 16516.699219 16413.400391 16415.199219 200746600 1
10-11
2019-
3 ^GSPTSE 16433.699219 16510.699219 16417.800781 16418.400391 212333000 1
10-15
2019-
4 ^GSPTSE 16435.099609 16445.599609 16408.699219 16427.199219 165968300 1
10-16
2020-
95 ^GSPTSE 17140.900391 17304.599609 17029.900391 17041.900391 303632000 1
02-26
2020-
96 ^GSPTSE 16803.400391 16816.199219 16456.800781 16717.400391 232685900 1
02-27
2020-
97 ^GSPTSE 16184.400391 16276.900391 15896.400391 16263.099609 590219300 1
02-28
2020-
98 ^GSPTSE 16325.000000 16566.699219 16166.299805 16553.300781 374721500 1
03-02
2020-
99 ^GSPTSE 16674.900391 16798.199219 16378.299805 16423.599609 393127900 1
03-03
In [40]: raw_data.isnull().values.any()
Out[40]: False
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 2/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
In [41]: pd.isnull(raw_data).count()
In [42]: raw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1256 entries, 0 to 1255
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 symbol 1256 non-null object
1 date 1256 non-null object
2 open 1256 non-null float64
3 high 1256 non-null float64
4 low 1256 non-null float64
5 close 1256 non-null float64
6 volume 1256 non-null int64
7 adjclose 1256 non-null float64
dtypes: float64(5), int64(1), object(2)
memory usage: 78.6+ KB
In [51]: data=pd.DataFrame(raw_data[['date','adjclose']])
data.columns=['date', 'adjclose']
data.drop(1255, inplace= True)
data
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 3/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
0 2019-10-09 16379.900391
1 2019-10-10 16422.699219
2 2019-10-11 16415.199219
3 2019-10-15 16418.400391
4 2019-10-16 16427.199219
data
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 4/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 5/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
In [68]: print("The standard deviation of the log returns is: " + str(statistics.stdev(data[
Now let´s find the annualized values for the mean and standard deviation (drift and volatility)
In [70]: print("The annualized mean of the log returns is: " + str((252/1254)*sum(data['log_
In [74]: print("The annualized standard deviation of the log returns is: " + str((math.sqrt
Bonus
Ussing the code seen in the python labs
"""
Stores common attributes of a stock option
"""
class StockOption(object):
def __init__(
self, S0, K, r=0.05, T=1, N=2, pu=0, pd=0,
div=0, sigma=0, is_put=False, is_am=False):
"""
Initialize the stock option base class.
Defaults to European call unless specified.
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 6/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
self.r = r
self.T = T
self.N = max(1, N)
self.STs = [] # Declare the stock prices tree
@property
def dt(self):
""" Single time step, in years """
return self.T/float(self.N)
@property
def df(self):
""" The discount factor """
return math.exp(-(self.r-self.div)*self.dt)
"""
Price a European option by the binomial tree model
"""
class BinomialEuropeanOption(StockOption):
def setup_parameters(self):
# Required calculations for the model
self.M = self.N+1 # Number of terminal nodes of tree
self.u = 1+self.pu # Expected value in the up state
self.d = 1-self.pd # Expected value in the down state
self.qu = (math.exp(
(self.r-self.div)*self.dt)-self.d)/(self.u-self.d)
self.qd = 1-self.qu # Probability up is qu and down dq
def init_stock_price_tree(self):
# Initialize terminal price nodes to zeros
self.STs = np.zeros(self.M)
def init_payoffs_tree(self):
"""
Returns the payoffs when the option
expires at terminal nodes
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 7/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
"""
if self.is_call:
return np.maximum(0, self.STs-self.K)
else:
return np.maximum(0, self.K-self.STs)
return payoffs
def begin_tree_traversal(self):
payoffs = self.init_payoffs_tree()
return self.traverse_tree(payoffs)
def price(self):
""" Entry point of the pricing implementation """
self.setup_parameters()
self.init_stock_price_tree()
payoffs = self.begin_tree_traversal()
In [103… (78*math.exp(0.3*math.sqrt(1/6))-80)/80
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 8/9
9/10/24, 8:21 p.m. FM 9587 Rodrigo Gonzalez Assignment 2
Out[103… 0.10203302569827084
In [109… (80-78*math.exp(-0.3*math.sqrt(1/6)))/78*math.exp(-0.3*math.sqrt(1/6))
Out[109… 0.12466934483941076
localhost:8985/doc/tree/Documents/2024/Western/Fall/Mathematics of Financial Options/Assignment 2/FM 9587 Rodrigo Gonzalez Assignment 2.ipynb 9/9