<jupyter_start><jupyter_text>Name: <NAME><jupyter_code>import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.stats
df = pd.read_excel(r'C:\Users\Harryzhu\Desktop\Study Files\Graduate UChicago\FINM 36700\MIDTERM\proshares_analysis_data.xlsx', sheet_name ='hedge_fund_series').set_index('date')<jupyter_output><empty_output><jupyter_text>1.Short Answers 1)FalseThe MV optimization doesn't tell you to long the highest Sharpe Ratio assets and shorts the lowest Sharpe Ratio assets. Rather, it tells you that the corelation between each single asset is important to look at. Investors love assets with low volatility and high returns, and MV methods build a tangency portfolio that can give you the optimal choice based on volatility and mean returns. The MV optimization tells you that you should invest in assets that has small corelation with other assets within your portfolio.  2)TrueIt is because long term is more profitable than short term 3)I suggest that we should include an intercept here because we want the regressors to variance of BITO, not both the mean and the variance of BITO, given the fact the we don't trust the estimate of the mean return. Therefore, we should help the regressors and let it only estimate the variance of BITO. 4)HDG is not very effetive when tracking HFRI in-sample. The Sharpe Ratio and mean of HDG are very different from those of HFRI. However, it is good for out of sample since it has high correlation with the target HFRI. 5)If the alpha is negative, it means the hedge funds do not beat the market. This could suggest that this hedge fund does not explain the market. 2. Allocation<jupyter_code>rets = pd.read_excel(r'C:\Users\Harryzhu\Desktop\Study Files\Graduate UChicago\FINM 36700\MIDTERM\proshares_analysis_data.xlsx', sheet_name ='merrill_factors').set_index('date')


retsx = rets.subtract(rets["USGG3M Index"], axis=0)
retsx = retsx.drop(columns=["USGG3M Index"])
retsx.head()
#1)calculate tangency portfolio
retsx_annual = retsx * 12
def compute_tangency(retsx):

    #variance-covariance matrix
    Sigma = retsx.cov()

    # N is the number of assets
    N = Sigma.shape[0]

    Sigma_adj = Sigma.copy()

    mu_tilde = retsx.mean()
    Sigma_inv = np.linalg.inv(Sigma_adj)
    weights = Sigma_inv @ mu_tilde / (np.ones(N) @ Sigma_inv @ mu_tilde)

    omega_tangency = pd.Series(weights, index=mu_tilde.index)

    return omega_tangency, mu_tilde, Sigma

omega_tangency, mu_tilde, Sigma = compute_tangency(retsx)

omega_tangency
#2)
def target_mv_portfolio(df_tilde, target_return):

    omega_tangency, mu_tilde, Sigma = compute_tangency(df_tilde)
    Sigma_adj = Sigma.copy()

    Sigma_inv = np.linalg.inv(Sigma_adj)
    N = Sigma_adj.shape[0]
    delta_tilde = ((np.ones(N) @ Sigma_inv @ mu_tilde)/(mu_tilde @ Sigma_inv @ mu_tilde)) * target_return

    omega_star = delta_tilde * omega_tangency

    return omega_star

# TODO: Annualized target return in footnote is not up to date

omega_star = target_mv_portfolio(retsx, target_return=0.02)

omega_star
# No, the optimal portfolio doesn't invested in the risk_free rate since we drop the risk free assets "USGG3M Index"
#3)
opt_portf = retsx @ omega_star
#mean annualized
mean_annual = opt_portf.mean() * 12
print(mean_annual)
#volatility annualized
vol_annual = opt_portf.std() * np.sqrt(12)
print(vol_annual)
#Sharp Ratio
SR = mean_annual/vol_annual
print(SR)
#4)


def target_mv_portfolio(df_tilde, target_return):

    omega_tangency, mu_tilde, Sigma = compute_tangency(df_tilde)
    Sigma_adj = Sigma.copy()

    Sigma_inv = np.linalg.inv(Sigma_adj)
    N = Sigma_adj.shape[0]
    delta_tilde = ((np.ones(N) @ Sigma_inv @ mu_tilde)/(mu_tilde @ Sigma_inv @ mu_tilde)) * target_return

    omega_star = delta_tilde * omega_tangency

    return omega_star

# TODO: Annualized target return in footnote is not up to date

omega_star = target_mv_portfolio(retsx, target_return=0.02)

omega_star
df_temp_18 = retsx.loc[:'2018',:]
df_temp_18
def target_mv_portfolio(df_tilde, target_return):

    omega_tangency, mu_tilde, Sigma = compute_tangency(df_tilde)
    Sigma_adj = Sigma.copy()

    Sigma_inv = np.linalg.inv(Sigma_adj)
    N = Sigma_adj.shape[0]
    delta_tilde = ((np.ones(N) @ Sigma_inv @ mu_tilde)/(mu_tilde @ Sigma_inv @ mu_tilde)) * target_return

    omega_star = delta_tilde * omega_tangency

    return omega_star


