STK++ 0.9.13
STK::Law::UniformDiscrete Class Reference

class for the Uniform law distribution. More...

#include <STK_Law_UniformDiscrete.h>

Inheritance diagram for STK::Law::UniformDiscrete:
Inheritance graph

Public Types

typedef IUnivLaw< intBase
 

Public Member Functions

 UniformDiscrete (int a, int b)
 constructor.
 
 UniformDiscrete (UniformDiscrete const &law)
 copy constructor.
 
virtual ~UniformDiscrete ()
 destructor.
 
int consta () const
 
int constb () const
 
Real constn () const
 
void setA (int a)
 
void setB (int b)
 
virtual int rand () const
 Generate a pseudo Uniform random variate.
 
virtual Real pdf (int const &x) const
 Give the value of the pdf at x.
 
virtual Real lpdf (int const &x) const
 Give the value of the log-pdf at x.
 
virtual Real cdf (Real const &t) const
 The cumulative distribution function is.
 
virtual int icdf (Real const &p) const
 The inverse cumulative distribution function is.
 
- Public Member Functions inherited from STK::Law::IUnivLaw< int >
virtual ~IUnivLaw ()
 Virtual destructor.
 
virtual Real lcdf (Real const &t) const
 compute the lower tail log-cumulative distribution function Give the log-probability that a random variate is less or equal to t.
 
virtual Real cdfc (Real const &t) const
 calculate the complement of cumulative distribution function, called in statistics the survival function.
 
virtual Real lcdfc (Real const &t) const
 calculate the log-complement of cumulative distribution function Give the log-probability that a random variate is greater than t.
 
- Public Member Functions inherited from STK::Law::ILawBase
String constname () const
 

Static Public Member Functions

static int rand (int a, int b)
 Generate a pseudo Uniform random variate.
 
static Real pdf (Real const &x, int a, int b)
 Give the value of the pdf at x.
 
static Real lpdf (Real const &p, int a, int b)
 Give the value of the log-pdf at x.
 
static Real cdf (Real const &t, int a, int b)
 Give the value of the cdf at t.
 
static int icdf (Real const &p, int a, int b)
 Give the value of the quantile at p.
 

Protected Attributes

int a_
 The lower bound.
 
int b_
 The upper bound.
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Private Attributes

Real n_
 

Additional Inherited Members

- Protected Member Functions inherited from STK::Law::IUnivLaw< int >
 IUnivLaw (String const &name)
 Constructor.
 
 IUnivLaw (IUnivLaw const &law)
 copy Constructor.
 
- Protected Member Functions inherited from STK::Law::ILawBase
 ILawBase (String const &name)
 Constructor.
 
 ~ILawBase ()
 destructor.
 

Detailed Description

class for the Uniform law distribution.

In probability theory and statistics, the discrete uniform distribution is a probability distribution whereby a finite number of values are equally likely to be observed; every one of n values has equal probability 1/n. Another way of saying "discrete uniform distribution" would be "a known, finite number of outcomes equally likely to happen".

The probability density function of the discrete uniform distribution is:

\[
  f(x; a, b) = \frac{1}{b-a+1} 1_{ a \leq x \leq b}, \quad a,b,x\in\mathbb{N}.
\]

Definition at line 58 of file STK_Law_UniformDiscrete.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ UniformDiscrete() [1/2]

STK::Law::UniformDiscrete::UniformDiscrete ( int  a,
int  b 
)
inline

constructor.

Parameters
a,bthe lower and upper bounds

Definition at line 65 of file STK_Law_UniformDiscrete.h.

65 : Base(_T("UniformDiscrete")), a_(a), b_(b), n_(b_ - a_ + 1)
66 {
67 if (n_ < 0.)
69 }
#define STKINVALIDARGUMENT_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:183
#define _T(x)
Let x unmodified.
UniformDiscrete(int a, int b)
constructor.

References a_, b_, n_, STKINVALIDARGUMENT_ERROR_2ARG, and UniformDiscrete().

