Stan Math Library  2.20.0
reverse mode automatic differentiation
normal_id_glm_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_NORMAL_ID_GLM_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_NORMAL_ID_GLM_LPDF_HPP
3 
12 #include <cmath>
13 
14 namespace stan {
15 namespace math {
16 
47 template <bool propto, typename T_y, typename T_x, typename T_alpha,
48  typename T_beta, typename T_scale>
50 normal_id_glm_lpdf(const T_y &y, const T_x &x, const T_alpha &alpha,
51  const T_beta &beta, const T_scale &sigma) {
52  static const char *function = "normal_id_glm_lpdf";
53  typedef typename stan::partials_return_type<T_y, T_x, T_alpha, T_beta,
54  T_scale>::type T_partials_return;
55  typedef typename std::conditional<
57  Eigen::Array<typename partials_return_type<T_scale>::type, -1, 1>,
58  typename partials_return_type<T_scale>::type>::type T_scale_val;
59 
60  using Eigen::Array;
61  using Eigen::Dynamic;
62  using Eigen::Matrix;
63 
64  const size_t N = x.rows();
65  const size_t M = x.cols();
66 
67  check_positive_finite(function, "Scale vector", sigma);
68  check_consistent_size(function, "Vector of dependent variables", y, N);
69  check_consistent_size(function, "Weight vector", beta, M);
70  if (is_vector<T_scale>::value)
71  check_consistent_sizes(function, "Vector of scale parameters", sigma,
72  "Vector of dependent variables", y);
74  check_consistent_sizes(function, "Vector of intercepts", alpha,
75  "Vector of dependent variables", y);
76  if (size_zero(y, x, beta, sigma))
77  return 0;
78 
80  return 0;
81 
82  const auto &x_val = value_of_rec(x);
83  const auto &beta_val = value_of_rec(beta);
84  const auto &alpha_val = value_of_rec(alpha);
85  const auto &sigma_val = value_of_rec(sigma);
86  const auto &y_val = value_of_rec(y);
87 
88  const auto &beta_val_vec = as_column_vector_or_scalar(beta_val);
89  const auto &alpha_val_vec = as_column_vector_or_scalar(alpha_val);
90  const auto &sigma_val_vec = as_column_vector_or_scalar(sigma_val);
91  const auto &y_val_vec = as_column_vector_or_scalar(y_val);
92 
93  T_scale_val inv_sigma = 1 / as_array_or_scalar(sigma_val_vec);
94 
95  Array<T_partials_return, Dynamic, 1> y_minus_mu_over_sigma
96  = x_val * beta_val_vec;
97  y_minus_mu_over_sigma = (as_array_or_scalar(y_val_vec) - y_minus_mu_over_sigma
98  - as_array_or_scalar(alpha_val_vec))
99  * inv_sigma;
100 
101  // Compute the derivatives.
103  y, x, alpha, beta, sigma);
104  double y_minus_mu_over_sigma_squared_sum; // the most efficient way to
105  // calculate this depends on
106  // template parameters
108  Matrix<T_partials_return, Dynamic, 1> mu_derivative
109  = inv_sigma * y_minus_mu_over_sigma;
111  ops_partials.edge1_.partials_ = -mu_derivative;
112  }
114  ops_partials.edge2_.partials_
115  = (beta_val_vec * mu_derivative.transpose()).transpose();
116  }
118  ops_partials.edge4_.partials_ = mu_derivative.transpose() * x_val;
119  }
122  ops_partials.edge3_.partials_ = mu_derivative;
123  else
124  ops_partials.edge3_.partials_[0] = sum(mu_derivative);
125  }
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;
133  } else {
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);
138  }
139  }
140  } else {
141  y_minus_mu_over_sigma_squared_sum
142  = sum(y_minus_mu_over_sigma * y_minus_mu_over_sigma);
143  }
144 
145  if (!std::isfinite(y_minus_mu_over_sigma_squared_sum)) {
146  check_finite(function, "Vector of dependent variables", y);
147  check_finite(function, "Weight vector", beta);
148  check_finite(function, "Intercept", alpha);
149  check_finite(function, "Matrix of independent variables",
150  y_minus_mu_over_sigma_squared_sum); // if all other checks
151  // passed this will only
152  // fail if x is not finite
153  }
154 
155  // Compute log probability.
156  T_partials_return logp(0.0);
158  logp += NEG_LOG_SQRT_TWO_PI * N;
160  if (is_vector<T_scale>::value)
161  logp -= sum(log(sigma_val_vec));
162  else
163  logp -= N * log(as_scalar(sigma_val));
164  }
166  logp -= 0.5 * y_minus_mu_over_sigma_squared_sum;
167  return ops_partials.build(logp);
168 }
169 
170 template <typename T_y, typename T_x, typename T_alpha, typename T_beta,
171  typename T_scale>
173 normal_id_glm_lpdf(const T_y &y, const T_x &x, const T_alpha &alpha,
174  const T_beta &beta, const T_scale &sigma) {
175  return normal_id_glm_lpdf<false>(y, x, alpha, beta, sigma);
176 }
177 } // namespace math
178 } // namespace stan
179 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
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...
Definition: conjunction.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
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.
Definition: size_zero.hpp:18
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.
Definition: beta.hpp:51
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
Definition: return_type.hpp:36
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.
Definition: as_scalar.hpp:20
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
Definition: transpose.hpp:20
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
Definition: constants.hpp:156
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_

     [ Stan Home Page ] © 2011–2018, Stan Development Team.