DiffGeometric.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Diff geometric 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_DIFFGEOMETRIC_H
33 #define SHARK_RNG_DIFFGEOMETRIC_H
34 
35 #include <shark/Rng/Rng.h>
36 
37 #include <boost/random.hpp>
38 #include <boost/random/geometric_distribution.hpp>
39 
40 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
41 #include <iostream>
42 #endif
43 
44 
45 #include <cmath>
46 
47 namespace shark{
48 
49 /**
50 * \brief Implements a diff geometric distribution.
51 */
52 template<class IntType = int, class RealType = double>
54 {
55  public:
56  typedef RealType input_type;
57  typedef IntType result_type;
58 
59  explicit DiffGeometric_distribution(const RealType& p = RealType(0.5))
60  :geom(p) {}
61 
62  RealType p() const
63  {
64  return geom.p();
65  }
66  void reset() { }
67 
68  template<class Engine>
69  result_type operator()(Engine& eng)
70  {
71  return geom(eng)-geom(eng);
72  }
73 
74 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
75  template<class CharT, class Traits>
76  friend std::basic_ostream<CharT,Traits>&
77  operator<<(std::basic_ostream<CharT,Traits>& os, const DiffGeometric_distribution& gd)
78  {
79  os << gd.geom;
80  return os;
81  }
82 
83  template<class CharT, class Traits>
84  friend std::basic_istream<CharT,Traits>&
85  operator>>(std::basic_istream<CharT,Traits>& is, DiffGeometric_distribution& gd)
86  {
87  is >> gd.geom;
88  return is;
89  }
90 #endif
91  private:
92  boost::geometric_distribution<IntType,RealType> geom;
93 };
94 
95 /**
96 * \brief Random variable with diff geometric distribution.
97 */
98 template<typename RngType = shark::DefaultRngType>
99 class DiffGeometric: public boost::variate_generator<RngType*,DiffGeometric_distribution<> >
100 {
101  private:
102  typedef boost::variate_generator<RngType*,DiffGeometric_distribution<> > Base;
103  public:
104 
105  DiffGeometric( RngType & rng, double mean = 0.5 )
106  :Base(&rng,DiffGeometric_distribution<>(1.0-mean))
107  {}
108 
109  using Base::operator();
110 
111  double operator()(double mean)
112  {
113  DiffGeometric_distribution<> dist(mean);
114  return dist(Base::engine());
115  }
116 
117  double mean()const
118  {
119  return 1-Base::distribution().p();
120  }
121  void mean(double newMean)
122  {
123  Base::distribution()=DiffGeometric_distribution<>(1-newMean);
124  }
125 
126  double p(double x)const
127  {
128  return( mean() * std::pow( 1 - mean(), std::abs(x) ) / ( 2 - mean() ) );
129  }
130 
131 };
132 
133 }
134 #endif