Skip to content

Commit

Permalink
Merge last branch
Browse files Browse the repository at this point in the history
  • Loading branch information
pfernique committed Mar 1, 2019
2 parents 6767db3 + d2ba4a3 commit d84b8ce
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 187 deletions.
116 changes: 107 additions & 9 deletions src/cpp/distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2357,12 +2357,12 @@ namespace statiskit

double BetaDistribution::ldf(const double& value) const
{
double p;
double l;
if(value < 0. || value > 1.)
{ p = 0.; }
{ l = -1 * std::numeric_limits< double >::infinity(); }
else
{ p = boost::math::lgamma(_alpha + _beta) - boost::math::lgamma(_alpha) - boost::math::lgamma(_beta) + (_alpha - 1) * log(value) + (_beta - 1) * log(1 - value); }
return p;
{ l = boost::math::lgamma(_alpha + _beta) - boost::math::lgamma(_alpha) - boost::math::lgamma(_beta) + (_alpha - 1) * log(value) + (_beta - 1) * log(1 - value); }
return l;
}

double BetaDistribution::pdf(const double& value) const
Expand All @@ -2377,12 +2377,17 @@ namespace statiskit

double BetaDistribution::cdf(const double& value) const
{
double p;
if(value < 0. || value > 1.)
{ p = 0.; }
double c;
if(value < 0.)
{ c = 0.; }
else
{ p = boost::math::ibeta(_alpha, _beta, value); }
return p;
{
if(value > 1.)
{ c = 1.; }
else
{ c = boost::math::ibeta(_alpha, _beta, value); }
}
return c;
}

double BetaDistribution::quantile(const double& p) const
Expand All @@ -2401,6 +2406,99 @@ namespace statiskit
double BetaDistribution::get_variance() const
{ return _alpha * _beta / (pow(_alpha + _beta, 2) * (_alpha + _beta + 1)); }


UniformDistribution::UniformDistribution()
{
_alpha = 0.;
_beta = 1.;
}

UniformDistribution::UniformDistribution(const double& alpha, const double& beta)
{
set_alpha(alpha);
set_beta(beta);
}

UniformDistribution::UniformDistribution(const UniformDistribution& uniform)
{
_alpha = uniform._alpha;
_beta = uniform._beta;
}

UniformDistribution::~UniformDistribution()
{}

unsigned int UniformDistribution::get_nb_parameters() const
{ return 2; }

const double& UniformDistribution::get_alpha() const
{ return _alpha; }

void UniformDistribution::set_alpha(const double& alpha)
{_alpha = alpha; }

const double& UniformDistribution::get_beta() const
{ return _beta; }

void UniformDistribution::set_beta(const double& beta)
{
if(beta <= _alpha)
{ throw lower_bound_error("beta", beta, _alpha, true); }
_beta = beta;
}

double UniformDistribution::ldf(const double& value) const
{
double l;
if(value < _alpha || value > _beta)
{ l = -1 * std::numeric_limits< double >::infinity(); }
else
{ l = -log(_beta - _alpha); }
return l;
}

double UniformDistribution::pdf(const double& value) const
{
double p;
if(value < _alpha || value > _beta)
{ p = 0.; }
else
{ p = 1/(_beta - _alpha); }
return p;
}

double UniformDistribution::cdf(const double& value) const
{
double c;
if(value <= _alpha)
{ c = 0.; }
else
{
if (value >= _beta)
{ c = 1.; }
else
{ c = (value - _alpha)/(_beta - _alpha); }
}
return c;
}

double UniformDistribution::quantile(const double& p) const
{ return _alpha + p * (_beta - _alpha); }

std::unique_ptr< UnivariateEvent > UniformDistribution::simulate() const
{
boost::uniform_01<> dist;
boost::variate_generator<boost::mt19937&, boost::uniform_01<> > simulator(__impl::get_random_generator(), dist);
return std::make_unique< ContinuousElementaryEvent >(_alpha + (_beta - _alpha) * simulator());
}

double UniformDistribution::get_mean() const
{ return 0.5 * (_alpha + _beta); }

