<jupyter_start><jupyter_text>HomeWork 2 Group 31 - <NAME>, <NAME>, <NAME>, <NAME>, <NAME><jupyter_code>import pandas as pd
import numpy as np
import statsmodels.api as sm
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from scipy.stats import skew, norm, kurtosis
hedgeX= pd.read_excel('proshares_analysis_data.xlsx', sheet_name=1)
hedgeX
hedgeX.set_index('date', inplace=True)
mean = hedgeX.mean()* 12
sigma= hedgeX.std() * np.sqrt(12)
display1= pd.DataFrame({'Mean':mean, 'Vol':sigma, 'SharpeRatio': mean/sigma})<jupyter_output><empty_output><jupyter_text>Question 1<jupyter_code>display1<jupyter_output><empty_output><jupyter_text>Question 2  2.A<jupyter_code>skewHedge= skew(hedgeX)
skewHedge= pd.Series(skewHedge, index= display1.index)
skewHedge<jupyter_output><empty_output><jupyter_text>2.B<jupyter_code>kurtHedge= kurtosis(hedgeX, fisher=True)
kurtHedge= pd.Series(kurtHedge, index= display1.index)
kurtHedge<jupyter_output><empty_output><jupyter_text>2.C<jupyter_code>var_level= 95
var_95= np.percentile(hedgeX,100-var_level, axis=0)
var= pd.Series(var_95, index=display1.index)
var<jupyter_output><empty_output><jupyter_text>2.D<jupyter_code>cvar= hedgeX[hedgeX< var]
cvar.dropna(inplace=True)
cvar.mean()<jupyter_output><empty_output><jupyter_text>2.E<jupyter_code>cum_returns= (1+ hedgeX).cumprod()
display2= pd.DataFrame(index= display1.index)
rolling_max= cum_returns.cummax()
drawdown= (cum_returns - rolling_max) / rolling_max
higestP= drawdown[drawdown==0]
higestP = higestP[(higestP.index>drawdown.idxmin(axis=0)[0])]
display2['Max dates']= drawdown.idxmax(axis=0)
display2['Min Date']= drawdown.idxmin(axis=0)
display2['Recovery Dates']= higestP.notna().idxmax()
display2
drawdown['HDG US Equity'].plot()<jupyter_output><empty_output><jupyter_text>Question 3<jupyter_code>merril= pd.read_excel('proshares_analysis_data.xlsx', sheet_name=2)
merril.set_index('date', inplace=True)
merril
display2=[]

for i in hedgeX.columns:
    y= hedgeX[i]
    x= sm.add_constant(merril['SPY US Equity'])
    results = sm.OLS(y,x,missing='drop').fit()
    #print(results.summary())
    print(i)
    print("\n")
    print("Beta is :"+ str(results.params['SPY US Equity']))
    print("Alpha is :" + str(results.params['const']))
    print("Info Ratio is :"+ str((results.params['const']/results.resid.std())*np.sqrt(12)))
    print("Treynor Ratio is :"+ str((y.mean()- merril['USGG3M Index'].mean()/results.params['SPY US Equity'])*12))
    print("\n")
    
    Beta=results.params['SPY US Equity']
    Alpha=results.params['const']
    IR=results.params['const']/results.resid.std()*np.sqrt(12)
    TR=(y.mean()- merril['USGG3M Index'].mean()/results.params['SPY US Equity'])*12
    display2.append([Beta,Alpha,IR,TR])
    #display2.append({'Beta':Beta,'Alpha':Alpha,'IR':IR,'TR':TR},ignore_index=True)
df = pd.DataFrame(display2, index=display1.index,columns=['Beta','Alpha','IR','TR'])
df<jupyter_output>HFRIFWI Index


Beta is :0.39432024402715266
Alpha is :-0.0006164139609061944
Info Ratio is :-0.24202467421125826
Treynor Ratio is :0.0363018332097037


MLEIFCTR Index


Beta is :0.359382321346525
Alpha is :-0.0011837833561779776
Info Ratio is :-0.6152647558585131
Treynor Ratio is :0.02293045023621085


MLEIFCTX Index


Beta is :0.3580335415604699
Alpha is :-0.0012914489163673732
Info Ratio is :-0.6712675672516665
Treynor Ratio is :0.02137959189198596


HDG US Equity


Beta is :0.3691986751238124
Alpha is :-0.002197876727125541
Info Ratio is :-1.0106116432179737
Treynor Ratio is :0.012632209511660941


