STK++ 0.9.13
STK_Law_Binomial.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_BINOMIAL_H
36#define STK_LAW_BINOMIAL_H
37
38#include "STK_Law_IUnivLaw.h"
39
40namespace STK
41{
42namespace Law
43{
64class Binomial: public IUnivLaw<Integer>
65{
66 public:
72 inline Binomial( int n =1, Real const& prob =0.5)
73 : Base(_T("Binomial")), n_(n), prob_(prob)
74 {
78 }
80 inline virtual ~Binomial() {}
82 inline int n() const { return n_;}
84 inline Real const& prob() const { return prob_;}
86 inline void setN(Integer n)
87 {
89 n_ = n;
90 }
98
100 virtual Integer rand() const;
106 virtual Real pdf(Integer const& x) const;
112 virtual Real lpdf(Integer const& x) const;
118 virtual Real cdf(Real const& t) const;
124 virtual Integer icdf(Real const& p) const;
125
130 static Integer rand(int n, Real const& prob);
137 static Real pdf(Integer x, int n, Real const& prob);
144 static Real lpdf(Integer x, int n, Real const& prob);
152 static Real cdf(Real const& t, int n, Real const& prob);
159 static Integer icdf(Real const& p, int n, Real const& prob);
160
161#ifdef IS_RTKPP_LIB
163 virtual Real lcdf( Real const& t) const;
165 virtual Real cdfc( Real const& t) const;
167 virtual Real lcdfc( Real const& t) const;
168#endif
169
170 protected:
172 int n_;
175};
176
177#ifdef IS_RTKPP_LIB
178
179inline int Binomial::rand() const
180{
181#ifdef _OPENMP
182//#pragma omp critical
183#endif
184GetRNGstate(); int s = Rf_rbinom(n_, prob_); PutRNGstate(); return s;
185}
186
187inline Real Binomial::pdf(int const& x) const { return (Real)Rf_dbinom((double)x, (double)n_, prob_, false);}
188inline Real Binomial::lpdf(int const& x) const { return (Real)Rf_dbinom((double)x, (double)n_, prob_, true);}
189inline Real Binomial::cdf(Real const& t) const { return (Real)Rf_pbinom((double)t, (double)n_, prob_, true, false);}
190inline Real Binomial::lcdf(Real const& t) const { return (Real)Rf_pbinom((double)t, (double)n_, prob_, true, true);}
191inline Real Binomial::cdfc(Real const& t) const { return (Real)Rf_pbinom(t, (double)n_, prob_, false, false);}
192inline Real Binomial::lcdfc(Real const& t) const { return (Real)Rf_pbinom(t, (double)n_, prob_, false, true);}
193inline int Binomial::icdf(Real const& p) const { return Rf_qbinom(p, (double)n_, prob_, true, false);}
194
195inline int Binomial::rand(int n, Real const& prob)
196{
197#ifdef _OPENMP
198//#pragma omp critical
199#endif
200GetRNGstate(); int s = Rf_rbinom(n, prob); PutRNGstate(); return s;
201}
202
203inline Real Binomial::pdf(int x, int n, Real const& prob)
204{ return (Real)Rf_dbinom(x, (double)n, prob, false);}
205inline Real Binomial::lpdf(int x, int n, Real const& prob)
206{ return (Real)Rf_dbinom((double)x, (double)n, prob, true);}
207inline Real Binomial::cdf(Real const& t, int n, Real const& prob)
208{ return (Real)Rf_pbinom(t, (double)n, prob, true, false);}
209inline int Binomial::icdf(Real const& p, int n, Real const& prob)
210{ return Rf_qbinom(p, (double)n, prob, true, false);}
211
212#endif
213
214} // namespace Law
215
216} // namespace STK
217
218#endif /* STK_LAW_BINOMIAL_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 STKDOMAIN_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:147
#define _T(x)
Let x unmodified.
Binomial probability law.
virtual Integer icdf(Real const &p) const
inverse cumulative distribution function The quantile is defined as the smallest value x such that F...
Real const & prob() const
virtual Real cdf(Real const &t) const
compute the cumulative distribution function Give the probability that a Binomial random variate is l...
virtual Integer rand() const
virtual Real lpdf(Integer const &x) const
compute the log probability distribution function Give the value of the log-pdf at the point x.
IUnivLaw< Integer > Base
virtual Real pdf(Integer const &x) const
compute the probability distribution function (density) Give the value of the pdf at the point x.
void setProb(Real const &prob)
Binomial(int n=1, Real const &prob=0.5)
constructor
Real prob_
probability of success in a Bernoulli trial
void setN(Integer n)
virtual ~Binomial()
destructor
int n_
number of trials
Interface base class for all the univariate distributions.
virtual Real cdfc(Real const &t) const
calculate the complement of cumulative distribution function, called in statistics the survival funct...
virtual Real lcdfc(Real const &t) const
calculate the log-complement of cumulative distribution function Give the log-probability that a rand...
virtual Real lcdf(Real const &t) const
compute the lower tail log-cumulative distribution function Give the log-probability that a random va...
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
int Integer
STK fundamental type of integer values.
The namespace STK is the main domain space of the Statistical ToolKit project.