/usr/include/shark/Rng/Erlang.h is in libshark-dev 3.0.1+ds1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /*!
*
*
* \brief Implements an Erlang distribution.
*
*
*
* \author O. Krause
* \date 2010-01-01
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef SHARK_RNG_ERLANG_H
#define SHARK_RNG_ERLANG_H
#include <shark/Rng/Gamma.h>
#include <boost/random/uniform_01.hpp>
#include <cmath>
#include <vector>
#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
#include <iostream>
#endif
namespace shark{
/// \brief Implements an Erlang distribution.
template<class RealType=double>
class Erlang_distribution
{
public:
typedef RealType input_type;
typedef RealType result_type;
explicit Erlang_distribution(RealType mean=0,RealType variance=1)
:mean_(mean),variance_(variance)
{
k = static_cast<std::size_t>(std::ceil(mean * mean / variance));
if (k == 0)
k = 1;
if (mean > 0)
a = k / mean;
else
a = 0.5;
}
RealType mean() const
{
return mean_;
}
RealType variance() const
{
return variance_;
}
void reset() { }
template<class Engine>
result_type operator()(Engine& eng)
{
double prod = 1;
if (k == 0 || a <= 0) return 0.;
//double maxEng = eng.max();
//double minEng = eng.min();
unsigned int kPrime = k;
while(kPrime--){
double uni = boost::uniform_01<RealType>()(eng);
prod *= uni;
}
return -std::log(prod) / a;
}
#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
template<class CharT, class Traits>
friend std::basic_ostream<CharT,Traits>&
operator<<(std::basic_ostream<CharT,Traits>& os, const Erlang_distribution& d)
{
os << d.alphas.size();
for(int i=0;i!=d.alphas_.size();++i)
os << d.alphas_[i];
return os;
}
template<class CharT, class Traits>
friend std::basic_istream<CharT,Traits>&
operator>>(std::basic_istream<CharT,Traits>& is, Erlang_distribution& d)
{
size_t size;
is >> size;
for(int i=0;i!=size;++i)
{
double element;
is >> element;
d.alphas_.push_back(element);
}
return is;
}
#endif
private:
double mean_;
double variance_;
std::size_t k;
RealType a;
};
/**
* \brief Erlang distributed random variable.
*/
template<typename RngType = shark::DefaultRngType>
class Erlang:public boost::variate_generator<RngType*,Erlang_distribution<> >
{
private:
/** \brief The base type this class inherits from. */
typedef boost::variate_generator<RngType*,Erlang_distribution<> > Base;
public:
/**
* \brief Default c'tor, associates the distribution with the supplied RNG.
* \param [in,out] rng The RNG to associate the distribution with.
* \param [in] mean The parameter mean, default value 0.
* \param [in] variance The parameter variance, default value 1.
*/
explicit Erlang( RngType & rng, double mean=0,double variance=1 )
:Base(&rng,Erlang_distribution<>(mean,variance))
{}
/**
* \brief Injects the default sampling operator.
*/
using Base::operator();
/**
* \brief Reinitializes the distribution for the supplied parameters and samples a new random number.
* Default values are omitted to distinguish the operator from the default one.
*
* \param [in] mean The new mean.
* \param [in] variance The new variance.
*/
double operator()(double mean,double variance)
{
Erlang_distribution<> dist(mean,variance);
return dist(Base::engine());
}
/**
* \brief Accesses the mean of the distribution.
*/
double mean()const
{
return Base::distribution().mean();
}
/**
* \brief Accesses the variance of the distribution.
*/
double variance()const
{
return Base::distribution().variance();
}
/**
* \brief Adjusts the mean of the distribution.
*/
void mean(double newMean)
{
Base::distribution()=Erlang_distribution<>(newMean,Base::distribution().variance());
}
/**
* \brief Adjusts the variance of the distribution.
*/
void variance(double newVariance)
{
Base::distribution()=Erlang_distribution<>(mean(),newVariance);
}
/**
* \brief Calculates the probability of x.
*/
double p(double&x)const
{
double k = mean() * mean() / variance() + 0.5;
if (k == 0)
k = 1;
double a=0;
if (mean() > 0)
a = k / mean();
else
a = 0.5;
if (k == 0 || a <= 0 || x < 0) return 0.;
return std::pow(k * a, k) * std::pow(x, k - 1) * std::exp(- k * a * x);
}
};
}
#endif
|