Skip to content

Commit cbb74b3

Browse files
authored
Merge pull request #3097 from stan-dev/experimental/laplace
[WIP] Embedded Laplace Approximation
2 parents 243c31b + 41fe942 commit cbb74b3

File tree

112 files changed

+7856
-422
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+7856
-422
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ benchmarks/*.csv
3535
*.o-*
3636
*.exe
3737
*.a
38+
# mac debug folders
39+
*.dSYM/
3840

3941
# Intel template building blocks (TBB)
4042
lib/tbb

Jenkinsfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ pipeline {
322322
}
323323
post { always { retry(3) { deleteDir() } } }
324324
}
325+
stage('Laplace Unit Tests') {
326+
agent {
327+
docker {
328+
image 'stanorg/ci:gpu-cpp17'
329+
label 'linux'
330+
args '--cap-add SYS_PTRACE'
331+
}
332+
}
333+
when {
334+
expression {
335+
!skipRemainingStages
336+
}
337+
}
338+
steps {
339+
unstash 'MathSetup'
340+
sh "echo CXXFLAGS += -march=native -mtune=native >> make/local"
341+
sh "echo O=3 >> make/local"
342+
script {
343+
if (params.optimizeUnitTests || isBranch('develop') || isBranch('master')) {
344+
sh "echo CXXFLAGS += -fsanitize=address >> make/local"
345+
}
346+
runTests("test/unit/math/laplace/*_test.cpp", false)
347+
}
348+
}
349+
post { always { retry(3) { deleteDir() } } }
350+
}
325351
stage('OpenCL GPU tests') {
326352
agent {
327353
docker {

doxygen/doxygen.cfg

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,3 +2741,38 @@ MSCGEN_TOOL =
27412741
# command).
27422742

27432743
MSCFILE_DIRS =
2744+
2745+
ALIASES += laplace_options="\
2746+
\param[in] tolerance controls the convergence criterion when finding the mode in the Laplace approximation. \
2747+
\param[in] max_num_steps maximum number of steps before the Newton solver breaks and returns an error. \
2748+
\param[in] hessian_block_size Block size of Hessian of log likelihood w.r.t latent Gaussian variable theta. \
2749+
\param[in] solver Type of Newton solver. Each corresponds to a distinct choice of B matrix (i.e. application SWM formula): \
2750+
1. computes square-root of negative Hessian. \
2751+
2. computes square-root of covariance matrix. \
2752+
3. computes no square-root and uses LU decomposition. \
2753+
\param[in] max_steps_line_search Number of steps after which the algorithm gives up on doing a line search. If 0, no linesearch. \
2754+
"
2755+
2756+
ALIASES += laplace_common_template_args="\
2757+
\tparam ThetaVec A type inheriting from `Eigen::EigenBase` with dynamic sized rows and 1 column. \
2758+
\tparam CovarFun A functor with an `operator()(CovarArgsElements..., {TrainTupleElements...| PredTupleElements...})` \
2759+
method. The `operator()` method should accept as arguments the \
2760+
inner elements of `CovarArgs`. The return type of the `operator()` method \
2761+
should be a type inheriting from `Eigen::EigenBase` with dynamic sized \
2762+
rows and columns. \
2763+
\tparam CovarArgs A tuple of types to passed as the first arguments of `CovarFun::operator()`\
2764+
"
2765+
2766+
ALIASES += laplace_common_args="\
2767+
\param[in] theta_0 the initial guess for the Laplace approximation. \
2768+
\param[in] covariance_function a function which returns the prior covariance. \
2769+
\param[in] covar_args arguments for the covariance function. \
2770+
"
2771+
2772+
ALIASES += msg_arg="\
2773+
\param[in, out] msgs stream for messages from likelihood and covariance \
2774+
"
2775+
2776+
ALIASES += rng_arg="\
2777+
\param[in, out] rng Random number generator \
2778+
"

make/tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ benchmarks/%$(EXE) : benchmarks/%.cpp $(GTEST)/src/gtest-all.o $(MPI_TARGETS) $(
2323
test/% : CXXFLAGS += $(CXXFLAGS_GTEST)
2424
test/% : CPPFLAGS += $(CPPFLAGS_GTEST)
2525
test/% : INC += $(INC_GTEST)
26-
2726
test/%$(EXE) : test/%.o $(GTEST)/src/gtest_main.cc $(GTEST)/src/gtest-all.o $(MPI_TARGETS) $(TBB_TARGETS)
2827
$(LINK.cpp) $^ $(LDLIBS) $(OUTPUT_OPTION)
2928

stan/math/fwd/meta/is_fvar.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ struct is_fvar<T,
2121
std::enable_if_t<internal::is_fvar_impl<std::decay_t<T>>::value>>
2222
: std::true_type {};
2323

24+
template <typename T>
25+
inline constexpr bool is_fvar_v = is_fvar<T>::value;
26+
2427
} // namespace stan
2528
#endif

stan/math/mix.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef STAN_MATH_MIX_HPP
22
#define STAN_MATH_MIX_HPP
33

4+
#include <stan/math/fwd/fun/Eigen_NumTraits.hpp>
5+
#include <stan/math/rev/core/Eigen_NumTraits.hpp>
6+
#include <stan/math/prim/fun/Eigen.hpp>
7+
48
#include <stan/math/mix/meta.hpp>
59
#include <stan/math/mix/fun.hpp>
610
#include <stan/math/mix/functor.hpp>
@@ -26,4 +30,6 @@
2630

2731
#include <stan/math/prim.hpp>
2832

33+
#include <stan/math/mix/prob.hpp>
34+
2935
#endif

stan/math/mix/functor.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
#define STAN_MATH_MIX_FUNCTOR_HPP
33

44
#include <stan/math/mix/functor/derivative.hpp>
5-
#include <stan/math/mix/functor/finite_diff_grad_hessian.hpp>
65
#include <stan/math/mix/functor/finite_diff_grad_hessian_auto.hpp>
6+
#include <stan/math/mix/functor/finite_diff_grad_hessian.hpp>
77
#include <stan/math/mix/functor/grad_hessian.hpp>
88
#include <stan/math/mix/functor/grad_tr_mat_times_hessian.hpp>
99
#include <stan/math/mix/functor/gradient_dot_vector.hpp>
1010
#include <stan/math/mix/functor/hessian.hpp>
11+
#include <stan/math/mix/functor/laplace_base_rng.hpp>
12+
#include <stan/math/mix/functor/laplace_likelihood.hpp>
13+
#include <stan/math/mix/functor/laplace_marginal_density.hpp>
14+
#include <stan/math/mix/functor/hessian_block_diag.hpp>
1115
#include <stan/math/mix/functor/hessian_times_vector.hpp>
1216
#include <stan/math/mix/functor/partial_derivative.hpp>
13-
1417
#endif

stan/math/mix/functor/derivative.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef STAN_MATH_MIX_FUNCTOR_DERIVATIVE_HPP
22
#define STAN_MATH_MIX_FUNCTOR_DERIVATIVE_HPP
33

4+
#include <stan/math/prim/fun/Eigen.hpp>
45
#include <stan/math/rev/core.hpp>
56
#include <stan/math/fwd/core.hpp>
6-
#include <stan/math/prim/fun/Eigen.hpp>
77
#include <vector>
88

99
namespace stan {
@@ -21,7 +21,7 @@ namespace math {
2121
* @param[out] dfx_dx Value of derivative
2222
*/
2323
template <typename T, typename F>
24-
void derivative(const F& f, const T& x, T& fx, T& dfx_dx) {
24+
inline void derivative(const F& f, const T& x, T& fx, T& dfx_dx) {
2525
fvar<T> x_fvar = fvar<T>(x, 1.0);
2626
fvar<T> fx_fvar = f(x_fvar);
2727
fx = fx_fvar.val_;

stan/math/mix/functor/finite_diff_grad_hessian.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ namespace math {
3838
* @param[in] epsilon perturbation size
3939
*/
4040
template <typename F>
41-
void finite_diff_grad_hessian(const F& f, const Eigen::VectorXd& x, double& fx,
42-
Eigen::MatrixXd& hess,
43-
std::vector<Eigen::MatrixXd>& grad_hess_fx,
44-
double epsilon = 1e-04) {
41+
inline void finite_diff_grad_hessian(const F& f, const Eigen::VectorXd& x,
42+
double& fx, Eigen::MatrixXd& hess,
43+
std::vector<Eigen::MatrixXd>& grad_hess_fx,
44+
double epsilon = 1e-04) {
4545
int d = x.size();
4646
grad_hess_fx.clear();
4747

stan/math/mix/functor/finite_diff_grad_hessian_auto.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ namespace math {
4141
* @param[out] grad_hess_fx gradient of Hessian of function at argument
4242
*/
4343
template <typename F>
44-
void finite_diff_grad_hessian_auto(const F& f, const Eigen::VectorXd& x,
45-
double& fx, Eigen::MatrixXd& hess,
46-
std::vector<Eigen::MatrixXd>& grad_hess_fx) {
44+
inline void finite_diff_grad_hessian_auto(
45+
const F& f, const Eigen::VectorXd& x, double& fx, Eigen::MatrixXd& hess,
46+
std::vector<Eigen::MatrixXd>& grad_hess_fx) {
4747
int d = x.size();
4848

4949
grad_hess_fx.clear();

0 commit comments

Comments
 (0)