Skip to content

Compiler support for laplace_latent_solve(_tol)#1649

Open
florence-bockting wants to merge 5 commits into
stan-dev:masterfrom
florence-bockting:laplace/return-cov-chol
Open

Compiler support for laplace_latent_solve(_tol)#1649
florence-bockting wants to merge 5 commits into
stan-dev:masterfrom
florence-bockting:laplace/return-cov-chol

Conversation

@florence-bockting

@florence-bockting florence-bockting commented Jul 18, 2026

Copy link
Copy Markdown

Submission Checklist

This PR refers to math PR#337 in which we implemented laplace_latent_solve and laplace_latent_solve_tol.

Release notes

  • Adds Stan compiler support for Math’s laplace_latent_solve(_tol), which return tuple(vector, matrix) (posterior mean and Cholesky factor).

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

This PR used AI assistance (Cursor).

@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.51%. Comparing base (5496e41) to head (ec9a2f8).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1649   +/-   ##
=======================================
  Coverage   91.51%   91.51%           
=======================================
  Files          67       67           
  Lines        9977     9979    +2     
=======================================
+ Hits         9130     9132    +2     
  Misses        847      847           
Files with missing lines Coverage Δ
src/frontend/Typechecker.ml 94.88% <100.00%> (+<0.01%) ⬆️
src/stan_math_signatures/Stan_math_signatures.ml 86.27% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@florence-bockting
florence-bockting marked this pull request as ready for review July 18, 2026 19:14
@WardBrian

Copy link
Copy Markdown
Member

Thanks @florence-bockting -- question about 6e2c28b -- is it actually a requirement that these functions only be called in generated quantities? If so we will want to enforce that in the typechecker

@florence-bockting

Copy link
Copy Markdown
Author

Thanks @florence-bockting -- question about 6e2c28b -- is it actually a requirement that these functions only be called in generated quantities? If so we will want to enforce that in the typechecker

Thank you for pointing me towards the typechecker!
Short about the background: As laplace_latent_solve currently only goes through laplace_base_rng it inherits the constraint on CovarArgs and LLArgs:

template <bool ReturnMeanAndCovCholesky = false, typename LLFunc,
          typename LLArgs, typename CovarFun, typename CovarArgs,
          bool InitTheta, typename RNG,
          require_t<is_all_arithmetic_scalar<CovarArgs, LLArgs>>* = nullptr>

For the model block this fails as some of the arguments are variables. Here an example snippet of a corresponding model and generated quantities block:

model {
  rho ~ inv_gamma(rho_location_prior, rho_scale_prior);
  alpha ~ inv_gamma(alpha_location_prior, alpha_scale_prior);
  eta ~ normal(0, 1);

  target += laplace_marginal(ll_function, (eta, log_ye, y), hessian_block_size,
                             K_function, (x, n_obs, alpha, rho));

  target += laplace_marginal_tol(ll_function, (eta, log_ye, y), hessian_block_size,
                                 K_function, (x, n_obs, alpha, rho),
                                (theta_0, tolerance, max_num_steps,
                                solver, max_steps_line_search, allow_fallthrough));
}
generated quantities {
  tuple(vector[n_obs], matrix[n_obs, n_obs]) mean_chol
      = laplace_latent_solve(ll_function, (eta, log_ye, y), hessian_block_size,
                             K_function, (x, n_obs, alpha, rho));
}

I am not sure whether there are use cases where a user wants to call laplace_latent_solve in the model block (@avehtari )? In this case, we would have to change the definition in math. Otherwise I would add the corresponding constraint in the Typechecker.ml.

@SteveBronder

Copy link
Copy Markdown
Contributor

So since we never actually use the rng for laplace_latent_solve idt there is any reason we could not have it in the model block. So language wise I don't see an issue arising because of a user calling this in the model block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants