Skip to content

Commit

Permalink
Adjust GP2 conditional mean and variance
Browse files Browse the repository at this point in the history
  • Loading branch information
manuhuth committed Nov 20, 2024
1 parent 9ad4575 commit 85f2d90
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions R/cocoResid.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ cocoResid <- function(coco, val.num = 1e-10) {
if (bool_covariates){
lambda <- lambdas[j]
}
fitted_value <- alpha * data[j - seasonality[1]] + lambda
varX_value <- alpha * (1 - alpha) * data[j - seasonality[1]] + lambda
fitted_value <- alpha * data[j - seasonality[1]] + lambda / (1-eta)
varX_value <- alpha * (1 - alpha) * data[j - seasonality[1]] + lambda / (1-eta)^3
return(list(fitted = fitted_value, varX = varX_value))
})

Expand Down Expand Up @@ -97,14 +97,22 @@ cocoResid <- function(coco, val.num = 1e-10) {
meanR <- 0
varR <- 0
prob_accumulated <- 0
probabilities <- c()
r <- 0
while (prob_accumulated < (1 - val.num)) {
prob <- dR2(r, y, z, lambda, alpha1, alpha2, alpha3, eta)
probabilities <- c(probabilities, prob)
prob_accumulated <- prob_accumulated + prob
meanR <- meanR + r * prob
varR <- varR + (r - meanR)^2 * prob
r <- r + 1
}

#define objects for cond. mean and cond. variance computation
n_relevant_probabilities <- r-1 #r-1 as +1 is added in the end within the while loop
relevant_counts <- 0:(r-1)

#compute conditional mean and conditional variance
meanR <- sum(probabilities * relevant_counts)
varR <- sum(probabilities * ((relevant_counts - meanR)^2))

fitted_value <- meanR + lambda / (1 - eta)
varX_value <- varR + lambda / (1 - eta)^3
Expand All @@ -129,7 +137,7 @@ cocoResid <- function(coco, val.num = 1e-10) {
time <- end.time - start.time

list_out <- list(
"fitted" = fitted, "resdiuals" = residuals, "pe.resid" = peResid,
"fitted" = fitted, "residuals" = residuals, "pe.resid" = peResid,
"cond.var" = varX, "type" = coco$type, "order" = coco$order, "ts" = coco$ts,
"par" = par,"duration" = time, xreg = coco$cov
)
Expand Down

0 comments on commit 85f2d90

Please sign in to comment.