All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
log_mix.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_SCAL_FUN_LOG_MIX_HPP
#define STAN_MATH_REV_SCAL_FUN_LOG_MIX_HPP
#include <stan/math/prim/scal/err/check_bounded.hpp>
#include <stan/math/prim/scal/err/check_not_nan.hpp>
#include <stan/math/prim/scal/fun/log1m.hpp>
#include <stan/math/prim/scal/fun/value_of.hpp>
#include <stan/math/prim/scal/fun/log_mix.hpp>
#include <stan/math/rev/scal/fun/value_of.hpp>
#include <stan/math/prim/scal/meta/is_constant_struct.hpp>
#include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
#include <cmath>
namespace stan {
namespace math {
/* Computes shared terms in log_mix partial derivative calculations
*
* @param[in] theta_val value of mixing proportion theta.
* @param[in] lambda1_val value of log density multiplied by theta.
* @param[in] lambda2_val value of log density multiplied by 1 - theta.
* @param[out] one_m_exp_lam2_m_lam1 shared term in deriv calculation.
* @param[out] one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
* @param[out] one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1 shared term in deriv calculation.
*/
inline void
log_mix_partial_helper(const double& theta_val,
const double& lambda1_val,
const double& lambda2_val,
double& one_m_exp_lam2_m_lam1,
double& one_m_t_prod_exp_lam2_m_lam1,
double& one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1) {
using std::exp;
double lam2_m_lam1 = lambda2_val - lambda1_val;
double exp_lam2_m_lam1 = exp(lam2_m_lam1);
one_m_exp_lam2_m_lam1 = 1 - exp_lam2_m_lam1;
double one_m_t = 1 - theta_val;
one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
= 1 / (theta_val + one_m_t_prod_exp_lam2_m_lam1);
}
template <typename T_theta,
typename T_lambda1,
typename T_lambda2>
inline
typename return_type<T_theta, T_lambda1, T_lambda2>::type
log_mix(const T_theta& theta,
const T_lambda1& lambda1,
const T_lambda2& lambda2) {
using std::log;
using stan::math::log_mix;
using stan::math::log1m;
using stan::is_constant_struct;
OperandsAndPartials<T_theta, T_lambda1, T_lambda2>
operands_and_partials(theta, lambda1, lambda2);
double theta_double = value_of(theta);
const double lambda1_double = value_of(lambda1);
const double lambda2_double = value_of(lambda2);
double log_mix_function_value
= log_mix(theta_double, lambda1_double, lambda2_double);
double one_m_exp_lam2_m_lam1(0.0);
double one_m_t_prod_exp_lam2_m_lam1(0.0);
double one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1(0.0);
if (lambda1 > lambda2) {
log_mix_partial_helper(theta_double,
lambda1_double,
lambda2_double,
one_m_exp_lam2_m_lam1,
one_m_t_prod_exp_lam2_m_lam1,
one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
} else {
log_mix_partial_helper(1.0 - theta_double,
lambda2_double,
lambda1_double,
one_m_exp_lam2_m_lam1,
one_m_t_prod_exp_lam2_m_lam1,
one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1);
one_m_exp_lam2_m_lam1 = -one_m_exp_lam2_m_lam1;
theta_double = one_m_t_prod_exp_lam2_m_lam1;
one_m_t_prod_exp_lam2_m_lam1 = 1.0 - value_of(theta);
}
if (!is_constant_struct<T_theta>::value)
operands_and_partials.d_x1[0]
= one_m_exp_lam2_m_lam1
* one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
if (!is_constant_struct<T_lambda1>::value)
operands_and_partials.d_x2[0]
= theta_double
* one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
if (!is_constant_struct<T_lambda2>::value)
operands_and_partials.d_x3[0]
= one_m_t_prod_exp_lam2_m_lam1
* one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
return operands_and_partials.to_var(log_mix_function_value);
}
} // namespace math
} // namespace stan
#endif
check_not_nan.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 promote_args< T_theta, T_lambda1, T_lambda2 >::type(&partials_array)[N])
Definition: log_mix.hpp:31
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::to_var
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Definition: OperandsAndPartials.hpp:322
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::math::OperandsAndPartials::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:253
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:16
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::OperandsAndPartials::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:259
stan::math::OperandsAndPartials
A variable implementation that stores operands and derivatives with respect to the variable...
Definition: OperandsAndPartials.hpp:238
log_mix.hpp
OperandsAndPartials.hpp
log1m.hpp
check_bounded.hpp
stan::math::OperandsAndPartials::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:256
is_constant_struct.hpp
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::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16

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