stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
756 stars 188 forks source link

Compound glms (bernoulli_logit_glm_lpmf, poisson_log_glm_lpmf, etc) give wrong answer if only intercept #1398

Closed jgabry closed 5 years ago

jgabry commented 5 years ago

Description

When X has 0 columns, beta has 0 elements, and alpha has 1 element functions like bernoulli_logit_glm_lpmf (I've checked poisson too but not others yet) ignore alpha and return 0, that is,

bernoulli_logit_glm_lpmf(y | X, alpha, beta) = 0

but it should take into account alpha.

I noticed this problem because of https://github.com/stan-dev/rstanarm/issues/396, where models with only an intercept give the wrong inferences. Turning off using the compound glm functions (bernoulli_logit_glm_lpmf, poisson_log_glm_lpmf) fixes the problem. Below is an example Stan program illustrating the problem.

Example

If I run the following Stan program:

data {
  int<lower=0> N;
  int<lower=0,upper=1> y[N];
}
transformed data {
  matrix[N, 0] X;  // 0 columns
}
parameters {
  real alpha;
  vector[0] beta; // 0 elements 
}
model {
  alpha ~ normal(5, 10);
  target += bernoulli_logit_glm_lpmf(y | X, alpha, beta);
}
generated quantities {
  real test_lpmf = bernoulli_logit_glm_lpmf(y | X, alpha, beta);
}

then test_lpmf always has the value 0.0e+00 (i.e., 0) but it shouldn't be zero due to alpha. I have also tested the poisson case and the same problem occurs.

Expected Output

It should take alpha into account when calculating the lpmf.

Current Version:

v2.20.0 (via CmdStan 2.20.0)

t4c1 commented 5 years ago

I think all glms are affected.

t4c1 commented 5 years ago

Actually it turns out ordered_logistic_glm_lpmf() was not affected. All others were.

rok-cesnovar commented 5 years ago

Fixed via #1399

jgabry commented 5 years ago

Thanks for fixing so quickly!