stan-dev / stan-dev/math

Hessian NaN with ordered_logistic_lpmf

Open
#3,016 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

I'm testing this via CmdStanR $hessian() method, but I guess the problem is in math.

$hessian() method works for the following Stan model if data y=2 but gives NaN if y=3. $laplace() method is able to compute finite Hessian (finite difference?), so the reason is not that Hessian would not exist (gradient is also close to 0, so the Hessian is computed (near) the mode).

data {
  int Y;
}
parameters {
  real mu;
}
model {
  target += ordered_logistic_lpmf(Y | mu, [-1, 1]');
  target += std_normal_lpdf(mu);
}

R code to reproduce

library(cmdstanr)
code_ordered <-
"data {
  int Y;
}
parameters {
  real mu;
}
model {
  target += ordered_logistic_lpmf(Y | mu, [-1, 1]');
  target += std_normal_lpdf(mu);
}
"
file_ordered <- write_stan_file(code_ordered)
model_ordered <- cmdstan_model(file_ordered,
                               compile_model_methods=TRUE,
                               compile_hessian_method=TRUE,
                               force_recompile=TRUE)
# This works
data_ordered <- list(Y = 2)
opt_ordered <- model_ordered$optimize(data = data_ordered, refresh=0)
opt_ordered$hessian(as.numeric(opt_ordered$unconstrain_draws(draws=opt_ordered$draws())))

# This works
lap_ordered <- model_ordered$laplace(data = data_ordered, refresh=0)
-1/var(lap_ordered$draws(variables="mu")) # Hessian from the draws

# This gives NaN
data_ordered <- list(Y = 3)
opt_ordered <- model_ordered$optimize(data = data_ordered, refresh=0);
opt_ordered$hessian(as.numeric(opt_ordered$unconstrain_draws(draws=opt_ordered$draws())))

# This works
lap_ordered <- model_ordered$laplace(data = data_ordered, refresh=0)
-1/var(lap_ordered$draws(variables="mu")) # Hessian from the draws

Contributor guide

Open the contributing guide

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

Reproduce the issue using the supplied Stan model and CmdStanR $hessian() calls, comparing Y=2 with Y=3 and the corresponding $laplace() results. Start by tracing the ordered_logistic_lpmf evaluation and Hessian path in the Stan Math code; done means identifying and correcting the NaN result while preserving finite Hessian behavior for both inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, r
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.