Cauchy.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Standard Cauchy 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_CAUCHY_H
33 #define SHARK_RNG_CAUCHY_H
34 
35 #include <shark/Core/Math.h>
36 #include <shark/Rng/Rng.h>
37 
38 #include <boost/random.hpp>
39 #include <boost/random/cauchy_distribution.hpp>
40 #include <cmath>
41 
42 namespace shark{
43 
44  /*!
45  * \brief Cauchy distribution
46  *
47  * This class is a thin wrapper for the boost::cauchy_distribution class.
48  * The %Cauchy distribution (aka "Lorentzian") is defined by:
49  *
50  * \f$
51  * f(x) = \frac{1}{\pi \sigma (1 + \left[\frac {(x-x_0)} \sigma\right]^2 )}
52  * \f$
53  *
54  * <br>
55  * The %Cauchy distribution is important as an example of a pathological
56  * case. The %Cauchy distribution looks similar to a Normal distribution,
57  * but has much heavier tails. When studying hypothesis tests that assume
58  * normality, seeing how the tests perform on data from a %Cauchy
59  * distribution is a good indicator of how sensitive the tests are to
60  * heavy-tail departures from normality. Likewise, it is a good check
61  * for robust techniques that are designed to work well under a wide
62  * variety of distributional assumptions.
63  */
64  template<typename RngType = shark::DefaultRngType>
65  class Cauchy:public boost::variate_generator<RngType*,boost::cauchy_distribution<> >
66  {
67  private:
68  typedef boost::variate_generator<RngType*,boost::cauchy_distribution<> > Base;
69  public:
70 
71  /*!
72  * \brief Creates a new %Cauchy random generator instance
73  *
74  *\param median the median of the distribution
75  *\param sigma the width of the distribution
76  *\param rng the used random number generator
77  */
78  Cauchy(RngType& rng,double median=0,double sigma=1)
79  :Base(&rng,boost::cauchy_distribution<>(median,sigma))
80  {}
81 
82  //! creates a cauchy distributed number using the preset parameters
83  using Base::operator();
84 
85  /*!
86  *\brief creates a cauchy distributed number from parameters
87  *
88  *\param median the median of the distribution
89  *\param sigma the width of the distribution
90  */
91  double operator()(double median,double sigma)
92  {
93  boost::cauchy_distribution<> dist(median,sigma);
94  return dist(Base::engine());
95  }
96 
97  //! returns the current median of the distribution
98  double median()const
99  {
100  return Base::distribution().median();
101  }
102 
103  //! returns the width of the distribution
104  double sigma()const
105  {
106  return Base::distribution().sigma();
107  }
108  //! sets the median of the distribution
109  //! \param newMedian the new value for the Median
110  void median(double newMedian)
111  {
112  Base::distribution()=boost::cauchy_distribution<>(newMedian,sigma());
113  }
114  //! sets the width of the distribution
115  //! \param newSigma the new value for sigma
116  void sigma(double newSigma)
117  {
118  Base::distribution()=boost::cauchy_distribution<>(median(),newSigma);
119  }
120  //! Returns the probability for the occurrence of random number "x".
121  //! \param x the point for which to calculate the propability
122  double p(double x)const {
123  return 1.0/(sigma()*M_PI*(1+shark::sqr((x-median())/sigma())));
124  }
125 
126 
127  };
128  //! Returns the entropy of the Cauchy distribution
129  template<typename RngType>
130  double entropy(const Cauchy<RngType> & distribution);
131 }
132 #endif