STK++ 0.9.13
STK_Law_Uniform.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_Uniform.h"
38#endif
39
40//
41namespace STK
42{
43namespace Law
44{
45
46#ifndef IS_RTKPP_LIB
47
48/* Generate a pseudo Uniform random variate. */
50{
51 return ((range_ <= 1.) || (a_ <=1.)) ? a_ + range_ * generator.randUnif()
52 : a_ * (1. + generator.randUnif()*range_/a_) ;
53}
54/* Give the value of the pdf at x.
55 * @param x a real value
56 **/
57Real Uniform::pdf( Real const& x) const
58{
59 if (!Arithmetic<Real>::isFinite(x) ) return x;
60 if ((x < a_)||(x > b_)) return 0.;
61 return 1./range_;
62}
63/* Give the value of the log-pdf at x.
64 * @param x a real value
65 **/
66Real Uniform::lpdf( Real const& x) const
67{
68 if (!Arithmetic<Real>::isFinite(x) ) return x;
69 if ((x < a_)||(x > b_)) return -Arithmetic<Real>::infinity();
70 return -std::log(range_);
71}
72/* The cumulative distribution function is
73 * \f[
74 * F(t; a,b)= \frac{t - a}{b-a}
75 * \f]
76 * @param t a real value
77 **/
78Real Uniform::cdf( Real const& t) const
79{
80 if (!Arithmetic<Real>::isFinite(t) ) return t;
81 if (t <= a_) return 0.;
82 if (t >= b_) return 1.;
83 return (b_ - t)/range_;
84}
85
86/* The inverse cumulative distribution function is
87 * \f[
88 * F^{-1}(p; \lambda) = p (b-a) + a.
89 * \f]
90 * @param p a probability
91 **/
92Real Uniform::icdf( Real const& p) const
93{
94 // check parameter
95 if ((p > 1.) || (p < 0.))
97
98 if (!Arithmetic<Real>::isFinite(p) ) return p;
99 if (p == 1.) return b_;
100 if (p == 0.) return a_;
101 return a_ + p * range_;
102}
103
104/* Generate a pseudo Uniform random variate with the specified
105 * parameter.
106 * @param scale the scale of the distribution
107 **/
108Real Uniform::rand( Real const& a, Real const& b)
109{
110 return( (b-a <= 1.) ? a + (b-a) * generator.randUnif()
111 : a + generator.rand(b-a));
112}
113/* Give the value of the pdf at x.
114 * @param x a real value
115 * @param scale the scale of the distribution
116 **/
117Real Uniform::pdf( Real const& x, Real const& a, Real const& b)
118{
119 if (!Arithmetic<Real>::isFinite(x) ) return x;
120 if ((x < a)||(x > b)) return 0.;
121 return 1./(b-a);
122}
123/* Give the value of the log-pdf at x.
124 * @param x a real value
125 * @param scale the scale of the distribution
126 **/
127Real Uniform::lpdf( Real const& x, Real const& a, Real const& b)
128{
129 if (!Arithmetic<Real>::isFinite(x) ) return x;
130 if ((x < a)||(x > b)) return -Arithmetic<Real>::infinity();
131 return -std::log(b-a);
132}
133
134Real Uniform::cdf(const Real& t, const Real& a, const Real& b)
135{ return (b - t)/(b-a);}
136
137
138Real Uniform::icdf(const Real& p, const Real& a, const Real& b)
139{ return std::max(a,std::min((1.-p) * a + p * b, b));}
140
141#endif /* !IS_RTKPP_LIB */
142
143} // namespace Law
144
145} // namespace STK
146
#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 icdf(Real const &p) const
The inverse cumulative distribution function is.
Real const & b() const
virtual Real rand() const
Generate a pseudo Uniform random variate.
Real a_
The lower bound.
Real const & a() const
virtual Real pdf(Real const &x) const
Give the value of the pdf at x.
Real b_
The upper bound.
virtual Real lpdf(Real const &x) const
Give the value of the log-pdf at x.
virtual Real cdf(Real const &t) const
The cumulative distribution function is.
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.