Skip to content

Add functors for the embedded Laplace approximation#1513

Merged
WardBrian merged 61 commits intomasterfrom
laplace-functors
Jun 3, 2025
Merged

Add functors for the embedded Laplace approximation#1513
WardBrian merged 61 commits intomasterfrom
laplace-functors

Conversation

@WardBrian
Copy link
Copy Markdown
Member

This is a draft until stan-dev/math#3097 is updated and merged. It could probably also do with some more code-gen tests.

Similar to reduce_sum, these functions have too many rules to fit into the general variadic framework, so they have custom typechecking and errors, which make up most of this PR.

Closes #1511

Submission Checklist

Release notes

Added new functions for the embedded Laplace approximation.

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)

@WardBrian
Copy link
Copy Markdown
Member Author

As of stan-dev/math@80904889a2, this is now passing CI

Copy link
Copy Markdown
Contributor

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few Qs but things look good!

@WardBrian WardBrian marked this pull request as ready for review June 3, 2025 14:10
match tes with
| {expr= Variable lik_fun; _} :: lik_tupl :: tes ->
let lik_fun, lik_tupl =
needs_higher_order_autodiff lik_fun;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] It would be nice to have a comment here that this changes an object at the global scope

let cov_fun_type, cov_tupl =
check_function_callable_with_tuple cf tenv id cov_fun cov_tupl
(UnsizedType.ReturnType UMatrix) in
(* note for future: pred_rng typechecking would need to look at second tuple here *)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(* note for future: pred_rng typechecking would need to look at second tuple here *)
(* note for future: pred_rng typechecking would need to look at tuple for the training prediction and test prediction *)

Comment on lines -1302 to +1519
(* Function is non existent *)
Semantic_error.invalid_tilde_no_such_dist loc name
(List.hd_exn argumenttypes |> snd |> UnsizedType.is_int_type)
|> error
(* The laplace_marginal helpers are not in the
environment lookup, so we patch them back here *)
if Stan_math_signatures.is_embedded_laplace_fn (id.name ^ "_lupmf") then
let id = {id with name= id.name ^ "_lupmf"} in
let fn_expr =
check_laplace_fn ~is_cond_dist:true loc cf tenv id arguments in
match fn_expr.expr with
| CondDistApp (kind, _, args) -> (args, kind)
| _ ->
Common.ICE.internal_compiler_error
[%message
"Typechecking a laplace marginal did not return a distribution "
(fn_expr : Ast.typed_expression)]
else
(* Otherwise, the function is non existent *)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not add these because they take in tuples of variadic size so they do not fit in the distributions list? It would be nice if we could even make a separate list for them instead of catching a failure

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct -- because they allow any function for the covariance, they also take in any length of tuple the user wants

Copy link
Copy Markdown
Contributor

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few small comments but I think this is good to merge as is! 🥳

@WardBrian WardBrian merged commit 4426837 into master Jun 3, 2025
3 checks passed
@WardBrian WardBrian deleted the laplace-functors branch June 3, 2025 22:35
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.

Add signatures for laplace Expose Laplace Approximation

3 participants