Skip to content

Commit 02de278

Browse files
committed
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
1 parent fd52669 commit 02de278

5 files changed

Lines changed: 93 additions & 126 deletions

File tree

stan/math/opencl/kernels/multinomial_logit_glm_lpmf.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ static constexpr const char* multinomial_logit_glm_kernel_code = STRINGIFY(
3030
* and delta_global, without re-reading x_beta_global or alpha_global.
3131
*
3232
* @param[out] logp_global partial logp sums, one per work group
33-
* @param[out] delta_global residual matrix N_instances x N_classes (col-major)
33+
* @param[out] delta_global residual matrix N_instances x N_classes
34+
* (col-major)
3435
* @param[in] y_global outcome counts, N_instances x N_classes (col-major)
35-
* @param[in] x_beta_global product x*beta, N_instances x N_classes (col-major)
36+
* @param[in] x_beta_global product x*beta, N_instances x N_classes
37+
* (col-major)
3638
* @param[in] alpha_global intercepts: K values if is_alpha_vector, else
3739
* N_instances x N_classes (col-major)
3840
* @param N_instances number of instances
@@ -66,7 +68,8 @@ static constexpr const char* multinomial_logit_glm_kernel_code = STRINGIFY(
6668
eta_max = eta_k;
6769
}
6870

69-
// Pass 2: sum_exp, S_n, logp; if need_delta stash exp_k in delta_global.
71+
// Pass 2: sum_exp, S_n, logp; if need_delta stash exp_k in
72+
// delta_global.
7073
double sum_exp = 0;
7174
int S_n = 0;
7275
for (int k = 0; k < N_classes; k++) {

stan/math/prim/prob/multinomial_logit_glm_lpmf.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ inline return_type_t<T_x, T_alpha, T_beta> multinomial_logit_glm_lpmf(
146146
const Array<double, Dynamic, Dynamic> y_mat = to_matrix(y).array();
147147
const Array<double, Dynamic, 1> instance_totals = y_mat.rowwise().sum();
148148

149-
// lmultiply implements 0*log(0)=0: classes with softmax=0 and y=0 contribute 0.
149+
// lmultiply implements 0*log(0)=0: classes with softmax=0 and y=0 contribute
150+
// 0.
150151
T_partials_return logp = lmultiply(y_mat, softmax_mat).sum();
151152
if constexpr (include_summand<propto>::value) {
152153
logp += lgamma(instance_totals + 1.0).sum() - lgamma(y_mat + 1.0).sum();
@@ -174,7 +175,8 @@ inline return_type_t<T_x, T_alpha, T_beta> multinomial_logit_glm_lpmf(
174175
}
175176
if constexpr (is_autodiff_v<T_beta>) {
176177
partials<2>(ops_partials)
177-
= x_val.transpose().template cast<T_partials_return>() * delta.matrix();
178+
= x_val.transpose().template cast<T_partials_return>()
179+
* delta.matrix();
178180
}
179181
if constexpr (is_autodiff_v<T_x>) {
180182
edge<0>(ops_partials).partials_ = delta.matrix() * beta_val.transpose();

test/unit/math/opencl/rev/multinomial_logit_glm_lpmf_test.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,56 @@ TEST(ProbDistributionsMultinomialLogitGLM, error_checking) {
5151
matrix_cl<double> alpha_cl(alpha), alpha_bad_k_cl(alpha_bad_k),
5252
alpha_inf_cl(alpha_inf);
5353
matrix_cl<double> alpha_mat_cl(alpha_mat),
54-
alpha_mat_bad_n_cl(alpha_mat_bad_n),
55-
alpha_mat_bad_k_cl(alpha_mat_bad_k);
54+
alpha_mat_bad_n_cl(alpha_mat_bad_n), alpha_mat_bad_k_cl(alpha_mat_bad_k);
5655

5756
EXPECT_NO_THROW(
5857
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl, beta_cl));
59-
EXPECT_NO_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_mat_cl,
60-
beta_cl));
58+
EXPECT_NO_THROW(
59+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_mat_cl, beta_cl));
6160

62-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y_bad_n, x_cl, alpha_cl,
63-
beta_cl),
64-
std::invalid_argument);
65-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y_bad_k, x_cl, alpha_cl,
66-
beta_cl),
67-
std::invalid_argument);
61+
EXPECT_THROW(
62+
stan::math::multinomial_logit_glm_lpmf(y_bad_n, x_cl, alpha_cl, beta_cl),
63+
std::invalid_argument);
64+
EXPECT_THROW(
65+
stan::math::multinomial_logit_glm_lpmf(y_bad_k, x_cl, alpha_cl, beta_cl),
66+
std::invalid_argument);
6867
EXPECT_THROW(
6968
stan::math::multinomial_logit_glm_lpmf(y_neg, x_cl, alpha_cl, beta_cl),
7069
std::domain_error);
7170

