Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
pareto_type_2_cdf_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_PARETO_TYPE_2_CDF_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_PARETO_TYPE_2_CDF_LOG_HPP
3 
4 #include <boost/random/variate_generator.hpp>
17 #include <cmath>
18 
19 
20 namespace stan {
21  namespace math {
22 
23  template <typename T_y, typename T_loc, typename T_scale, typename T_shape>
24  typename return_type<T_y, T_loc, T_scale, T_shape>::type
25  pareto_type_2_cdf_log(const T_y& y, const T_loc& mu,
26  const T_scale& lambda, const T_shape& alpha) {
27  typedef
29  T_partials_return;
30 
31  // Check sizes
32  // Size checks
33  if ( !( stan::length(y)
34  && stan::length(mu)
35  && stan::length(lambda)
36  && stan::length(alpha) ) )
37  return 0.0;
38 
39  // Check errors
40  static const char* function("stan::math::pareto_type_2_cdf_log");
41 
50  using stan::math::log1m;
51  using std::log;
52 
53  T_partials_return P(0.0);
54 
55  check_greater_or_equal(function, "Random variable", y, mu);
56  check_not_nan(function, "Random variable", y);
57  check_nonnegative(function, "Random variable", y);
58  check_positive_finite(function, "Scale parameter", lambda);
59  check_positive_finite(function, "Shape parameter", alpha);
60  check_consistent_sizes(function,
61  "Random variable", y,
62  "Scale parameter", lambda,
63  "Shape parameter", alpha);
64 
65  // Wrap arguments in vectors
66  VectorView<const T_y> y_vec(y);
67  VectorView<const T_loc> mu_vec(mu);
68  VectorView<const T_scale> lambda_vec(lambda);
69  VectorView<const T_shape> alpha_vec(alpha);
70  size_t N = max_size(y, mu, lambda, alpha);
71 
73  operands_and_partials(y, mu, lambda, alpha);
74 
75  VectorBuilder<true, T_partials_return,
76  T_y, T_loc, T_scale, T_shape>
77  cdf_log(N);
78 
79  VectorBuilder<true, T_partials_return,
80  T_y, T_loc, T_scale, T_shape>
81  inv_p1_pow_alpha_minus_one(N);
82 
84  T_partials_return, T_y, T_loc, T_scale, T_shape>
85  log_1p_y_over_lambda(N);
86 
87  for (size_t i = 0; i < N; i++) {
88  const T_partials_return temp = 1.0 + (value_of(y_vec[i])
89  - value_of(mu_vec[i]))
90  / value_of(lambda_vec[i]);
91  const T_partials_return p1_pow_alpha
92  = pow(temp, value_of(alpha_vec[i]));
93  cdf_log[i] = log1m(1.0 / p1_pow_alpha);
94 
95  inv_p1_pow_alpha_minus_one[i] = 1.0 / (p1_pow_alpha - 1.0);
96 
98  log_1p_y_over_lambda[i] = log(temp);
99  }
100 
101  // Compute vectorized CDF and its gradients
102 
103  for (size_t n = 0; n < N; n++) {
104  // Pull out values
105  const T_partials_return y_dbl = value_of(y_vec[n]);
106  const T_partials_return mu_dbl = value_of(mu_vec[n]);
107  const T_partials_return lambda_dbl = value_of(lambda_vec[n]);
108  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
109 
110  const T_partials_return grad_1_2 = alpha_dbl
111  * inv_p1_pow_alpha_minus_one[n] / (lambda_dbl - mu_dbl + y_dbl);
112 
113  // Compute
114  P += cdf_log[n];
115 
117  operands_and_partials.d_x1[n] += grad_1_2;
119  operands_and_partials.d_x2[n] -= grad_1_2;
121  operands_and_partials.d_x3[n] += (mu_dbl - y_dbl) * grad_1_2
122  / lambda_dbl;
124  operands_and_partials.d_x4[n] += log_1p_y_over_lambda[n]
125  * inv_p1_pow_alpha_minus_one[n];
126  }
127 
128  return operands_and_partials.to_var(P, y, mu, lambda, alpha);
129  }
130  }
131 }
132 #endif
bool check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is greater or equal than low.
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
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)
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
A variable implementation that stores operands and derivatives with respect to the variable...
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
return_type< T_y, T_loc, T_scale, T_shape >::type pareto_type_2_cdf_log(const T_y &y, const T_loc &mu, const T_scale &lambda, const T_shape &alpha)
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16

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