Stan Math Library  2.14.0
reverse mode automatic differentiation
log_mix.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_SCAL_FUN_LOG_MIX_HPP
2 #define STAN_MATH_REV_SCAL_FUN_LOG_MIX_HPP
3 
12 #include <cmath>
13 
14 namespace stan {
15  namespace math {
16 
17  /* Computes shared terms in log_mix partial derivative calculations
18  *
19  * @param[in] theta_val value of mixing proportion theta.
20  * @param[in] lambda1_val value of log density multiplied by theta.
21  * @param[in] lambda2_val value of log density multiplied by 1 - theta.
22  * @param[out] one_m_exp_lam2_m_lam1 shared term in deriv calculation.
23  * @param[out] one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
24  * @param[out] one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
25  */
26  inline void
27  log_mix_partial_helper(double theta_val,
28  double lambda1_val,
29  double lambda2_val,
30  double& one_m_exp_lam2_m_lam1,
31  double& one_m_t_prod_exp_lam2_m_lam1,
32  double& one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1) {
33  using std::exp;
34  double lam2_m_lam1 = lambda2_val - lambda1_val;
35  double exp_lam2_m_lam1 = exp(lam2_m_lam1);
36  one_m_exp_lam2_m_lam1 = 1 - exp_lam2_m_lam1;
37  double one_m_t = 1 - theta_val;
38  one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
39  one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
40  = 1 / (theta_val + one_m_t_prod_exp_lam2_m_lam1);
41  }
42 
82  template <typename T_theta,
83  typename T_lambda1,
84  typename T_lambda2>
85  inline
87  log_mix(const T_theta& theta,
88  const T_lambda1& lambda1,
89  const T_lambda2& lambda2) {
90  using std::log;
92 
94  operands_and_partials(theta, lambda1, lambda2);
95 
96  double theta_double = value_of(theta);
97  const double lambda1_double = value_of(lambda1);
98  const double lambda2_double = value_of(lambda2);
99 
100  double log_mix_function_value
101  = log_mix(theta_double, lambda1_double, lambda2_double);
102 
103  double one_m_exp_lam2_m_lam1(0.0);
104  double one_m_t_prod_exp_lam2_m_lam1(0.0);
105  double one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1(0.0);
106 
107  if (lambda1 > lambda2) {
108  log_mix_partial_helper(theta_double,
109  lambda1_double,
110  lambda2_double,
111  one_m_exp_lam2_m_lam1,
112  one_m_t_prod_exp_lam2_m_lam1,
113  one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
114  } else {
115  log_mix_partial_helper(1.0 - theta_double,
116  lambda2_double,
117  lambda1_double,
118  one_m_exp_lam2_m_lam1,
119  one_m_t_prod_exp_lam2_m_lam1,
120  one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
121  one_m_exp_lam2_m_lam1 = -one_m_exp_lam2_m_lam1;
122  theta_double = one_m_t_prod_exp_lam2_m_lam1;
123  one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta);
124  }
125 
127  operands_and_partials.d_x1[0]
128  = one_m_exp_lam2_m_lam1
129  * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
131  operands_and_partials.d_x2[0]
132  = theta_double
133  * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
135  operands_and_partials.d_x3[0]
136  = one_m_t_prod_exp_lam2_m_lam1
137  * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
138 
139  return operands_and_partials.value(log_mix_function_value);
140  }
141 
142  }
143 }
144 #endif
VectorView< T_return_type, false, true > d_x2
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
This class builds partial derivatives with respect to a set of operands.
VectorView< T_return_type, false, true > d_x3
void log_mix_partial_helper(const T_theta &theta, const T_lambda1 &lambda1, const T_lambda2 &lambda2, typename boost::math::tools::promote_args< T_theta, T_lambda1, T_lambda2 >::type(&partials_array)[N])
Definition: log_mix.hpp:28
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:116
VectorView< T_return_type, false, true > d_x1

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