double UniformDistribution::get_variance() const
{ return pow(_beta - _alpha, 2)/12.; }


double UnivariateConditionalDistribution::loglikelihood(const UnivariateConditionalData& data) const
{
double llh = 0.;
Expand Down
129 changes: 118 additions & 11 deletions src/cpp/distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -1980,9 +1980,9 @@ namespace statiskit
};


/** \brief This class represents a Gamma distribution.
/** \brief This class represents a beta distribution.
*
* \details The Gamma distribution is an univariate continuous distribution.
* \details The beta distribution is an univariate continuous distribution.
* The support is the set of positive real values \f$\mathbb{R}_+^*\f$.
* */
class STATISKIT_CORE_API BetaDistribution : public PolymorphicCopy< UnivariateDistribution, BetaDistribution, ContinuousUnivariateDistribution >
Expand All @@ -1999,10 +1999,10 @@ namespace statiskit

/** \brief An alternative constructor
*
* \details This constructor is usefull for Gamma distribution instantiation with specified \f$\alpha\f$ and \f$\beta\f$ parameters.
* \details This constructor is usefull for beta distribution instantiation with specified \f$\alpha\f$ and \f$\beta\f$ parameters.
*
* \param alpha the specified shape parameter \f$ \alpha \in \mathbb{R}_+^* \f$.
* \param beta the specified rate parameter \f$ \beta \in \mathbb{R}_+^* \f$.
* \param beta the specified shape parameter \f$ \beta \in \mathbb{R}_+^* \f$.
* */
BetaDistribution(const double& alpha, const double& beta);

Expand All @@ -2012,10 +2012,10 @@ namespace statiskit
/// \brief A destructor
virtual ~BetaDistribution();

/** \brief Returns the number of parameters of the Gamma distribution.
/** \brief Returns the number of parameters of the beta distribution.
*
* \details In the general case the number of parameters of a Gamma distribution is \f$2\f$ (\f$alpha\f$ and \f$beta\f$).
* When \f$\alpha=1.\f$, the Gamma distribution is equivalent to the exponential distribution.
* \details In the general case the number of parameters of a beta distribution is \f$2\f$ (\f$alpha\f$ and \f$beta\f$).
* When \f$\alpha=1.\f$, the beta distribution is equivalent to the exponential distribution.
* Therefore, in this case the number of parameters is \f$1\f$.
* */
virtual unsigned int get_nb_parameters() const;
Expand All @@ -2026,10 +2026,10 @@ namespace statiskit
/// \brief Set the value of the shape parameter \f$\alpha\f$.
void set_alpha(const double& mu);

/// \brief Get the value of the rate parameter \f$\beta\f$.
/// \brief Get the value of the shape parameter \f$\beta\f$.
const double& get_beta() const;

/// \brief Set the value of the rate parameter \f$\beta\f$.
/// \brief Set the value of the shape parameter \f$\beta\f$.
void set_beta(const double& sigma);