QAI US Equity


Beta is :0.2849928131066539
Alpha is :-0.001379967486253515
Info Ratio is :-0.6872833977830201
Treynor Ratio is :0.005452486679202466<jupyter_text>Question 4  4.A Q: the differences between SPY and the hedge-fund series?<jupyter_code>spy_mean= merril['SPY US Equity'].mean()*12
spy_vol= merril['SPY US Equity'].std()*np.sqrt(12)
spy_sharpe_ratio= spy_mean/spy_vol 
spy_mean,spy_vol,spy_sharpe_ratio<jupyter_output><empty_output><jupyter_text>SPY has a much higher mean return and also higher volatility compared to the hedge funds. SPY also has a better sharpe ration to the hedge funds. We can also note that from the regression, the R-Squared for the hedgefunds and SPY is also relatively high, implying SPY explains most of the variations in the Hedge Funds.   4.B Q: which performs better between HDG and QAI. QAI has a better sharpe ration compared to the HDG.  Since the Beta and R squared for HDG is higher than QAI, SPY explains more of the returns for HDG, but  HDG has a lower information ratio. Hence, HDG is taking more external risk compared to QAI to achieve just 0.003 excess mean return.   4.C Q: whether HDG and the ML series capture the most notable properties of HFRI. HDG and ML series both capture most of the notable properties of HFRI well, with a correlation of 0.91 between the ML series and HFRI and 0.89 between HDG and HFRI.HFRI has the best alpha while having high R squared to SPY. The ML series would come second in terms of overall perfromace and HDG is the third best. This is explained by their respective information ratios.   Question 5  5.A<jupyter_code>corrmat= hedgeX.corr()
corrmat[corrmat==1]= None
sns.heatmap(corrmat)
corrmat<jupyter_output><empty_output><jupyter_text>5.B Q: Which series have the highest and lowest correlations? The pair of HDG and QAI has lowest correlation.The pair of MLEIFCTR and MLEIFCTX has highest Correlation.  Question 6  6.A<jupyter_code>y= hedgeX['HFRIFWI Index']
x= sm.add_constant(merril)
results_q6 = sm.OLS(y,x,missing='drop').fit()
print(results_q6.summary())
params= results_q6.params
intercept= params['const']
intercept, params<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:          HFRIFWI Index   R-squared:                       0.856
Model:                            OLS   Adj. R-squared:                  0.848
Method:                 Least Squares   F-statistic:                     113.7
Date:                Mon, 11 Oct 2021   Prob (F-statistic):           5.84e-46
Time:                        14:23:26   Log-Likelihood:                 437.27
No. Observations:                 122   AIC:                            -860.5
Df Residuals:                     115   BIC:                            -840.9
Df Model:                           6                                         
Covariance Type:            nonrobust                                         
=================================================================================
                    coef    std err          t   [...]<jupyter_text>6.B Q: Are the betas realistic position sizes, or do they require huge long-short positions? The betas are realtistic position sizes of similar quantities and don't require huge long-short position.   6.C Q: Report the R-squared. The r-squared is 0.856, this is fairly high, implying that these factors explain most of the variation is HFRIFWI.  6.D Q: Report the volatility of εmerr, (the tracking error.)<jupyter_code>error = results.resid.std()
error<jupyter_output><empty_output><jupyter_text>Question 7  7.A<jupyter_code>merril_time_before= merril[merril.index<='2016-07-31']
hedge_time_before= hedgeX[hedgeX.index<='2016-07-31']