Referenced by UniformDiscrete().

◆ UniformDiscrete() [2/2]

STK::Law::UniformDiscrete::UniformDiscrete ( UniformDiscrete const law)
inline

copy constructor.

Parameters
lawthe law to copy

Definition at line 73 of file STK_Law_UniformDiscrete.h.

74 : Base(law), a_(law.a_), b_(law.b_), n_(law.n_)
75 {}

◆ ~UniformDiscrete()

virtual STK::Law::UniformDiscrete::~UniformDiscrete ( )
inlinevirtual

destructor.

Definition at line 77 of file STK_Law_UniformDiscrete.h.

77{}

Member Function Documentation

◆ a()

int const & STK::Law::UniformDiscrete::a ( ) const
inline
Returns
the lower bound

Definition at line 79 of file STK_Law_UniformDiscrete.h.

79{ return a_;}

References a_.

Referenced by cdf(), icdf(), lpdf(), pdf(), rand(), and setA().

◆ b()

int const & STK::Law::UniformDiscrete::b ( ) const
inline
Returns
the upper bound

Definition at line 81 of file STK_Law_UniformDiscrete.h.

81{ return b_;}

References b_.

Referenced by cdf(), icdf(), lpdf(), pdf(), rand(), and setB().

◆ cdf() [1/2]

Real STK::Law::UniformDiscrete::cdf ( Real const t) const
virtual

The cumulative distribution function is.

\[
 F(t; a,b)= \frac{t - a}{b-a}
\]

Parameters
ta real value

Implements STK::Law::IUnivLaw< int >.

Definition at line 75 of file STK_Law_UniformDiscrete.cpp.

76{
77 if (!Arithmetic<Real>::isFinite(t) ) return t;
78 if (t <= a_) return 0.;
79 if (t >= b_) return 1.;
80 return (b_ - (int)t)/n_;
81}
static bool isFinite(Type const &x)

References a_, b_, and n_.

◆ cdf() [2/2]

Real STK::Law::UniformDiscrete::cdf ( Real const t,
int  a,
int  b 
)
static

Give the value of the cdf at t.

Parameters
ta real value
a,bthe lower and upper bounds

Definition at line 130 of file STK_Law_UniformDiscrete.cpp.

131{ return (b - t)/(b-a+1);}

References a(), and b().

◆ icdf() [1/2]

int STK::Law::UniformDiscrete::icdf ( Real const p) const
virtual

The inverse cumulative distribution function is.

\[
F^{-1}(p; \lambda) = p (b-a) + a.
\]

Parameters
pa probability

Implements STK::Law::IUnivLaw< int >.

Definition at line 89 of file STK_Law_UniformDiscrete.cpp.

90{
91 // check parameter
92 if ((p > 1.) || (p < 0.))
93 STKDOMAIN_ERROR_1ARG(Exponential::icdf,p,invalid argument);
94
95 if (!Arithmetic<Real>::isFinite(p) ) return p;
96 if (p == 1.) return b_;
97 if (p == 0.) return a_;
98 return(int)((1.-p) * a_ + p * b_);
99}
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
virtual Real icdf(Real const &p) const
The inverse cumulative distribution function is.

References a_, b_, STK::Law::Exponential::icdf(), and STKDOMAIN_ERROR_1ARG.

◆ icdf() [2/2]

int STK::Law::UniformDiscrete::icdf ( Real const p,
int  a,
int  b 
)
static

Give the value of the quantile at p.

Parameters
pa probability
a,bthe lower and upper bounds

Definition at line 134 of file STK_Law_UniformDiscrete.cpp.

135{ return (int)((1.-p) * a + p * b);}

References a(), and b().

◆ lpdf() [1/2]

Real STK::Law::UniformDiscrete::lpdf ( int const x) const
virtual

Give the value of the log-pdf at x.

Parameters
xa real value

Reimplemented from STK::Law::IUnivLaw< int >.

Definition at line 63 of file STK_Law_UniformDiscrete.cpp.

