[shark] 50/58: added free functions to draw from the most used distributions using a free Rng

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Mar 16 10:05:33 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository shark.

commit 0d56b0fddab32abdb4037008a19cf92fa9f0bc3e
Author: Oswin Krause <oswin.krause at di.ku.dk>
Date:   Mon Feb 29 16:43:25 2016 +0100

    added free functions to draw from the most used distributions using a free Rng
---
 include/shark/Rng/Bernoulli.h       |  10 ++-
 include/shark/Rng/Binomial.h        |   8 ++-
 include/shark/Rng/DiscreteUniform.h | 128 +++++++++++++++++-------------------
 include/shark/Rng/GlobalRng.h       |   5 --
 include/shark/Rng/Normal.h          |   9 ++-
 include/shark/Rng/Uniform.h         |   7 ++
 6 files changed, 89 insertions(+), 78 deletions(-)

diff --git a/include/shark/Rng/Bernoulli.h b/include/shark/Rng/Bernoulli.h
index b0aceec..8e8a341 100644
--- a/include/shark/Rng/Bernoulli.h
+++ b/include/shark/Rng/Bernoulli.h
@@ -132,8 +132,12 @@ namespace shark{
 		}
 
 	};
-	//! Returns the entropy of the bernoulli distribution
-	template<typename RngType>
-	double entropy(const Bernoulli<RngType> & coin);
+	
+	///\brief Flips a coin with probability of heads being pHeads by drawing random numbers from rng.
+	template<class RngType>
+	bool coinToss(RngType& rng, double pHeads){
+		Bernoulli<RngType> dist(rng, pHeads);
+		return dist();
+	}
 }
 #endif
diff --git a/include/shark/Rng/Binomial.h b/include/shark/Rng/Binomial.h
index a633828..ccf87e8 100644
--- a/include/shark/Rng/Binomial.h
+++ b/include/shark/Rng/Binomial.h
@@ -118,8 +118,12 @@ namespace shark{
 
 	};
 
-	template<typename RngType>
-	inline double entropy(const Binomial<RngType> & coin);
+	///\brief Returns a random number from Binomial(n,p) by drawing random numbers from rng
+	template<class RngType>
+	bool coinToss(RngType& rng, unsigned int n, double pHeads){
+		Binomial<RngType> dist(rng, n, pHeads);
+		return dist();
+	}
 }
 #endif
 
diff --git a/include/shark/Rng/DiscreteUniform.h b/include/shark/Rng/DiscreteUniform.h
index d107988..37be695 100644
--- a/include/shark/Rng/DiscreteUniform.h
+++ b/include/shark/Rng/DiscreteUniform.h
@@ -41,81 +41,77 @@
 #include <limits>
 
 namespace shark{
-	/** 
-	* \brief Implements the discrete uniform distribution.
-	*/
-	template<typename RngType = shark::DefaultRngType>
-	class DiscreteUniform : public boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > {
-	private:
-		/** \brief The base type this class inherits from. */
-		typedef boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > Base;
-	public:
-		/**
-		* \brief C'tor, initializes the interval the random numbers are sampled from.
-		* \param [in] low_ The lower bound of the interval, defaults to 0.
-		* \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
-		*/
-		/*DiscreteUniform( std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
-			:Base(&Rng::globalRng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
-		{}*/
+/** 
+* \brief Implements the discrete uniform distribution.
+*/
+template<typename RngType = shark::DefaultRngType>
+class DiscreteUniform : public boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > {
+private:
+	/** \brief The base type this class inherits from. */
+	typedef boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > Base;
+public:
 
-		/**
-		* \brief C'tor, initializes the interval the random numbers are sampled from and associates the distribution
-		* with the supplied RNG.
-		* \param [in,out] rng The RNG this distribution is associated with.
-		* \param [in] low_ The lower bound of the interval, defaults to 0.
-		* \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
-		*/
-		DiscreteUniform(RngType & rng, std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
-			:Base(&rng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
-		{}
+	/**
+	* \brief C'tor, initializes the interval the random numbers are sampled from and associates the distribution
+	* with the supplied RNG.
+	* \param [in,out] rng The RNG this distribution is associated with.
+	* \param [in] low_ The lower bound of the interval, defaults to 0.
+	* \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
+	*/
+	DiscreteUniform(RngType & rng, std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
+		:Base(&rng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
+	{}
 
-		/**
-		* \brief Injects the default sampling operator.
-		*/
-		using Base::operator();
+	/**
+	* \brief Injects the default sampling operator.
+	*/
+	using Base::operator();
 
-		/**
-		* \brief Reinitializes the distribution for the supplied bounds and samples a new random number.
-		* Default values are omitted to distinguish the operator from the default one.
-		* 
-		* \param [in] low_ The lower bound of the interval.
-		* \param [in] high_ The upper bound of the interval.
-		*/
-		typename Base::result_type operator()(std::size_t low_,std::size_t high_)
-		{
-			boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
-			return dist(Base::engine());
-		}
+	/**
+	* \brief Reinitializes the distribution for the supplied bounds and samples a new random number.
+	* Default values are omitted to distinguish the operator from the default one.
+	* 
+	* \param [in] low_ The lower bound of the interval.
+	* \param [in] high_ The upper bound of the interval.
+	*/
+	typename Base::result_type operator()(std::size_t low_,std::size_t high_)
+	{
+		boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
+		return dist(Base::engine());
+	}
 
-		/** \brief Returns the lower bound of the interval. */
-		std::size_t low()const
-		{
-			return Base::distribution().min();
-		}
+	/** \brief Returns the lower bound of the interval. */
+	std::size_t low()const
+	{
+		return Base::distribution().min();
+	}
 
-		/** \brief Adjusts the upper bound of the interval */
-		std::size_t high()const
-		{
-			return Base::distribution().max();
-		}
+	/** \brief Adjusts the upper bound of the interval */
+	std::size_t high()const
+	{
+		return Base::distribution().max();
+	}
 
-		/** \brief Adjusts the range of the interval. */
-		void setRange(std::size_t low_,std::size_t high_)
-		{
-			boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
-			Base::distribution()=dist;
-		}
+	/** \brief Adjusts the range of the interval. */
+	void setRange(std::size_t low_,std::size_t high_)
+	{
+		boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
+		Base::distribution()=dist;
+	}
 
-		/** \brief Calculates the probability of x. */
-		double p( std::size_t x ) const {
-			return 1.0/(high()-low()+1);
-		}
+	/** \brief Calculates the probability of x. */
+	double p( std::size_t x ) const {
+		return 1.0/(high()-low()+1);
+	}
 
-	};
+};
 
-	template<typename RngType>
-	double entropy(const DiscreteUniform<RngType> & uniform);
+///\brief Draws a discrete number in {low,low+1,...,high} by drawing random numbers from rng.
+template<class RngType>
+std::size_t discrete(RngType& rng, std::size_t low, std::size_t high){
+	DiscreteUniform<RngType> dist(rng, low, high);
+	return dist();
+}
 
 }
 #endif
diff --git a/include/shark/Rng/GlobalRng.h b/include/shark/Rng/GlobalRng.h
index bc8788a..d2b8070 100644
--- a/include/shark/Rng/GlobalRng.h
+++ b/include/shark/Rng/GlobalRng.h
@@ -210,11 +210,6 @@ namespace shark {
 
 	ANNOUNCE_SHARK_RNG( shark::FastRngType,		FastRng );
 	ANNOUNCE_SHARK_RNG( shark::DefaultRngType,	Rng		);
-	/*
-	typedef BaseRng< boost::rand48 > FastRng; FastRng::rng_type FastRng::globalRng = FastRng::rng_type();
-		typedef BaseRng< boost::mt19937 > Rng; Rng::rng_type Rng::globalRng = Rng::rng_type();*/
-
-
 }
 
 #endif
diff --git a/include/shark/Rng/Normal.h b/include/shark/Rng/Normal.h
index 3304faf..8f0930a 100644
--- a/include/shark/Rng/Normal.h
+++ b/include/shark/Rng/Normal.h
@@ -107,8 +107,13 @@ public:
 	}
 };
 
-template<typename RngType>
-double entropy(const Normal< RngType > & normal);
+///\brief Draws a number from the normal distribution with given mean and variance by drawing random numbers from rng.
+template<class RngType>
+double gauss(RngType& rng, double mean, double variance){
+	Normal<RngType> dist(rng, mean, variance);
+	return dist();
+}
+
 
 } // namespace shark {
 
diff --git a/include/shark/Rng/Uniform.h b/include/shark/Rng/Uniform.h
index 66cf108..da66c0f 100644
--- a/include/shark/Rng/Uniform.h
+++ b/include/shark/Rng/Uniform.h
@@ -118,6 +118,13 @@ public:
 	}
 };
 
+///\brief Draws a number uniformly in [lower,upper] by drawing random numbers from rng.
+template<class RngType>
+double uni(RngType& rng, double lower, double upper){
+	Uniform<RngType> dist(rng, lower, upper);
+	return dist();
+}
+
 } // namespace shark {
 
 #endif // SHARK_RNG_UNIFORM_H

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/shark.git



More information about the debian-science-commits mailing list