n-kall / priorsense

priorsense: an R package for prior diagnostics and sensitivity
https://n-kall.github.io/priorsense/
GNU General Public License v3.0
53 stars 5 forks source link

Infinite likelihood passed to `loo::relative_eff(x = exp(-log_ratios))` in `powerscale` #22

Closed jflournoy closed 3 months ago

jflournoy commented 11 months ago

In priorsense::powerscale.priorsense_data, r_eff is computed as part of the call to is_method:

https://github.com/n-kall/priorsense/blob/ee8dc8b4c3e2940e5206b0f1a54ccba7c15fd368/R/powerscale.R#L100-L105

Debuging this, it's specifically for the likelihood when alpha is 1.01 (and maybe other values):

Browse[2]> component
[1] "likelihood"
Browse[2]> alpha
[1] 1.01
Browse[2]> head(as.numeric(log_ratios))
[1] -1003.796 -1004.436 -1004.138 -1004.078 -1004.100 -1004.466

I get the following error as a result of the call to loo::relative_eff in the above lines:

Browse[2]> loo::relative_eff(x = exp(-log_ratios))
Error in if (varx == 0) { : missing value where TRUE/FALSE needed

I believe this is because exp(-log_ratios) evaluates to:

Browse[2]> exp(-log_ratios)
# A draws_array: 1000 iterations, 4 chains, and 1 variables
, , variable = sum

         chain
iteration   1   2   3   4
        1 Inf Inf Inf Inf
        2 Inf Inf Inf Inf
        3 Inf Inf Inf Inf
        4 Inf Inf Inf Inf
        5 Inf Inf Inf Inf

# ... with 995 more iterations
n-kall commented 11 months ago

Thanks for reporting this. Looks like an issue with too small log ratios. I might add a warning and not calculate the r_eff in this case, or it might need some further fixing.

avehtari commented 9 months ago

A fix would be to change exp(-log_ratios) to exp(-(log_ratios-max(log_ratios)))

n-kall commented 8 months ago

This should be fixed now with @avehtari suggestion