64{
65 if (!Arithmetic<Real>::isFinite(x) ) return x;
66 if ((x < a_)||(x > b_)) return -Arithmetic<Real>::infinity();
67 return -std::log(n_);
68}

References a_, b_, and n_.

◆ lpdf() [2/2]

Real STK::Law::UniformDiscrete::lpdf ( Real const p,
int  a,
int  b 
)
static

Give the value of the log-pdf at x.

Parameters
pa probablility
a,bthe lower and upper bounds

Definition at line 123 of file STK_Law_UniformDiscrete.cpp.

124{
125 if (!Arithmetic<Real>::isFinite(x) ) return x;
126 if ((x < a)||(x > b)) return -Arithmetic<Real>::infinity();
127 return -std::log(b-a+1);
128}

References a(), and b().

◆ n()

Real const & STK::Law::UniformDiscrete::n ( ) const
inline
Returns
the value b-a+1

Definition at line 83 of file STK_Law_UniformDiscrete.h.

83{ return n_;}

References n_.

◆ pdf() [1/2]

Real STK::Law::UniformDiscrete::pdf ( int const x) const
virtual

Give the value of the pdf at x.

Parameters
xa real value

Implements STK::Law::IUnivLaw< int >.

Definition at line 54 of file STK_Law_UniformDiscrete.cpp.

55{
56 if (!Arithmetic<Real>::isFinite(x) ) return x;
57 if ((x < a_)||(x > b_)) return 0.;
58 return 1./n_;
59}

References a_, b_, and n_.

◆ pdf() [2/2]

Real STK::Law::UniformDiscrete::pdf ( Real const x,
int  a,
int  b 
)
static

Give the value of the pdf at x.

Parameters
xa real value
a,bthe lower and upper bounds

Definition at line 112 of file STK_Law_UniformDiscrete.cpp.

113{
114 if (!Arithmetic<Real>::isFinite(x) ) return x;
115 if ((x < a)||(x > b)) return 0.;
116 return 1./Real(b-a+1);
117}
double Real
STK fundamental type of Real values.

References a(), and b().

◆ rand() [1/2]

int STK::Law::UniformDiscrete::rand ( ) const
virtual

◆ rand() [2/2]

int STK::Law::UniformDiscrete::rand ( int  a,
int  b 
)
static

Generate a pseudo Uniform random variate.

Parameters
a,bthe lower and upper bounds

Definition at line 105 of file STK_Law_UniformDiscrete.cpp.

106{ return a + int(generator.rand(double(b - a +1)));}

References a(), and b().

◆ setA()

void STK::Law::UniformDiscrete::setA ( int  a)
inline
Parameters
aset the lower bound

Definition at line 85 of file STK_Law_UniformDiscrete.h.

85{ a_ =a; n_ = b_-a_+1;}

References a(), a_, b_, and n_.

◆ setB()

void STK::Law::UniformDiscrete::setB ( int  b)
inline
Parameters
bset the upper bound

Definition at line 87 of file STK_Law_UniformDiscrete.h.

87{ b_ =b; n_ = b_ - a_ + 1;}

References a_, b(), b_, and n_.

Member Data Documentation

◆ a_

int STK::Law::UniformDiscrete::a_
protected

The lower bound.

Definition at line 141 of file STK_Law_UniformDiscrete.h.

Referenced by a(), cdf(), icdf(), lpdf(), pdf(), rand(), setA(), setB(), and UniformDiscrete().

◆ b_

int STK::Law::UniformDiscrete::b_
protected

The upper bound.

Definition at line 143 of file STK_Law_UniformDiscrete.h.

Referenced by b(), cdf(), icdf(), lpdf(), pdf(), setA(), setB(), and UniformDiscrete().

◆ n_

Real STK::Law::UniformDiscrete::n_
private

Definition at line 146 of file STK_Law_UniformDiscrete.h.

Referenced by cdf(), lpdf(), n(), pdf(), rand(), setA(), setB(), and UniformDiscrete().


The documentation for this class was generated from the following files: