Skip to content
Coursera Modul 1 Quiz
# task 1 & 3
import pandas as pd
# read dataset
df = pd.read_csv("Portfolios_Formed_on_ME_monthly_EW.csv", parse_dates=["Unnamed: 0"], index_col=["Unnamed: 0"])
df = df[['Lo 20', 'Hi 20']] # substract relevant columns
n_months = df.shape[0] # number of months
returns = df/100 # transform the percentage return into decimal number
return_per_month = (returns+1).prod()**(1/n_months)-1 # monthly return over this period
annulized_return = (return_per_month+1)**12-1 # annulized return
print("annulized_return:\n", annulized_return)
# task 2 & 4
annulized_volatility = returns.std()*(12**0.5) # volatility = standard deviation of return
print("annulized_volatility:\n", annulized_volatility)
# task 5 - 8
returns_1999_2015 = returns[(returns.index >= 199901) & (returns.index <= 201512)] # substract the data from 1999 to 2015
n_months_1999_2015 = returns_1999_2015.shape[0] # number of months from 1999 to 2015
return_per_month_1999_2015 = (returns_1999_2015+1).prod()**(1/n_months_1999_2015)-1 # monthly return
annulized_return_1999_2015 = (return_per_month_1999_2015+1)**12-1 # annualized return
print("annulized_return (from 1999 to 2015):\n", annulized_return_1999_2015)
annulized_volatility_1999_2015 = returns_1999_2015.std()*(12**0.5)
print("annulized_volatility (from 1999 to 2015):\n", annulized_volatility_1999_2015)
# task 9 - 12
# calculate the cumulated return
returns["cumreturns Lo 20"] = (returns["Lo 20"]+1).cumprod()
returns["cumreturns Hi 20"] = (returns["Hi 20"]+1).cumprod()
# calculate the previous peaks
returns["previous peaks Lo 20"] = returns[["cumreturns Lo 20"]].cummax()
returns["previous peaks Hi 20"] = returns[["cumreturns Hi 20"]].cummax()
returns["drawdown Lo 20"] = (returns["cumreturns Lo 20"]-returns["previous peaks Lo 20"])/returns["previous peaks Lo 20"] # calculate the drawdown of Lo 20
returns["drawdown Hi 20"] = (returns["cumreturns Hi 20"]-returns["previous peaks Hi 20"])/returns["previous peaks Hi 20"] # calculate the drawdown of Hi 20
drawdown = returns[["drawdown Lo 20", "drawdown Hi 20"]]*(-1) # transform the drawdown in positive number
drawdown_1999_2015 = drawdown[(drawdown.index >= 199901) & (drawdown.index <= 201512)] # substract the drawdown from 1999 to 2015
display(drawdown_1999_2015.head()) # check the drawdown data
max_drawdown = drawdown_1999_2015.max() # identify the maximum drawdown
print(max_drawdown)
max_drawdown_idx = drawdown_1999_2015.idxmax() # identify the index (month) of the maximun drawdown
print(drawdown_1999_2015.loc[max_drawdown_idx])
# task 13 - 16
import pandas as pd
edhec_hf_idx = pd.read_csv("edhec-hedgefundindices.csv", parse_dates=['date'], index_col=['date'])
edhec_hf_idx_since_2009 = edhec_hf_idx[edhec_hf_idx.index.year >= 2009]
display(edhec_hf_idx_since_2009.head())
# the function "semideviation3" is cited by the course material
def semideviation3(r):
"""
Returns the semideviation aka negative semideviation of r
r must be a Series or a DataFrame, else raises a TypeError
"""
excess= r-r.mean() # We demean the returns
excess_negative = excess[excess<0] # We take only the returns below the mean
excess_negative_square = excess_negative**2 # We square the demeaned returns below the mean
n_negative = (excess<0).sum() # number of returns under the mean
return (excess_negative_square.sum()/n_negative)**0.5 # semideviation
# calculate the semideviation
semideviation_edhec_hf_idx_since_2009 = semideviation3(edhec_hf_idx_since_2009)
print('Semideviation:\n', semideviation_edhec_hf_idx_since_2009)
# Calculate skewness
skewness_edhec_hf_idx_since_2009 = edhec_hf_idx_since_2009.skew()
# Calculate kurtosis
edhec_hf_idx_since_2000 = edhec_hf_idx[edhec_hf_idx.index.year >= 2000]
kurtosis_edhec_hf_idx_since_2000 = edhec_hf_idx_since_2000.kurt()
print('\nSkewness:\n', skewness_edhec_hf_idx_since_2009)
print('\nKurtosis:\n', kurtosis_edhec_hf_idx_since_2000)