Skip to content

Commit d959c81

Browse files
authored
Merge pull request #3197 from stan-dev/fix/theta_0_arena
Force theta to be an Eigen::VectorXd in laplace_marginal_density_est
2 parents 4904f89 + a8192b4 commit d959c81

File tree

4 files changed

+299
-226
lines changed

4 files changed

+299
-226
lines changed

stan/math/mix/functor/laplace_likelihood.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ inline auto compute_s2(F&& f, Theta&& theta, AMat&& A,
226226
Matrix<fvar<fvar<var>>, Dynamic, 1> theta_ffvar(theta_size);
227227
auto shallow_copy_args
228228
= shallow_copy_vargs<fvar<fvar<var>>>(std::forward_as_tuple(args...));
229-
// build a “row” index 0,1,2,…,total-1
230-
Eigen::Index total = n_blocks * hessian_block_size;
231229
for (Eigen::Index i = 0; i < hessian_block_size; ++i) {
232230
nested_rev_autodiff nested;
233231
v.setZero();

stan/math/mix/functor/laplace_marginal_density.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ inline auto laplace_marginal_density_est(LLFun&& ll_fun, LLTupleArgs&& ll_args,
500500
};
501501
auto ll_args_vals = value_of(ll_args);
502502
const Eigen::Index theta_size = theta_0.size();
503-
std::decay_t<ThetaVec> theta = theta_0;
503+
Eigen::VectorXd theta = std::forward<ThetaVec>(theta_0);
504504
double objective_old = std::numeric_limits<double>::lowest();
505505
double objective_new = std::numeric_limits<double>::lowest() + 1;
506506
Eigen::VectorXd a_prev = Eigen::VectorXd::Zero(theta_size);
@@ -1039,7 +1039,7 @@ inline auto laplace_marginal_density(const LLFun& ll_fun, LLTupleArgs&& ll_args,
10391039
// Solver 3
10401040
arena_t<Eigen::MatrixXd> LU_solve_covariance;
10411041
// Solver 1, 2, 3
1042-
arena_t<promote_scalar_t<double, std::decay_t<ThetaVec>>> s2(
1042+
arena_t<promote_scalar_t<double, plain_type_t<std::decay_t<ThetaVec>>>> s2(
10431043
theta_0.size());
10441044
// Make one hard copy here
10451045
using laplace_likelihood::internal::conditional_copy_and_promote;

test/unit/math/laplace/laplace_marginal_lpdf_test.cpp

Lines changed: 0 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -20,228 +20,6 @@ struct poisson_log_likelihood2 {
2020
}
2121
};
2222

23-
template <typename T1, typename T2>
24-
auto in_throw_list(T1&& test_values, T2&& test_arr) {
25-
for (auto&& x : test_values) {
26-
if (x[0] == test_arr[0] && x[1] == test_arr[1] && x[2] == test_arr[2]) {
27-
return true;
28-
}
29-
}
30-
return false;
31-
}
32-
33-
struct poisson_log_likelihood_tuple {
34-
template <typename Theta, typename Eta>
35-
auto operator()(const Theta& theta, const std::vector<int>& delta_int,
36-
Eta&& eta, std::ostream* pstream) const {
37-
return stan::math::poisson_log_lpmf(delta_int, theta) + std::get<0>(eta)
38-
+ std::get<1>(eta);
39-
}
40-
};
41-
42-
struct poisson_log_likelihood_tuple_expanded {
43-
template <typename Theta, typename Eta, typename Eta1, typename Eta2>
44-
auto operator()(const Theta& theta, const std::vector<int>& delta_int,
45-
Eta&& eta, Eta1&& eta1, Eta2&& eta2,
46-
std::ostream* pstream) const {
47-
return stan::math::poisson_log_lpmf(delta_int, theta) + std::get<0>(eta)
48-
+ std::get<1>(eta) + stan::math::sum(eta1) + stan::math::sum(eta2);
49-
}
50-
};
51-
52-
TEST(laplace, poisson_log_phi_dim_2_tuple_extended) {
53-
using stan::math::laplace_marginal;
54-
using stan::math::laplace_marginal_tol;
55-
using stan::math::to_vector;
56-
using stan::math::value_of;
57-
using stan::math::var;
58-
// logger->current_test_name_ = "poisson_log_phi_dim_2";
59-
int dim_phi = 2;
60-
Eigen::Matrix<double, Eigen::Dynamic, 1> phi_dbl(dim_phi);
61-
phi_dbl << 1.6, 0.45;
62-
63-
int dim_theta = 2;
64-
Eigen::VectorXd theta_0(dim_theta);
65-
theta_0 << 0, 0;
66-
67-
int dim_x = 2;
68-
std::vector<Eigen::VectorXd> x(dim_theta);
69-
Eigen::VectorXd x_0{{0.05100797, 0.16086164}};
70-
Eigen::VectorXd x_1{{-0.59823393, 0.98701425}};
71-
x[0] = x_0;
72-
x[1] = x_1;
73-
74-
Eigen::VectorXd y_dummy;
75-
76-
std::vector<int> n_samples = {1, 1};
77-
std::vector<int> sums = {1, 0};
78-
79-
constexpr double tolerance = 1e-12;
80-
constexpr int max_num_steps = 100;
81-
using stan::is_var_v;
82-
using stan::scalar_type_t;
83-
using stan::math::test::laplace_issue;
84-
constexpr std::array known_issues{laplace_issue{0, 0, 0}};
85-
stan::test::ad_tolerances tols;
86-
tols.gradient_grad_ = 1e-1;
87-
stan::math::test::run_solver_grid(
88-
[&](int solver_num, int hessian_block_size, int max_steps_line_search,
89-
auto&& theta_0) {
90-
auto f_ll = [&](auto&& eta1, auto&& eta2, auto&& eta3) {
91-
auto eta1_tuple = std::make_tuple(eta1(0), eta1(1));
92-
return laplace_marginal_tol<false>(
93-
poisson_log_likelihood_tuple_expanded{},
94-
std::forward_as_tuple(sums, eta1_tuple, eta2, eta3), theta_0,
95-
stan::math::test::squared_kernel_functor{},
96-
std::forward_as_tuple(x, std::make_tuple(phi_dbl(0), phi_dbl(1))),
97-
tolerance, max_num_steps, hessian_block_size, solver_num,
98-
max_steps_line_search, nullptr);
99-
};
100-
Eigen::VectorXd test1(phi_dbl);
101-
std::vector<double> test2 = {1.0, 1.0};
102-
stan::test::expect_ad<true>(tols, f_ll, phi_dbl, test1, test2);
103-
},
104-
theta_0);
105-
}
106-
107-
TEST(laplace, poisson_log_phi_dim_2_tuple) {
108-
using stan::math::laplace_marginal;
109-
using stan::math::laplace_marginal_tol;
110-
using stan::math::to_vector;
111-
using stan::math::value_of;
112-
using stan::math::var;
113-
// logger->current_test_name_ = "poisson_log_phi_dim_2";
114-
int dim_phi = 2;
115-
Eigen::Matrix<double, Eigen::Dynamic, 1> phi_dbl(dim_phi);
116-
phi_dbl << 1.6, 0.45;
117-
118-
int dim_theta = 2;
119-
Eigen::VectorXd theta_0(dim_theta);
120-
theta_0 << 0, 0;
121-
122-
int dim_x = 2;
123-
std::vector<Eigen::VectorXd> x(dim_theta);
124-
Eigen::VectorXd x_0{{0.05100797, 0.16086164}};
125-
Eigen::VectorXd x_1{{-0.59823393, 0.98701425}};
126-
x[0] = x_0;
127-
x[1] = x_1;
128-
129-
Eigen::VectorXd y_dummy;
130-
131-
std::vector<int> n_samples = {1, 1};
132-
std::vector<int> sums = {1, 0};
133-
134-
constexpr double tolerance = 1e-12;
135-
constexpr int max_num_steps = 100;
136-
using stan::is_var_v;
137-
using stan::scalar_type_t;
138-
using stan::math::test::laplace_issue;
139-
constexpr std::array known_issues{laplace_issue{0, 0, 0}};
140-
stan::test::ad_tolerances tols;
141-
tols.gradient_grad_ = 1e-1;
142-
stan::math::test::run_solver_grid(
143-
[&](int solver_num, int hessian_block_size, int max_steps_line_search,
144-
auto&& theta_0) {
145-
auto f_covar = [&](auto&& x_v, auto&& alpha, auto&& rho) {
146-
return laplace_marginal_tol<false>(
147-
poisson_log_likelihood2{}, std::forward_as_tuple(sums), theta_0,
148-
stan::math::test::squared_kernel_functor{},
149-
std::forward_as_tuple(x_v, std::make_tuple(alpha, rho)),
150-
tolerance, max_num_steps, hessian_block_size, solver_num,
151-
max_steps_line_search, nullptr);
152-
};
153-
stan::test::expect_ad<true>(tols, f_covar, x, phi_dbl[0], phi_dbl[1]);
154-
},
155-
theta_0);
156-
stan::math::test::run_solver_grid(
157-
[&](int solver_num, int hessian_block_size, int max_steps_line_search,
158-
auto&& theta_0) {
159-
auto f_ll = [&](auto&& alpha_rho, auto&& eta1, auto&& eta2) {
160-
return laplace_marginal_tol<false>(
161-
poisson_log_likelihood_tuple{},
162-
std::forward_as_tuple(sums, std::make_tuple(eta1, eta2)), theta_0,
163-
stan::math::test::squared_kernel_functor{},
164-
std::forward_as_tuple(
165-
x, std::make_tuple(alpha_rho(0), alpha_rho(1))),
166-
tolerance, max_num_steps, hessian_block_size, solver_num,
167-
max_steps_line_search, nullptr);
168-
};
169-
auto test1 = 1.0;
170-
auto test2 = 1.0;
171-
stan::test::expect_ad<true>(tols, f_ll, phi_dbl, test1, test2);
172-
},
173-
theta_0);
174-
}
175-
176-
struct poisson_log_likelihood_array_tuple {
177-
template <typename Theta, typename Eta>
178-
auto operator()(const Theta& theta, const std::vector<int>& delta_int,
179-
Eta&& eta, std::ostream* pstream) const {
180-
return stan::math::poisson_log_lpmf(delta_int, theta) + std::get<0>(eta[0])
181-
+ std::get<1>(eta[0]);
182-
}
183-
};
184-
185-
TEST(laplace, poisson_log_phi_dim_2_array_tuple) {
186-
using stan::math::laplace_marginal;
187-
using stan::math::laplace_marginal_tol;
188-
using stan::math::to_vector;
189-
using stan::math::value_of;
190-
using stan::math::var;
191-
// logger->current_test_name_ = "poisson_log_phi_dim_2";
192-
int dim_phi = 2;
193-
Eigen::Matrix<double, Eigen::Dynamic, 1> phi_dbl(dim_phi);
194-
phi_dbl << 1.6, 0.45;
195-
196-
int dim_theta = 2;
197-
Eigen::VectorXd theta_0(dim_theta);
198-
theta_0 << 0, 0;
199-
200-
int dim_x = 2;
201-
std::vector<Eigen::VectorXd> x(dim_theta);
202-
Eigen::VectorXd x_0{{0.05100797, 0.16086164}};
203-
Eigen::VectorXd x_1{{-0.59823393, 0.98701425}};
204-
x[0] = x_0;
205-
x[1] = x_1;
206-
207-
Eigen::VectorXd y_dummy;
208-
209-
std::vector<int> n_samples = {1, 1};
210-
std::vector<int> sums = {1, 0};
211-
212-
constexpr double tolerance = 1e-12;
213-
constexpr int max_num_steps = 100;
214-
using stan::is_var_v;
215-
using stan::scalar_type_t;
216-
using stan::math::test::laplace_issue;
217-
constexpr std::array known_issues{laplace_issue{0, 0, 0}};
218-
stan::test::ad_tolerances tols;
219-
tols.gradient_grad_ = 1e-1;
220-
stan::math::test::run_solver_grid(
221-
[&](int solver_num, int hessian_block_size, int max_steps_line_search,
222-
auto&& theta_0) {
223-
auto f_ll = [&](auto&& alpha_rho, auto&& eta1, auto&& eta2) {
224-
std::vector<std::tuple<std::decay_t<decltype(eta1)>,
225-
std::decay_t<decltype(eta2)>>>
226-
eta_tuple;
227-
eta_tuple.push_back(std::make_tuple(eta1, eta2));
228-
using alpha_scalar = stan::scalar_type_t<decltype(alpha_rho)>;
229-
std::vector<std::tuple<alpha_scalar, alpha_scalar>> alpha_tuple;
230-
alpha_tuple.push_back(std::make_tuple(alpha_rho(0), alpha_rho(1)));
231-
return laplace_marginal_tol<false>(
232-
poisson_log_likelihood_array_tuple{},
233-
std::forward_as_tuple(sums, eta_tuple), theta_0,
234-
stan::math::test::squared_kernel_functor{},
235-
std::forward_as_tuple(x, alpha_tuple), tolerance, max_num_steps,
236-
hessian_block_size, solver_num, max_steps_line_search, nullptr);
237-
};
238-
auto test1 = 1.0;
239-
auto test2 = 1.0;
240-
stan::test::expect_ad<true>(tols, f_ll, phi_dbl, test1, test2);
241-
},
242-
theta_0);
243-
}
244-
24523
TEST(laplace, poisson_log_phi_dim_2) {
24624
using stan::math::laplace_marginal;
24725
using stan::math::laplace_marginal_tol;

0 commit comments

Comments
 (0)