mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
weibull.hpp
Go to the documentation of this file.
1 /** \file weibull.hpp
2  * \brief The Weibull distribution.
3  * \ingroup gen_math_files
4  * \author Jared R. Males (jaredmales@gmail.com)
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef weibull_hpp
28 #define weibull_hpp
29 
30 namespace mx
31 {
32 namespace math
33 {
34 namespace func
35 {
36 
37 ///The MLE of the Weibull distribution lambda parameter.
38 /**
39  * \tparam realT a real floating point type
40  *
41  * \returns the MLE of lambda given the data.
42  *
43  * \ingroup gen_math_weibull
44  */
45 template<typename realT>
46 realT weibull_lambda( std::vector<realT> & x, ///<[in] the data points
47  realT k, ///< [in] the shape parameter
48  realT x0 = 0 ///< [in] [optional] the location parameter
49  )
50 {
51  int n = x.size();
52 
53  realT lambda = 0;
54 
55  for(int i=0; i< n; ++i) lambda += pow( x[i] - x0, k);
56 
57  lambda /= n;
58 
59  return pow(lambda, 1.0/k);
60 }
61 
62 ///The general shifted Weibull distribution at a point.
63 /** Calculates the value of the Weibull distribution at a location specified by x.
64  *
65  * \f[
66  *
67  * f(x; x_o, \lambda, k) =
68  * \begin{cases}
69  * \frac{k}{\lambda}\left( \frac{x-x_o}{\lambda}\right)^{k-1} \exp \left( -\left(\frac{x-x_o}{\lambda}\right)^k \right), & x \ge 0 \\
70  * 0, & x < 0
71  * \end{cases}
72  * \f]
73  *
74  * \tparam realT a real floating point type
75  *
76  * \returns the value of the Weibull distribution at x.
77  *
78  * \ingroup gen_math_weibull
79  */
80 template<typename realT>
81 realT weibull( realT x, ///< [in] the location at which to calculate the distribution
82  realT x0, ///< [in] the location parameter
83  realT k, ///< [in] the shape parameter
84  realT lambda ///< [in] the scale parameter
85  )
86 {
87  if(x - x0 < 0) return 0.0;
88 
89  realT rat = (x-x0)/lambda;
90 
91  return (k/lambda) * pow(rat, k-1) * exp( -pow(rat, k) );
92 
93 }
94 
95 ///The Weibull distribution at a point.
96 /** Calculates the value of the Weibull distribution at a location specified by x.
97  *
98  * \f[
99  *
100  * f(x; \lambda, k) =
101  * \begin{cases}
102  * \frac{k}{\lambda}\left( \frac{x}{\lambda}\right)^{k-1} \exp \left( -\left(\frac{x}{\lambda}\right)^k \right), & x \ge 0 \\
103  * 0, & x < 0
104  * \end{cases}
105  * \f]
106  *
107  * \tparam realT a real floating point type
108  *
109  * \returns the value of the Weibull distribution at x.
110  *
111  * \overload
112  *
113  * \ingroup gen_math_weibull
114  */
115 template<typename realT>
116 realT weibull( realT x, ///< [in] the location at which to calculate the distribution
117  realT k, ///< [in] the shape parameter
118  realT lambda ///< [in] the scale parameter
119  )
120 {
121  return weibull(x, static_cast<realT>(0), k, lambda);
122 }
123 
124 } //namespace func
125 } //namespace math
126 } //namespace mx
127 
128 #endif //weibull_hpp
constexpr units::realT k()
Boltzmann Constant.
Definition: constants.hpp:71
realT weibull(realT x, realT k, realT lambda)
The Weibull distribution at a point.
Definition: weibull.hpp:116
realT weibull_lambda(std::vector< realT > &x, realT k, realT x0=0)
The MLE of the Weibull distribution lambda parameter.
Definition: weibull.hpp:46
The mxlib c++ namespace.
Definition: mxError.hpp:107