reconhub / distcrete

:chart_with_upwards_trend::point_right::bar_chart: Discretise distributions
http://reconhub.github.io/distcrete
Other
5 stars 1 forks source link

Non-finite values flanked by finite values in the tail of the distribution #20

Open CyGei opened 2 years ago

CyGei commented 2 years ago

Hello, In the example below I would not expect non-finite values flanked by finite values in the tail of the distribution (see from index 92) when generating the values of the probability mass function. Shouldn't all subsequent values after the 1st -Inf also be -Inf? Thank you

library(distcrete)
#> Warning: package 'distcrete' was built under R version 4.1.1
packageVersion("distcrete")
#> [1] '1.0.3'

dist <- distcrete::distcrete(
  "gamma",
  shape = 2.652245,
  scale = 2.149123,
  interval = 1,
  w = 1
)

log(dist$d(1:100))
#>   [1]  -3.734979  -2.467984  -2.085171  -1.990748  -2.038198  -2.170087
#>   [7]  -2.358006  -2.585823  -2.843500  -3.124365  -3.423756  -3.738292
#>  [13]  -4.065437  -4.403246  -4.750190  -5.105048  -5.466827  -5.834710
#>  [19]  -6.208019  -6.586181  -6.968708  -7.355186  -7.745253  -8.138598
#>  [25]  -8.534946  -8.934056  -9.335717  -9.739739 -10.145952 -10.554207
#>  [31] -10.964365 -11.376305 -11.789914 -12.205092 -12.621746 -13.039792
#>  [37] -13.459152 -13.879755 -14.301536 -14.724434 -15.148393 -15.573362
#>  [43] -15.999292 -16.426139 -16.853860 -17.282418 -17.711775 -18.141897
#>  [49] -18.572753 -19.004313 -19.436548 -19.869432 -20.302941 -20.737049
#>  [55] -21.171736 -21.606980 -22.042761 -22.479060 -22.915860 -23.353143
#>  [61] -23.790895 -24.229099 -24.667734 -25.106803 -25.546273 -25.986137
#>  [67] -26.426416 -26.867025 -27.308048 -27.749354 -28.191020 -28.633306
#>  [73] -29.075273 -29.517891 -29.961434 -30.403521 -30.847923 -31.294383
#>  [79] -31.732854 -32.182924 -32.625927 -33.073239 -33.517925 -33.964212
#>  [85] -34.338905 -34.945041 -35.127363 -36.043653 -36.043653 -36.736801
#>  [91] -36.736801       -Inf       -Inf -36.736801       -Inf       -Inf
#>  [97]       -Inf       -Inf       -Inf       -Inf
log(dist$d(1:100)[93])
#> [1] -Inf
log(dist$d(1:100)[94])
#> [1] -36.7368
log(dist$d(1:100)[95])
#> [1] -Inf

Created on 2022-03-07 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> - Session info -------------------------------------------------------------- #> hash: flag: Madagascar, closed mailbox with raised flag, person in suit levitating: medium skin tone #> #> setting value #> version R version 4.1.0 (2021-05-18) #> os Windows 10 x64 (build 22000) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United Kingdom.1252 #> ctype English_United Kingdom.1252 #> tz Europe/London #> date 2022-03-07 #> pandoc 2.11.4 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown) #> #> - Packages ------------------------------------------------------------------- #> package * version date (UTC) lib source #> backports 1.3.0 2021-10-27 [1] CRAN (R 4.1.1) #> cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1) #> crayon 1.5.0 2022-02-14 [1] CRAN (R 4.1.2) #> digest 0.6.28 2021-09-23 [1] CRAN (R 4.1.1) #> distcrete * 1.0.3 2017-11-23 [1] CRAN (R 4.1.1) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.1) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.1) #> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.1) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.1) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.1) #> glue 1.5.0 2021-11-07 [1] CRAN (R 4.1.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.1.1) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1) #> knitr 1.37 2021-12-16 [1] CRAN (R 4.1.2) #> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.1) #> pillar 1.6.5 2022-01-25 [1] CRAN (R 4.1.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.1) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.1) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.2) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.1) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.1) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.2) #> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.1) #> rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.1) #> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.1) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.1) #> sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.0) #> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.1) #> styler 1.6.2 2021-09-23 [1] CRAN (R 4.1.2) #> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.1) #> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.1) #> withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2) #> xfun 0.28 2021-11-04 [1] CRAN (R 4.1.0) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.1) #> #> [1] C:/Users/cygei/R/win-library/4.1 #> [2] C:/Program Files/R/R-4.1.0/library #> #> ------------------------------------------------------------------------------ ```
thibautjombart commented 2 years ago

A first pass at this would be to have scripts using distcrete objects for MCMC or optimization problems use something along the lines of:

x <- dist$d(1:100)
is_positive <- is.finite(log(x))
is_positive
to_replace <- !is_positive
to_replace
val_replace <- min(x[is_positive])
x[to_replace] <- val_replace