| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/err/check_bounded.hpp>
5 #include <stan/math/prim/scal/err/check_not_nan.hpp>
6 #include <stan/math/prim/scal/fun/log1m.hpp>
7 #include <stan/math/prim/scal/fun/value_of.hpp>
8 #include <stan/math/prim/scal/fun/log_mix.hpp>
9 #include <stan/math/rev/scal/fun/value_of.hpp>
10 #include <stan/math/prim/scal/meta/is_constant_struct.hpp>
11 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
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
86 typename return_type<T_theta, T_lambda1, T_lambda2>::type
87 log_mix(const T_theta& theta,
88 const T_lambda1& lambda1,
89 const T_lambda2& lambda2) {
90 using std::log;
91 using stan::is_constant_struct;
92
93 OperandsAndPartials<T_theta, T_lambda1, T_lambda2>
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
126 if (!is_constant_struct<T_theta>::value)
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;
130 if (!is_constant_struct<T_lambda1>::value)
131 operands_and_partials.d_x2[0]
132 = theta_double
133 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
134 if (!is_constant_struct<T_lambda2>::value)
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
check_not_nan.hpp
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:39
stan
Definition: log_sum_exp.hpp:8
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::OperandsAndPartials::value
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
Definition: OperandsAndPartials.hpp:67
is_constant_struct.hpp
stan::return_type::type
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
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:13
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::OperandsAndPartials
This class builds partial derivatives with respect to a set of operands.
Definition: OperandsAndPartials.hpp:37
stan::math::OperandsAndPartials::d_x3
VectorView< T_return_type, false, true > d_x3
Definition: OperandsAndPartials.hpp:40
log_mix.hpp
log1m.hpp
OperandsAndPartials.hpp
check_bounded.hpp
stan::math::log_mix_partial_helper
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
value_of.hpp
stan::math::log_mix
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
value_of.hpp
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:38

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