使用 tinyquant 计算夏普比率
获取数据
以格力股票为例:
GL <- tq_get('000651.sz', from='2017-10-01', to='2018-07-11')
数据集结构:
# A tibble: 186 x 7
date open high low close volume adjusted
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2017-10-09 38.5 39.6 38.0 39.1 116683015 39.1
2 2017-10-10 39.3 39.5 38.2 38.8 85889566 38.8
3 2017-10-11 38.7 40.2 38.5 40.0 140421926 40.0
4 2017-10-12 40.0 40.6 39.8 40.4 96935922 40.4
5 2017-10-13 40.5 40.6 39.8 40.3 53133407 40.3
6 2017-10-16 40.3 40.6 39.5 39.5 56472293 39.5
7 2017-10-17 39.8 40.7 39.4 40.7 83201264 40.7
8 2017-10-18 40.7 41.2 40.4 41.2 77649402 41.2
9 2017-10-19 41.0 41.5 40.6 41.1 66751228 41.1
10 2017-10-20 40.9 40.9 40.0 40.5 49367747 40.5
# ... with 176 more rows
获取每月回报率
gl_monthly_return <- GL %>% tq_transmute(select = adjusted, mutate_fun = periodReturn, period='monthly', col_rename = 'Ra')
返回数据结构:
# A tibble: 10 x 2
date Ra
<date> <dbl>
1 2017-10-31 0.0882
2 2017-11-30 -0.00235
3 2017-12-29 0.0294
4 2018-01-31 0.280
5 2018-02-28 -0.0760
6 2018-03-30 -0.0927
7 2018-04-27 -0.0601
8 2018-05-31 0.0776
9 2018-06-29 -0.00737
10 2018-07-10 -0.0267
添加固定国债利率
这边选取定期银行存款以年利率 2.5%为例,月利率为(0.025/12)= 0.002
data <- mutate(gl_monthly_return, Bank=0.002)
返回数据结构:
# A tibble: 10 x 3
date Ra Bank
<date> <dbl> <dbl>
1 2017-10-31 0.0882 0.002
2 2017-11-30 -0.00235 0.002
3 2017-12-29 0.0294 0.002
4 2018-01-31 0.280 0.002
5 2018-02-28 -0.0760 0.002
6 2018-03-30 -0.0927 0.002
7 2018-04-27 -0.0601 0.002
8 2018-05-31 0.0776 0.002
9 2018-06-29 -0.00737 0.002
10 2018-07-10 -0.0267 0.002
计算夏普比率
sharpRatio <- data %>% tq_performance(Ra=Ra, Rb=Bank, performance_fun = SharpeRatio)
返回数据:
# A tibble: 1 x 3
`ESSharpe(Rf=0.2%,p=95%)` `StdDevSharpe(Rf=0.2%,p=95%)` `VaRSharpe(Rf=0.2%,p=95%)`
<dbl> <dbl> <dbl>
1 0.102 0.174 0.182
夏普比率越高,承受单位风险的回报率越大,传统的夏普比率计算方式是以标准差作为分母,现在推荐使用改良过的夏普比率计算方式,使用 VaR
或者 ES
。
Value-at-Risk(VaR)
This function provides several estimation methods for the Value at Risk (typically written as VaR) of a return series and the Component VaR of a portfolio. Take care to capitalize VaR in the commonly accepted manner, to avoid confusion with var (variance) and VAR (vector auto-regression). VaR is an industry standard for measuring downside risk. For a return series, VaR is defined as the high quantile (e.g. ~a 95 quantile) of the negative value of the returns. This quantile needs to be estimated. With a sufficiently large data set, you may choose to utilize the empirical quantile calculated using quantile. More efficient estimates of VaR are obtained if a (correct) assumption is made on the return distribution, such as the normal distribution. If your return series is skewed and/or has excess kurtosis, Cornish-Fisher estimates of VaR can be more appropriate. For the VaR of a portfolio, it is also of interest to decompose total portfolio VaR into the risk contributions of each of the portfolio components. For the above mentioned VaR estimators, such a decomposition is possible in a financially meaningful way.
Expected Shortfall(ES) (also known as) Conditional Value at Risk(CVaR) or Expected Tail Loss (ETL)
This function provides several estimation methods for the Expected Shortfall (ES) (also called Expected Tail Loss (ETL) or Conditional Value at Risk (CVaR)) of a return series and the Component ES (ETL/CVaR) of a portfolio.
At a preset probability level denoted c, which typically is between 1 and 5 per cent, the ES of a return series is the negative value of the expected value of the return when the return is less than its c-quantile. Unlike value-at-risk, conditional value-at-risk has all the properties a risk measure should have to be coherent and is a convex function of the portfolio weights (Pflug, 2000). With a sufficiently large data set, you may choose to estimate ES with the sample average of all returns that are below the c empirical quantile. More efficient estimates of VaR are obtained if a (correct) assumption is made on the return distribution, such as the normal distribution. If your return series is skewed and/or has excess kurtosis, Cornish-Fisher estimates of ES can be more appropriate. For the ES of a portfolio, it is also of interest to decompose total portfolio ES into the risk contributions of each of the portfolio components. For the above mentioned ES estimators, such a decomposition is possible in a financially meaningful way.
夏普比率的计算公式: https://baike.baidu.com/item/%E5%A4%8F%E6%99%AE%E6%AF%94%E7%8E%87
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: R 调用 Python
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论