@@ -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// -----------------------------------------------------------------------
7168TEST (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) {
10496TEST (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