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
8#include <cmath>
9
10namespace stan {
11namespace math {
12
13/* Computes shared terms in log_mix partial derivative calculations
14 *
15 * @param[in] theta_val value of mixing proportion theta.
16 * @param[in] lambda1_val value of log density multiplied by theta.
17 * @param[in] lambda2_val value of log density multiplied by 1 - theta.
18 * @param[out] one_m_exp_lam2_m_lam1 shared term in deriv calculation.
19 * @param[out] one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
20 * @param[out] one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1 shared term in deriv
21 * calculation.
22 */
24 double theta_val, double lambda1_val, double lambda2_val,
25 double& one_m_exp_lam2_m_lam1, double& one_m_t_prod_exp_lam2_m_lam1,
26 double& one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1) {
27 using std::exp;
28 double lam2_m_lam1 = lambda2_val - lambda1_val;
29 double exp_lam2_m_lam1 = exp(lam2_m_lam1);
30 one_m_exp_lam2_m_lam1 = 1 - exp_lam2_m_lam1;
31 double one_m_t = 1 - theta_val;
32 one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
33 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
34 = 1 / (theta_val + one_m_t_prod_exp_lam2_m_lam1);
35}
36
76template <typename T_theta, typename T_lambda1, typename T_lambda2,
79 const T_theta& theta, const T_lambda1& lambda1, const T_lambda2& lambda2) {
80 using std::log;
81
82 auto ops_partials = make_partials_propagator(theta, lambda1, lambda2);
83
84 double theta_double = value_of(theta);
85 const double lambda1_double = value_of(lambda1);
86 const double lambda2_double = value_of(lambda2);
87
88 double log_mix_function_value
89 = log_mix(theta_double, lambda1_double, lambda2_double);
90
91 double one_m_exp_lam2_m_lam1(0.0);
92 double one_m_t_prod_exp_lam2_m_lam1(0.0);
93 double one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1(0.0);
94
95 if (lambda1 > lambda2) {
96 log_mix_partial_helper(theta_double, lambda1_double, lambda2_double,
97 one_m_exp_lam2_m_lam1, one_m_t_prod_exp_lam2_m_lam1,
98 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
99 } else {
100 log_mix_partial_helper(1.0 - theta_double, lambda2_double, lambda1_double,
101 one_m_exp_lam2_m_lam1, one_m_t_prod_exp_lam2_m_lam1,
102 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
103 one_m_exp_lam2_m_lam1 = -one_m_exp_lam2_m_lam1;
104 theta_double = one_m_t_prod_exp_lam2_m_lam1;
105 one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta);
106 }
107
109 partials<0>(ops_partials)[0]
110 = one_m_exp_lam2_m_lam1 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
111 }
113 partials<1>(ops_partials)[0]
114 = theta_double * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
115 }
117 partials<2>(ops_partials)[0] = one_m_t_prod_exp_lam2_m_lam1
118 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
119 }
120
121 return ops_partials.build(log_mix_function_value);
122}
123
124} // namespace math
125} // namespace stan
126#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:29
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
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...