我正在尝试将季度收益转换为年度收益。给定一个季度回报向量,该怎么做?
我是R编程的新手,所以我真的一无所获。
给定一个a包含季度收益的向量:
a <- c(0.11, 0.02, 0.01, 0.1, 0.08, 0.04, 0.02, 0.03) # Two years worth of returns
Run Code Online (Sandbox Code Playgroud)
我想应用一些函数,该函数使用公式输出带有年收益的长度为2的向量:
Year 1: ((1 + 0.11) * (1 + 0.02) * (1 + 0.01) * (1 + 0.1))-1 = 0,2578742
Year 2: ((1 + 0.08) * (1 + 0.04) * (1 + 0.02) * (1 + 0.03))-1 = 0,180033
Run Code Online (Sandbox Code Playgroud)
最终向量:
yearly_vec <- c(0.2578742, 0.180033)
Run Code Online (Sandbox Code Playgroud)
使用apply:
apply(matrix(a, nrow = 4), 2, function(x) prod(1 + x) - 1)
#[1] 0.2578742 0.180033
Run Code Online (Sandbox Code Playgroud)