72-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_bad_n_cl, alpha_cl,
73-
beta_cl),
74-
std::invalid_argument);
75-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_bad_m_cl, alpha_cl,
76-
beta_cl),
77-
std::invalid_argument);
71+
EXPECT_THROW(
72+
stan::math::multinomial_logit_glm_lpmf(y, x_bad_n_cl, alpha_cl, beta_cl),
73+
std::invalid_argument);
74+
EXPECT_THROW(
75+
stan::math::multinomial_logit_glm_lpmf(y, x_bad_m_cl, alpha_cl, beta_cl),
76+
std::invalid_argument);
7877

79-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_bad_k_cl,
80-
beta_cl),
81-
std::invalid_argument);
78+
EXPECT_THROW(
79+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_bad_k_cl, beta_cl),
80+
std::invalid_argument);
8281
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(
8382
y, x_cl, alpha_mat_bad_n_cl, beta_cl),
8483
std::invalid_argument);
8584
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(
8685
y, x_cl, alpha_mat_bad_k_cl, beta_cl),
8786
std::invalid_argument);
8887

89-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl,
90-
beta_bad_m_cl),
91-
std::invalid_argument);
92-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl,
93-
beta_bad_k_cl),
94-
std::invalid_argument);
88+
EXPECT_THROW(
89+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl, beta_bad_m_cl),
90+
std::invalid_argument);
91+
EXPECT_THROW(
92+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl, beta_bad_k_cl),
93+
std::invalid_argument);
9594

9695
EXPECT_THROW(
9796
stan::math::multinomial_logit_glm_lpmf(y, x_inf_cl, alpha_cl, beta_cl),
9897
std::domain_error);
99-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_inf_cl,
100-
beta_cl),
101-
std::domain_error);
102-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl,
103-
beta_inf_cl),
104-
std::domain_error);
98+
EXPECT_THROW(
99+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_inf_cl, beta_cl),
100+
std::domain_error);
101+
EXPECT_THROW(
102+
stan::math::multinomial_logit_glm_lpmf(y, x_cl, alpha_cl, beta_inf_cl),
103+
std::domain_error);
105104
}
106105

