资产收益率的时间序列
在 R 中计算单期收益率非常直接。当需要针对不同日期计算收益率时,R 包 PerformanceAnalytics 中的函数 Return.calculate() 和 Return.portfolio() 非常有用。它们要求输入数据是 xts 时间序列类,课程环境已预加载。您将在本练习中探索 PerformanceAnalytics 包的用法。
工作区中已提供包含苹果公司(aapl)和微软公司(msft)股票的对象 prices。
本练习是课程的一部分
R 中的投资组合分析入门
练习说明
- 在当前 R 会话中加载
PerformanceAnalytics包。 - 分别使用
head()和tail()查看prices的前 6 行和后 6 行。 - 使用函数
Return.calculate(),仅以prices作为参数,按日期计算相对于前一日期的价格百分比变化作为收益率,并将结果命名为returns。 - 打印
returns的前 6 行。 - 删除
returns的第一行。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Load package PerformanceAnalytics
# Print the first six rows and last six rows of prices
# Create the variable returns using Return.calculate()
# Print the first six rows of returns. Note that the first observation is NA because there is no prior price.
# Remove the first row of returns
returns <- returns[-1, ]