66#include < stan/math/prim/fun/as_column_vector_or_scalar.hpp>
77#include < stan/math/prim/fun/as_array_or_scalar.hpp>
88#include < stan/math/prim/fun/constants.hpp>
9+ #include < stan/math/prim/fun/isfinite.hpp>
910#include < stan/math/prim/fun/log.hpp>
1011#include < stan/math/prim/fun/size.hpp>
1112#include < stan/math/prim/fun/size_zero.hpp>
1213#include < stan/math/prim/fun/sum.hpp>
1314#include < stan/math/prim/fun/to_ref.hpp>
14- #include < stan/math/prim/fun/value_of_rec .hpp>
15+ #include < stan/math/prim/fun/value_of .hpp>
1516#include < stan/math/prim/functor/partials_propagator.hpp>
1617#include < cmath>
1718
@@ -59,6 +60,7 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
5960 using Eigen::Dynamic;
6061 using Eigen::Matrix;
6162 using Eigen::VectorXd;
63+ using std::isfinite;
6264 constexpr int T_x_rows = T_x::RowsAtCompileTime;
6365 using T_partials_return
6466 = partials_return_t <T_y, T_x, T_alpha, T_beta, T_scale>;
@@ -86,7 +88,7 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
8688 N_instances);
8789 check_consistent_size (function, " Vector of intercepts" , alpha, N_instances);
8890 T_sigma_ref sigma_ref = sigma;
89- const auto & sigma_val = value_of_rec (sigma_ref);
91+ const auto & sigma_val = value_of (sigma_ref);
9092 const auto & sigma_val_vec = to_ref (as_column_vector_or_scalar (sigma_val));
9193 check_positive_finite (function, " Scale vector" , sigma_val_vec);
9294
@@ -102,11 +104,10 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
102104 T_alpha_ref alpha_ref = alpha;
103105 T_beta_ref beta_ref = beta;
104106
105- const auto & y_val = value_of_rec (y_ref);
106- const auto & x_val
107- = to_ref_if<!is_constant<T_beta>::value>(value_of_rec (x_ref));
108- const auto & alpha_val = value_of_rec (alpha_ref);
109- const auto & beta_val = value_of_rec (beta_ref);
107+ const auto & y_val = value_of (y_ref);
108+ const auto & x_val = to_ref_if<!is_constant<T_beta>::value>(value_of (x_ref));
109+ const auto & alpha_val = value_of (alpha_ref);
110+ const auto & beta_val = value_of (beta_ref);
110111
111112 const auto & y_val_vec = as_column_vector_or_scalar (y_val);
112113 const auto & alpha_val_vec = as_column_vector_or_scalar (alpha_val);
@@ -116,7 +117,7 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
116117 T_scale_val inv_sigma = 1.0 / as_array_or_scalar (sigma_val_vec);
117118
118119 // the most efficient way to calculate this depends on template parameters
119- double y_scaled_sq_sum;
120+ T_partials_return y_scaled_sq_sum;
120121
121122 Array<T_partials_return, Dynamic, 1 > y_scaled (N_instances);
122123 if (T_x_rows == 1 ) {
@@ -178,7 +179,8 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
178179 } else {
179180 y_scaled_sq_sum = sum (y_scaled * y_scaled);
180181 partials<4 >(ops_partials)[0 ]
181- = (y_scaled_sq_sum - N_instances) * forward_as<double >(inv_sigma);
182+ = (y_scaled_sq_sum - N_instances)
183+ * forward_as<partials_return_t <T_sigma_ref>>(inv_sigma);
182184 }
183185 } else {
184186 y_scaled_sq_sum = sum (y_scaled * y_scaled);
@@ -187,7 +189,7 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
187189 y_scaled_sq_sum = sum (y_scaled * y_scaled);
188190 }
189191
190- if (!std:: isfinite (y_scaled_sq_sum)) {
192+ if (!isfinite (y_scaled_sq_sum)) {
191193 check_finite (function, " Vector of dependent variables" , y_val_vec);
192194 check_finite (function, " Weight vector" , beta_val_vec);
193195 check_finite (function, " Intercept" , alpha_val_vec);
@@ -204,7 +206,8 @@ return_type_t<T_y, T_x, T_alpha, T_beta, T_scale> normal_id_glm_lpdf(
204206 if (is_vector<T_scale>::value) {
205207 logp -= sum (log (sigma_val_vec));
206208 } else {
207- logp -= N_instances * log (forward_as<double >(sigma_val_vec));
209+ logp -= N_instances
210+ * log (forward_as<partials_return_t <T_sigma_ref>>(sigma_val_vec));
208211 }
209212 }
210213 logp -= 0.5 * y_scaled_sq_sum;
0 commit comments