possible bug in logLik() computation
beanumber opened this issue · 2 comments
beanumber commented
I don't know if you will consider this a bug or not, but it baffled me for a while, so I'd figured I would report it.
data <- tidychangepoint::CET |>
as.ts()
library(changepoint)
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#> Successfully loaded changepoint package version 2.2.4
#> See NEWS for details of changes.
x <- cpt.meanvar(data, method = "PELT")
x@cpts
#> [1] 55 57 309 311 330 362
logLik(x)
#> -2*logLik -2*Loglike+pen
#> -Inf -Inf
y <- cpt.meanvar(data, method = "PELT", minseglen = 3)
y@cpts
#> [1] 237 330 362
logLik(y)
#> -2*logLik -2*Loglike+pen
#> 619.9626 656.9956
Created on 2024-04-04 with reprex v2.1.0
The problem occurs here, because there is a segment of length 2 with 0 variance, and thus the likelihood computation returns -Inf
.
It seems weird to me that the algorithm works, but the logLik()
returns -Inf
. If not an error, perhaps at least a warning should be thrown?
rkillick commented
Yes, this is a "feature" :-)
The logLik() function should handle this in the same way that the C code handles segments with zero variance.
Thanks for catching!
beanumber commented
OK, thanks.