我有两个数据集。两者都是xts
对象。
> dput(head(all_data[,2:3]))
structure(c(0.00108166576527857, 0.00324149108589955, 0, 0, 0.00484652665589658,
0.00267952840300101, 0.00606980273141122, 0.00301659125188536,
0.00526315789473686, -0.00149588631264019, 0, -0.00299625468164799
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), .indexTZ = "UTC", tclass = c("POSIXct", "POSIXt"), tzone = "UTC", index = structure(c(1453716060,
1453716120, 1453716180, 1453716240, 1453716300, 1453716360), tzone = "UTC", tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("ClosePrice_AGL.1",
"ClosePrice_AMC")))
> dput(head(all_data[,1]))
structure(c(0.00108166576527857, 0.00324149108589955, 0, 0, 0.00484652665589658,
0.00267952840300101), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "UTC", tclass = c("POSIXct", "POSIXt"), tzone = "UTC", index = structure(c(1453716060,
1453716120, 1453716180, 1453716240, 1453716300, 1453716360), tzone = "UTC", tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "ClosePrice_AGL"))
> dput(head(mydata_train[,1:3]))
structure(c(-0.00155763239875384, -0.0279251170046803, -0.00225324987963404,
-0.000479333950998528, 0.0042195179257094, -0.00163456299477571,
-0.00526315789473697, -0.0222222222222221, -0.00431818181818178,
-0.00218475886131686, 0.00217864923747269, -0.00217391304347825,
-0.00651612903225807, -0.0221442950840964, -0.00385177314384377,
0.00333333333333319, -0.00365448504983379, -0.0160053351117039
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), tclass = c("POSIXct", "POSIXt"), tzone = "", index = structure(c(1527255180,
1527256080, 1527256260, 1527256440, 1527256800, 1527256980), tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("ACBFF.Close",
"APHQF.Close", "WDDMF.Close")))
> dput(head(mydata_train[,4]))
structure(c(0.00429610046265694, -0.00789733464955589, -0.00165837479270303,
-0.00299003322259139, 0.00333222259246901, -0.00199269345732311
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), tclass = c("POSIXct", "POSIXt"), tzone = "", index = structure(c(1527255180,
1527256080, 1527256260, 1527256440, 1527256800, 1527256980), tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "MJ.Close"))
我正在从以下位置运行spIndexTrack
:
library(sparseIndexTracking)
test <- spIndexTrack(all_data[,2:3] , all_data[,1], lambda = 1e-7, u = 0.5, measure = 'ete')
test <- spIndexTrack(mydata_train[,1:3] , mydata_train[,4], lambda = 1e-7, u = 0.5, measure = 'ete')
第二个函数给出:
w
ACBFF.Close 0.47083543
APHQF.Close 0.42967200
WDDMF.Close 0.09949257
但第一个失败:
Error in if (abs(a + 1) < 1e-06) { :
missing value where TRUE/FALSE needed
我没有NA
s
all_data <- all_data[complete.cases(all_data),]
any(is.na(all_data) == TRUE)
我的所有数据都是数字。
storage.mode(my_data) <- "numeric"
我可以进行回归分析而不会出错:
lm(all_data[,1] ~ all_data[,2:3])
这不是我的数据帧中包含0的结果
all_data[all_data==0] <- 1e-9
尝试将换行作为矩阵:
as.matrix(all_data)
不知道出了什么问题。
如果有人想使用在线google / yahoo数据运行完整的示例,则可以:
library(sparseIndexTracking)
library(xts)
library(gquote)
library(PerformanceAnalytics)
#######################################
############ SET PARAMETERS #########
#######################################
# Data
minute_interval <- 3
n_periods <- 10000
#######################################
############ GET DATA #########
#######################################
# pull yahoo / google data for the portfolio (2 stocks)
mydata <- merge(getIntradayPrice('ACBFF', period=n_periods, interval = minute_interval),
getIntradayPrice('APHQF', period=n_periods, interval = minute_interval),
getIntradayPrice('WDDMF', period=n_periods, interval = minute_interval),
getIntradayPrice('MJ', period=n_periods, interval = minute_interval),
getIntradayPrice('HMLSF', period=n_periods, interval = minute_interval)
)
#select just closing prices
mydata <- mydata[,c(1,6, 11, 16)]
# remove NA values
mydata <- mydata[complete.cases(mydata),]
# replace all with returns of the two series - can use 'log' or 'discrete'
mydata <- Return.calculate(mydata, method = 'discrete')
# remove NA values again
mydata <- mydata[complete.cases(mydata),]
## split set into first 50% training data second 50% test data
mydata_train <- mydata[1:floor(nrow(mydata) * 0.5),]
mydata_test <- mydata[floor(nrow(mydata) * 0.5 +1):nrow(mydata),]
# remove NA values again
mydata_train <- mydata_train[complete.cases(mydata_train),]
# Generate weights see : https://cran.r-project.org/web/packages/sparseIndexTracking/vignettes/SparseIndexTracking-vignette.pdf
w_ete <- spIndexTrack(mydata_train[,1:3] , mydata_train[,4], lambda = 1e-7, u = 1.5, measure = 'ete')
w_ete
我被困住了。不知道是否有人可以提供帮助。预先感谢。
答案 0 :(得分:4)
数据准备
spIndexTrack
首先在您的输入对象上应用as.matrix
,因为函数需要一个矩阵和一个向量
spIndexTrack(X, r, lambda, u = 1, measure = c("ete", "dr", "hete", "hdr"),
hub = NULL, w0 = NULL, thres = 1e-09)
参数:
X: m-by-n matrix of net returns (m samples, n assets).
r: m dimensional vector of the net returns of the index.
为简单起见,您只能dput(datamat <- as.matrix(all_data))
,即
datamat <-
structure(c(0.00108166576527857, 0.00324149108589955, 0, 0, 0.00484652665589658,
0.00267952840300101, 0.00108166576527857, 0.00324149108589955,
0, 0, 0.00484652665589658, 0.00267952840300101, 0.00606980273141122,
0.00301659125188536, 0.00526315789473686, -0.00149588631264019,
0, -0.00299625468164799), .Dim = c(6L, 3L), .Dimnames = list(
c("2016-01-25 10:01:00", "2016-01-25 10:02:00", "2016-01-25 10:03:00",
"2016-01-25 10:04:00", "2016-01-25 10:05:00", "2016-01-25 10:06:00"
), c("ClosePrice_AGL", "ClosePrice_AGL.1", "ClosePrice_AMC"
)))
然后设置
X <- datamat[, 2:3]
r <- datamat[, 1]
确定问题
请注意,X[, 1]
和r
是相同的:
identical(X[, 1], r)
#[1] TRUE
您可以运行lm(r ~ 0 + X)
,但是r
可以完美地解释X[, 1]
,X[, 2]
的系数最终为零:
XClosePrice_AGL.1 XClosePrice_AMC
1.000e+00 -6.346e-19
根据SparseIndexTracking vignette: explanation of the algorithms,spIndexTrack
正在使用类似套索的正则化方法进行约束最小二乘法。
对于上面的X
和r
,不受约束的普通最小二乘已经为您提供最佳的稀疏组合:(1, 0)
。您还希望spIndexTrack
做什么?
## your call to `spIndexTrack`
spIndexTrack(X, r, lambda = 1e-7, u = 0.5, measure = 'ete')
尤其是,您设置了u = 0.5
,这要求任何投资组合权重都不得大于0.5
。假设最佳权重向量为(1, 0)
,该算法可能会费劲。
我认为您最好不要更改默认值u = 1
。此默认设置表示该算法可以将除一个功能以外的所有功能都缩小为零。
现在,即使以下情况也会失败。
spIndexTrack(X, r, lambda = 1e-7, u = 1, measure = 'ete')
因此,我计划将正则化设置为零,即lambda = 0
,但仍然失败。我必须将其设置为微小的负数才能使其正常工作:
spIndexTrack(X, r, lambda = -1e-16, u = 1, measure = 'ete')
# w
#ClosePrice_AGL.1 9.999996e-01
#ClosePrice_AMC 3.633128e-07
如您所见,结果接近(1, 0)
。
包装的数值稳定性不足
我在CRAN上检查了这个软件包。截至今天(2018年7月30日),它仍处于第一个版本(版本0.1.0)。
通过浏览spIndexTrack
的R代码,我发现基本上没有进行数值稳定性测试。您得到的错误仅仅是因为您得到0 / 0
,因此得到了变量NaN
的{{1}}。
我不感兴趣通过数学算法来思考应该进行哪些先前的数值测试。以下建议是最可靠的建议,但可能过于局限,但是您问题中的另一个可行示例(与a
关联)满足了该条件。
mydata_train
具有完整排名,即X
等于qr(X)$rank
; ncol(X)
具有完整排名,也就是说,cbind(X, r)
的残差不是全零。软件包作者有责任考虑这一点。但是最后,该函数应首先检查数值错误并返回早期的信息错误。