generated from FinTechIntro/2024-Spring-HW3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markowitz_2.py
286 lines (230 loc) · 8.38 KB
/
Markowitz_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Package Import
"""
import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import quantstats as qs
import gurobipy as gp
import warnings
import argparse
from scipy.optimize import minimize
"""
Project Setup
"""
warnings.simplefilter(action="ignore", category=FutureWarning)
assets = [
"SPY",
"XLB",
"XLC",
"XLE",
"XLF",
"XLI",
"XLK",
"XLP",
"XLRE",
"XLU",
"XLV",
"XLY",
]
data = pd.DataFrame()
# Fetch the data for each stock and concatenate it to the `data` DataFrame
for asset in assets:
raw = yf.download(asset, start="2012-01-01", end="2024-04-01")
raw["Symbol"] = asset
data = pd.concat([data, raw], axis=0)
# Initialize df and df_returns
Bdf = portfolio_data = data.pivot_table(
index="Date", columns="Symbol", values="Adj Close"
)
df = Bdf.loc["2019-01-01":"2024-04-01"]
"""
Strategy Creation
Create your own strategy, you can add parameter but please remain "price" and "exclude" unchanged
"""
class MyPortfolio:
"""
NOTE: You can modify the initialization function
"""
def __init__(self, price, exclude, lookback=50, gamma=0):
self.price = price
self.returns = price.pct_change().fillna(0)
self.exclude = exclude
self.lookback = lookback
self.gamma = gamma
def calculate_weights(self):
# Get the assets by excluding the specified column
assets = self.price.columns[self.price.columns != self.exclude]
# Calculate the rolling returns for the lookback period
rolling_returns = self.price[assets].pct_change(self.lookback)
# Calculate the mean returns and covariance matrix for the lookback period
mean_returns = rolling_returns.mean()
cov_matrix = rolling_returns.cov()
# Define the objective function for mean-variance optimization
def objective(weights):
return - (weights @ mean_returns) / np.sqrt(weights @ cov_matrix @ weights)
# Define the constraints and bounds
constraints = ({'type': 'eq', 'fun': lambda weights: np.sum(weights) - 1})
bounds = [(0, 1) for _ in assets]
# Initial guess for the weights
initial_weights = np.array([1/len(assets)] * len(assets))
# Solve the optimization problem
result = minimize(objective, initial_weights, method='SLSQP', bounds=bounds, constraints=constraints)
# Get the optimized weights
weights = result.x
# Convert the weights to a DataFrame
self.portfolio_weights = pd.DataFrame(index=self.price.index, columns=self.price.columns)
self.portfolio_weights.loc[:, assets] = weights
self.portfolio_weights[self.exclude] = 0 # Set exclude column weights to 0
# Ensure the weights are filled forward for all time periods
self.portfolio_weights.ffill(inplace=True)
self.portfolio_weights.fillna(0, inplace=True)
def calculate_portfolio_returns(self):
# Ensure weights are calculated
if not hasattr(self, "portfolio_weights"):
self.calculate_weights()
# Calculate the portfolio returns
self.portfolio_returns = self.returns.copy()
assets = self.price.columns[self.price.columns != self.exclude]
self.portfolio_returns["Portfolio"] = (
self.portfolio_returns[assets]
.mul(self.portfolio_weights[assets])
.sum(axis=1)
)
def get_results(self):
# Ensure portfolio returns are calculated
if not hasattr(self, "portfolio_returns"):
self.calculate_portfolio_returns()
return self.portfolio_weights, self.portfolio_returns
"""
Assignment Judge
The following functions will help check your solution.
"""
class AssignmentJudge:
def __init__(self):
self.mp = MyPortfolio(df, "SPY").get_results()
self.Bmp = MyPortfolio(Bdf, "SPY").get_results()
def plot_performance(self, price, strategy):
# Plot cumulative returns
_, ax = plt.subplots()
returns = price.pct_change().fillna(0)
(1 + returns["SPY"]).cumprod().plot(ax=ax, label="SPY")
(1 + strategy[1]["Portfolio"]).cumprod().plot(ax=ax, label=f"MyPortfolio")
ax.set_title("Cumulative Returns")
ax.set_xlabel("Date")
ax.set_ylabel("Cumulative Returns")
ax.legend()
plt.show()
return None
def plot_allocation(self, df_weights):
df_weights = df_weights.fillna(0).ffill()
# long only
df_weights[df_weights < 0] = 0
# Plotting
_, ax = plt.subplots()
df_weights.plot.area(ax=ax)
ax.set_xlabel("Date")
ax.set_ylabel("Allocation")
ax.set_title("Asset Allocation Over Time")
plt.show()
return None
def report_metrics(self, price, strategy, show=False):
df_bl = pd.DataFrame()
returns = price.pct_change().fillna(0)
df_bl["SPY"] = returns["SPY"]
df_bl[f"MP"] = pd.to_numeric(strategy[1]["Portfolio"], errors="coerce")
qs.reports.metrics(df_bl, mode="full", display=show)
sharpe_ratio = qs.stats.sharpe(df_bl)
return sharpe_ratio
def cumulative_product(self, dataframe):
(1 + dataframe.pct_change().fillna(0)).cumprod().plot()
def check_sharp_ratio_greater_than_one(self):
if not self.check_portfolio_position(self.mp[0]):
return 0
print(self.report_metrics(Bdf, self.Bmp)[0])
print(self.report_metrics(Bdf, self.Bmp)[1])
if self.report_metrics(df, self.mp)[1] > 1:
print("Problem 4.1 Success - Get 10 points")
return 10
else:
print("Problem 4.1 Fail")
return 0
def check_sharp_ratio_greater_than_spy(self):
if not self.check_portfolio_position(self.mp[0]):
return 0
print(self.report_metrics(Bdf, self.Bmp)[0])
if (
self.report_metrics(Bdf, self.Bmp)[1]
> self.report_metrics(Bdf, self.Bmp)[0]
):
print("Problem 4.2 Success - Get 10 points")
return 10
else:
print("Problem 4.2 Fail")
return 0
def check_portfolio_position(self, portfolio_weights):
if (portfolio_weights.sum(axis=1) <= 1.01).all():
return True
print("Portfolio Position Exceeds 1. No Leverage.")
return False
def check_all_answer(self):
score = 0
score += self.check_sharp_ratio_greater_than_one()
score += self.check_sharp_ratio_greater_than_spy()
return score
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Introduction to Fintech Assignment 3 Part 12"
)
parser.add_argument(
"--score",
action="append",
help="Score for assignment",
)
parser.add_argument(
"--allocation",
action="append",
help="Allocation for asset",
)
parser.add_argument(
"--performance",
action="append",
help="Performance for portfolio",
)
parser.add_argument(
"--report", action="append", help="Report for evaluation metric"
)
parser.add_argument(
"--cumulative", action="append", help="Cumulative product result"
)
args = parser.parse_args()
judge = AssignmentJudge()
if args.score:
if ("one" in args.score) or ("spy" in args.score):
if "one" in args.score:
judge.check_sharp_ratio_greater_than_one()
if "spy" in args.score:
judge.check_sharp_ratio_greater_than_spy()
elif "all" in args.score:
print(f"==> totoal Score = {judge.check_all_answer()} <==")
if args.allocation:
if "mp" in args.allocation:
judge.plot_allocation(judge.mp[0])
if "bmp" in args.allocation:
judge.plot_allocation(judge.Bmp[0])
if args.performance:
if "mp" in args.performance:
judge.plot_performance(df, judge.mp)
if "bmp" in args.performance:
judge.plot_performance(Bdf, judge.Bmp)
if args.report:
if "mp" in args.report:
judge.report_metrics(df, judge.mp, show=True)
if "bmp" in args.report:
judge.report_metrics(Bdf, judge.Bmp, show=True)
if args.cumulative:
if "mp" in args.cumulative:
judge.cumulative_product(df)
if "bmp" in args.cumulative:
judge.cumulative_product(Bdf)