/** \brief \copybrief statiskit::ContinuousUnivariateDistribution::ldf()
Expand Down Expand Up @@ -2073,17 +2073,124 @@ namespace statiskit

virtual std::unique_ptr< UnivariateEvent > simulate() const;

/// \brief Get mean of the Gamma distribution \f$ E(X) = \frac{\alpha}{\beta}\f$
/// \brief Get mean of the beta distribution \f$ E(X) = \frac{\alpha}{\beta}\f$
virtual double get_mean() const;

/// \brief Get variance of the Gamma distribution \f$ V(X) = \frac{\alpha}{\beta^2} \f$.
/// \brief Get variance of the beta distribution \f$ V(X) = \frac{\alpha}{\beta^2} \f$.
virtual double get_variance() const;

protected:
double _alpha;
double _beta;
};




/** \brief This class represents a uniform distribution.
*
* \details The uniform distribution is an univariate continuous distribution.
* The support is the interval \f$[\alpha,\beta]\f$ where \f$\alpha\f$ and \f$\beta\f$ are two real values such that \f$\alpha<\beta\f$.
* */
class STATISKIT_CORE_API UniformDistribution : public PolymorphicCopy< UnivariateDistribution, UniformDistribution, ContinuousUnivariateDistribution >
{
public:
/** \brief The default constructor
*
* \details The default constructor instantiate a uniform distribution with
*
* - \f$ \alpha = 0.\f$,
* - \f$ \beta = 1.\f$.
* */
UniformDistribution();

/** \brief An alternative constructor
*
* \details This constructor is usefull for uniform distribution instantiation with specified \f$\alpha\f$ and \f$\beta\f$ parameters.
*
* \param alpha the specified support parameter \f$ \alpha \in \mathbb{R} \f$.
* \param beta the specified support parameter \f$ \beta \in \mathbb{R} \f$ such that \f$\alpha<\beta\f$.
* */
UniformDistribution(const double& alpha, const double& beta);

/// \brief A copy constructor
UniformDistribution(const UniformDistribution& uniform);

/// \brief A destructor
virtual ~UniformDistribution();

/** \brief Returns the number of parameters of the uniform distribution.
*
* \details The number of parameters is \f$2\f$.
* */
virtual unsigned int get_nb_parameters() const;

/// \brief Get the value of the support parameter \f$\alpha\f$.
const double& get_alpha() const;

/// \brief Set the value of the support parameter \f$\alpha\f$.
void set_alpha(const double& mu);

/// \brief Get the value of the support parameter \f$\beta\f$.
const double& get_beta() const;

/// \brief Set the value of the support parameter \f$\beta\f$.
void set_beta(const double& sigma);

/** \brief \copybrief statiskit::ContinuousUnivariateDistribution::ldf()
*
* \details Let \f$x \in \mathbb{R} \f$ denote the value,
* \f[
* \ln f(x) = \ln \{ \boldsymbol{1}_{[\alpha,\beta]}(x) \} - \ln (\beta-\alpha).
* \f]
* \param value The considered value \f$x\f$.
* */
virtual double ldf(const double& value) const;

/** \brief \copybrief statiskit::ContinuousUnivariateDistribution::pdf()
*
* \details Let \f$x \in \mathbb{R} \f$ denote the value,
* \f[
* f(x) = \frac{1}{\beta-\alpha} \boldsymbol{1}_{[\alpha,\beta]}(x).
* \f]
* \param value The considered value \f$x\f$.
* */
virtual double pdf(const double& value) const;

/** \brief \copybrief statiskit::ContinuousUnivariateDistribution::cdf()
*
* \details Let \f$x \in \mathbb{R} \f$ denote the value,
* \f[
* P(X \leq x) = \frac{x-\alpha}{\beta-\alpha} \boldsymbol{1}_{(\alpha,\beta)}(x) + \boldsymbol{1}_{[\beta,+\infty)}(x).
* \f]
* \param value The considered value \f$x\f$.
* */
virtual double cdf(const double& value) const;

/** \brief \copybrief statiskit::ContinuousUnivariateDistribution::quantile()
*
* \details Let \f$x \in \mathbb{R}^{+}_{*} \f$ denote the value to compute and $p \in \left(0,1\right)$ denote a given probability,
* \f[
* x = \alpha + p (\beta-\alpha)
* \f]
* */
virtual double quantile(const double& p) const;

virtual std::unique_ptr< UnivariateEvent > simulate() const;

/// \brief Get mean of the beta distribution \f$ E(X) = \frac{1}{2} (\alpha+\beta)\f$
virtual double get_mean() const;

/// \brief Get variance of the beta distribution \f$ V(X) = \frac{1}{12} (\alpha-\beta)^2 \f$.
virtual double get_variance() const;

protected:
double _alpha;
double _beta;
};



/** \Brief This class UnivariateConditionalDistribution represents the conditional distribution \f$ Y \vert \boldsymbol{X} \f$ of an univariate random component \f$ Y\f$ given a multivariate component \f$ \boldsymbol{X} \f$.
*
*/
Expand Down
Loading

0 comments on commit d84b8ce

Please sign in to comment.