1 // This is brl/bbas/bsta/bsta_weibull.h
2 #ifndef bsta_weibull_h_
3 #define bsta_weibull_h_
4 //:
5 // \file
6 // \brief A Weibull distribution
7 // \author Joseph L. Mundy
8 // \date November 8, 2008
9 //  The three-parameter, shifted,  Weibull distribution has the form
10 //  \verbatim
11 //                                            _    _ k
12 //                                           | x-mu |
13 //                              _    _ k-1  -|------|
14 //                        k    | x-mu |      |lambda|
15 //  p(x;lambda, k, mu)= ------ |------|    e -      -
16 //                      lambda |lambda|
17 //                             -      -
18 //  \endverbatim
19 // where lambda is called the scale parameter, k is the shape parameter and
20 // mu is the location parameter.
21 // \verbatim
22 //  Modifications
23 //   (none yet)
24 // \endverbatim
25 
26 #include <iostream>
27 #include <iosfwd>
28 #include "bsta_distribution.h"
29 #include <vnl/vnl_gamma.h>
30 #include <cassert>
31 #ifdef _MSC_VER
32 #  include <vcl_msvc_warnings.h>
33 #endif
34 
35 // A Weibull distribution does not have a natural, unique extension to
36 // multi-dimensional variables. However, various approaches do exist
37 // and could be implemented. Thus, it does make sense to still template over n.
38 // However, only the case for n = 1 will be implemented now.
39 template <class T>
40 class bsta_weibull : public bsta_distribution<T,1>
41 {
42   typedef typename bsta_distribution<T,1>::vector_type vector_;
43   typedef typename bsta_distribution<T,1>::vector_type covar_type_;
44 
45  public:
46   bsta_weibull();
47   //: two parameter form
48   bsta_weibull(vector_ const& lambda, vector_ const& k);
49 
50   //: three parameter form (the "shifted" Weibull)
51   bsta_weibull(vector_ const& lambda, vector_ const& k, vector_ const& mu);
52 
53   //: destructor
54   ~bsta_weibull()= default;
55 
56   //: the scale parameter
lambda()57   vector_ lambda() const {return lambda_;}
58 
59   //: the shape parameter
k()60   vector_ k() const {return k_;}
61 
62   //: the location parameter
mu()63   vector_ mu() const {return mu_;}
64 
65   //: The mean of the distribution, for 1-d the vector_ is typedefed to T
mean()66   vector_ mean() const
67   {
68     double dk = static_cast<double>(k_);
69     assert(dk>0);
70     double la = static_cast<double>(lambda_);
71     assert(la>0);
72     double m = static_cast<double>(mu_);
73     return static_cast<vector_>(m+la*vnl_gamma(1.0+1/dk));
74   }
75 
76   //: The variance of the distribution
var()77   covar_type_ var() const
78   {
79     double dk = static_cast<double>(k_);
80     assert(dk>0);
81     double la = static_cast<double>(lambda_);
82     assert(la>0);
83     double m = vnl_gamma(1.0+1/dk);
84     double v = vnl_gamma(1.0+2/dk);
85     double ret = la*la*(v-m*m);
86     return static_cast<vector_>(ret);
87   }
88   //: The co_variance of the distribution same as variance for 1-d case
covar()89   covar_type_ covar() const
90     {return this->var();}
91 
92   //: The probability density at this sample
93   T prob_density(const vector_& pt) const;
94 
95   //: The probability integrated over a box
96   T probability(const vector_& min_pt,
97                 const vector_& max_pt) const;
98  protected:
99   vector_ lambda_;
100   vector_ mu_;
101   vector_ k_;
102 };
103 
104 template <class T >
105 inline std::ostream& operator<< (std::ostream& os,
106                                 bsta_weibull<T> const& w)
107 {
108   os << "weibull:lambda(" << w.lambda() << ")\n"
109      << "weibull:k(" << w.k() << ")\n"
110      << "weibull:mu(" << w.mu() << ")\n";
111   return os;
112 }
113 
114 #endif // bsta_weibull_h_
115