Return 0 when using log_prob() with option adjust_transform = TRUE
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 1.1k
- Forks
- 266
- Avg merge
- 2h 56m
- Merged PRs (30d)
- 1
Description
Summary:
Return 0 when using log_prob() with option adjust_transform = TRUE
Description:
I had this issue when I tried to use function log_prob() to recover the "lp__" value for reference samples in r package "posteriordb".
Reproducible Steps:
The following is the example R code. One might need to install package posteriordb from Github to make it work.
rm(list = ls())
library(rstan)
# run the following line to install package posteriordb
# remotes::install_github("MansMeg/posteriordb", subdir = "rpackage")
library(posteriordb)
pd <- pdb_default() # Posterior database connection
po <- posterior("gp_pois_regr-gp_regr", pdb = pd) # pick model: gp_pois_regr-gp_regr
sc <- stan_code(po) # access data and model
dat <- get_data(po) # Check data
model <- stan_model(model_code = sc) # compile the model
posterior <- sampling(model, data = dat, chains = 1, iter = 1, refresh = 0,
algorithm = "Fixed_param")
# check option adjust_transform on two points: theta1 and theta2 #
theta1 <- c(6.0, 1.4, 1.3); names(theta1) <- c("rho", "alpha", "sigma")
theta2 <- c(5.0, 2.0, 1.0); names(theta2) <- c("rho", "alpha", "sigma")
fn <- function(theta){log_prob(posterior, unconstrain_pars(posterior, theta),
adjust_transform = TRUE)}
fn2 <- function(theta){log_prob(posterior, unconstrain_pars(posterior, theta),
adjust_transform = FALSE)}
fn(theta1); fn(theta2)
#[1] -29.90545
#[1] -31.21401
fn2(theta1); fn2(theta2)
#[1] 0
#[1] 0
As you can see here, the outputs of fn2 on two different points theta1 and theta2 are both 0. I also ran the function with option gradient = TRUE below. But the results with and without option gradient = TRUE do not match with each other.
# check the corresponding results with option gradient = TRUE
gn <- function(theta){ log_prob(posterior, unconstrain_pars(posterior, theta),
adjust_transform = TRUE, gradient = TRUE)}
gn2 <- function(theta){log_prob(posterior, unconstrain_pars(posterior, theta),
adjust_transform = FALSE, gradient = TRUE)}
gn(theta1); gn(theta2)
# [1] 2.861269
# attr(,"gradient")
# [1] 4.593643 6.009790 4.235825
# [1] 1.552708
# attr(,"gradient")
# [1] 8.182420 1.811042 7.207582
gn2(theta1); gn2(theta2)
# [1] 0.4706731
# attr(,"gradient")
# [1] 3.593643 5.009790 3.235825
# [1] -0.7498768
# attr(,"gradient")
# [1] 7.1824202 0.8110424 6.2075820
RStan Version:
> packageVersion("rstan")
[1] ‘2.21.2’
R Version:
> R.version.string
[1] "R version 3.6.3 (2020-02-29)"
Operating System:
NAME="Pop!_OS"
VERSION="20.04 LTS"
ID=pop
ID_LIKE="ubuntu debian"
PRETTY_NAME="Pop!_OS 20.04 LTS"
VERSION_ID="20.04"
The system is built upon Ubuntu 20.04
Thanks a lot!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the RStan log_prob() entry point and run the provided posteriordb reproduction with theta1 and theta2. Trace the adjust_transform and gradient paths, then verify that adjust_transform = FALSE no longer produces incorrect zero values and that the gradient results are consistent with the corresponding log probabilities.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100