Weibull.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Implements a Weibull distribution.
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2010-01-01
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_RNG_WEIBULL_H
33 #define SHARK_RNG_WEIBULL_H
34 
35 #include <shark/Core/Exception.h>
36 #include <shark/Rng/Rng.h>
37 
38 
39 #include <boost/random.hpp>
40 #include <boost/random/uniform_01.hpp>
41 #include <cmath>
42 #include <istream>
43 namespace shark{
44 
45 /// \brief Weibull distribution.
46 template<class RealType = double>
48  {
49  public:
50  typedef RealType input_type;
51  typedef RealType result_type;
52 
53  explicit Weibull_distribution(RealType alpha,RealType beta)
54  :alpha_(alpha),beta_(beta)
55  {}
56 
57  RealType alpha() const
58  {
59  return alpha_;
60  }
61  RealType beta()const
62  {
63  return beta_;
64  }
65 
66  void reset() { }
67 
68  template<class Engine>
69  result_type operator()(Engine& eng)
70  {
71  double uni = boost::uniform_01<RealType>()(eng);
72  return std::pow(-beta_ * std::log(1. - uni), 1 / alpha_);
73  }
74 
75  template<class CharT, class Traits>
76  friend std::basic_ostream<CharT,Traits>&
77  operator<<(std::basic_ostream<CharT,Traits>& os, const Weibull_distribution& d)
78  {
79  os << d.alpha_;
80  os << d.beta_;
81  return os;
82  }
83 
84  template<class CharT, class Traits>
85  friend std::basic_istream<CharT,Traits>&
86  operator>>(std::basic_istream<CharT,Traits>& is, Weibull_distribution& d)
87  {
88  is >> d.alpha_;
89  is >> d.beta_;
90  return is;
91  }
92  private:
93  RealType alpha_;
94  RealType beta_;
95  };
96 
97 /// \brief Weibull distributed random variable.
98 template<typename RngType = shark::DefaultRngType>
99 class Weibull:public boost::variate_generator<RngType*,Weibull_distribution<> >
100  {
101  private:
102  typedef boost::variate_generator<RngType*,Weibull_distribution<> > Base;
103  public:
104 
105  Weibull( RngType & rng, double alpha = 1, double beta = 1 )
106  :Base(&rng,Weibull_distribution<>(alpha,beta))
107  {}
108 
109  using Base::operator();
110 
111  double operator()(double alpha,double beta)
112  {
113  Weibull_distribution<> dist(alpha,beta);
114  return dist(Base::engine());
115  }
116 
117  double alpha()const
118  {
119  return Base::distribution().alpha();
120  }
121  double beta()const
122  {
123  return Base::distribution().beta();
124  }
125  void alpha(double newAlpha)
126  {
127  Base::distribution()=Weibull_distribution<>(newAlpha,beta());
128  }
129  void vbeta(double newBeta)
130  {
131  Base::distribution()=Weibull_distribution<>(alpha(),newBeta);
132  }
133 
134  double p(double x)const
135  {
136  SHARK_RUNTIME_CHECK(x > 0,"Weibull distribution not defined for x <= 0");
137  return alpha() / beta() * exp(-pow(x / beta(), alpha())) * pow(x / beta(), alpha() - 1.);
138 
139  }
140 
141  };
142 }
143 #endif
144 
145 
146