Stan Math Library  2.20.0
reverse mode automatic differentiation
log_mix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_LOG_MIX_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_LOG_MIX_HPP
3 
13 #include <vector>
14 
15 namespace stan {
16 namespace math {
17 
40 template <typename T_theta, typename T_lam>
41 typename return_type<T_theta, T_lam>::type log_mix(const T_theta& theta,
42  const T_lam& lambda) {
43  static const char* function = "log_mix";
45  T_partials_return;
46 
47  typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
48 
49  const int N = length(theta);
50 
51  check_bounded(function, "theta", theta, 0, 1);
52  check_not_nan(function, "lambda", lambda);
53  check_not_nan(function, "theta", theta);
54  check_finite(function, "lambda", lambda);
55  check_finite(function, "theta", theta);
56  check_consistent_sizes(function, "theta", theta, "lambda", lambda);
57 
58  scalar_seq_view<T_theta> theta_vec(theta);
59  T_partials_vec theta_dbl(N);
60  for (int n = 0; n < N; ++n)
61  theta_dbl[n] = value_of(theta_vec[n]);
62 
63  scalar_seq_view<T_lam> lam_vec(lambda);
64  T_partials_vec lam_dbl(N);
65  for (int n = 0; n < N; ++n)
66  lam_dbl[n] = value_of(lam_vec[n]);
67 
68  T_partials_return logp = log_sum_exp((log(theta_dbl) + lam_dbl).eval());
69 
70  T_partials_vec theta_deriv(N);
71  theta_deriv.array() = (lam_dbl.array() - logp).exp();
72 
73  T_partials_vec lam_deriv = theta_deriv.cwiseProduct(theta_dbl);
74 
75  operands_and_partials<T_theta, T_lam> ops_partials(theta, lambda);
77  for (int n = 0; n < N; ++n)
78  ops_partials.edge1_.partials_[n] = theta_deriv[n];
79  }
80 
82  for (int n = 0; n < N; ++n)
83  ops_partials.edge2_.partials_[n] = lam_deriv[n];
84  }
85 
86  return ops_partials.build(logp);
87 }
88 
116 template <typename T_theta, typename T_lam, int R, int C>
118 log_mix(const T_theta& theta,
119  const std::vector<Eigen::Matrix<T_lam, R, C> >& lambda) {
120  static const char* function = "log_mix";
121  typedef typename stan::partials_return_type<
122  T_theta, std::vector<Eigen::Matrix<T_lam, R, C> > >::type
123  T_partials_return;
124 
125  typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
126 
127  typedef typename Eigen::Matrix<T_partials_return, -1, -1> T_partials_mat;
128 
129  typedef typename std::vector<Eigen::Matrix<T_lam, R, C> > T_lamvec_type;
130 
131  const int N = length(lambda);
132  const int M = theta.size();
133 
134  check_bounded(function, "theta", theta, 0, 1);
135  check_not_nan(function, "theta", theta);
136  check_finite(function, "theta", theta);
137  for (int n = 0; n < N; ++n) {
138  check_not_nan(function, "lambda", lambda[n]);
139  check_finite(function, "lambda", lambda[n]);
140  check_consistent_sizes(function, "theta", theta, "lambda", lambda[n]);
141  }
142 
143  scalar_seq_view<T_theta> theta_vec(theta);
144  T_partials_vec theta_dbl(M);
145  for (int m = 0; m < M; ++m)
146  theta_dbl[m] = value_of(theta_vec[m]);
147 
148  T_partials_mat lam_dbl(M, N);
149  vector_seq_view<T_lamvec_type> lam_vec(lambda);
150  for (int n = 0; n < N; ++n)
151  for (int m = 0; m < M; ++m)
152  lam_dbl(m, n) = value_of(lam_vec[n][m]);
153 
154  T_partials_mat logp_tmp = log(theta_dbl).replicate(1, N) + lam_dbl;
155 
156  T_partials_vec logp(N);
157  for (int n = 0; n < N; ++n)
158  logp[n] = log_sum_exp(logp_tmp.col(n).eval());
159 
160  operands_and_partials<T_theta, T_lamvec_type> ops_partials(theta, lambda);
161 
163  T_partials_mat derivs
164  = (lam_dbl - logp.transpose().replicate(M, 1))
165  .unaryExpr([](T_partials_return x) { return exp(x); });
167  for (int m = 0; m < M; ++m)
168  ops_partials.edge1_.partials_[m] = derivs.row(m).sum();
169  }
170 
172  for (int n = 0; n < N; ++n)
173  ops_partials.edge2_.partials_vec_[n]
174  = derivs.col(n).cwiseProduct(theta_dbl);
175  }
176  }
177  return ops_partials.build(logp.sum());
178 }
206 template <typename T_theta, typename T_lam>
208  const T_theta& theta, const std::vector<std::vector<T_lam> >& lambda) {
209  static const char* function = "log_mix";
210  typedef typename stan::partials_return_type<
211  T_theta, std::vector<std::vector<T_lam> > >::type T_partials_return;
212 
213  typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
214 
215  typedef typename Eigen::Matrix<T_partials_return, -1, -1> T_partials_mat;
216 
217  typedef typename std::vector<std::vector<T_lam> > T_lamvec_type;
218 
219  const int N = length(lambda);
220  const int M = theta.size();
221 
222  check_bounded(function, "theta", theta, 0, 1);
223  check_not_nan(function, "theta", theta);
224  check_finite(function, "theta", theta);
225  for (int n = 0; n < N; ++n) {
226  check_not_nan(function, "lambda", lambda[n]);
227  check_finite(function, "lambda", lambda[n]);
228  check_consistent_sizes(function, "theta", theta, "lambda", lambda[n]);
229  }
230 
231  scalar_seq_view<T_theta> theta_vec(theta);
232  T_partials_vec theta_dbl(M);
233  for (int m = 0; m < M; ++m)
234  theta_dbl[m] = value_of(theta_vec[m]);
235 
236  T_partials_mat lam_dbl(M, N);
237  for (int n = 0; n < N; ++n)
238  for (int m = 0; m < M; ++m)
239  lam_dbl(m, n) = value_of(lambda[n][m]);
240 
241  T_partials_mat logp_tmp = log(theta_dbl).replicate(1, N) + lam_dbl;
242 
243  T_partials_vec logp(N);
244  for (int n = 0; n < N; ++n)
245  logp[n] = log_sum_exp(logp_tmp.col(n).eval());
246 
247  T_partials_mat derivs
248  = (lam_dbl - logp.transpose().replicate(M, 1))
249  .unaryExpr([](T_partials_return x) { return exp(x); });
250 
251  T_partials_mat lam_deriv(M, N);
252  for (int n = 0; n < N; ++n)
253  lam_deriv.col(n) = derivs.col(n).cwiseProduct(theta_dbl);
254 
255  operands_and_partials<T_theta, T_lamvec_type> ops_partials(theta, lambda);
257  for (int m = 0; m < M; ++m)
258  ops_partials.edge1_.partials_[m] = derivs.row(m).sum();
259  }
260 
262  for (int n = 0; n < N; ++n)
263  for (int m = 0; m < M; ++m)
264  ops_partials.edge2_.partials_vec_[n][m] = lam_deriv(m, n);
265  }
266  return ops_partials.build(logp.sum());
267 }
268 } // namespace math
269 } // namespace stan
270 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
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
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
Template metaprogram to calculate the base scalar return type resulting from promoting all the scalar...
Definition: return_type.hpp:33
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:12
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
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
internal::ops_partials_edge< double, Op2 > edge2_
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
fvar< T > log_mix(const fvar< T > &theta, const fvar< T > &lambda1, const fvar< T > &lambda2)
Return the log mixture density with specified mixing proportion and log densities and its derivative ...
Definition: log_mix.hpp:107
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, Op1 > edge1_

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