Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_logit_glm_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_RNG_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_RNG_HPP
3
11#include <boost/random/bernoulli_distribution.hpp>
12#include <boost/random/variate_generator.hpp>
13#include <vector>
14
15namespace stan {
16namespace math {
17
42template <typename T_x, typename T_alpha, typename T_beta, class RNG>
44 const T_x& x, const T_alpha& alpha, const T_beta& beta, RNG& rng) {
45 using boost::bernoulli_distribution;
46 using boost::variate_generator;
47 using T_x_ref = ref_type_t<T_x>;
48 using T_alpha_ref = ref_type_t<T_alpha>;
49 using T_beta_ref = ref_type_t<T_beta>;
50
51 const size_t N = x.cols();
52 const size_t M = x.rows();
53
54 static constexpr const char* function = "bernoulli_logit_glm_rng";
55 check_consistent_size(function, "Weight vector", beta, N);
56 check_consistent_size(function, "Vector of intercepts", alpha, M);
57 T_x_ref x_ref = x;
58 T_alpha_ref alpha_ref = alpha;
59 T_beta_ref beta_ref = beta;
60 check_finite(function, "Matrix of independent variables", x_ref);
61 check_finite(function, "Weight vector", beta_ref);
62 check_finite(function, "Intercept", alpha_ref);
63
64 const auto& beta_vector = as_column_vector_or_scalar(beta_ref);
65
66 Eigen::VectorXd x_beta;
68 x_beta = x_ref * beta_vector;
69 } else {
70 x_beta = (x_ref.array() * forward_as<double>(beta_vector)).rowwise().sum();
71 }
72
73 scalar_seq_view<T_alpha> alpha_vec(alpha_ref);
74
76
77 for (size_t m = 0; m < M; ++m) {
78 double theta_m = alpha_vec[m] + x_beta(m);
79 variate_generator<RNG&, bernoulli_distribution<>> bernoulli_rng(
80 rng, bernoulli_distribution<>(inv_logit(theta_m)));
81 output[m] = bernoulli_rng();
82 }
83
84 return output.data();
85}
86} // namespace math
87} // namespace stan
88#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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...
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
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...
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if x is consistent with size expected_size.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
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
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...