<jupyter_start><jupyter_text>Mid-term Exam  problem 1 1. False. The weight in Mean-variance optimization portfolio mostly depends covariance. The portfolio often long asset with highest sharp ratio and short the asset with second highest sharp ratio to make sure it has enough money to buy more high sharp ratio asset.  2. False. LETF performs better in short term. In long term, it will derive from the index it tracks because the flucuation in the market will be magnified by the leverage in the ETF. Moreover, the weights in ETF need to adjust in long term, which will also impact its performance.  3. We should not include an intercept. Because we want to replicate the whole BITO index, not just its volatility. Secondly, since the mean of BITO is unreliable, so we cannot use an constant intercept to represent it.  4. It tracks HFRI well in-sample, but maybe not very well out-sample. Because HDG is highly corelated to HFRI, but when using the same portfolio of HDG to replicate HFRI out of sample, the accuracy will reduce a lot, since HDG includes some other factors that has impact on return while HFRI does not, such as transaction fees, and cost to short equities.  5.  Because a single hedge fund maybe low correlated to market, which generates the high alpha. But factors in Merrill-Lynch model are all hedge funds, so after the regression, the return was mostly offset by the 6 hedge fund factors, leaving a negative alpha.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import statsmodels.api as sm
import os
os.chdir('/Users/trady/Desktop/36700 Portfolio Theory')<jupyter_output><empty_output><jupyter_text>Problem 2 Allocation  1.<jupyter_code>data = pd.read_excel('proshares_analysis_data.xlsx', sheet_name='merrill_factors').set_index('date')
data = data.subtract(data['USGG3M Index'], axis = 0)
data = data.drop(columns=['USGG3M Index'])
data.head()
def tangency_weights(returns,dropna=True,scale_cov=1):
    if dropna:
        returns = returns.dropna()

    covmat_full = returns.cov()
    covmat_diag = np.diag(np.diag(covmat_full))
    covmat = scale_cov * covmat_full + (1-scale_cov) * covmat_diag

    weights = np.linalg.solve(covmat,returns.mean())
    weights = weights / weights.sum()

    return pd.DataFrame(weights, index=returns.columns, columns=['tangency weights'])
omega_tang = tangency_weights(data)
omega_tang<jupyter_output><empty_output><jupyter_text>2.<jupyter_code>mu = data.mean()
scaler = 0.02 / (data @ omega_tang).mean()
omega_optimize = scaler * omega_tang
omega_optimize<jupyter_output><empty_output><jupyter_text>3.<jupyter_code>def performanceMetrics(returns,annualization=1):
    metrics = pd.DataFrame(index=returns.columns)
    metrics['Mean'] = returns.mean() * annualization
    metrics['Vol'] = returns.std() * np.sqrt(annualization)
    metrics['Sharpe'] = (returns.mean() / returns.std()) * np.sqrt(annualization)

    return metrics

performanceMetrics(data@omega_optimize, 12)<jupyter_output><empty_output><jupyter_text>4.<jupyter_code>data_train = data[:'2018']
data_test = data['2019':]
omega_tang_2018 = tangency_weights(data_train)
scaler_2018 = 0.02 / (data @ omega_tang_2018).mean()
omega_optimize_2018 = scaler * omega_tang
performanceMetrics(data_test @ omega_optimize_2018)<jupyter_output><empty_output><jupyter_text>5. I think the out-of-sample fragility problem would be better. Because there are more factors that influence the return of commodity assets, compared to equity indexes, so the correlation beteween commodity assets may be lower than equity indexs. Therefore, we will have a more stable covariance, so the performance of in-sample and out-sample data will be more similar.   Problem 3  1. For every dollar invested in EEM, I will invest 0.9257 dollar in SPY.<jupyter_code>Y = data['EEM US Equity'] - data['USGG3M Index']
X = data['SPY US Equity'] - data['USGG3M Index']
model = sm.OLS(Y, X).fit()
model.summary()<jupyter_output><empty_output><jupyter_text>2.<jupyter_code>beta = model.params[0]
performanceMetrics(pd.DataFrame(Y - beta * X))<jupyter_output><empty_output><jupyter_text>3. The hedged position has different mean compared to EEM index. Because we did not include an intercept the regression, so the hedge will also offset the mean return of SPY. In other words, after hedging the market index, we also lose the change to earn the return from the market.<jupyter_code>performanceMetrics(pd.DataFrame(Y))<jupyter_output><empty_output><jupyter_text>4. Because as shown in charts below, the coefficient of SPY changed a lot after adding IWM to the regression. This is due to the high correlation between SPY and IWM. So the weight on SPY and IWM will be very sensitive to changes in the two indexes, which means we have to adjust our portfolio very often when hedging.<jupyter_code>Y = data['EEM US Equity']- data['USGG3M Index']
X = data[['SPY US Equity','IWM US Equity']].subtract(data['USGG3M Index'], axis=0)
model = sm.OLS(Y, X).fit()
model.summary()
X.corr()<jupyter_output><empty_output><jupyter_text>Problem 4  1.<jupyter_code>from scipy.stats import norm
data = pd.read_excel('proshares_analysis_data.xlsx', sheet_name='merrill_factors').set_index('date')
new_data = data[['SPY US Equity', 'EFA US Equity']]
new_data['diff'] = new_data['SPY US Equity'] - new_data['EFA US Equity']
log_data = np.log(1+new_data)
average = log_data['diff'].mean() * 12
sigma = log_data['diff'].std() * np.sqrt(12)
x = (np.sqrt(10)*average)/sigma
print("The probability  that SPY will overperform EFA over the next 10 years is " + str(round(norm.cdf(x),4)))<jupyter_output>The probability  that SPY will overperform EFA over the next 10 years is 0.9994<jupyter_text>2.<jupyter_code>vol = pd.DataFrame()
efa = np.log(1+new_data['EFA US Equity'])
vol['Rolling Window'] = (efa**2).shift(1).rolling(60).mean() 
vol = (vol ** 0.5).dropna()
latest_vol = vol.iloc[-1,:]
z_value = norm.ppf(0.01)
var = z_value * latest_vol
print("The  1-month, 1% VaR is " + str(round(var,4)))<jupyter_output>The  1-month, 1% VaR is Rolling Window   -0.0978
Name: 2021-09-30 00:00:00, dtype: float64
{'fraction_non_alphanumeric': 0.10265660872417186, 'fraction_numerical': 0.021810429649065266, 'mean_word_length': 5.447145877378436, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '482076', 'license': 'MIT', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1931, 'n_words': 660, 'path': 'solutions/mid1/submissions/yaoconghan_172873_6241701_pt-mid term-3.ipynb', 'repo_name': 'tulyu96/finm-portfolio-2021', 'size': '6098'}