Skip to content

Laplace: Add mean argument to helper distributions #3210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2025

Conversation

WardBrian
Copy link
Member

@WardBrian WardBrian commented Jun 17, 2025

Summary

Closes #3202.

Adds a mean argument to the helpers. It is templated such that it can be a vector or scalar.

The poisson_log_2 helper is redundant with this change and is therefore removed.

Additionally, it fixes the y_index argument to be 1-indexed to match the rest of Stan

Tests

Existing tests updated to pass 0 as the mean. It would be nice to add some tests
with a non-zero mean, if there are suggestions (@avehtari @charlesm93)

Side Effects

Release notes

Checklist

  • Copyright holder: (fill in copyright holder information)

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@avehtari
Copy link
Member

This example https://discourse.mc-stan.org/t/embedded-laplace-numerical-problem/39700 uses non-zero mean (mu) and Poisson. So with the helper it should look something like (not certain what is the argument order now)

laplace_marginal_poisson_log_lpmf(y | mu, [1], cov_fun, (sigmaz, N), theta_0)

@WardBrian
Copy link
Member Author

Thanks @avehtari.

Writing that test made me realize something else: The y_index argument to laplace_marginal_poisson_log_lpmf is currently 0-indexed rather than 1-indexed. Is that intentional @charlesm93 @SteveBronder?

@charlesm93
Copy link
Contributor

Writing that test made me realize something else: The y_index argument to laplace_marginal_poisson_log_lpmf is currently 0-indexed rather than 1-indexed. Is that intentional @charlesm93 @SteveBronder?

It should be 1-indexed to be consistent with the rest of Stan.

@WardBrian
Copy link
Member Author

@charlesm93 y_index is now 1-indexed and I removed the newly redundant poisson_2_log helper

@WardBrian WardBrian force-pushed the laplace/helpers-mean-argument branch from d3fdfac to 6b19281 Compare July 1, 2025 14:01
Comment on lines 70 to 80
const ThetaVec& theta_0, CovarFun&& covariance_function,
CovarArgs&& covar_args, double tolerance, int max_num_steps,
const Mean& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,
const ThetaVec& theta_0, double tolerance, int max_num_steps,
Copy link
Collaborator

Choose a reason for hiding this comment

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

@charlesm93 @avehtari so with this change the user will always have to supply a mean. Are there a lot of instances where the mean would be zero? aka do we want a signature without a mean as well?

Copy link
Member Author

@WardBrian WardBrian Jul 2, 2025

Choose a reason for hiding this comment

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

Re-reading the OP and discussion in #3202, it seems like the consensus is to have the signature with the mean be the only one at the moment. We can add one that allows a scalar (as @charlesm93 requested) to make it easier to pass e.g. 0 instead of a rep_vector(0, N), or a signature without it, but both of those would require some extra work as the compiler isn't currently set up for 'overloads' of the laplace functions as easily

Copy link
Collaborator

@SteveBronder SteveBronder Jul 7, 2025

Choose a reason for hiding this comment

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

it seems like the consensus is to have the signature without the mean be the only one at the moment.

Sorry @WardBrian I'm confused. Does that mean we should close this PR for now?

Copy link
Member Author

@WardBrian WardBrian Jul 7, 2025

Choose a reason for hiding this comment

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

That means we should merge this PR... I meant "with"

Copy link
Collaborator

Choose a reason for hiding this comment

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

lol icic got it

@WardBrian WardBrian force-pushed the laplace/helpers-mean-argument branch from 6b19281 to 0acf4e4 Compare July 1, 2025 18:30
@stan-dev stan-dev deleted a comment from SteveBronder Jul 1, 2025
@WardBrian
Copy link
Member Author

@SteveBronder similar to the last PR, the tests for math are passing, the failure is just because the compiler update needs to be merged simultaneously. So this is ready to review

Copy link
Collaborator

@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 things but looks good!

* \laplace_common_args
* \rng_arg
* \msg_arg
*/
template <typename CovarFun, typename CovarArgs, typename RNG>
template <typename Mean, typename CovarFun, typename CovarArgs, typename RNG,
require_eigen_vector_t<Mean>* = nullptr>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we delete this so that it is easier to make into a scalar later?

Suggested change
require_eigen_vector_t<Mean>* = nullptr>
>

inline Eigen::VectorXd laplace_latent_tol_poisson_log_rng(
const std::vector<int>& y, const std::vector<int>& y_index,
CovarFun&& covariance_function, CovarArgs&& covar_args, ThetaVec&& theta_0,
const double tolerance, const int max_num_steps,
const Mean& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const Mean& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,
Mean&& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,

const int hessian_block_size, const int solver,
const int max_steps_line_search, RNG& rng, std::ostream* msgs) {
laplace_options_user_supplied ops{hessian_block_size, solver,
max_steps_line_search, tolerance,
max_num_steps, value_of(theta_0)};
return laplace_base_rng(poisson_log_likelihood{},
std::forward_as_tuple(y, y_index),
std::forward_as_tuple(y, y_index, mean),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
std::forward_as_tuple(y, y_index, mean),
std::forward_as_tuple(y, y_index, std::forward<Mean>(mean)),

Copy link
Collaborator

Choose a reason for hiding this comment

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

This and the forwarding for all the functions

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@WardBrian WardBrian requested a review from SteveBronder July 7, 2025 20:32
@@ -4,12 +4,9 @@

#include <boost/random/mersenne_twister.hpp>
#include <boost/math/distributions.hpp>
#include "Eigen/src/Core/Matrix.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this need to be here?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it was 'helpfully' inserted by autocomplete in vscode...

SteveBronder
SteveBronder previously approved these changes Jul 8, 2025
Copy link
Collaborator

@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.

One header include needs changed then this is good!

@WardBrian
Copy link
Member Author

@SteveBronder stan-dev/stanc3#1523 still needs to be reviewed as well so they can be merged together

@WardBrian WardBrian merged commit c8beeaf into develop Jul 9, 2025
33 of 34 checks passed
@WardBrian WardBrian deleted the laplace/helpers-mean-argument branch July 9, 2025 13:33
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.

Include prior mean argument to built-in Laplace marginal likelihood functions
4 participants