STK++ 0.9.13
STK_Law_UniformDiscrete.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::STatistiK::Law
27 * created on: 23 janv. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_LAW_UNIFORMDISCRETE_H
36#define STK_LAW_UNIFORMDISCRETE_H
37
38#include "STK_Law_IUnivLaw.h"
39
40namespace STK
41{
42
43namespace Law
44{
58class UniformDiscrete: public IUnivLaw<int>
59{
60 public:
65 inline UniformDiscrete( int a, int b): Base(_T("UniformDiscrete")), a_(a), b_(b), n_(b_ - a_ + 1)
66 {
67 if (n_ < 0.)
69 }
73 inline UniformDiscrete( UniformDiscrete const& law)
74 : Base(law), a_(law.a_), b_(law.b_), n_(law.n_)
75 {}
77 inline virtual ~UniformDiscrete() {}
79 inline int const& a() const { return a_;}
81 inline int const& b() const { return b_;}
83 inline Real const& n() const { return n_;}
85 inline void setA(int a) { a_ =a; n_ = b_-a_+1;}
87 inline void setB(int b){ b_ =b; n_ = b_ - a_ + 1;}
88
90 virtual int rand() const;
94 virtual Real pdf( int const& x) const;
98 virtual Real lpdf( int const& x) const;
105 virtual Real cdf( Real const& t) const;
112 virtual int icdf( Real const& p) const;
113
117 static int rand( int a, int b);
122 static Real pdf( Real const& x, int a, int b);
127 static Real lpdf( Real const& p, int a, int b);
132 static Real cdf( Real const& t, int a, int b);
137 static int icdf( Real const& p, int a, int b);
138
139 protected:
141 int a_;
143 int b_;
144
145 private:
147};
148
149#ifdef IS_RTKPP_LIB
150
151inline int UniformDiscrete::rand() const
152{
153#ifdef _OPENMP
154//#pragma omp critical
155#endif
156GetRNGstate(); Real s = a_ + Rf_runif(0, double(n_)); PutRNGstate(); return s;
157}
158
159inline Real UniformDiscrete::pdf( int const& x) const
160{
161 if (!Arithmetic<Real>::isFinite(x) ) return x;
162 if ((x < a_)||(x > b_)) return 0.;
163 return 1./n_;
164}
165inline Real UniformDiscrete::lpdf( int const& x) const
166{
167 if (!Arithmetic<Real>::isFinite(x) ) return x;
168 if ((x < a_)||(x > b_)) return -Arithmetic<Real>::infinity();
169 return -std::log(n_);
170}
171inline Real UniformDiscrete::cdf( Real const& t) const
172{
173 if (!Arithmetic<Real>::isFinite(t) ) return t;
174 if (t <= a_) return 0.;
175 if (t >= b_) return 1.;
176 return (b_ - (int)t)/n_;
177}
178inline int UniformDiscrete::icdf( Real const& p) const
179{
180 // check parameter
181 if ((p > 1.) || (p < 0.))
182 STKDOMAIN_ERROR_1ARG(Exponential::icdf,p,invalid argument);
183
184 if (!Arithmetic<Real>::isFinite(p) ) return p;
185 if (p == 1.) return b_;
186 if (p == 0.) return a_;
187 return(int)((1.-p) * a_ + p * b_);
188}
189
190inline int UniformDiscrete::rand( int a, int b)
191{
192#ifdef _OPENMP
193//#pragma omp critical
194#endif
195 GetRNGstate(); Real s = a + Rf_runif(0, double(b - a + 1)); PutRNGstate(); return s;
196}
197
198inline Real UniformDiscrete::pdf( Real const& x, int a, int b)
199{
200 if (!Arithmetic<Real>::isFinite(x) ) return x;
201 if ((x < a)||(x > b)) return 0.;
202 return 1./Real(b-a+1);
203}
204inline Real UniformDiscrete::lpdf( Real const& x, int a, int b)
205{
206 if (!Arithmetic<Real>::isFinite(x) ) return x;
207 if ((x < a)||(x > b)) return -Arithmetic<Real>::infinity();
208 return -std::log(b-a+1);
209}
210inline Real UniformDiscrete::cdf(const Real& t, int a, int b)
211{ return (b - t)/(b-a+1);}
212inline int UniformDiscrete::icdf(const Real& p, int a, int b)
213{ return (int)((1.-p) * a + p * b);}
214
215#endif
216
217} // namespace Law
218
219} // namespace STK
220
221#endif /*STK_LAW_UNIFORMDISCRETE_H*/
In this file we define the interface base class IUnivLaw for all probabilities laws.
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
#define STKINVALIDARGUMENT_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:183
#define _T(x)
Let x unmodified.
virtual Real icdf(Real const &p) const
The inverse cumulative distribution function is.
Interface base class for all the univariate distributions.
class for the Uniform law distribution.
UniformDiscrete(int a, int b)
constructor.
virtual Real lpdf(int const &x) const
Give the value of the log-pdf at x.
virtual ~UniformDiscrete()
destructor.
virtual Real pdf(int const &x) const
Give the value of the 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.
UniformDiscrete(UniformDiscrete const &law)
copy constructor.
virtual int rand() const
Generate a pseudo Uniform random variate.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static bool isFinite(Type const &x)