<jupyter_start><jupyter_text>Midterm  Short answer1.(5pts) True or False.  (And explain your reason.)Mean-variance  optimization  goes  long  the  highest  Sharpe-Ratio  assets  and  shorts  the  lowestSharpe-ratio assets.False. Mean Variance optimization allocate assets based on the covariance of assets. It accounts for covariances instead of just volatilities.2.(5pts) True or False.  (And explain your reason.)Investing in an LETF makes more sense for a long-term horizon than a short-term horizon.False. Investing in an LETF for a long-term horizon will cause trouble. Although the correlation of LETFs are good, it does not replicate the cumulative mean of targeting index. In the long run, this tracking error would be futher magnified.3.(5pts) This week ProShares launches BITO on the NYSE. The ETF holds Bitcoin futures con-tracts.  Suppose in a year from now, we want to try to replicate BITO using SPY and IEF as regressors in a LFD. Because BITO will only have a year of data, we do not trust that we will have a good estimate of the mean return.Do  you  suggest  that  we  (in  a  year)  estimate  the  regression  with  an  intercept  or  without  anintercept?  Why?I suggest we estimate the regession without an intercept. Because since we are not confident in our estimate in the mean return, including a intercept to fit the data would result in overfitting to possible error results.  4.(5pts) Is HDG effective at tracking HFRI in-sample?  And out of sample?It is not effective at tracking HFRI in-sample or out of sample because when dealling with actual tradable assets, we need to consider operational fees and tracking errors.5.(5pts) A hedge fund claims to beat the market by having a very high alpha.  After regressing the hedge fund returns on the 6 Merrill-Lynch style factors, you find the alpha to be negative. Explain why this discrepancy can happen.The defination of market here might be different for the hedge fund and us. If the hedge fund didn't invest in one of the 6 Merrill-Lynch style factors and outperforms the other five, our regression might fit that unexplained alpha with the 6th factor and got a negative alpha.<jupyter_code>import pandas as pd
import numpy as np
import scipy.stats
import seaborn as sns
from matplotlib import pyplot as plt
import itertools
import statsmodels.api as sm
from arch import arch_model
from arch.univariate import GARCH, EWMAVariance<jupyter_output><empty_output><jupyter_text>2<jupyter_code>df = pd.read_excel('proshares_analysis_data.xlsx', sheet_name = 'merrill_factors')
df = df.set_index('date')
df.head()
for col in df.columns:
    if col != "USGG3M Index":
        df[col] = df[col] - df["USGG3M Index"]
        
df = df.drop("USGG3M Index", 1)
df.head()<jupyter_output><empty_output><jupyter_text>2.1<jupyter_code>def calculate_mv_froniter(df):
    mu = df.mean() * 12
    sigma = np.array(df.cov())
    sigma_inv = np.linalg.inv(sigma)
    weights = sigma_inv @ mu / (np.ones(sigma.shape[0]) @ sigma_inv @ mu)
    return weights, mu, sigma

def calculate_portfolio_weights_w_target(df,target):
    weights, mu, sigma = calculate_mv_froniter(df)
    sigma_inv =  np.linalg.inv(sigma)
    delta = ((np.ones(sigma.shape[0]) @ sigma_inv @ mu) / (mu @ sigma_inv @ mu )) * target
    weights_t = delta * weights
    return weights_t, mu, sigma

def calculate_performace(weights, mu, sigma):
  mean = mu @ weights 
  vol = np.sqrt(weights @ sigma @ weights) * np.sqrt(12)
  sharpe_ratio = mean / vol
  return pd.DataFrame({"mean":[mean],"volatility":[vol] ,"sharpe":[sharpe_ratio]})
weights, mu, sigma = calculate_mv_froniter(df)
q2a_df = pd.DataFrame(weights, index=df.columns, columns=['Weights of tangency portfolio'])
display(q2a_df.style.set_caption('Solution Table 2.1: weights of the tangency portfolio'))<jupyter_output><empty_output><jupyter_text>2.2<jupyter_code>weights, mu, sigma = calculate_portfolio_weights_w_target(df, 0.02*12)
q2a_df = pd.DataFrame(weights, index=df.columns, columns=['Weights of tangency portfolio'])
display(q2a_df.style.set_caption('Solution Table 2.2: weights of the tangency portfolio'))<jupyter_output><empty_output><jupyter_text>The optimal portfolio is invested in the risk-free rate because the sum of the weights is not 1  2.3<jupyter_code>calculate_performace(weights, mu, sigma)<jupyter_output><empty_output><jupyter_text>2.4<jupyter_code>df_in_sample = df[:'2018']
df_out_sample = df['2019':'2021']
weights, mu, sigma = calculate_portfolio_weights_w_target(df_in_sample, 0.02*12)
q2a_df = pd.DataFrame(weights, index=df.columns, columns=['Weights of tangency portfolio'])
display(q2a_df.style.set_caption('Solution Table 2.4: weights of the tangency portfolio'))
mu_os = df_out_sample.mean() * 12
sigma_out_sample = df_out_sample.cov()

