Model for ulam + log_lik yields wrong simulation (potential dup).

Open
#480 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
r
Domain
data

Research direction

Start from the reproduction script: fit the ulam model with log_lik = TRUE, then call sim(m_bug, data = d_new) and compare its means with the hand-built rnbinom simulation from extract.samples. The sim() method for ulam fits and the dgampois likelihood are the places to look, and the first thing to check is whether the log_lik setting changes how new data is passed through. Done means the simulated means match the reference values, and the log_lik case has a regression test.

Written by the indexing model from the issue text.

Description

The following model fails to proper simulate, due to a combination of log_lik = TRUE that is probably a variation of issue 269. The problem was found when simulating a model for the Hurricanes example on chapter 12.

library(rethinking)

d_train <- list(
  y  = c(10L, 150L),
  x1 = c(0, 1),
  x2 = c(1, 2)
)

m_bug <- ulam(
  alist(
    y ~ dgampois(lambda, phi),
    lambda <- exp(a + b * x1 * x2),
    a ~ dnorm(3, 1),
    b ~ dnorm(0, 2),
    phi ~ dexp(1)
  ),
  data = d_train, chains = 4, cores = 4, log_lik = TRUE
)

d_new <- list(
  x1 = c(0, 0, 0),
  x2 = c(0, 5, 10)
)

sim_buggy <- sim(m_bug, data = d_new)

post <- extract.samples(m_bug)
sim_correct <- sapply(1:3, function(j) {
  lam <- exp(post$a + post$b * d_new$x1[j] * d_new$x2[j])
  rnbinom(length(lam), size = post$phi, mu = lam)
})

data.frame(
  row = 1:3,
  x1_input = d_new$x1,
  x2_input = d_new$x2,
  sim_buggy_mean = apply(sim_buggy, 2, mean),
  sim_correct_mean = apply(sim_correct, 2, mean)
)

precis(m_bug)

yields:

> data.frame(
  row = 1:3,
  x1_input = d_new$x1,
  x2_input = d_new$x2,
  sim_buggy_mean = apply(sim_buggy, 2, mean),
  sim_correct_mean = apply(sim_correct, 2, mean)
)
 row x1_input x2_input sim_buggy_mean sim_correct_mean
1   1        0        0         22.308          24.5405
2   2        0        5        885.890          24.3275
3   3        0       10         21.515          22.1910
> precis(m_bug)
    mean   sd 5.5% 94.5% rhat ess_bulk ess_tail
a   2.80 0.75 1.67  4.05 1.00   974.99  1032.00
b   1.23 0.63 0.23  2.22 1.01   795.26  1000.43
phi 1.44 1.06 0.30  3.46 1.00   830.41  1029.27

If relevant, env is very plain:

$R --version
R version 4.6.2 (2026-10-29) -- "Diversified Portfolio"
Copyright (C) 2026 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

> packageVersion("rethinking")
[1] ‘2.46’
Dominant language
R
Stars
2.4k
Forks
632
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

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.

More from rmcelreath/rethinking

All issues in rmcelreath/rethinking

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.