STK++ 0.9.13
STK_Law_Bernoulli.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_BERNOULLI_H
36#define STK_LAW_BERNOULLI_H
37
38#include "STK_Law_IUnivLaw.h"
39
40namespace STK
41{
42
43namespace Law
44{
45
63class Bernoulli: public IUnivLaw<Binary>
64{
65 public:
70 inline Bernoulli(Real const& prob =0.5): Base(String(_T("Bernoulli")) ), prob_(prob)
71 {
74 }
76 inline virtual ~Bernoulli() {}
78 inline Real const& prob() const { return prob_;}
86
88 virtual Binary rand() const;
94 virtual Real pdf(Binary const& x) const;
100 virtual Real lpdf(Binary const& x) const;
106 virtual Real cdf(Real const& t) const;
112 virtual Binary icdf(Real const& prob) const;
113
114
118 static Binary rand(Real const& prob);
125 static Real pdf(Binary const& x, Real const& prob);
132 static Real lpdf(Binary const& x, Real const& prob);
133
134 protected:
137};
138
140{ return (Law::generator.randUnif()<=prob_) ? one_ : zero_;}
141
142inline Real Bernoulli::pdf(Binary const& x) const
143{
144 switch (x)
145 {
146 case zero_: return 1.-prob_;
147 case one_: return prob_;
148 default: break;
149 }
150 return Arithmetic<Real>::NA();
151}
152inline Real Bernoulli::lpdf(Binary const& x) const
153{
154 switch (x)
155 {
156 case zero_: return (prob_ == 1) ? -Arithmetic<Real>::infinity() : std::log(1.-prob_);
157 case one_: return (prob_ == 0) ? -Arithmetic<Real>::infinity() : std::log(prob_);
158 default: break;
159 }
160 return Arithmetic<Real>::NA();
161}
162inline Real Bernoulli::cdf(Real const& t) const
163{ return (t<0.) ? 0. : (t<1.) ? 1.-prob_ : 1.;}
164inline Binary Bernoulli::icdf(Real const& prob) const
165{
166#ifdef STK_STATISTIK_DEBUG
169#endif
170 return (prob==0) ? zero_ : (prob==1) ? one_ : (prob <= 1.-prob_) ? zero_ : one_;
171}
172
173
174inline Binary Bernoulli::rand(Real const& prob)
175{
176#ifdef STK_STATISTIK_DEBUG
179#endif
180 return (generator.randUnif()<=prob) ? one_ : zero_;
181}
182
183inline Real Bernoulli::pdf(Binary const& x, Real const& prob)
184{
185#ifdef STK_STATISTIK_DEBUG
188#endif
189 switch (x)
190 {
191 case zero_: return 1.-prob;
192 case one_: return prob;
193 default: break;
194 }
195 return Arithmetic<Real>::NA();
196}
197inline Real Bernoulli::lpdf(Binary const& x, Real const& prob)
198{
199#ifdef STK_STATISTIK_DEBUG
202#endif
203 switch (x)
204 {
205 case zero_: return (prob == 1) ? -Arithmetic<Real>::infinity() : std::log(1.-prob);
206 case one_: return (prob == 0) ? -Arithmetic<Real>::infinity() : std::log(prob);
207 default: break;
208 }
209 return Arithmetic<Real>::NA();
210}
211
212} // namespace Law
213
214} // namespace STK
215
216#endif /* STK_LAW_BERNOULLI_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 _T(x)
Let x unmodified.
Bernoulli probability law.
Bernoulli(Real const &prob=0.5)
constructor
virtual Binary rand() const
virtual Binary icdf(Real const &prob) const
inverse cumulative distribution function The quantile is defined as the smallest value x such that F...
virtual Real cdf(Real const &t) const
compute the cumulative distribution function Give the probability that a Bernoulli random variate is ...
virtual Real pdf(Binary const &x) const
compute the probability distribution function (density) Give the value of the pdf at the point x.
Real prob_
probability of success in a Bernoulli trial
Real const & prob() const
virtual Real lpdf(Binary const &x) const
compute the log probability distribution function Give the value of the log-pdf at the point x.
virtual ~Bernoulli()
destructor
void setProb(Real const &prob)
IUnivLaw< Binary > Base
Interface base class for all the univariate distributions.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Binary
STK fundamental type of a binary.
Definition STK_Binary.h:49
@ zero_
0 value
Definition STK_Binary.h:49
@ one_
1 value
Definition STK_Binary.h:50
std::basic_string< Char > String
STK fundamental type of a String.
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 Type NA()
Adding a Non Available (NA) special number.