Stan Math Library  2.20.0
reverse mode automatic differentiation
bernoulli_logit_glm_rng.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_BERNOULLI_LOGIT_GLM_RNG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_BERNOULLI_LOGIT_GLM_RNG_HPP
3 
10 #include <boost/random/bernoulli_distribution.hpp>
11 #include <boost/random/variate_generator.hpp>
12 #include <vector>
13 
14 namespace stan {
15 namespace math {
16 
41 template <typename T_x, typename T_alpha, typename T_beta, class RNG>
43  const T_x &x, const T_alpha &alpha, const T_beta &beta, RNG &rng) {
44  using boost::bernoulli_distribution;
45  using boost::variate_generator;
46 
47  static const char *function = "bernoulli_logit_glm_rng";
48 
49  const size_t N = x.col(0).size();
50  const size_t M = x.row(0).size();
51 
52  check_finite(function, "Matrix of independent variables", x);
53  check_finite(function, "Weight vector", beta);
54  check_finite(function, "Intercept", alpha);
55  check_consistent_size(function, "Weight vector", beta, M);
56  check_consistent_size(function, "Vector of intercepts", alpha, N);
57 
58  scalar_seq_view<T_beta> beta_vec(beta);
59  Eigen::VectorXd beta_vector(M);
60  for (int i = 0; i < M; ++i)
61  beta_vector[i] = beta_vec[i];
62 
63  Eigen::VectorXd x_beta = x * beta_vector;
64 
65  scalar_seq_view<T_alpha> alpha_vec(alpha);
66 
68 
69  for (size_t n = 0; n < N; ++n) {
70  double theta_n = alpha_vec[n] + x_beta(n);
71  variate_generator<RNG &, bernoulli_distribution<>> bernoulli_rng(
72  rng, bernoulli_distribution<>(inv_logit(theta_n)));
73  output[n] = bernoulli_rng();
74  }
75 
76  return output.data();
77 }
78 } // namespace math
79 } // namespace stan
80 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
VectorBuilder< true, int, T_theta >::type bernoulli_rng(const T_theta &theta, RNG &rng)
Return a Bernoulli random variate with specified chance of success parameter using the specified rand...
VectorBuilder< true, int, T_alpha >::type bernoulli_logit_glm_rng(const T_x &x, const T_alpha &alpha, const T_beta &beta, RNG &rng)
Returns a draw from the Generalized Linear Model (GLM) with Bernoulli distribution and logit link fun...
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition: inv_logit.hpp:20
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...
VectorBuilder allocates type T1 values to be used as intermediate values.

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