greta-dev / greta-dev/greta

dodgy simulations from truncated distributions

Open
#428 0 comments 0 reactions 0 assignees View on GitHub
distributions
Dominant language
C++
Stars
607
Forks
67
Avg merge
3d 8h
Merged PRs (30d)
1

Description

When simulating from a truncated normal distribution, for certain parameter values (probably where sampling is a bit numerically treacherous), greta returns some infinite or invalid parameter values.

This may be due to an issue in TF or TF probability, which we lean on for sampling these, and the algorithm they are using hitting numerical issues. Maybe we should return NA values if these are outside of the parameter's support?

In the below, I would expect all the drawn values to be finite and positive, and probably unique! Though many are infinite or negative, and there are lots of duplicated values. Note that the choice of standard deviation here is what drives this. A larger value samples fine, and a smaller value gives all infinite samples.

``` r
library(greta)
#>
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#>
#> binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#>
#> %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#> eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#> tapply
. <- variable()
#> ℹ Initialising python and checking dependencies
#> ✓ Initialising python and checking dependencies ... done
#>

set.seed(1)

x <- normal(-1, 0.123, truncation = c(0, Inf))
calculate(x, nsim = 100)
#> $x
#> , , 1
#>
#> [,1]
#> [1,] 0.0097729466
#> [2,] 0.0097729466
#> [3,] 0.0097729466
#> [4,] -0.0005154482
#> [5,] 0.0097729466
#> [6,] 0.0097729466
#> [7,] 0.0097729466
#> [8,] 0.0097729466
#> [9,] 0.0097729466
#> [10,] Inf
#> [11,] 0.0097729466
#> [12,] 0.0097729466
#> [13,] 0.0097729466
#> [14,] 0.0097729466
#> [15,] Inf
#> [16,] 0.0097729466
#> [17,] 0.0097729466
#> [18,] 0.0097729466
#> [19,] 0.0097729466
#> [20,] 0.0097729466
#> [21,] Inf
#> [22,] Inf
#> [23,] -0.0005154482
#> [24,] 0.0097729466
#> [25,] Inf
#> [26,] Inf
#> [27,] Inf
#> [28,] Inf
#> [29,] Inf
#> [30,] 0.0097729466
#> [31,] -0.0005154482
#> [32,] 0.0097729466
#> [33,] 0.0097729466
#> [34,] Inf
#> [35,] -0.0005154482
#> [36,] -0.0005154482
#> [37,] 0.0097729466
#> [38,] 0.0097729466
#> [39,] 0.0097729466
#> [40,] Inf
#> [41,] -0.0005154482
#> [42,] 0.0097729466
#> [43,] 0.0097729466
#> [44,] Inf
#> [45,] Inf
#> [46,] 0.0097729466
#> [47,] 0.0097729466
#> [48,] -0.0005154482
#> [49,] 0.0097729466
#> [50,] Inf
#> [51,] -0.0005154482
#> [52,] 0.0097729466
#> [53,] -0.0005154482
#> [54,] Inf
#> [55,] 0.0097729466
#> [56,] Inf
#> [57,] 0.0097729466
#> [58,] 0.0097729466
#> [59,] Inf
#> [60,] Inf
#> [61,] -0.0005154482
#> [62,] -0.0005154482
#> [63,] 0.0097729466
#> [64,] 0.0097729466
#> [65,] -0.0005154482
#> [66,] Inf
#> [67,] 0.0097729466
#> [68,] Inf
#> [69,] 0.0097729466
#> [70,] -0.0005154482
#> [71,] Inf
#> [72,] -0.0005154482
#> [73,] -0.0005154482
#> [74,] Inf
#> [75,] -0.0005154482
#> [76,] 0.0097729466
#> [77,] 0.0097729466
#> [78,] 0.0097729466
#> [79,] 0.0097729466
#> [80,] 0.0097729466
#> [81,] 0.0097729466
#> [82,] 0.0097729466
#> [83,] 0.0097729466
#> [84,] 0.0097729466
#> [85,] Inf
#> [86,] Inf
#> [87,] 0.0097729466
#> [88,] 0.0097729466
#> [89,] 0.0097729466
#> [90,] 0.0097729466
#> [91,] 0.0097729466
#> [92,] Inf
#> [93,] 0.0097729466
#> [94,] 0.0097729466
#> [95,] 0.0097729466
#> [96,] 0.0097729466
#> [97,] -0.0005154482
#> [98,] 0.0097729466
#> [99,] 0.0097729466
#> [100,] 0.0097729466
```

Created on 2021-06-09 by the [reprex package](https://reprex.tidyverse.org) (v2.0.0)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.