Stan  1.0
probability, sampling & optimization
trunc_normal.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__TRUNC_NORMAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__TRUNC_NORMAL_HPP__
3 
6 #include <stan/prob/traits.hpp>
8 
9 namespace stan {
10 
11  namespace prob {
41  template <bool propto,
42  typename T_y, typename T_loc, typename T_scale, typename T_alpha, typename T_beta,
43  class Policy>
44  typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_alpha,T_beta>::type
45  trunc_normal_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
46  const T_alpha& alpha, const T_beta& beta,
47  const Policy&) {
48  static const char* function = "stan::prob::trunc_normal_log(%1%)";
49 
50 
53  using boost::math::tools::promote_args;
54  using boost::math::isinf;
55  using stan::math::Phi;
56 
57  typename promote_args<T_y,T_loc,T_scale,T_alpha,T_beta>::type lp(0.0);
58 
59  if (!check_not_nan(function, alpha, "Lower bound",
60  &lp, Policy()))
61  return lp;
62  if (!check_not_nan(function, beta, "Upper bound",
63  &lp, Policy()))
64  return lp;
65  if (!check_greater(function, beta, alpha, "Upper bound",
66  &lp, Policy()))
67  return lp;
68 
69  if (y < alpha || y > beta) {
70  lp = LOG_ZERO;
71  }
72  else {
73  lp = normal_log<propto>(y,mu,sigma,Policy());
75  if (isinf(sigma))
76  lp -= log(beta - alpha);
77  else
78  lp -= log(Phi((beta - mu)/sigma) - Phi((alpha - mu)/sigma));
79  }
80  }
81 
82  return lp;
83  }
84 
85 
86  template <bool propto,
87  typename T_y, typename T_loc, typename T_scale, typename T_alpha, typename T_beta>
88  inline
89  typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_alpha,T_beta>::type
90  trunc_normal_log(const T_y& y, const T_loc& mu, const T_scale& sigma, const T_alpha& alpha, const T_beta& beta) {
91  return trunc_normal_log<propto>(y,mu,sigma,alpha,beta,stan::math::default_policy());
92  }
93 
94  template <typename T_y, typename T_loc, typename T_scale, typename T_alpha, typename T_beta,
95  class Policy>
96  inline
97  typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_alpha,T_beta>::type
98  trunc_normal_log(const T_y& y, const T_loc& mu, const T_scale& sigma, const T_alpha& alpha, const T_beta& beta,
99  const Policy&) {
100  return trunc_normal_log<false>(y,mu,sigma,alpha,beta,Policy());
101  }
102 
103  template <typename T_y, typename T_loc, typename T_scale, typename T_alpha, typename T_beta>
104  inline
105  typename boost::math::tools::promote_args<T_y,T_loc,T_scale,T_alpha,T_beta>::type
106  trunc_normal_log(const T_y& y, const T_loc& mu, const T_scale& sigma, const T_alpha& alpha, const T_beta& beta) {
107  return trunc_normal_log<false>(y,mu,sigma,alpha,beta,stan::math::default_policy());
108  }
109  }
110 }
111 #endif
bool isinf(const stan::agrad::var v)
Checks if the given number is infinite.
var Phi(const stan::agrad::var &a)
The unit normal cumulative density function for variables (stan).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
boost::math::tools::promote_args< T >::type Phi(T x)
The unit normal cumulative distribution function.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
boost::math::tools::promote_args< T_y, T_loc, T_scale, T_alpha, T_beta >::type trunc_normal_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_alpha &alpha, const T_beta &beta, const Policy &)
The log of the truncated normal density for the given y, mean, and standard deviation.
Probability, optimization and sampling library.
Definition: agrad.cpp:6
int isinf(const stan::agrad::var &a)
Checks if the given number is infinite.
Definition: agrad.hpp:2374
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33

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