gpt4 book ai didi

r - 逐行对列表中的 xts 元素求和

转载 作者:行者123 更新时间:2023-12-04 12:37:12 25 4
gpt4 key购买 nike

我有一个名为 data 的 xts 对象,其中包含从 2015-01-01 17:00:00 到 2015-12-31 17:00:00 期间的 5 分钟返回。每个交易日从17:00:00开始至次日同一时间结束,共计288个日 yield [(24小时*60分钟)/5分钟=288个盘中 yield ]。返回表示为

head(data, 5)
DPRICE
2015-01-01 17:00:00 0.000000e+00
2015-01-01 17:05:00 9.797714e-05
2015-01-01 17:10:00 2.027022e-04
2015-01-01 17:15:00 2.735798e-04
2015-01-01 17:20:00 7.768653e-05

tail(data, 5)
DPRICE
2015-12-31 16:40:00 0.0001239429
2015-12-31 16:45:00 0.0001272704
2015-12-31 16:50:00 0.0010186764
2015-12-31 16:55:00 0.0006841370
2015-12-31 17:00:00 0.0002481227

我正在尝试根据 McMillan 和 Speight 每日外汇波动率预测(2012 年),按照每 5 分钟日内间隔的平均绝对值对数据进行标准化。

数学公式是: enter image description here

我的*代码是

library(xts)
std_data = abs(data) #create absolute returns
D <- split(std_data, "days") #splits data to days
mts.days <- lapply(seq_along(D) - 1, function(i) {
if (i > 0) rbind(D[[i]]["T17:00:00/T23:55:00"], D[[i + 1]]["T00:00:00/T16:55:00"])
}) #creates a list with 365 elements each containing 288 unique returns
dummy = mapply(sum, mts.days) #add the first,second... observations from each element

使用这段代码,我创建了一个包含 365 个 xts 元素的列表,每个元素都有维度

> dim(mts.days[[2]])
[1] 288 1

我想从每个元素添加相同的观察结果来创建上述函数的分母。

最佳答案

我不明白你的要求,但还是会试一试。

## generate bogus data
library(quantmod)
set.seed(123)
ndays <- 3
ndatperday <- 288
data <- cumsum(do.call("rbind", lapply(13:15, function(dd){
xts(rnorm(ndatperday)/1e4,
seq(as.POSIXct(paste0("2016-08-",dd," 17:00:00")),
length = ndatperday, by = 300))

})))
colnames(data) <- "DPRICE"

## calculate percentage returns
ret <- ROC(data, type="discrete")

## this is probably not what you need: returns divided by the overall mean
ret/mean(abs(ret), na.rm=T)

## I suspect indeed that you need returns divided by the daily mean return
library(dplyr)
ret.df <- data.frame(ret)
## create a factor identifying the 3 days of bogus data
ret.df$day <- rep(paste0("2016-08-",13:15),each=ndatperday)
## compute daily mean return
dail <- ret.df %>%
group_by(day) %>%
summarise(mean=mean(abs(DPRICE), na.rm=TRUE))
## attach daily mean returns to the days they actually are associated to
ret.df <- ret.df %>% left_join(dail)
## normalize
ret.df$DPRICE <- ret.df$DPRICE/ret.df$mean

%%%%%%%%%

第二枪:看完论文(http://onlinelibrary.wiley.com/doi/10.1002/for.1222/full)我可能已经明白你在找什么了:

library(quantmod)
library(dplyr)
set.seed(123)

## generate bogus 5-min series
ndays <- 365
ndatperday <- 288
data <- as.xts(zoo(0.1+cumsum(rt(ndays*ndatperday, df=3))/1e4,
seq(as.POSIXct("2015-01-01 17:00"),
as.POSIXct("2015-12-31 17:00"), by=300)))
colnames(data) <- "DPRICE"

## calculate 5-min percentage returns
ret <- ROC(data, type="discrete")

## create a factor identifying the 5-minute intra-day interval
ret.df <- as.data.frame(ret)
ret.df$intra5 <- strftime(index(ret), format="%H:%M")

## compute mean returns (over the year) for each of the 288 5-minute intra-day intervals
dail <- ret.df %>%
group_by(intra5) %>%
summarise(mean=mean(abs(DPRICE), na.rm=TRUE))

## attach mean returns to each datapoint
ret.df <- ret.df %>% left_join(dail)

## normalize
ret.df$DPRICE <- ret.df$DPRICE/ret.df$mean

关于r - 逐行对列表中的 xts 元素求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com