def main(number_of_obs=None): # Call the function from edhec_risk_kit hfi = erk.get_hfi_returns() # Semi deviation hfi.std(ddof=0) # average deviation from the mean hfi[hfi < 0].std(ddof=0) erk.semideviation[hfi] # VaR and CVaR # Value at Risk and Conditional Value at Risk # Historic VaR # What's value at risk at 5%: 5 percentile VaR np.percentile(hfi, 5, axis=0) erk.var_historic(hfi) # Parametric VaR - Gaussian parametric z = norm.ppf( .05 ) # normal dist: return a z-score where half of the distribution lies below it -hfi.mean() + z * hfi.std(ddof=0) # Modified Cornish - Fisher VaR var_list = [ erk.var_gaussian(hfi), erk.var_gaussian(hfi, modified=True), erk.var_historic(hfi) ] comparison = pd.concat(var_list) comparison.columns = ['Gaussian', 'Cornish-fisher', 'Historic'] comparison.plot.bar(title='EDHEC Hedfe Fund Indicies: VaR') # Beyond Car aka. CVaR erk.cvar_historic(hfi)
def main(number_of_obs=None): # Call the function from edhec_risk_kit hfi = erk.get_hfi_returns() print(hfi.head()) # Measure skewness print(pd.concat([hfi.mean(), hfi.median(), hfi.mean() > hfi.median()], axis = 'columns')) erk.skewness(hfi).sort_values() # Comparing built in method (scipy) with our own function scipy.stats.skew(hfi) erk.skewness(hfi) #hfi.shape() normal_rets = np.random.normal(0, .15, size = (263, 1)) # Random generated returns in the form of 263x1 array erk.skewness(normal_rets) # Measure kurtosis erk.kurtosis(normal_rets) scipy.stats.kurtosis(normal_rets) # Excess kurtosis # Jarque Bera Test scipy.stats.jarque_bera(normal_rets) scipy.stats.jarque_bera(hfi) erk.is_normal(normal_rets) erk.is_normal(hfi) hfi.aggregate(erk.is_normal) # Take the given function, apply it on every col and give result ffme = erk.get_ffme_returns() erk.skewness(ffme) erk.kurtosis(ffme) erk.is_normal(ffme) ffme.aggregate(erk.is_normal)
import pandas as pd import edhec_risk_kit as erk %load_ext autoreload %autoreload 2 %matplotlib inline hfi = erk.get_hfi_returns() hfi.std(ddof=0) #filter those returns which are <0, its a boolean mask hfi[hfi<0].std(ddof=0) erk.semideviation(hfi) import numpy as np #we want to run percentile only on columns which axis=0 np.percentile(hfi,5, axis=0) # how to interpret percentile info: # first column is Convertible Arbitrage. # There is 5% chance that in any given month Convertible Arbitrage # can lose 1.5% (-0.01576) or worse def var_historic(r, level=5): """ VaR Historic """ if isinstance(r, pd.DataFrame): return r.aggregate(var_historic, level=level) elif isinstance (r, pd.Series):
ann_ret3 = er.annualize_rets(rets.loc['1999-01':'2015-12', 'SmallCap'], 12) ann_vol3 = er.annualize_vol(rets.loc['1999-01':'2015-12', 'SmallCap'], 12) ann_ret4 = er.annualize_rets(rets.loc['1999-01':'2015-12', 'LargeCap'], 12) ann_vol4 = er.annualize_vol(rets.loc['1999-01':'2015-12', 'LargeCap'], 12) dd = er.drawdown(rets.loc['1999-01':'2015-12':, 'SmallCap']) min(dd.Drawdown) dd[dd.Drawdown == min(dd.Drawdown)] dd2 = er.drawdown(rets.loc['1999-01':'2015-12':, 'LargeCap']) min(dd2.Drawdown) dd2[dd2.Drawdown == min(dd2.Drawdown)] #%% hfi = er.get_hfi_returns() sd = er.semideviation(hfi.loc['2009-01':, :]) sd.sort_values() er.skewness(hfi.loc['2009-01':, :]).sort_values() er.kurtosis(hfi.loc['2000-01':, :]).sort_values() #%% # ============================================================================= # 1. 15.2 # 2. 33.7 # 3. 9.8 # 4. 19.5 # 5. 11.4 # 6. 22.9