omega_star_18 = target_mv_portfolio(df_temp_18, target_return=0.02)

omega_star_18
df_temp_19 = retsx.loc['2019':, :]

omega_tangency_19, mu_tilde_19, Sigma_19 = compute_tangency(df_temp_19)

mean_19 = omega_star_18 @ mu_tilde_19

vol_19 = np.sqrt(omega_star_18 @ Sigma_19 @ omega_star_18)/np.sqrt(12)

sharpe_ratio_out_of_sample = mean_19/vol_19

print('The mean of 2019 is')
print(mean_19)
print('The volatility of 2019 is')
print(vol_19)
print('The sharp ratio of 2019 is')
print(sharpe_ratio_out_of_sample)
df_temp_20 = retsx.loc['2020':, :]

omega_tangency_20, mu_tilde_20, Sigma_20 = compute_tangency(df_temp_20)

mean_20 = omega_star_18 @ mu_tilde_20

vol_20 = np.sqrt(omega_star_18 @ Sigma_20 @ omega_star_18)/np.sqrt(12)

sharpe_ratio_out_of_sample = mean_20/vol_20

print('The mean of 2020 is')
print(mean_20)
print('The volatility of 2020 is')
print(vol_20)
print('The sharp ratio of 2020 is')
print(sharpe_ratio_out_of_sample)
df_temp_21 = retsx.loc['2021':, :]

omega_tangency_21, mu_tilde_21, Sigma_21 = compute_tangency(df_temp_21)

mean_21 = omega_star_18 @ mu_tilde_21

vol_21 = np.sqrt(omega_star_18 @ Sigma_21 @ omega_star_18)/np.sqrt(12)

sharpe_ratio_out_of_sample = mean_21/vol_21

print('The mean of 2021 is')
print(mean_21)
print('The volatility of 2021 is')
print(vol_21)
print('The sharp ratio of 2021 is')
print(sharpe_ratio_out_of_sample)<jupyter_output>The mean of 2021 is
0.03512672989461035
The volatility of 2021 is
0.02148024166194399
The sharp ratio of 2021 is
1.6353042227100967<jupyter_text>5)The out of sample would be better than those five risky assets. Becuase those commondities are not risky, and it is safe to invest on those. Thus the out of sample will be good.  3.Hedging & Replication<jupyter_code>#1)
retsx.head()
X = retsx['SPY US Equity']
y = retsx['EEM US Equity']
model1 = sm.OLS(y,X).fit()
model1.params<jupyter_output><empty_output><jupyter_text>for every dollar invested in EEM, you should invest 0.927 dollar in SPY<jupyter_code>#2)
hedged_position = retsx['SPY US Equity']
#mean
mean1 = hedged_position.mean() * 12
#vol
vol1 = hedged_position.std()/np.sqrt(12)
#SR
SR1 = mean1/vol1

print('The mean is')
print(mean1)
print('The volatility is')
print(vol1)
print('The sharp ratio is')
print(SR1)
hedged_position1 = retsx['EEM US Equity']
#mean
mean2 = hedged_position1.mean() * 12
#vol
vol2 = hedged_position1.std()/np.sqrt(12)
#SR
SR2 = mean2/vol2
print('The mean is')
print(mean2)
print('The volatility is')
print(vol2)
print('The sharp ratio is')
print(SR2)<jupyter_output>The mean is
0.03778436455229617
The volatility is
0.01528149367033578
The sharp ratio is
2.4725570266499957<jupyter_text>It doesn't have the same mean as EEM because there might be some skills or other factors that can not be replicated. 4)The reason why it is difficult to use multifactor regression to hedge is that when including every assets in one LDF, those assets have corraltion with each other, which could make beta lose its power.<jupyter_code>#4)Modeling Risk
rets = pd.read_excel(r'C:\Users\Harryzhu\Desktop\Study Files\Graduate UChicago\FINM 36700\MIDTERM\proshares_analysis_data.xlsx', sheet_name ='merrill_factors').set_index('date')
rets.head()
#1)
def prob(mu,sigma,h,val):
    z = (val - mu) / (sigma/np.sqrt(h))
    p = scipy.stats.norm.cdf(z)
    return p

mu_spy = np.log(1 + rets).mean()['SPY US Equity'] * 12
mu_efa = np.log(1 + rets).mean()['EFA US Equity'] * 12
sigma_spy = np.log(1 + rets).std()['SPY US Equity'] *np.sqrt(12)
prob_smaller_than_real = prob(mu_spy,sigma_spy,10,mu_efa)
print(prob_smaller_than_real)
#2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09454770755885998, 'fraction_numerical': 0.047459727385377945, 'mean_word_length': 5.345125786163522, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '490834', 'license': 'MIT', 'n_tokens_mistral': 3240, 'n_tokens_neox': 2862, 'n_words': 811, 'path': 'solutions/mid1/submissions/zhuzihan_170941_6241852_Midterm Zihan Zhu.ipynb', 'repo_name': 'tulyu96/finm-portfolio-2021', 'size': '8073'}