Stan  1.0
probability, sampling & optimization
logistic.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP__
3 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12  namespace prob {
13 
14  // Logistic(y|mu,sigma) [sigma > 0]
15  // FIXME: document
16  template <bool propto,
17  typename T_y, typename T_loc, typename T_scale,
18  class Policy>
20  logistic_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
21  const Policy&) {
22  static const char* function = "stan::prob::logistic_log(%1%)";
23 
29 
30  // check if any vectors are zero length
31  if (!(stan::length(y)
32  && stan::length(mu)
33  && stan::length(sigma)))
34  return 0.0;
35 
36 
37  // set up return value accumulator
38  double logp(0.0);
39 
40  // validate args (here done over var, which should be OK)
41  if (!check_finite(function, y, "Random variable", &logp, Policy()))
42  return logp;
43  if (!check_finite(function, mu, "Location parameter",
44  &logp, Policy()))
45  return logp;
46  if (!check_finite(function, sigma, "Scale parameter", &logp,
47  Policy()))
48  return logp;
49  if (!check_positive(function, sigma, "Scale parameter",
50  &logp, Policy()))
51  return logp;
52  if (!(check_consistent_sizes(function,
53  y,mu,sigma,
54  "Random variable","Location parameter","Scale parameter",
55  &logp, Policy())))
56  return logp;
57 
58  // check if no variables are involved and prop-to
60  return 0.0;
61 
62 
63  // set up template expressions wrapping scalars into vector views
64  agrad::OperandsAndPartials<T_y, T_loc, T_scale> operands_and_partials(y, mu, sigma);
65 
66  VectorView<const T_y> y_vec(y);
67  VectorView<const T_loc> mu_vec(mu);
68  VectorView<const T_scale> sigma_vec(sigma);
69  size_t N = max_size(y, mu, sigma);
70 
71  DoubleVectorView<true,T_scale> inv_sigma(length(sigma));
73  for (size_t i = 0; i < length(sigma); i++) {
74  inv_sigma[i] = 1.0 / value_of(sigma_vec[i]);
76  log_sigma[i] = log(value_of(sigma_vec[i]));
77  }
78 
80  exp_mu_div_sigma(max_size(mu,sigma));
82  exp_y_div_sigma(max_size(y,sigma));
84  for (size_t n = 0; n < max_size(mu,sigma); n++)
85  exp_mu_div_sigma[n] = exp(value_of(mu_vec[n]) / value_of(sigma_vec[n]));
86  for (size_t n = 0; n < max_size(y,sigma); n++)
87  exp_y_div_sigma[n] = exp(value_of(y_vec[n]) / value_of(sigma_vec[n]));
88  }
89 
90  using stan::math::log1p;
91  for (size_t n = 0; n < N; n++) {
92  const double y_dbl = value_of(y_vec[n]);
93  const double mu_dbl = value_of(mu_vec[n]);
94 
95  const double y_minus_mu = y_dbl - mu_dbl;
96  const double y_minus_mu_div_sigma = y_minus_mu * inv_sigma[n];
97  double exp_m_y_minus_mu_div_sigma(0);
99  exp_m_y_minus_mu_div_sigma = exp(-y_minus_mu_div_sigma);
100  double inv_1p_exp_y_minus_mu_div_sigma(0);
102  inv_1p_exp_y_minus_mu_div_sigma = 1 / (1 + exp(y_minus_mu_div_sigma));
103 
105  logp -= y_minus_mu_div_sigma;
107  logp -= log_sigma[n];
109  logp -= 2.0 * log1p(exp_m_y_minus_mu_div_sigma);
110 
112  operands_and_partials.d_x1[n] += (2 * inv_1p_exp_y_minus_mu_div_sigma - 1) * inv_sigma[n];
114  operands_and_partials.d_x2[n] +=
115  (1 - 2 * exp_mu_div_sigma[n] / (exp_mu_div_sigma[n] + exp_y_div_sigma[n])) * inv_sigma[n];
117  operands_and_partials.d_x3[n] +=
118  ((1 - 2 * inv_1p_exp_y_minus_mu_div_sigma)*y_minus_mu*inv_sigma[n] - 1) * inv_sigma[n];
119  }
120  return operands_and_partials.to_var(logp);
121  }
122 
123  template <bool propto,
124  typename T_y, typename T_loc, typename T_scale>
125  inline
127  logistic_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
128  return logistic_log<propto>(y,mu,sigma,stan::math::default_policy());
129  }
130 
131  template <typename T_y, typename T_loc, typename T_scale,
132  class Policy>
133  inline
135  logistic_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
136  const Policy&) {
137  return logistic_log<false>(y,mu,sigma,Policy());
138  }
139 
140  template <typename T_y, typename T_loc, typename T_scale>
141  inline
143  logistic_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
144  return logistic_log<false>(y,mu,sigma,stan::math::default_policy());
145  }
146 
147  }
148 }
149 #endif
var log1p(const stan::agrad::var &a)
The log (1 + x) function for variables (C99).
double value_of(const agrad::var &v)
Return the value of the specified variable.
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
Definition: agrad.hpp:1716
boost::math::tools::promote_args< T >::type log1p(T x)
Return the natural logarithm of one plus the specified value.
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_loc, T_scale >::type logistic_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Definition: logistic.hpp:20
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
A variable implementation that stores operands and derivatives with respect to the variable.
VectorView< double *, is_vector< T1 >::value > d_x1
VectorView< double *, is_vector< T2 >::value > d_x2
T_return_type to_var(double logp)
VectorView< double *, is_vector< T3 >::value > d_x3
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Definition: traits.hpp:39
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33
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: traits.hpp:368

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