Automatic Differentiation
 
Loading...
Searching...
No Matches
double_exponential_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DOUBLE_EXPONENTIAL_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_DOUBLE_EXPONENTIAL_LCDF_HPP
3
15#include <cmath>
16
17namespace stan {
18namespace math {
19
34template <typename T_y, typename T_loc, typename T_scale,
36 T_y, T_loc, T_scale>* = nullptr>
38 const T_y& y, const T_loc& mu, const T_scale& sigma) {
39 using T_partials_return = partials_return_t<T_y, T_loc, T_scale>;
40 using std::exp;
41 using std::log;
42 using T_y_ref = ref_type_t<T_y>;
43 using T_mu_ref = ref_type_t<T_loc>;
44 using T_sigma_ref = ref_type_t<T_scale>;
45 static constexpr const char* function = "double_exponential_lcdf";
46 check_consistent_sizes(function, "Random variable", y, "Location parameter",
47 mu, "Scale Parameter", sigma);
48 T_y_ref y_ref = y;
49 T_mu_ref mu_ref = mu;
50 T_sigma_ref sigma_ref = sigma;
51 check_not_nan(function, "Random variable", y_ref);
52 check_finite(function, "Location parameter", mu_ref);
53 check_positive_finite(function, "Scale parameter", sigma_ref);
54
55 if (size_zero(y, mu, sigma)) {
56 return 0;
57 }
58
59 T_partials_return cdf_log(0.0);
60 auto ops_partials = make_partials_propagator(y_ref, mu_ref, sigma_ref);
61
62 scalar_seq_view<T_y_ref> y_vec(y_ref);
63 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
64 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
65 size_t size_sigma = stan::math::size(sigma);
66 size_t N = max_size(y, mu, sigma);
67
69 for (size_t i = 0; i < size_sigma; i++) {
70 inv_sigma[i] = inv(sigma_vec.val(i));
71 }
72
73 for (size_t n = 0; n < N; n++) {
74 const T_partials_return y_dbl = y_vec.val(n);
75 const T_partials_return mu_dbl = mu_vec.val(n);
76 const T_partials_return scaled_diff = (y_dbl - mu_dbl) * inv_sigma[n];
77
78 const T_partials_return rep_deriv
79 = y_dbl < mu_dbl ? inv_sigma[n]
80 : inv_sigma[n] * inv(2 * exp(scaled_diff) - 1);
81
82 if (y_dbl < mu_dbl) {
83 cdf_log += LOG_HALF + scaled_diff;
84 } else {
85 cdf_log += log1m(0.5 * exp(-scaled_diff));
86 }
87
89 partials<0>(ops_partials)[n] += rep_deriv;
90 }
92 partials<1>(ops_partials)[n] -= rep_deriv;
93 }
95 partials<2>(ops_partials)[n] -= rep_deriv * scaled_diff;
96 }
97 }
98 return ops_partials.build(cdf_log);
99}
100} // namespace math
101} // namespace stan
102#endif
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_y_cl, T_loc_cl, T_scale_cl > double_exponential_lcdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
Returns the double exponential log cumulative density function.
size_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
static constexpr double LOG_HALF
The natural logarithm of 0.5, .
Definition constants.hpp:92
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:12
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...