calculate_performace(weights, mu_os, sigma_out_sample)<jupyter_output><empty_output><jupyter_text>2.5 5.(5pts) Suppose that instead of optimizing these 5 risky assets, we optimized 5 commodity futures:oil, coffee, cocoa, lumber, cattle, and gold. Do you think the out-of-sample fragility problem would be better or worse than what we have seen optimizing equities?I think the out-of-sample fragility problem would be worse because commedity tends to have higher correlation and volility  3.1<jupyter_code>EEM = df['EEM US Equity']
spy = df['SPY US Equity']

results = sm.OLS(EEM,spy,missing='drop').fit()
print(results.summary())
print('optimal hedging ratio is: ' + str(-results.params['SPY US Equity']))<jupyter_output>OLS Regression Results                                
=======================================================================================
Dep. Variable:          EEM US Equity   R-squared (uncentered):                   0.509
Model:                            OLS   Adj. R-squared (uncentered):              0.505
Method:                 Least Squares   F-statistic:                              125.4
Date:                Mon, 18 Oct 2021   Prob (F-statistic):                    2.05e-20
Time:                        18:32:12   Log-Likelihood:                          229.09
No. Observations:                 122   AIC:                                     -456.2
Df Residuals:                     121   BIC:                                     -453.4
Df Model:                           1                                                  
Covariance Type:            nonrobust                                                  
================================[...]<jupyter_text>3.2<jupyter_code>df['SPY Hedge'] = -results.params['SPY US Equity'] * spy
mean = df['SPY Hedge'].mean()*12
vol = df['SPY Hedge'].std() * (12**0.5)
sharpe = mean / vol

print(" hedged position:  mean: " + str(mean) + " vol: " + str(vol) + " shapre: " + str(sharpe))
print(" EEM mean: " + str(df['EEM US Equity'].mean()*12))<jupyter_output>hedged position:  mean: -0.12623039841549863 vol: 0.12584695974813245 shapre: -1.0030468647644217
 EEM mean: 0.032073667830984684<jupyter_text>3.3It doesn't have the same mean as EEM because SPY is not a perfect hedge to EEM (corr != -1).   3.4Suppose we estimated a multifactor regression where in addition to SPY, we had IWM as a regressor.  Why might this regression be difficult to use for attribution or even hedging?Because this regression has the problem of Multicollinearity. SPY and IWM will have correlation and thus our parameter estimation can swing badly and the percsion of betas might be reduced  4.1 1.(10pts)  SPY  and  EFA  are  highly  correlated,  yet  SPY  has  had  a  much  higher  return.  How confident are we that SPY will overperform EFA over the next 10 years?To answer the question,•use statistical estimates of the total returns of SPY and EFA over the full sample.•Assume that log returns for both assets are normally distributed.<jupyter_code>df = pd.read_excel('proshares_analysis_data.xlsx', sheet_name = 'merrill_factors')
df = df.drop("USGG3M Index", 1)
EFA = df['EFA US Equity']
spy = df['SPY US Equity']
def calc_prob(h, tilde_mu, tilde_sigma):
    L = - np.sqrt(h) * tilde_mu / tilde_sigma
    prob = scipy.stats.norm.cdf(L)
    return prob

def output_performance_df(df, tilde_mu, tilde_sigma):
    time_periods = [10]
    report_table = pd.DataFrame(columns=['probability'], index= time_periods)
    report_table ['probability'] = calc_prob(report_table.index, tilde_mu, tilde_sigma)

    return report_table

EFA_log = np.log(EFA + 1)
SPY_log = np.log(spy + 1)
tilda_log = EFA_log - SPY_log

tilde_mu = tilda_log.mean() * 12
tilde_sigma = tilda_log.std() * (12 ** 0.5)

report_table_4_1 = output_performance_df(df, tilde_mu, tilde_sigma)
report_table_4_1<jupyter_output><empty_output><jupyter_text>2.(10pts) Calculate the 60-month rolling volatility of EFA.Use the latest estimate of the volatility (Sep 2021), along with the normality formula, to calculatea Sep 2021 estimate of the 1-month, 1% VaR. In using the VaR formula, assume that the meanis zero.<jupyter_code>rolling_sigma = np.sqrt(df['EFA US Equity'].shift(1).dropna().rolling(60).apply(lambda x: ((x**2).sum()/len(x))))
latest_vol = rolling_sigma[-1:]
display(rolling_sigma)
display(latest_vol)
z_thresh = stats.norm.ppf(0.01)
var_multiple = -stats.norm.pdf(z_thresh)

VaR = var_multiple * latest_vol
print("Sep 2021 estimate of the 1-month, 1% VaR: " + str(VaR))<jupyter_output>Sep 2021 estimate of the 1-month, 1% VaR: 121   -0.001117
Name: EFA US Equity, dtype: float64
{'fraction_non_alphanumeric': 0.10368712702472294, 'fraction_numerical': 0.03015771526001705, 'mean_word_length': 3.8301595470921255, 'pattern_counts': {'":': 4, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '485747', 'license': 'MIT', 'n_tokens_mistral': 3205, 'n_tokens_neox': 2935, 'n_words': 988, 'path': 'solutions/mid1/submissions/jiangshawn_167731_6241793_midterm.ipynb', 'repo_name': 'tulyu96/finm-portfolio-2021', 'size': '9384'}