Last active
January 1, 2018 19:56
-
-
Save Demandrel/132af312a972cec4f18d1a991d4d2135 to your computer and use it in GitHub Desktop.
Mesures de risque et benchmark de stratégie small cap en R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Importation et installation des librairires utiles | |
# 0.1 Instalation et chargement des packages | |
install.packages("tidyquant") #Permet la construction de portefeuille et la manipulation des données financiéres | |
install.packages("PortfolioAnalytics") #Permet le calcul des rendements et mesures de risques | |
#Puis on les charges | |
library("tidyquant") | |
library("PortfolioAnalytics") | |
# REMARQUE | |
# On assume que il existe une variable xts du nom de portfolio_returns qui contient les retours MENSUELS du portefeuille à tester | |
#------------------------------ CALCUL DES PERFORMANCES ET MESURES DE RISQUES ------------------------------ | |
#3.0 Choix du benchmark : Russel2000 | |
#Calcul du Russel2000 qui est indice de small caps US et qui servira de benchmark de référence | |
portfolio_start_date = start(portfolio_returns) | |
portfolio_end_date = end(portfolio_returns) | |
russel2000 = getSymbols(Symbols = "^RUT", from = portfolio_start_date, to = portfolio_end_date, auto.assign = FALSE) | |
russel2000 = russel2000$RUT.Adjusted | |
russel2000_return = Return.calculate(russel2000) | |
russel2000_return = russel2000_return[-1,] | |
#3.1 Calcul des variables statistiques de base : moyenne, eccart type, variance, skew, kurt, etc. | |
#3.1.1 Retour anualisé | |
pf_Return.annualized = Return.annualized(portfolio_returns) | |
benchmark_Return.annualized = Return.annualized(russel2000_return) | |
#On fait en moyenne 15,25% par an contre le benchmark qui fait 13.11% | |
#3.1.2 Sd, Ske et kurt | |
pf_sd_annualized = StdDev.annualized(portfolio_returns) | |
benchmark_sd_annualized = StdDev.annualized(russel2000_return) | |
pf_skewness = skewness(portfolio_returns) | |
benchmark_skewness = skewness(russel2000_return) | |
pf_kurt = kurtosis(portfolio_returns) | |
benchmark_kurt = kurtosis(russel2000_return) | |
#3.2 Calcul du beta de Fama, qui mesure la perte de diversification. | |
pf_fama_beta = FamaBeta(portfolio_returns, russel2000_return) | |
benchkmark_fama_beta = 1 | |
#3.3 Calcul de la Var à 95% | |
pf_var = VaR(portfolio_returns) | |
benchmark_var = VaR(russel2000_return) | |
#3.4 Calcul du ratio de Sharpe ajusté | |
#Calcul du Ratio de Sharpe ajusté, recomandé par Shape. Rf est la moyenne de l'actif sans risque sur la période | |
pf_sharpe_ratio = AdjustedSharpeRatio(portfolio_returns, Rf = mean(free_risk_1month)) | |
benchmark_sharpe_ratio = AdjustedSharpeRatio(russel2000_return, Rf = mean(free_risk_1month)) | |
#3.5 Calcul du ratio de Trenor | |
pf_treynor_ratio = TreynorRatio(portfolio_returns, Rb = russel2000_return, Rf = mean(free_risk_1month)) | |
benchmark_treynor_ratio = TreynorRatio(russel2000_return, Rb = russel2000_return, Rf = mean(free_risk_1month)) | |
#3.6 Calcul de l'alpha de Jensen | |
pf_jensen_alpha = CAPM.jensenAlpha(portfolio_returns, Rb = russel2000_return, Rf = mean(free_risk_1month)) | |
benchmark_jensen_alpha = CAPM.jensenAlpha(russel2000_return, Rb = russel2000_return, Rf = mean(free_risk_1month)) | |
#3.7 Calcul du ratio de Sortino | |
pf_sortino = SortinoRatio(portfolio_returns) | |
benchmark_sortino = SortinoRatio(russel2000_return) | |
#3.8 DR Ratio | |
#Ce ratio mesure la part de rendements positifs par rapports aux rendements négatif. Proche de 0 est le mieux. | |
pf_drRatio = DRatio(portfolio_returns) | |
benchmark_drRatio = DRatio(russel2000_return) | |
#3.9 DrawdownPeak | |
#classement des drawdown | |
pf_drawdowns = sortDrawdowns(findDrawdowns(portfolio_returns)) | |
benchmark_drawdowns = sortDrawdowns(findDrawdowns(russel2000_return)) | |
#Une visualisation des drawdowns est possible ici : | |
charts.PerformanceSummary(portfolio_returns) | |
# Et un classement ici, les chiffres affichés étants directement en % : | |
show(benchmark_drawdowns$return*100) | |
#3.10 SkewnessKurtosisRatio | |
pf_SkewnessKurtosisRatio = SkewnessKurtosisRatio(portfolio_returns) | |
benchmark_SkewnessKurtosisRatio = SkewnessKurtosisRatio(russel2000_return) | |
#3.11 Résumé des mesures de risque | |
pf_risk_mesures = c(pf_Return.annualized,pf_sd_annualized, pf_skewness, pf_kurt, pf_fama_beta, pf_var, pf_sharpe_ratio, pf_treynor_ratio, pf_jensen_alpha, pf_sortino, pf_drRatio) | |
benchmark_risk_mesures = c(benchmark_Return.annualized, benchmark_sd_annualized, benchmark_skewness, benchmark_kurt, benchmark_fama_beta, benchmark_var, benchmark_sharpe_ratio, benchmark_treynor_ratio, benchmark_jensen_alpha, benchmark_sortino, benchmark_drRatio) | |
#On merge cela dans un dataframe : | |
risk_resume = data.frame(portefeuille = pf_risk_mesures, russel2000 = benchmark_risk_mesures) | |
#On le transpose et on nomme les colonnes pour obtenir le résultat sous forme d'un tableau correct | |
risk_resume = t(risk_resume) | |
colnames(risk_resume) = c("retour anualisé", "sd anualisée", "skeweness", "kurtosis", "beta de fama", "var 5%", "ratio de Sharpe", "ratio de Treynor", "alpha de Jensen", "ratio de Sortino", "drRatio") | |
#Visualisation des résultats | |
View(risk_resume) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment