Last active
October 12, 2015 09:28
-
-
Save henry0312/4006248 to your computer and use it in GitHub Desktop.
Beta Distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* BetaDistribution.hpp | |
* | |
* Copyright (c) 2012 Tsukasa OMOTO <[email protected]> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
/* This file is available under an MIT license. */ | |
#ifndef BETA_DISTRIBUTION_H | |
#define BETA_DISTRIBUTION_H | |
#include <random> | |
template <class RealType = double> | |
class beta_distribution | |
{ | |
std::gamma_distribution<> g_alpha; | |
std::gamma_distribution<> g_beta; | |
public: | |
explicit beta_distribution(const RealType alpha, const RealType beta); | |
~beta_distribution() = default; | |
template <class Generator> double operator()(Generator& gen); | |
}; | |
/** | |
* Constructor | |
* | |
* @param const doulbe alpha is one of shape parameters, Beta(alpha, beta). | |
* @param const double beta is the other shape parameters, Beta(alpha, beta) | |
* @see http://en.wikipedia.org/wiki/Beta_distribution | |
*/ | |
template <class RealType> | |
beta_distribution<RealType>::beta_distribution(const RealType alpha, const RealType beta) | |
:g_alpha(alpha, 1), g_beta(beta, 1) | |
{ | |
} | |
/** | |
* Generates the next random number in the distribution | |
* | |
* @param Generator& gen an uniform random number generator object | |
* @see http://en.wikipedia.org/wiki/Gamma_distribution | |
* @see http://en.wikipedia.org/wiki/Gamma_distribution#Others | |
*/ | |
template <class RealType> | |
template <class Generator> | |
double beta_distribution<RealType>::operator()(Generator& gen) { | |
double x = g_alpha(gen); | |
if (x < 1.0e-7) { | |
// avoid underflow | |
x = g_alpha.alpha(); // Mean; | |
} | |
double y = g_beta(gen); | |
if (y < 1.0e-7) { | |
// avoid underflow | |
y = g_beta.alpha(); // Mean; | |
} | |
return x / (x + y); | |
} | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment