stan-dev / stan-dev/rstan

log_prob doesn't drop additive constants

Open
#890 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Hello,

log_prob states that:

log_prob returns a value (up to an additive constant) the log posterior

and it is my understanding that syntax of the form y ~ dist() drops additive constants. However that doesn't seem to be the case for the examples I have tried

To reproduce:

library(rstan)

writeLines(readLines("propto.stan"))
## ==>
## parameters {
##     real x;
## }
## model {
##     x ~ std_normal();
## }

writeLines(readLines("target.stan"))
## parameters {
##     real x;
## }
## model {
##     target += std_normal_lpdf(x);
## }

propto <- stan("propto.stan", chains = 0)
target <- stan("target.stan", chains = 0)

x <- 1.0

log_prob(propto, x)
## ==>
## [1] -1.418939

log_prob(target, x)
## ==>
## [1] -1.418939

I would expect the propto code to drop the second term in

-0.5 * x^2 - 0.5 * log(2 * pi)
## ==>
## [1] -1.418939

Not sure if this is an rstsan or stan issue. Let me know if there is anything I can do to help - thanks!

rstan: 2.21.2
R: R version 4.0.3 (2020-10-10)
OS: ubuntu 18.04

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the discrepancy with the propto.stan and target.stan examples through RStan's log_prob function. Compare the generated behavior for the sampling statement and the explicit std_normal_lpdf call, then determine whether the fix belongs in RStan or Stan. Done means the documented additive-constant behavior and the reproduced outputs agree.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.