Stan Math Library: stan/math/rev/scal/fun/log_mix.hpp Source File

| Stan Math Library 2.10.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
16 namespace math {
17
18 /* Computes shared terms in log_mix partial derivative calculations
19 *
20 * @param[in] theta_val value of mixing proportion theta.
21 * @param[in] lambda1_val value of log density multiplied by theta.
22 * @param[in] lambda2_val value of log density multiplied by 1 - theta.
23 * @param[out] one_m_exp_lam2_m_lam1 shared term in deriv calculation.
24 * @param[out] one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
25 * @param[out] one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
26 */
27 inline void
28 log_mix_partial_helper(const double& theta_val,
29 const double& lambda1_val,
30 const double& lambda2_val,
31 double& one_m_exp_lam2_m_lam1,
32 double& one_m_t_prod_exp_lam2_m_lam1,
33 double& one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1) {
34 using std::exp;
35 double lam2_m_lam1 = lambda2_val - lambda1_val;
36 double exp_lam2_m_lam1 = exp(lam2_m_lam1);
37 one_m_exp_lam2_m_lam1 = 1 - exp_lam2_m_lam1;
38 double one_m_t = 1 - theta_val;
39 one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
40 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
41 = 1 / (theta_val + one_m_t_prod_exp_lam2_m_lam1);
42 }
43
83 template <typename T_theta,
84 typename T_lambda1,
85 typename T_lambda2>
86 inline
87 typename return_type<T_theta, T_lambda1, T_lambda2>::type
88 log_mix(const T_theta& theta,
89 const T_lambda1& lambda1,
90 const T_lambda2& lambda2) {
91 using std::log;
92 using stan::math::log_mix;
93 using stan::math::log1m;
94 using stan::is_constant_struct;
95
96 OperandsAndPartials<T_theta, T_lambda1, T_lambda2>
97 operands_and_partials(theta, lambda1, lambda2);
98
99 double theta_double = value_of(theta);
100 const double lambda1_double = value_of(lambda1);
101 const double lambda2_double = value_of(lambda2);
102
103 double log_mix_function_value
104 = log_mix(theta_double, lambda1_double, lambda2_double);
105
106 double one_m_exp_lam2_m_lam1(0.0);
107 double one_m_t_prod_exp_lam2_m_lam1(0.0);
108 double one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1(0.0);
109
110 if (lambda1 > lambda2) {
111 log_mix_partial_helper(theta_double,
112 lambda1_double,
113 lambda2_double,
114 one_m_exp_lam2_m_lam1,
115 one_m_t_prod_exp_lam2_m_lam1,
116 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
117 } else {
118 log_mix_partial_helper(1.0 - theta_double,
119 lambda2_double,
120 lambda1_double,
121 one_m_exp_lam2_m_lam1,
122 one_m_t_prod_exp_lam2_m_lam1,
123 one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
124 one_m_exp_lam2_m_lam1 = -one_m_exp_lam2_m_lam1;
125 theta_double = one_m_t_prod_exp_lam2_m_lam1;
126 one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta);
127 }
128
129 if (!is_constant_struct<T_theta>::value)
130 operands_and_partials.d_x1[0]
131 = one_m_exp_lam2_m_lam1
132 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
133 if (!is_constant_struct<T_lambda1>::value)
134 operands_and_partials.d_x2[0]
135 = theta_double
136 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
137 if (!is_constant_struct<T_lambda2>::value)
138 operands_and_partials.d_x3[0]
139 = one_m_t_prod_exp_lam2_m_lam1
140 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
141
142 return operands_and_partials.value(log_mix_function_value);
143 }
144
145 } // namespace math
146
147 } // namespace stan
148
149 #endif
check_not_nan.hpp
stan::math::OperandsAndPartials::d_x2
VectorView< T_return_type, false, true > d_x2
Definition: OperandsAndPartials.hpp:40
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:15
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:68
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:38
stan::math::OperandsAndPartials::d_x3
VectorView< T_return_type, false, true > d_x3
Definition: OperandsAndPartials.hpp:41
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:29
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:117
value_of.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16
stan::math::OperandsAndPartials::d_x1
VectorView< T_return_type, false, true > d_x1
Definition: OperandsAndPartials.hpp:39

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