Spaces:
Sleeping
Sleeping
Bhanu Prasanna
commited on
Commit
·
4db4bdc
1
Parent(s):
e41e3ea
Update main.py
Browse files
main.py
CHANGED
@@ -72,201 +72,202 @@ if num_tick > 1:
|
|
72 |
com_data.dropna(inplace=True)
|
73 |
num_tick = len(com_data.columns)
|
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 |
-
"Portfolio Sharpe Ratio": sharpe_ratio,
|
125 |
-
},
|
126 |
-
index=[0],
|
127 |
-
)
|
128 |
|
129 |
-
|
130 |
-
"<h5 style='text-align: center;'>Random Weights Metrics</h5>",
|
131 |
-
unsafe_allow_html=True,
|
132 |
-
)
|
133 |
-
st.dataframe(metrics_df, use_container_width=True)
|
134 |
|
135 |
-
|
136 |
|
137 |
-
|
|
|
138 |
|
139 |
-
|
140 |
-
|
141 |
|
142 |
-
|
143 |
-
|
144 |
|
145 |
-
|
146 |
-
|
147 |
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
-
sharpe_arr = np.zeros(num_of_port)
|
153 |
|
154 |
-
|
|
|
|
|
|
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
weig = np.array(np.random.random(num_tick))
|
159 |
-
weig = weig / np.sum(weig)
|
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 |
-
st.markdown(
|
188 |
-
"<h4 style='text-align: center;'>Simulation Results</h4>",
|
189 |
-
unsafe_allow_html=True,
|
190 |
-
)
|
191 |
-
st.dataframe(sim_df.head(), use_container_width=True)
|
192 |
|
193 |
-
|
194 |
-
|
195 |
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
min_volatility_weights_df = pd.DataFrame(
|
214 |
-
data={
|
215 |
-
"company_name": com_sel_name_temp,
|
216 |
-
"random_weights": min_volatility["Portfolio Weights"],
|
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 |
-
|
|
|
72 |
com_data.dropna(inplace=True)
|
73 |
num_tick = len(com_data.columns)
|
74 |
|
75 |
+
if num_tick > 1:
|
76 |
+
com_sel_name_temp = []
|
77 |
+
for i in com_data.columns:
|
78 |
+
com_sel_name_temp.append(company_symbol_dict[i])
|
79 |
|
80 |
+
com_sel = com_data.columns.to_list()
|
81 |
+
com_sel_name.sort()
|
82 |
|
83 |
+
st.dataframe(com_data, use_container_width=True)
|
84 |
|
85 |
+
## Log-Return of Company Dataset
|
86 |
+
log_return = np.log(1 + com_data.pct_change())
|
87 |
|
88 |
+
## Generate Random Weights
|
89 |
+
rand_weig = np.array(np.random.random(num_tick))
|
90 |
|
91 |
+
## Rebalancing Random Weights
|
92 |
+
rebal_weig = rand_weig / np.sum(rand_weig)
|
93 |
|
94 |
+
## Calculate the Expected Returns, Annualize it by * 247.0
|
95 |
+
exp_ret = np.sum((log_return.mean() * rebal_weig) * 247)
|
96 |
|
97 |
+
## Calculate the Expected Volatility, Annualize it by * 247.0
|
98 |
+
exp_vol = np.sqrt(np.dot(rebal_weig.T, np.dot(log_return.cov() * 247, rebal_weig)))
|
99 |
|
100 |
+
## Calculate the Sharpe Ratio.
|
101 |
+
sharpe_ratio = exp_ret / exp_vol
|
102 |
|
103 |
+
# Put the weights into a data frame to see them better.
|
104 |
+
weights_df = pd.DataFrame(
|
105 |
+
data={
|
106 |
+
"company_name": com_sel_name_temp,
|
107 |
+
"random_weights": rand_weig,
|
108 |
+
"rebalance_weights": rebal_weig,
|
109 |
+
}
|
110 |
+
)
|
111 |
|
112 |
+
st.divider()
|
113 |
|
114 |
+
st.markdown(
|
115 |
+
"<h5 style='text-align: center;'>Random Portfolio Weights</h5>",
|
116 |
+
unsafe_allow_html=True,
|
117 |
+
)
|
118 |
+
st.dataframe(weights_df, use_container_width=True)
|
119 |
+
|
120 |
+
# Do the same with the other metrics.
|
121 |
+
metrics_df = pd.DataFrame(
|
122 |
+
data={
|
123 |
+
"Expected Portfolio Returns": exp_ret,
|
124 |
+
"Expected Portfolio Volatility": exp_vol,
|
125 |
+
"Portfolio Sharpe Ratio": sharpe_ratio,
|
126 |
+
},
|
127 |
+
index=[0],
|
128 |
+
)
|
129 |
|
130 |
+
st.markdown(
|
131 |
+
"<h5 style='text-align: center;'>Random Weights Metrics</h5>",
|
132 |
+
unsafe_allow_html=True,
|
133 |
+
)
|
134 |
+
st.dataframe(metrics_df, use_container_width=True)
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
st.divider()
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
## Let's get started with Monte Carlo Simulations
|
139 |
|
140 |
+
## How many times should we run Monte Carlo
|
141 |
+
num_of_port = 8000
|
142 |
|
143 |
+
## Create an Array to store the weights as they are generated
|
144 |
+
all_weights = np.zeros((num_of_port, num_tick))
|
145 |
|
146 |
+
## Create an Array to store the returns as they are generated
|
147 |
+
ret_arr = np.zeros(num_of_port)
|
148 |
|
149 |
+
## Create an Array to store the volatilities as they are generated
|
150 |
+
vol_arr = np.zeros(num_of_port)
|
151 |
|
152 |
+
## Create an Array to store the Sharpe Ratios as they are generated
|
153 |
+
sharpe_arr = np.zeros(num_of_port)
|
154 |
|
155 |
+
## Let's start the Monte Carlo Simulation
|
|
|
156 |
|
157 |
+
for ind in range(num_of_port):
|
158 |
+
## Let's first Calculate the Weights
|
159 |
+
weig = np.array(np.random.random(num_tick))
|
160 |
+
weig = weig / np.sum(weig)
|
161 |
|
162 |
+
## Append the Weights to Weigths array
|
163 |
+
all_weights[ind, :] = weig
|
|
|
|
|
164 |
|
165 |
+
## Calculate and Append the Expected Log Returns to Returns Array
|
166 |
+
ret_arr[ind] = np.sum((log_return.mean() * weig) * 247)
|
167 |
|
168 |
+
## Calculate and Append the Volatility to the Volatitlity Array
|
169 |
+
vol_arr[ind] = np.sqrt(np.dot(weig.T, np.dot(log_return.cov() * 247, weig)))
|
170 |
|
171 |
+
## Calculate and Append the Sharpe Ratio to Sharpe Ratio Array
|
172 |
+
sharpe_arr[ind] = ret_arr[ind] / vol_arr[ind]
|
173 |
|
174 |
+
## Let's create a Data Frame with Weights, Returns, Volatitlity, and the Sharpe Ratio
|
175 |
+
sim_data = [ret_arr, vol_arr, sharpe_arr, all_weights]
|
176 |
|
177 |
+
## Create a Data Frame using above, then Transpose it
|
178 |
+
sim_df = pd.DataFrame(data=sim_data).T
|
179 |
|
180 |
+
## Give the columns in Simulation Data Proper Names
|
181 |
+
sim_df.columns = ["Returns", "Volatility", "Sharpe Ratio", "Portfolio Weights"]
|
182 |
|
183 |
+
## Make sure the Data Types are correct in the Data Frame
|
184 |
+
sim_df = sim_df.infer_objects()
|
185 |
|
186 |
+
# Print out the results.
|
187 |
+
st.write("\n\n")
|
188 |
+
st.markdown(
|
189 |
+
"<h4 style='text-align: center;'>Simulation Results</h4>",
|
190 |
+
unsafe_allow_html=True,
|
191 |
+
)
|
192 |
+
st.dataframe(sim_df.head(), use_container_width=True)
|
193 |
|
194 |
+
# Return the Max Sharpe Ratio from the run.
|
195 |
+
max_sharpe_ratio = sim_df.loc[sim_df["Sharpe Ratio"].idxmax()]
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
+
# Return the Min Volatility from the run.
|
198 |
+
min_volatility = sim_df.loc[sim_df["Volatility"].idxmin()]
|
199 |
|
200 |
+
max_sharpe_weights_df = pd.DataFrame(
|
201 |
+
data={
|
202 |
+
"company_name": com_sel_name_temp,
|
203 |
+
"random_weights": max_sharpe_ratio["Portfolio Weights"],
|
204 |
+
}
|
205 |
+
)
|
206 |
|
207 |
+
st.markdown(
|
208 |
+
"<h5 style='text-align: center;'>Portfolio with Max Sharpe Ratio</h5>",
|
209 |
+
unsafe_allow_html=True,
|
210 |
+
)
|
211 |
+
st.dataframe(max_sharpe_ratio, use_container_width=True)
|
212 |
+
st.dataframe(max_sharpe_weights_df, use_container_width=True)
|
213 |
+
|
214 |
+
min_volatility_weights_df = pd.DataFrame(
|
215 |
+
data={
|
216 |
+
"company_name": com_sel_name_temp,
|
217 |
+
"random_weights": min_volatility["Portfolio Weights"],
|
218 |
+
}
|
219 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
+
st.markdown(
|
222 |
+
"<h5 style='text-align: center;'>Portfolio with Min Volatility</h5>",
|
223 |
+
unsafe_allow_html=True,
|
224 |
+
)
|
225 |
+
st.dataframe(min_volatility, use_container_width=True)
|
226 |
+
st.dataframe(min_volatility_weights_df, use_container_width=True)
|
227 |
|
228 |
+
st.divider()
|
229 |
|
230 |
+
st.markdown("<h1 style='text-align: center;'>Plotting</h1>", unsafe_allow_html=True)
|
231 |
|
232 |
+
fig = go.Figure(
|
233 |
+
data=go.Scatter(
|
234 |
+
x=sim_df["Volatility"],
|
235 |
+
y=sim_df["Returns"],
|
236 |
+
mode="markers",
|
237 |
+
marker=dict(color=sim_df["Sharpe Ratio"], colorscale="RdYlBu", size=10),
|
238 |
+
)
|
239 |
)
|
240 |
+
|
241 |
+
# Add color bar
|
242 |
+
fig.update_layout(coloraxis_colorbar=dict(title="Sharpe Ratio"))
|
243 |
+
|
244 |
+
# Add title and axis labels
|
245 |
+
fig.update_layout(
|
246 |
+
title="Portfolio Returns Vs. Risk",
|
247 |
+
xaxis=dict(title="Standard Deviation / Volatility"),
|
248 |
+
yaxis=dict(title="Returns"),
|
249 |
+
)
|
250 |
+
|
251 |
+
# Plot the Max Sharpe Ratio, using a `Red Star`.
|
252 |
+
fig.add_trace(
|
253 |
+
go.Scatter(
|
254 |
+
x=[max_sharpe_ratio[1]],
|
255 |
+
y=[max_sharpe_ratio[0]],
|
256 |
+
mode="markers",
|
257 |
+
marker=dict(color="red", symbol="star", size=20),
|
258 |
+
name="Max Sharpe Ratio",
|
259 |
+
)
|
260 |
)
|
261 |
+
|
262 |
+
# Plot the Min Volatility, using a `Blue Star`.
|
263 |
+
fig.add_trace(
|
264 |
+
go.Scatter(
|
265 |
+
x=[min_volatility[1]],
|
266 |
+
y=[min_volatility[0]],
|
267 |
+
mode="markers",
|
268 |
+
marker=dict(color="blue", symbol="star", size=20),
|
269 |
+
name="Min Volatility",
|
270 |
+
)
|
271 |
)
|
|
|
272 |
|
273 |
+
st.plotly_chart(fig, use_container_width=True)
|