rkillick / changepoint

A place for the development version of the changepoint package on CRAN.
127 stars 33 forks source link

possible bug in logLik() computation #78

Open beanumber opened 6 months ago

beanumber commented 6 months ago

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 6 months ago

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 5 months ago

OK, thanks.