Created
September 7, 2019 17:06
-
-
Save julian-west/7d7ab41b49766ec2eadbee07f179c885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_rankings_to_string(ranking): | |
""" | |
Concatenates list of node and correlation into a single string which is the | |
preferred format for the plotly tooltip. | |
Inserts html "<br>" inbetween each item in order to add a new line in the tooltip | |
""" | |
s = '' | |
for r in ranking: | |
s += r + "<br>" | |
return s | |
def calculate_stats(returns=log_returns_df): | |
"""calculate annualised returns and volatility for all ETFs | |
Output | |
------- | |
Outputs the annualised volatility and returns as a list of floats (for use in assigning node colours | |
and sizes) and also as a lists of formatted strings to be used in the tool tips. | |
""" | |
#log returns are additive, 252 trading days | |
annualized_returns = list(np.mean(returns)*252*100) | |
annualized_volatility = [np.std(returns[col]*100)*(252**0.5) | |
for col in list(returns.columns)] | |
# create string for tooltip | |
annualized_volatility_2dp = ["Annualized Volatility: ""%.1f" % r + "%" for r in annualized_volatility] | |
annualized_returns_2dp = ["Annualized Returns: ""%.1f" % r + "%" for r in annualized_returns] | |
return annualized_volatility, annualized_returns, annualized_volatility_2dp, annualized_returns_2dp | |
def get_top_and_bottom_three(df=correlation_matrix): | |
""" | |
get a list of the top 3 and bottom 3 most/least correlated assests | |
for each node. | |
Parameters -> df - pandas correlation matrix | |
Returns -> top_3_list : list of lists containing the top 3 correlations (name and value) | |
bottom_3_list: list of lists containing the bottom three correlations (name and value) | |
""" | |
top_3_list = [] | |
bottom_3_list = [] | |
for col in df.columns: | |
# exclude self correlation #reverse order of the list returned | |
top_3 = list(np.argsort(abs(df[col]))[-4:-1][::-1]) | |
# bottom 3 list is returned in correct order | |
bottom_3 = list(np.argsort(abs(df[col]))[:3]) | |
# get column index | |
col_index = df.columns.get_loc(col) | |
# find values based on index locations | |
top_3_values = [df.index[x] + ": %.2f" % | |
df.iloc[x, col_index] for x in top_3] | |
bottom_3_values = [df.index[x] + ": %.2f" % | |
df.iloc[x, col_index] for x in bottom_3] | |
top_3_list.append(convert_rankings_to_string(top_3_values)) | |
bottom_3_list.append(convert_rankings_to_string(bottom_3_values)) | |
return top_3_list, bottom_3_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment