# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] pycharm={"name": "#%% md\n"}
# # Limits of Diversification
#
# Diversfication allow to hold only porftfolio that award risk. But that does work only under market conditions, not under
# severe market conditions, e.g.: financial crises. If everything goes down there's not much that you can do.
#
# Diversification cann't help you with systematic risk.
#
# Hedging: Only avoiding risk taking is the only effective way to obtain downside protection. The problem is that hedging
# is symmetric, so you don't get upsides, so hedging works only for very wealthy investors that only want to secure their
# current wealth.
#
# Insurance: Get downside protection while allowing for upside potential. Insurance gives the best of both worlds.
# Insurance is Dynamic Hedging. Adjust risk-taking as function of margin of error.

# + pycharm={"name": "#%%\n"}
import matplotlib.pyplot as plt
# %load_ext autoreload
# %autoreload 2
# %matplotlib inline

import edhec_risk_kit as erk
import pandas as pd
import numpy as np

ind_return = erk.get_ind_returns()
ind_nfirms = erk.get_ind_n_firms()
ind_size = erk.get_ind_size()

# + pycharm={"name": "#%%\n"}
# In this section we are going to build a market index

# Compute market capitalization
ind_mktcap = ind_nfirms * ind_size

# Compute total market capitalization
total_mktcap = ind_mktcap.sum(axis='columns')
total_mktcap.plot()

# Compute the capitalization weight. This calculates the participation of each industry
# in the total market capitalization
ind_capweight = ind_mktcap.divide(total_mktcap, axis='rows')
ind_capweight[['Fin', 'Steel']].plot(figsize=(12, 6))

# Weighted average of returns, whole market
total_market_return = (ind_capweight * ind_return).sum(axis='columns')
plt.figure()
total_market_index = erk.drawdown(total_market_return).wealth

total_market_index.plot(figsize=(10, 5), title='Total Market Capweighted Index')
# Returns of the market over 3-years periods
# End goal: Wealth vs. Market correlation
total_market_index['1980':].rolling(window=36).mean().plot() # moving average of 36 months

# now time aggregate those returns annualized and compare them with the market ones
tmi_tr36rets = total_market_return.rolling(window=36).aggregate(erk.annualize_rets,
                                                                periods_per_year=12)

plt.figure()
tmi_tr36rets.plot(figsize=(12, 6), label='Tr.36 Mth. Ret.', legend=True)
total_market_return.plot(figsize=(12, 6), label='Returns', legend=True)

# + [markdown] pycharm={"name": "#%% md\n"}
# ## Rolling Correlations (using MultiIndexes)

# + pycharm={"name": "#%%\n"}
ts_corr = ind_return.rolling(window=36).corr()
ts_corr.index.names = ['date', 'industry']
ts_corr.tail()

# + pycharm={"name": "#%%\n"}
ind_tr36corr = ts_corr.groupby(level='date').apply(lambda cormat: cormat.values.mean())

# When the market is falling, the correlations increase, so diversification
# doesn't help you very much
ind_tr36corr.plot(label='Tr36 Mo Rets', figsize=(12, 6), legend=True, secondary_y=True)
tmi_tr36rets.plot(label='Tr 36 Mo Cor', figsize=(12, 6), legend=True)


# Calculate the correlation between the return and the inter-industry correlation
tmi_tr36rets.corr(ind_tr36corr)

# + pycharm={"name": "#%%\n"}

ts_corr['2018-1':'2018-1'].values

StarcoderdataJupyterScriptsDedupFiltered