merril_time_after= merril[merril.index>'2016-07-31']
hedge_time_after= hedgeX[hedgeX.index>'2016-07-31']
y= hedge_time_before['HFRIFWI Index']
x= sm.add_constant(merril_time_before)
results = sm.OLS(y,x,missing='drop').fit()
print(results.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:          HFRIFWI Index   R-squared:                       0.859
Model:                            OLS   Adj. R-squared:                  0.843
Method:                 Least Squares   F-statistic:                     53.65
Date:                Mon, 11 Oct 2021   Prob (F-statistic):           8.94e-21
Time:                        14:23:26   Log-Likelihood:                 229.08
No. Observations:                  60   AIC:                            -444.2
Df Residuals:                      53   BIC:                            -429.5
Df Model:                           6                                         
Covariance Type:            nonrobust                                         
=================================================================================
                    coef    std err          t   [...]<jupyter_text>7.B<jupyter_code>alpha = results.params['const']
params= results.params
xi= merril_time_after[merril_time_after.index=='2016-08-31']
betas= params[1:]
hfri_61 = alpha + (xi @ betas)
hfri_61<jupyter_output><empty_output><jupyter_text>7.C<jupyter_code>merril_factor= ['SPY US Equity', 'USGG3M Index', 'EEM US Equity', 'EFA US Equity', 'EUO US Equity', 'IWM US Equity']
date_range= merril.index
date_df= pd.Series(index=date_range, name='date_df')
for date in date_range:
    date_month_prior= pd.DatetimeIndex([date]).shift(periods=-1, freq='M')[0]
    date_month_start= pd.DatetimeIndex([date]).shift(periods=-60, freq='M')[0]
    df_subset= merril[date_month_start:date_month_prior]
    rhs = sm.add_constant(merril[merril_factor])
    lhs= hedgeX['HFRIFWI Index']
    res = sm.OLS(lhs, rhs, drop="missing").fit()
    alpha = res.params['const']
    beta = res.params.drop(index='const')
    x_t = merril.loc[date, merril_factor]
    predicted_next_value = alpha + x_t @ beta
    date_df[date] = predicted_next_value

date_df['01-2015':].plot(figsize=(14,3))
hedgeX.loc['01-2015':,'HFRIFWI Index'].plot()
plt.legend()
plt.show()
None



(date_df['01-2015':] + 1).cumprod().plot(figsize=(14,3))
(hedgeX.loc['01-2015':,'HFRIFWI Index'] + 1).cumprod().plot()
plt.legend()
plt.show()
None

display((pd.DataFrame([date_df, hedgeX.loc['01-2015':,'HFRIFWI Index']])).T.corr())<jupyter_output><ipython-input-23-f4fdf770b18b>:3: DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  date_df= pd.Series(index=date_range, name='date_df')<jupyter_text>Q: How well does the out-of-sample replication perform with respect to the target? From the graph we see that out of sample replication performs in line with our regression.   Question 8  8.A<jupyter_code>y= hedgeX['HFRIFWI Index']
x= merril
results_q8 = sm.OLS(y,x,missing='drop').fit()
print(results_q8.summary())
print('Beta without intercept')
Betaq8=results_q8.params
print(Betaq8)
print("\n")
print('Beta with intercept')
print(results_q6.params[1:7])<jupyter_output>Beta without intercept
SPY US Equity    0.087505
USGG3M Index     0.334503
EEM US Equity    0.069936
EFA US Equity    0.101524
EUO US Equity    0.023479
IWM US Equity    0.128999
dtype: float64


Beta with intercept
SPY US Equity    0.072022
USGG3M Index    -0.400591
EEM US Equity    0.072159
EFA US Equity    0.106318
EUO US Equity    0.022431
IWM US Equity    0.130892
dtype: float64<jupyter_text>Q: How does it compare to the estimated beta with an intercept, β merr? From our analysis we see that the beta values are different for regression with a intercept and one without intercept. The beta for the risk free rate is negative for the regression without an intercept.   8.B Anualized mean of fitted values<jupyter_code>Xt_q8=merril
predicts_q8=Xt_q8@Betaq8
predicts_q8.mean()*12
display1<jupyter_output><empty_output><jupyter_text>Q: How does it compare to the mean of the HFRI? The mean of HFRI is higher than the mean for fitted values.   8.C<jupyter_code>Betaq6= results_q6.params[1:7]
predictsq6=Xt_q8@Betaq6
fc=pd.concat([predicts_q8,hedgeX['HFRIFWI Index']],axis=1)
corrmatq8=fc.corr()
corrmatq8[corrmatq8==1]=None
corrmatq8
fc2=pd.concat([predictsq6,hedgeX['HFRIFWI Index']],axis=1)
corrmatq82=fc2.corr()
corrmatq82[corrmatq82==1]=None
corrmatq82<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13623501997336884, 'fraction_numerical': 0.065745672436751, 'mean_word_length': 3.8416599516518937, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '322303', 'license': 'MIT', 'n_tokens_mistral': 4727, 'n_tokens_neox': 4157, 'n_words': 1015, 'path': 'solutions/hw2/Portfolio-HW-2-final draft.ipynb', 'repo_name': 'tulyu96/finm-portfolio-2021', 'size': '12039'}