<jupyter_start><jupyter_text>Assigment week 1<jupyter_code>import pandas as pd
import numpy as np

%load_ext autoreload
%autoreload 2
%matplotlib inline

me_m = pd.read_csv(r"C:\Users\fiu126\Desktop\Coursera\Python\Introduction to Portfolio Construction and Analysis with Python\data\Portfolios_Formed_on_ME_monthly_EW.csv",
                       header=0, index_col=0, na_values=-99.99)
rets = me_m[['Lo 20', 'Hi 20']]
#rets.columns = ['SmallCap', 'LargeCap']
rets = rets/100
rets.index = pd.to_datetime(rets.index, format="%Y%m").to_period('M')
rets.head()
#Retorno Anual
n_meses=rets.shape[0]
retorno_mensal= (1+rets).prod()**(1/n_meses)-1
retorno_mensal
retorno_anual=(1+retorno_mensal)**12-1
retorno_anual*100
#Q1 - 15.20
#Q3 - 9.85
#Volatilidade Anual
Volatilidade_anual=rets.std()*np.sqrt(12)
Volatilidade_anual*100
#Q2 - 33.6
#Q4 - 19.51
# periodo entre 1999 e 2015
rets["1999":"2015"].shape
rets_2=rets["1999":"2015"]
#Retorno Anual
n_meses=rets_2.shape[0]
retorno_mensal= (1+rets_2).prod()**(1/n_meses)-1
retorno_mensal
retorno_anual=(1+retorno_mensal)**12-1
retorno_anual*100
#Q5 - 11.44
#Q7 -6.29
#Volatilidade Anual
Volatilidade_anual=rets_2.std()*np.sqrt(12)
Volatilidade_anual*100
#Q6 - 22.89
#Q8 - 17.27
# Drawdown
### 1) Compute the wealth_index
### 2) Compute previous peakes
### 3) Comput drawdown
Wealth_Index=1000*(1+rets["1999":"2015"]["Lo 20"]).cumprod()
Wealth_Index.plot()
Previous_Peaks=Wealth_Index.cummax()
Previous_Peaks.plot()
Drawdown=-(Wealth_Index-Previous_Peaks)/Previous_Peaks
Drawdown.plot()
var_list=[Wealth_Index,Previous_Peaks,Wealth_Index-Previous_Peaks]
comparison=pd.concat(var_list,axis=1)
comparison.columns=['Wealth_Index','Previous_Peaks','Wealth_Index-Previous_Peaks']
comparison.plot(title="Lo 20")
Drawdown.max()*100
#Q9 - 62.48
Drawdown.idxmax()
#Q10 - 2009-02
Wealth_Index=1000*(1+rets["1999":"2015"]["Hi 20"]).cumprod()
rets.head()
Previous_Peaks=Wealth_Index.cummax()
Previous_Peaks.plot()
Drawdown=-(Wealth_Index-Previous_Peaks)/Previous_Peaks
Drawdown.plot()
var_list=[Wealth_Index,Previous_Peaks,Wealth_Index-Previous_Peaks]
comparison=pd.concat(var_list,axis=1)
comparison.columns=['Wealth_Index','Previous_Peaks','Wealth_Index-Previous_Peaks']
comparison.plot(title="Hi 20")
Drawdown.max()*100
#Q11 - 55.27
Drawdown.idxmax()
#Q12 - 2009-02
hfi=pd.read_csv(r"C:\Users\fiu126\Desktop\Coursera\Python\Introduction to Portfolio Construction and Analysis with Python\data\edhec-hedgefundindices.csv",header=0,index_col=0,parse_dates=True)
hfi = hfi/100
hfi.index =hfi.index.to_period('M')
hfi=hfi["2009":"2018"]
hfi.head()
hfi.shape
hfi[hfi<0].std(ddof=0).sort_values()
#Q13 - Short Selling  
def skewness(r):
    """
    Alternative to scipy.stats.skew()
    Computes the skewness of the supplied Series or DataFrame
    Returns a float or a Series
    """
    demeaned_r = r - r.mean()
    # use the population standard deviation, so set dof=0
    sigma_r = r.std(ddof=0)
    exp = (demeaned_r**3).mean()
    return exp/sigma_r**3
skewness(hfi).sort_values()
#Q14 - Equity Market Neutral
hfi=pd.read_csv(r"C:\Users\fiu126\Desktop\Coursera\Python\Introduction to Portfolio Construction and Analysis with Python\data\edhec-hedgefundindices.csv",header=0,index_col=0,parse_dates=True)
hfi = hfi/100
hfi.index =hfi.index.to_period('M')
hfi=hfi["2000":"2018"]
hfi.head()
def kurtosis(r):
    """
    Alternative to scipy.stats.kurtosis()
    Computes the kurtosis of the supplied Series or DataFrame
    Returns a float or a Series
    """
    demeaned_r = r - r.mean()
    # use the population standard deviation, so set dof=0
    sigma_r = r.std(ddof=0)
    exp = (demeaned_r**4).mean()
    return exp/sigma_r**4

kurtosis(hfi).sort_values()
#Q16 - Fixed Income Arbitrage<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup