update plot.py (#10)
Browse files- update plot.py (b53e87a56aa9dd6875c948bb70e2f13047ec2060)
Co-authored-by: handepehlivan <handepeh@users.noreply.huggingface.co>
plots.py
CHANGED
@@ -53,7 +53,7 @@ def beta(stock_df, choices):
|
|
53 |
|
54 |
def ER(stock_df, choices):
|
55 |
symbols, weights, investment = choices.values()
|
56 |
-
symbols_ =symbols
|
57 |
tickers = symbols
|
58 |
tickers.append('sp500')
|
59 |
#print(tickers)
|
@@ -98,11 +98,11 @@ def ER(stock_df, choices):
|
|
98 |
# print('Expected Return based on CAPM for {} is {}%'.format(i, ER_[i]))
|
99 |
#print(ER)
|
100 |
symbols.remove('sp500')
|
101 |
-
st.subheader('Expected Annual Return Based on CAPM Model')
|
102 |
|
103 |
-
Expected_return = {'Assets':
|
104 |
# Creates a header for streamlit
|
105 |
-
st.dataframe(Expected_return)
|
106 |
|
107 |
|
108 |
# calculate expected return for the portfolio
|
@@ -123,13 +123,20 @@ def ER(stock_df, choices):
|
|
123 |
portfolio_weights = (portfolio_weights / total_portfolio_value)*100
|
124 |
ER_portfolio= []
|
125 |
ER_portfolio = sum(list(ER_) * portfolio_weights)/100
|
126 |
-
print(
|
127 |
|
128 |
-
st.subheader('Expected Portfolio Return Based on CAPM Model')
|
129 |
# Creates a header for streamlit
|
130 |
-
st.write('Expected Portfolio Return is:', ER_portfolio)
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
def basic_portfolio(stock_df):
|
135 |
"""Uses the stock dataframe to graph the normalized historical cumulative returns of each asset.
|
@@ -145,24 +152,23 @@ def basic_portfolio(stock_df):
|
|
145 |
st.line_chart(cumulative_return)
|
146 |
|
147 |
|
148 |
-
def display_heat_map(stock_df):
|
149 |
-
|
150 |
-
|
151 |
# Calcuilates the correlation of the assets in the portfolio
|
152 |
-
price_correlation =
|
153 |
|
154 |
# Creates the title for streamlit
|
155 |
st.subheader('Heatmap Showing Correlation Of Assets')
|
156 |
# Generates a figure for the heatmap
|
157 |
fig, ax = plt.subplots()
|
158 |
-
|
159 |
# Displays the heatmap on streamlit
|
160 |
st.write(fig)
|
161 |
# Creates a header for the correlation data
|
162 |
-
st.subheader('Correlation Data')
|
163 |
# Displays the correlation data on streamlit
|
164 |
-
st.dataframe(price_correlation)
|
165 |
-
|
166 |
|
167 |
#def display_portfolio_return(stock_df, choices):
|
168 |
"""Uses the stock dataframe and the chosen weights from choices to calculate and graph the historical cumulative portfolio return.
|
|
|
53 |
|
54 |
def ER(stock_df, choices):
|
55 |
symbols, weights, investment = choices.values()
|
56 |
+
symbols_ =symbols.copy()
|
57 |
tickers = symbols
|
58 |
tickers.append('sp500')
|
59 |
#print(tickers)
|
|
|
98 |
# print('Expected Return based on CAPM for {} is {}%'.format(i, ER_[i]))
|
99 |
#print(ER)
|
100 |
symbols.remove('sp500')
|
101 |
+
#st.subheader('Expected Annual Return Based on CAPM Model')
|
102 |
|
103 |
+
Expected_return = {'Assets': symbols_, 'Expected Annual Return': ER_}
|
104 |
# Creates a header for streamlit
|
105 |
+
#st.dataframe(Expected_return)
|
106 |
|
107 |
|
108 |
# calculate expected return for the portfolio
|
|
|
123 |
portfolio_weights = (portfolio_weights / total_portfolio_value)*100
|
124 |
ER_portfolio= []
|
125 |
ER_portfolio = sum(list(ER_) * portfolio_weights)/100
|
126 |
+
#print(ER_portfolio)
|
127 |
|
128 |
+
#st.subheader('Expected Portfolio Return Based on CAPM Model')
|
129 |
# Creates a header for streamlit
|
130 |
+
#st.write('Expected Portfolio Return is:', ER_portfolio)
|
131 |
+
Bar_output = Expected_return.copy()
|
132 |
+
Bar_output['Assets'].append('Portfolio')
|
133 |
+
Bar_output['Expected Annual Return'].append(ER_portfolio)
|
134 |
+
fig = px.bar(Bar_output, x='Assets', y="Expected Annual Return",color='Assets')
|
135 |
+
fig.update_layout(title_text = 'Annual Expected Return of the Assets and Portfolio',
|
136 |
+
title_x=0.458)
|
137 |
+
st.plotly_chart(fig, use_container_width=True)
|
138 |
+
|
139 |
+
return beta, cash_value_stocks
|
140 |
|
141 |
def basic_portfolio(stock_df):
|
142 |
"""Uses the stock dataframe to graph the normalized historical cumulative returns of each asset.
|
|
|
152 |
st.line_chart(cumulative_return)
|
153 |
|
154 |
|
155 |
+
def display_heat_map(stock_df,choices):
|
156 |
+
symbols, weights, investment = choices.values()
|
157 |
+
selected_stocks = stock_df[symbols]
|
158 |
# Calcuilates the correlation of the assets in the portfolio
|
159 |
+
price_correlation = selected_stocks.corr()
|
160 |
|
161 |
# Creates the title for streamlit
|
162 |
st.subheader('Heatmap Showing Correlation Of Assets')
|
163 |
# Generates a figure for the heatmap
|
164 |
fig, ax = plt.subplots()
|
165 |
+
fig = px.imshow(price_correlation,text_auto=True, aspect="auto")
|
166 |
# Displays the heatmap on streamlit
|
167 |
st.write(fig)
|
168 |
# Creates a header for the correlation data
|
169 |
+
#st.subheader('Correlation Data')
|
170 |
# Displays the correlation data on streamlit
|
171 |
+
#st.dataframe(price_correlation)
|
|
|
172 |
|
173 |
#def display_portfolio_return(stock_df, choices):
|
174 |
"""Uses the stock dataframe and the chosen weights from choices to calculate and graph the historical cumulative portfolio return.
|