1 #ifndef STAN_MATH_PRIM_MAT_PROB_NORMAL_ID_GLM_LPDF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_NORMAL_ID_GLM_LPDF_HPP 47 template <
bool propto,
typename T_y,
typename T_x,
typename T_alpha,
48 typename T_beta,
typename T_scale>
51 const T_beta &
beta,
const T_scale &sigma) {
52 static const char *
function =
"normal_id_glm_lpdf";
54 T_scale>::type T_partials_return;
55 typedef typename std::conditional<
57 Eigen::Array<typename partials_return_type<T_scale>::type, -1, 1>,
64 const size_t N = x.rows();
65 const size_t M = x.cols();
70 if (is_vector<T_scale>::value)
72 "Vector of dependent variables", y);
75 "Vector of dependent variables", y);
95 Array<T_partials_return, Dynamic, 1> y_minus_mu_over_sigma
96 = x_val * beta_val_vec;
103 y, x, alpha, beta, sigma);
104 double y_minus_mu_over_sigma_squared_sum;
108 Matrix<T_partials_return, Dynamic, 1> mu_derivative
109 = inv_sigma * y_minus_mu_over_sigma;
111 ops_partials.
edge1_.partials_ = -mu_derivative;
114 ops_partials.
edge2_.partials_
115 = (beta_val_vec * mu_derivative.transpose()).
transpose();
118 ops_partials.
edge4_.partials_ = mu_derivative.transpose() * x_val;
122 ops_partials.
edge3_.partials_ = mu_derivative;
124 ops_partials.
edge3_.partials_[0] =
sum(mu_derivative);
127 if (is_vector<T_scale>::value) {
128 Array<T_partials_return, Dynamic, 1> y_minus_mu_over_sigma_squared
129 = y_minus_mu_over_sigma * y_minus_mu_over_sigma;
130 y_minus_mu_over_sigma_squared_sum =
sum(y_minus_mu_over_sigma_squared);
131 ops_partials.
edge5_.partials_
132 = (y_minus_mu_over_sigma_squared - 1) * inv_sigma;
134 y_minus_mu_over_sigma_squared_sum
135 =
sum(y_minus_mu_over_sigma * y_minus_mu_over_sigma);
136 ops_partials.
edge5_.partials_[0]
137 = (y_minus_mu_over_sigma_squared_sum - N) *
as_scalar(inv_sigma);
141 y_minus_mu_over_sigma_squared_sum
142 =
sum(y_minus_mu_over_sigma * y_minus_mu_over_sigma);
146 check_finite(
function,
"Vector of dependent variables", y);
149 check_finite(
function,
"Matrix of independent variables",
150 y_minus_mu_over_sigma_squared_sum);
156 T_partials_return logp(0.0);
160 if (is_vector<T_scale>::value)
161 logp -=
sum(
log(sigma_val_vec));
166 logp -= 0.5 * y_minus_mu_over_sigma_squared_sum;
167 return ops_partials.
build(logp);
170 template <
typename T_y,
typename T_x,
typename T_alpha,
typename T_beta,
174 const T_beta &
beta,
const T_scale &sigma) {
175 return normal_id_glm_lpdf<false>(y, x, alpha,
beta, sigma);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
const Eigen::Matrix< T, Eigen::Dynamic, 1 > & as_column_vector_or_scalar(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &a)
Converts input argument to a column vector or a scalar.
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
bool isfinite(const stan::math::var &v)
Checks if the given number has finite value.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
internal::ops_partials_edge< double, Op5 > edge5_
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
fvar< T > log(const fvar< T > &x)
internal::ops_partials_edge< double, Op4 > edge4_
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if the dimension of x is consistent, which is defined to be expected_size if x is a vector or 1...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
Template metaprogram to calculate the partial derivative type resulting from promoting all the scalar...
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
double as_scalar(const std::vector< T > &a)
Converts input to a scalar.
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
return_type< T_y, T_x, T_alpha, T_beta, T_scale >::type normal_id_glm_lpdf(const T_y &y, const T_x &x, const T_alpha &alpha, const T_beta &beta, const T_scale &sigma)
Returns the log PDF of the Generalized Linear Model (GLM) with Normal distribution and id link functi...
const double NEG_LOG_SQRT_TWO_PI
internal::ops_partials_edge< double, Op2 > edge2_
Eigen::ArrayWrapper< const Eigen::Matrix< T, R, C > > as_array_or_scalar(const Eigen::Matrix< T, R, C > &v)
Converts a matrix type to an array.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_