107106
TEST(ProbDistributionsMultinomialLogitGLM,
@@ -165,8 +164,7 @@ TEST(ProbDistributionsMultinomialLogitGLM, opencl_matches_cpu_zero_instances) {
165164
stan::math::test::compare_cpu_opencl_prim_rev(f_propto, x, alpha, beta);
166165
}
167166

168-
TEST(ProbDistributionsMultinomialLogitGLM,
169-
opencl_matches_cpu_zero_attributes) {
167+
TEST(ProbDistributionsMultinomialLogitGLM, opencl_matches_cpu_zero_attributes) {
170168
int N = 3, K = 3;
171169
vector<vector<int>> y{{1, 2, 0}, {0, 3, 1}, {2, 0, 2}};
172170
Matrix<double, Dynamic, Dynamic> x(N, 0);
@@ -184,8 +182,7 @@ TEST(ProbDistributionsMultinomialLogitGLM,
184182
stan::math::test::compare_cpu_opencl_prim_rev(f_propto, x, alpha, beta);
185183
}
186184

187-
TEST(ProbDistributionsMultinomialLogitGLM,
188-
opencl_matches_cpu_single_instance) {
185+
TEST(ProbDistributionsMultinomialLogitGLM, opencl_matches_cpu_single_instance) {
189186
int N = 1, M = 3, K = 4;
190187
vector<vector<int>> y{{2, 0, 3, 1}};
191188
Matrix<double, Dynamic, Dynamic> x(N, M);

test/unit/math/prim/prob/multinomial_logit_glm_lpmf_test.cpp

Lines changed: 40 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ TEST(ProbMultinomialLogitGLM, matchesMultinomialLogit) {
1313
Eigen::RowVectorXd alpha(4);
1414
alpha << 0.1, -0.3, 0.5, -0.2;
1515
Eigen::MatrixXd beta(3, 4);
16-
beta << 0.2, -0.1, 0.4, 0.0,
17-
-0.3, 0.5, -0.2, 0.1,
18-
0.1, 0.0, 0.3, -0.4;
16+
beta << 0.2, -0.1, 0.4, 0.0, -0.3, 0.5, -0.2, 0.1, 0.1, 0.0, 0.3, -0.4;
1917

2018
// eta = alpha^T + beta^T x^T (K-vector for multinomial_logit_lpmf)
2119
Eigen::VectorXd eta = alpha.transpose() + beta.transpose() * x.transpose();
@@ -46,8 +44,7 @@ TEST(ProbMultinomialLogitGLM, matchesBinomialLogitLpmf) {
4644
// Multinomial parameters: alpha=[0,0], beta_mult = [[beta_bin], [0]]
4745
const Eigen::RowVectorXd alpha_mult = Eigen::RowVectorXd::Zero(2);
4846
Eigen::MatrixXd beta_mult(2, 2);
49-
beta_mult << beta_bin(0), 0.0,
50-
beta_bin(1), 0.0;
47+
beta_mult << beta_bin(0), 0.0, beta_bin(1), 0.0;
5148

5249
const std::vector<int> n_success{3, 2};
5350
const std::vector<int> n_trials{5, 4};
@@ -59,9 +56,9 @@ TEST(ProbMultinomialLogitGLM, matchesBinomialLogitLpmf) {
5956

6057
// Binomial logit uses eta = x * beta_bin (no intercept, matches alpha=[0,0])
6158
const Eigen::VectorXd eta = x * beta_bin;
62-
EXPECT_FLOAT_EQ(stan::math::binomial_logit_lpmf(n_success, n_trials, eta),
63-
stan::math::multinomial_logit_glm_lpmf(y, x, alpha_mult,
64-
beta_mult));
59+
EXPECT_FLOAT_EQ(
60+
stan::math::binomial_logit_lpmf(n_success, n_trials, eta),
61+
stan::math::multinomial_logit_glm_lpmf(y, x, alpha_mult, beta_mult));
6562
}
6663

6764
// -----------------------------------------------------------------------
@@ -70,25 +67,20 @@ TEST(ProbMultinomialLogitGLM, matchesBinomialLogitLpmf) {
7067
// -----------------------------------------------------------------------
7168
TEST(ProbMultinomialLogitGLM, K3_N3_manual) {
7269
Eigen::MatrixXd x(3, 2);
73-
x << 1.0, 0.5,
74-
-0.5, 1.0,
75-
0.0, -1.0;
70+
x << 1.0, 0.5, -0.5, 1.0, 0.0, -1.0;
7671

7772
Eigen::RowVectorXd alpha(3);
7873
alpha << 0.2, -0.1, 0.5;
7974

8075
Eigen::MatrixXd beta(2, 3);
81-
beta << 0.3, -0.2, 0.1,
82-
-0.1, 0.4, -0.3;
76+
beta << 0.3, -0.2, 0.1, -0.1, 0.4, -0.3;
8377

84-
std::vector<std::vector<int>> y{
85-
{2, 1, 3},
86-
{0, 4, 1},
87-
{3, 0, 2}};
78+
std::vector<std::vector<int>> y{{2, 1, 3}, {0, 4, 1}, {3, 0, 2}};
8879

8980
double expected = 0;
9081
for (int n = 0; n < 3; ++n) {
91-
Eigen::VectorXd eta = alpha.transpose() + beta.transpose() * x.row(n).transpose();
82+
Eigen::VectorXd eta
83+
= alpha.transpose() + beta.transpose() * x.row(n).transpose();
9284
expected += stan::math::multinomial_logit_lpmf(y[n], eta);
9385
}
9486

@@ -104,31 +96,20 @@ TEST(ProbMultinomialLogitGLM, K3_N3_manual) {
10496
TEST(ProbMultinomialLogitGLM, matrixAlpha_fullX) {
10597
const int N = 4, K = 3, M = 2;
10698
Eigen::MatrixXd x(N, M);
107-
x << 1.0, 0.5,
108-
-0.5, 1.0,
109-
0.0, -1.0,
110-
0.7, 0.3;
99+
x << 1.0, 0.5, -0.5, 1.0, 0.0, -1.0, 0.7, 0.3;
111100

112101
Eigen::MatrixXd alpha(N, K);
113-
alpha << 0.2, -0.1, 0.5,
114-
-0.3, 0.4, 0.1,
115-
0.1, 0.0, -0.2,
116-
0.5, -0.3, 0.2;
102+
alpha << 0.2, -0.1, 0.5, -0.3, 0.4, 0.1, 0.1, 0.0, -0.2, 0.5, -0.3, 0.2;
117103

118104
Eigen::MatrixXd beta(M, K);
119-
beta << 0.3, -0.2, 0.1,
120-
-0.1, 0.4, -0.3;
105+
beta << 0.3, -0.2, 0.1, -0.1, 0.4, -0.3;
121106

122-
std::vector<std::vector<int>> y{
123-
{2, 1, 3},
124-
{0, 4, 1},
125-
{3, 0, 2},
126-
{1, 2, 1}};
107+
std::vector<std::vector<int>> y{{2, 1, 3}, {0, 4, 1}, {3, 0, 2}, {1, 2, 1}};
127108

128109
double expected = 0;
129110
for (int n = 0; n < N; ++n) {
130-
Eigen::VectorXd eta = alpha.row(n).transpose()
131-
+ beta.transpose() * x.row(n).transpose();
111+
Eigen::VectorXd eta
112+
= alpha.row(n).transpose() + beta.transpose() * x.row(n).transpose();
132113
expected += stan::math::multinomial_logit_lpmf(y[n], eta);
133114
}
134115

@@ -171,21 +152,16 @@ TEST(ProbMultinomialLogitGLM, negInfAlpha) {
171152
const int N = 2, K = 3, M = 2;
172153

173154
Eigen::MatrixXd x(N, M);
174-
x << 1.0, 0.5,
175-
0.3, -0.7;
155+
x << 1.0, 0.5, 0.3, -0.7;
176156

177157
Eigen::MatrixXd beta(M, K);
178-
beta << 0.3, -0.2, 0.1,
179-
-0.1, 0.4, -0.3;
158+
beta << 0.3, -0.2, 0.1, -0.1, 0.4, -0.3;
180159

181160
// Per-instance (N x K) alpha with -inf entries; y is 0 for those classes.
182161
Eigen::MatrixXd alpha(N, K);
183-
alpha << 0.2, -0.1, -stan::math::INFTY,
184-
-stan::math::INFTY, 0.4, 0.1;
162+
alpha << 0.2, -0.1, -stan::math::INFTY, -stan::math::INFTY, 0.4, 0.1;
185163

186-
std::vector<std::vector<int>> y{
187-
{2, 1, 0},
188-
{0, 3, 2}};
164+
std::vector<std::vector<int>> y{{2, 1, 0}, {0, 3, 2}};
189165

190166
const double logp = stan::math::multinomial_logit_glm_lpmf(y, x, alpha, beta);
191167
EXPECT_TRUE(std::isfinite(logp));
@@ -227,49 +203,43 @@ TEST(ProbMultinomialLogitGLM, throwsCorrectly) {
227203
std::vector<std::vector<int>> y{{1, 2, 3}, {2, 1, 0}};
228204

229205
// Mismatched number of instances (y has 1 row, x has 2)
230-
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf({{1, 2, 3}}, x, alpha,
231-
beta),
232-
std::invalid_argument);
233-
234-
// Mismatched number of classes in one outcome vector
235206
EXPECT_THROW(
236-
stan::math::multinomial_logit_glm_lpmf({{1, 2}, {2, 1, 0}}, x, alpha,
237-
beta),
207+
stan::math::multinomial_logit_glm_lpmf({{1, 2, 3}}, x, alpha, beta),
238208
std::invalid_argument);
239209

210+
// Mismatched number of classes in one outcome vector
211+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf({{1, 2}, {2, 1, 0}}, x,
212+
alpha, beta),
213+
std::invalid_argument);
214+
240215
// Negative counts
241-
EXPECT_THROW(
242-
stan::math::multinomial_logit_glm_lpmf({{-1, 2, 3}, {2, 1, 0}}, x,
243-
alpha, beta),
244-
std::domain_error);
216+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf({{-1, 2, 3}, {2, 1, 0}},
217+
x, alpha, beta),
218+
std::domain_error);
245219

246220
// Infinite alpha
247221
Eigen::RowVectorXd alpha_inf = alpha;
248222
alpha_inf[0] = stan::math::INFTY;
249-
EXPECT_THROW(
250-
stan::math::multinomial_logit_glm_lpmf(y, x, alpha_inf, beta),
251-
std::domain_error);
223+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x, alpha_inf, beta),
224+
std::domain_error);
252225

253226
// Infinite beta
254227
Eigen::MatrixXd beta_inf = beta;
255228
beta_inf(0, 0) = stan::math::INFTY;
256-
EXPECT_THROW(
257-
stan::math::multinomial_logit_glm_lpmf(y, x, alpha, beta_inf),
258-
std::domain_error);
229+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x, alpha, beta_inf),
230+
std::domain_error);
259231

260232
// Infinite x
261233
Eigen::MatrixXd x_inf = x;
262234
x_inf(0, 0) = stan::math::INFTY;
263-
EXPECT_THROW(
264-
stan::math::multinomial_logit_glm_lpmf(y, x_inf, alpha, beta),
265-
std::domain_error);
235+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x_inf, alpha, beta),
236+
std::domain_error);
266237

267238
// alpha cols mismatch with K (beta has 3 cols but alpha has 2 cols)
268239
Eigen::RowVectorXd alpha_bad(2);
269240
alpha_bad << 0.1, 0.2;
270-
EXPECT_THROW(
271-
stan::math::multinomial_logit_glm_lpmf(y, x, alpha_bad, beta),
272-
std::invalid_argument);
241+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x, alpha_bad, beta),
242+
std::invalid_argument);
273243

274244
// Matrix alpha with wrong number of rows
275245
Eigen::MatrixXd alpha_mat_bad_rows(3, 3);
@@ -284,8 +254,7 @@ TEST(ProbMultinomialLogitGLM, throwsCorrectly) {
284254
std::invalid_argument);
285255

286256
// beta rows mismatch with x cols (x has 2 cols, beta has 3 rows)
287-
EXPECT_THROW(
288-
stan::math::multinomial_logit_glm_lpmf(y, x, alpha,
289-
Eigen::MatrixXd(3, 3)),
290-
std::invalid_argument);
257+
EXPECT_THROW(stan::math::multinomial_logit_glm_lpmf(y, x, alpha,
258+
Eigen::MatrixXd(3, 3)),
259+
std::invalid_argument);
291260
}

0 commit comments

Comments
 (0)