我想得到n天的谐波平均值
例如
a <-c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
assuming month end is `3, 6, 9 ,12`
If n = 2, i want to get the harmonic mean of
1,2,3,4,5 (t-2~ t+2 with midpoint 3)
4,5,6,7,8 (t-2~ t+2 with midpoint 6) and so on
if n = 1 it would be
harmonic mean of 2,3,4 for month-end :3
我的每日收益为xts。我要做的就是得到n天的调和平均值。谢谢
答案 0 :(得分:1)
我不清楚您要做什么。我假设您要计算具有固定窗口宽度n
的滚动谐波平均值;为此,我们可以使用zoo::rollapplyr
n <- 5
rollapplyr(a, width = n, FUN = function(x) 1 / mean(1 / x), fill = NA)
#[1] NA NA NA NA 2.189781 3.448276 4.575163
#[8] 5.652759 6.705695 7.744315 8.773818 9.797130 10.816035 11.831685
#[15] 12.844861