+
+.computeDistsEndo <- function(data, today, tdays, predict_from)
+{
+ lastSerie = c( data$getSerie(today-1),
+ data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()] )
+ sapply(tdays, function(i) {
+ delta = lastSerie - c(data$getSerie(i-1),
+ data$getSerie(i)[if (predict_from>=2) 1:(predict_from-1) else c()])
+ sqrt(mean(delta^2))
+ })
+}
+
+.computeDistsExo <- function(data, today, tdays)
+{
+ M = matrix( ncol=1+length(tdays), nrow=1+length(data$getExo(1)) )
+ M[,1] = c( data$getLevelHat(today), as.double(data$getExoHat(today)) )
+ for (i in seq_along(tdays))
+ M[,i+1] = c( data$getLevel(tdays[i]), as.double(data$getExo(tdays[i])) )
+
+ sigma = cov(t(M)) #NOTE: robust covariance is way too slow
+ # TODO: 10 == magic number; more robust way == det, or always ginv()
+ sigma_inv =
+ if (length(tdays) > 10)
+ solve(sigma)
+ else
+ MASS::ginv(sigma)
+
+ # Distances from last observed day to days in the past
+ sapply(seq_along(tdays), function(i) {
+ delta = M[,1] - M[,i+1]
+ delta %*% sigma_inv %*% delta
+ })
+}