利用R语言结构投资组合的有效前沿
结构投资组合是金融投资阐明中长期弥新的问题。多年以来,学界、业界提出诸多对投资组合举办优化的要领。好比,最经典的基于收益率均值和收益率颠簸性举办组合优化,由于马克维滋提出用收益率方差暗示收益率的颠簸性,所以,这种要领又称为的 M-V 要领,即 Mean-Variance
要领的缩写;厥后,又衍生出基于夏普比率(Sharp Ratio
)的投资组合优化要领;连年来,跟着VaR
(Value at Risk
) 和 CVaR
(Conditional Vaule at Risk
) 观念的鼓起,基于 VaR
和 CVaR
对投资组合举办优化的思路也开始勃兴;除此之外,对冲基金届尚有一种很是有生命力的投资组合优化要领,即桥水公司(Bridge-Water
)公司提出的风险均派要领( Risk Pairy
),这种要领的焦点思路在于,预计组合中各个资产的风险度及其占组合风险的比率,然后,凭据该比例对组合头寸举办分派。
几种要领中,在学界和业界最收存眷的照旧 M-V
要领。而在 M-V
要领中最根基的一个常识点,就是结构投资组合的有效前沿。理论这里不再赘述,简朴说一下其在 R
语言中的实现。结构有效前沿的步调大抵可凭据获取数据、将数据加工处理惩罚为收益率矩阵、以收益率矩阵为输入计较获得有效前沿这三个步调来完成。下面漫衍来说一说。
第一步,获取数据。最简朴的要领是利用 quantmod
中的 getSymbols
函数。因为要要做的事是构建资产组合,因此,得同时获取多只股票的生意业务数据,这里取 QQQ/SPY/YHOO 三只股票为例。需要运行的代码:
# 载入 quatnmod 包
require(quantmod)
# 下载 QQQ/SPY/YHOO 生意业务数据
getSymbols(c('QQQ','SPY','YHOO'))
第二步,将生意业务数据处理惩罚为收益率数据。这一步可以用 dailyReturn 函数来完成。
# 计较收益率序列
QQQ_ret=dailyReturn(QQQ)
SPY_ret=dailyReturn(SPY)
YHOO_ret=dailyReturn(YHOO)
第三步,归并收益率序列。
dat=merge(QQQ_ret,SPY_ret,YHOO_ret)
第四步,计较投资组合的有效前沿。这一步利用 portfolioFrontier 函数来完成。由于 portfolioFrontier 函数的输入必需是 timeSeries 类,因而,得将数据范例举办转化。
## 转化为 timeSeries 类
require(timeSeries)
dat=as.timeSeries(dat)
## 载入 fPortfolio
require(fPortfolio)
## 求frontier
Frontier = portfolioFrontier(dat)
Frontier
Title:
MV Portfolio Frontier
Estimator: covEstimator
Solver: solveRquadprog
Optimize: minRisk
Constraints: LongOnly
Portfolio Points: 5 of 50
Portfolio Weights:
daily.returns daily.returns.1 daily.returns.2
1 0.0000 1.0000 0.0000
13 0.2409 0.7541 0.0050
25 0.4853 0.5090 0.0057
37 0.7296 0.2640 0.0065
50 1.0000 0.0000 0.0000
Covariance Risk Budgets:
daily.returns daily.returns.1 daily.returns.2
1 0.0000 1.0000 0.0000
13 0.2355 0.7596 0.0049
25 0.4877 0.5065 0.0058
37 0.7390 0.2545 0.0065
50 1.0000 0.0000 0.0000
Target Return and Risks:
mean mu Cov Sigma CVaR VaR
1 0.0002 0.0002 0.0151 0.0151 0.0368 0.0233
13 0.0003 0.0003 0.0149 0.0149 0.0361 0.0230
25 0.0003 0.0003 0.0148 0.0148 0.0358 0.0234
37 0.0004 0.0004 0.0149 0.0149 0.0356 0.0241
50 0.0005 0.0005 0.0152 0.0152 0.0357 0.0249
Description:
Fri Aug 09 11:21:31 2013 by user: Owner
上面功效中 title 部门表白的是本次操纵进程中利用的相关要领。Portfolio Weights
部门返回的是三只股票在投资组合中的头寸比例,每一行的和都是 1 。对付第二行,它暗示的是在投资组合中将总头寸以 24.09% 、 75.41% 、
0.50% 的比例分手到三只股票标的上。Covariance Risk Budgets 暗示的是协方差风险预算矩阵。Target Return
and Risks 暗示方针组合的预期收益率和风险数据。
挪用 plot 函数可以对上述功效举办画图,挪用 plot 之后,R 节制台会返回一组画图选项卡:
plot(Frontier)
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
各选项卡对应的画图范例依次是:有效前沿、最小风险组合、切线组合、单个资产的风险/收益、等权重投资组合、两资产投资组合的有效前沿(克制卖空)、模特卡罗模仿获得的投资组合、夏普比率。依次,选择可以看到相应的画图功效:
Selection: 1
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
#p#分页标题#e#
Selection: 2
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 3
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 4
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 5
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 6
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 7
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]
Selection: 8
Make a plot selection (or 0 to exit):
1: Plot Efficient Frontier
2: Add Minimum Risk Portfolio
3: Add Tangency Portfolio
4: Add Risk/Return of Single Assets
5: Add Equal Weights Portfolio
6: Add Two Asset Frontiers [LongOnly Only]
7: Add Monte Carlo Portfolios
8: Add Sharpe Ratio [Markowitz PF Only]