Automatic Differentiation
 
Loading...
Searching...
No Matches
log_mix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_LOG_MIX_HPP
2#define STAN_MATH_REV_FUN_LOG_MIX_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
14/* Computes shared terms in log_mix partial derivative calculations
15 *
16 * @param[in] theta_val value of mixing proportion theta.
17 * @param[in] lambda1_val value of log density multiplied by theta.
18 * @param[in] lambda2_val value of log density multiplied by 1 - theta.
19 * @param[out] one_m_exp_lam2_m_lam1 shared term in deriv calculation.
20 * @param[out] one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
21 * @param[out] one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1 shared term in deriv
22 * calculation.
23 */
25 double theta_val, double lambda1_val, double lambda2_val,
26 double& one_m_exp_lam2_m_lam1, double& one_m_t_prod_exp_lam2_m_lam1,
27 double& one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1) {
28 using std::exp;
29 double lam2_m_lam1 = lambda2_val - lambda1_val;
30 double exp_lam2_m_lam1 = exp(lam2_m_lam1);
31 one_m_exp_lam2_m_lam1 = 1 - exp_lam2_m_lam1;
32 double one_m_t = 1 - theta_val;
33 one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
34 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
35 = 1 / (theta_val + one_m_t_prod_exp_lam2_m_lam1);
36}
37
77template <typename T_theta, typename T_lambda1, typename T_lambda2,
80 const T_theta& theta, const T_lambda1& lambda1, const T_lambda2& lambda2) {
81 using std::log;
82
83 auto ops_partials = make_partials_propagator(theta, lambda1, lambda2);
84
85 double theta_double = value_of(theta);
86 const double lambda1_double = value_of(lambda1);
87 const double lambda2_double = value_of(lambda2);
88
89 double log_mix_function_value
90 = log_mix(theta_double, lambda1_double, lambda2_double);
91
92 double one_m_exp_lam2_m_lam1(0.0);
93 double one_m_t_prod_exp_lam2_m_lam1(0.0);
94 double one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1(0.0);
95
96 if (lambda1 > lambda2) {
97 log_mix_partial_helper(theta_double, lambda1_double, lambda2_double,
98 one_m_exp_lam2_m_lam1, one_m_t_prod_exp_lam2_m_lam1,
99 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
100 } else {
101 log_mix_partial_helper(1.0 - theta_double, lambda2_double, lambda1_double,
102 one_m_exp_lam2_m_lam1, one_m_t_prod_exp_lam2_m_lam1,
103 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
104 one_m_exp_lam2_m_lam1 = -one_m_exp_lam2_m_lam1;
105 theta_double = one_m_t_prod_exp_lam2_m_lam1;
106 one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta);
107 }
108
110 partials<0>(ops_partials)[0]
111 = one_m_exp_lam2_m_lam1 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
112 }
114 partials<1>(ops_partials)[0]
115 = theta_double * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
116 }
118 partials<2>(ops_partials)[0] = one_m_t_prod_exp_lam2_m_lam1
119 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
120 }
121
122 return ops_partials.build(log_mix_function_value);
123}
124
125} // namespace math
126} // namespace stan
127#endif
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
require_any_t< is_var< std::decay_t< Types > >... > require_any_var_t
Require any of the types satisfy is_var.
Definition is_var.hpp:39
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
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:98
void log_mix_partial_helper(const T_theta &theta, const T_lambda1 &lambda1, const T_lambda2 &lambda2, promote_args_t< T_theta, T_lambda1, T_lambda2 >(&partials_array)[N])
Definition log_mix.hpp:28
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...