Stan Math Library  2.20.0
reverse mode automatic differentiation
ordered_probit_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_ORDERED_PROBIT_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_ORDERED_PROBIT_LPMF_HPP
3 
11 #include <vector>
12 
13 namespace stan {
14 namespace math {
15 
39 template <bool propto, typename T_loc, typename T_cut>
41  int y, const T_loc& lambda,
42  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
43  using std::exp;
44  using std::log;
45 
46  static const char* function = "ordered_probit";
47 
48  int K = c.size() + 1;
49 
50  check_bounded(function, "Random variable", y, 1, K);
51  check_finite(function, "Location parameter", lambda);
52  check_greater(function, "Size of cut points parameter", c.size(), 0);
53  check_ordered(function, "Cut-points", c);
54  check_finite(function, "Cut-points", c);
55 
56  if (y == 1) {
57  return log1m(Phi(lambda - c[0]));
58  } else if (y == K) {
59  return log(Phi(lambda - c[K - 2]));
60  } else {
61  return log(Phi(lambda - c[y - 2]) - Phi(lambda - c[y - 1]));
62  }
63 }
64 
65 template <typename T_loc, typename T_cut>
67  int y, const T_loc& lambda,
68  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
69  return ordered_probit_lpmf<false>(y, lambda, c);
70 }
71 
97 template <bool propto, typename T_loc, typename T_cut>
99  const std::vector<int>& y,
100  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& lambda,
101  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
102  using std::exp;
103  using std::log;
104 
105  static const char* function = "ordered_probit";
106 
107  int N = lambda.size();
108  int K = c.size() + 1;
109 
110  check_consistent_sizes(function, "Integers", y, "Locations", lambda);
111  check_bounded(function, "Random variable", y, 1, K);
112  check_finite(function, "Location parameter", lambda);
113  check_ordered(function, "Cut-points", c);
114  check_greater(function, "Size of cut points parameter", c.size(), 0);
115  check_finite(function, "Cut-points", c);
116 
117  typename return_type<T_loc, T_cut>::type logp_n(0.0);
118 
119  for (int i = 0; i < N; ++i) {
120  if (y[i] == 1) {
121  logp_n += log1m(Phi(lambda[i] - c[0]));
122  } else if (y[i] == K) {
123  logp_n += log(Phi(lambda[i] - c[K - 2]));
124  } else {
125  logp_n
126  += log(Phi(lambda[i] - c[y[i] - 2]) - Phi(lambda[i] - c[y[i] - 1]));
127  }
128  }
129  return logp_n;
130 }
131 
132 template <typename T_loc, typename T_cut>
134  const std::vector<int>& y,
135  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& lambda,
136  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
137  return ordered_probit_lpmf<false>(y, lambda, c);
138 }
139 
167 template <bool propto, typename T_loc, typename T_cut>
169  const std::vector<int>& y,
170  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& lambda,
171  const std::vector<Eigen::Matrix<T_cut, Eigen::Dynamic, 1> >& c) {
172  using std::exp;
173  using std::log;
174 
175  static const char* function = "ordered_probit";
176 
177  int N = lambda.size();
178 
179  check_consistent_sizes(function, "Integers", y, "Locations", lambda);
180  check_consistent_sizes(function, "Integers", y, "Cut-points", c);
181 
182  for (int i = 0; i < N; ++i) {
183  int K = c[i].size() + 1;
184  check_bounded(function, "Random variable", y[i], 1, K);
185  check_greater(function, "Size of cut points parameter", c[i].size(), 0);
186  check_ordered(function, "Cut-points", c[i]);
187  }
188 
189  check_finite(function, "Location parameter", lambda);
190  check_finite(function, "Cut-points", c);
191 
192  typename return_type<T_loc, T_cut>::type logp_n(0.0);
193 
194  for (int i = 0; i < N; ++i) {
195  int K = c[i].size() + 1;
196 
197  if (y[i] == 1) {
198  logp_n += log1m(Phi(lambda[i] - c[i][0]));
199  } else if (y[i] == K) {
200  logp_n += log(Phi(lambda[i] - c[i][K - 2]));
201  } else {
202  logp_n += log(Phi(lambda[i] - c[i][y[i] - 2])
203  - Phi(lambda[i] - c[i][y[i] - 1]));
204  }
205  }
206  return logp_n;
207 }
208 
209 template <typename T_loc, typename T_cut>
211  const std::vector<int>& y,
212  const Eigen::Matrix<T_loc, Eigen::Dynamic, 1>& lambda,
213  const std::vector<Eigen::Matrix<T_cut, Eigen::Dynamic, 1> >& c) {
214  return ordered_probit_lpmf<false>(y, lambda, c);
215 }
216 } // namespace math
217 } // namespace stan
218 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
return_type< T_loc, T_cut >::type ordered_probit_lpmf(int y, const T_loc &lambda, const Eigen::Matrix< T_cut, Eigen::Dynamic, 1 > &c)
Returns the (natural) log probability of the specified integer outcome given the continuous location ...
void check_ordered(const char *function, const char *name, const std::vector< T_y > &y)
Check if the specified vector is sorted into strictly increasing order.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
fvar< T > Phi(const fvar< T > &x)
Definition: Phi.hpp:13
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
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.
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:12

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