STK++ 0.9.13
STK_Law_UniformDiscrete.cpp
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
36#ifndef IS_RTKPP_LIB
37#include "../include/STK_Law_UniformDiscrete.h"
38#endif
39
40//
41namespace STK
42{
43namespace Law
44{
45
46#ifndef IS_RTKPP_LIB
47
48/* Generate a pseudo UniformDiscrete random variate. */
50{ return a_ + int(generator.rand(double(n_)));}
51/* Give the value of the pdf at x.
52 * @param x a real value
53 **/
54Real UniformDiscrete::pdf( int const& x) const
55{
56 if (!Arithmetic<Real>::isFinite(x) ) return x;
57 if ((x < a_)||(x > b_)) return 0.;
58 return 1./n_;
59}
60/* Give the value of the log-pdf at x.
61 * @param x a real value
62 **/
63Real UniformDiscrete::lpdf( int const& x) const
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}
69/* The cumulative distribution function is
70 * \f[
71 * F(t; a,b)= \frac{t - a}{b-a+1}
72 * \f]
73 * @param t a real value
74 **/
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}
82
83/* The inverse cumulative distribution function is
84 * \f[
85 * F^{-1}(p; \lambda) = p (b-a) + a.
86 * \f]
87 * @param p a probability
88 **/
89int UniformDiscrete::icdf( Real const& p) const
90{
91 // check parameter
92 if ((p > 1.) || (p < 0.))
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}
100
101/* Generate a pseudo UniformDiscrete random variate with the specified
102 * parameter.
103 * @param scale the scale of the distribution
104 **/
105int UniformDiscrete::rand( int a, int b)
106{ return a + int(generator.rand(double(b - a +1)));}
107
108/* Give the value of the pdf at x.
109 * @param x a real value
110 * @param scale the scale of the distribution
111 **/
112Real UniformDiscrete::pdf( Real const& x, int a, int b)
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}
118
119/* Give the value of the log-pdf at x.
120 * @param x a real value
121 * @param scale the scale of the distribution
122 **/
123Real UniformDiscrete::lpdf( Real const& x, int a, int b)
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}
129
130Real UniformDiscrete::cdf(const Real& t, int a, int b)
131{ return (b - t)/(b-a+1);}
132
133
134int UniformDiscrete::icdf(const Real& p, int a, int b)
135{ return (int)((1.-p) * a + p * b);}
136
137
138#endif /* !IS_RTKPP_LIB */
139
140} // namespace Law
141
142} // namespace STK
143
#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.
virtual Real lpdf(int const &x) const
Give the value of the log-pdf at x.
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.
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.