upload shap_ratio.py (#18)
Browse files- upload shap_ratio.py (16e0161e8aea1ef924f8c04d65b8eca56e4702b2)
Co-authored-by: handepehlivan <handepeh@users.noreply.huggingface.co>
- sharp_ratio.py +150 -0
sharp_ratio.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from datetime import datetime
|
4 |
+
import streamlit as st
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import plotly.express as px
|
7 |
+
#import plotly.graph_objects as go
|
8 |
+
|
9 |
+
def preprocess(stocks,choices):
|
10 |
+
symbols, weights, investment = choices.values()
|
11 |
+
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close")
|
12 |
+
print('stocks',stocks)
|
13 |
+
logRet = np.log(stocks/stocks.shift())
|
14 |
+
log_returns = np.log(stocks/stocks.shift())
|
15 |
+
tickers_list = symbols.copy()
|
16 |
+
weights_list = weights.copy()
|
17 |
+
return logRet,tickers_list,weights_list
|
18 |
+
|
19 |
+
def cumulative_return(stocks,choices):
|
20 |
+
symbols, weights, investment = choices.values()
|
21 |
+
logRet,tickers_list,weights_list = preprocess(stocks,choices)
|
22 |
+
tkers = sorted(set(stocks['Ticker'].unique()))
|
23 |
+
|
24 |
+
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close")
|
25 |
+
|
26 |
+
stock_port = {}
|
27 |
+
for e in tickers_list: stock_port[e] = 0
|
28 |
+
# Convert Weights to Floats and Sum
|
29 |
+
weights = [float(x) for x in weights_list]
|
30 |
+
s = sum(weights)
|
31 |
+
# Calc Weight Proportions
|
32 |
+
new_weights = []
|
33 |
+
for i in weights: new_weights.append(i/s)
|
34 |
+
# Assign Weights to Ticker Dict
|
35 |
+
i = 0
|
36 |
+
for e in stock_port:
|
37 |
+
stock_port[e] = new_weights[i]
|
38 |
+
i += 1
|
39 |
+
|
40 |
+
port = dict.fromkeys(tkers, 0)
|
41 |
+
port.update(stock_port)
|
42 |
+
|
43 |
+
portfolio_dict = port
|
44 |
+
|
45 |
+
for e in portfolio_dict:
|
46 |
+
tmp = 0
|
47 |
+
if portfolio_dict[e] > tmp:
|
48 |
+
tmp = portfolio_dict[e]
|
49 |
+
tick = e
|
50 |
+
list_ =[]
|
51 |
+
for e in tickers_list:
|
52 |
+
if e not in list_:
|
53 |
+
list_.append(e)
|
54 |
+
|
55 |
+
df = stocks[list_]
|
56 |
+
df = df/df.iloc[0]
|
57 |
+
df.reset_index(inplace=True)
|
58 |
+
df=pd.DataFrame(df)
|
59 |
+
print(df)
|
60 |
+
fig = px.line(df, x='Date' ,y=df.columns[1:,])
|
61 |
+
|
62 |
+
|
63 |
+
#layout reference = https://linuxtut.com/en/b13e3e721519c2842cc9/
|
64 |
+
fig.update_layout(
|
65 |
+
xaxis=dict(
|
66 |
+
rangeselector=dict(
|
67 |
+
buttons=list([
|
68 |
+
dict(count=1,
|
69 |
+
label="1m",
|
70 |
+
step="month",
|
71 |
+
stepmode="backward"),
|
72 |
+
dict(count=6,
|
73 |
+
label="6m",
|
74 |
+
step="month",
|
75 |
+
stepmode="backward"),
|
76 |
+
dict(count=1,
|
77 |
+
label="YTD",
|
78 |
+
step="year",
|
79 |
+
stepmode="todate"),
|
80 |
+
dict(count=1,
|
81 |
+
label="1y",
|
82 |
+
step="year",
|
83 |
+
stepmode="backward"),
|
84 |
+
dict(step="all")
|
85 |
+
])
|
86 |
+
),
|
87 |
+
rangeslider=dict(
|
88 |
+
visible=True
|
89 |
+
),
|
90 |
+
type="date"
|
91 |
+
)
|
92 |
+
)
|
93 |
+
fig.update_layout(xaxis=dict(rangeselector = dict(font = dict( color = "black"))))
|
94 |
+
st.subheader('Portfolio Historical Normalized Cumulative Returns')
|
95 |
+
|
96 |
+
st.plotly_chart(fig, use_container_width=True)
|
97 |
+
|
98 |
+
def sharp_ratio_func(stocks,choices):
|
99 |
+
symbols, weights, investment = choices.values()
|
100 |
+
logRet,tickers_list,weights_list = preprocess(stocks,choices)
|
101 |
+
tkers = sorted(set(stocks['Ticker'].unique()))
|
102 |
+
|
103 |
+
stocks = stocks.pivot(index="Date", columns="Ticker", values="Adj. Close")
|
104 |
+
|
105 |
+
stock_port = {}
|
106 |
+
for e in tickers_list: stock_port[e] = 0
|
107 |
+
# Convert Weights to Floats and Sum
|
108 |
+
weights = [float(x) for x in weights_list]
|
109 |
+
s = sum(weights)
|
110 |
+
# Calc Weight Proportions
|
111 |
+
new_weights = []
|
112 |
+
for i in weights: new_weights.append(i/s)
|
113 |
+
# Assign Weights to Ticker Dict
|
114 |
+
i = 0
|
115 |
+
for e in stock_port:
|
116 |
+
stock_port[e] = new_weights[i]
|
117 |
+
i += 1
|
118 |
+
|
119 |
+
port = dict.fromkeys(tkers, 0)
|
120 |
+
port.update(stock_port)
|
121 |
+
|
122 |
+
portfolio_dict = port
|
123 |
+
|
124 |
+
sharp_ratio_list = []
|
125 |
+
for ticker in symbols:
|
126 |
+
logRet = np.log(stocks/stocks.shift())
|
127 |
+
stk = dict.fromkeys(tkers, 0)
|
128 |
+
stkTicker = {ticker:1}
|
129 |
+
stk.update(stkTicker)
|
130 |
+
ttlStk = np.sum(logRet*stk, axis=1)
|
131 |
+
stock_sharpe_ratio = ttlStk.mean() / ttlStk.std()
|
132 |
+
sharp_ratio_list.append(stock_sharpe_ratio)
|
133 |
+
|
134 |
+
sharp_ratio = {'Assets': symbols, 'Sharpe Ratio': sharp_ratio_list}
|
135 |
+
|
136 |
+
# Portfolio sharp Ratio Calculation
|
137 |
+
logRet = np.log(stocks/stocks.shift())
|
138 |
+
portfolio = dict.fromkeys(tkers, 0)
|
139 |
+
portfolio.update(portfolio_dict)
|
140 |
+
totalPortfolio = np.sum(logRet*portfolio, axis=1)
|
141 |
+
portfolio_sharpe_ratio = totalPortfolio.mean() / totalPortfolio.std()
|
142 |
+
|
143 |
+
sharp_ratio['Assets'].append('Portfolio')
|
144 |
+
sharp_ratio['Sharpe Ratio'].append(portfolio_sharpe_ratio)
|
145 |
+
|
146 |
+
fig = px.bar(sharp_ratio, x='Assets', y="Sharpe Ratio",color='Assets')
|
147 |
+
fig.update_layout(title_text = 'Sharpe Ratio of the Assets and Portfolio',
|
148 |
+
title_x=0.458)
|
149 |
+
st.plotly_chart(fig, use_container_width=True)
|
150 |
+
|