STK++ 0.9.13
STK_Law_Categorical.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_CATEGORICAL_H
36#define STK_LAW_CATEGORICAL_H
37
39#include "STK_Law_IUnivLaw.h"
40
41namespace STK
42{
43
44namespace Law
45{
46
68class Categorical: public IUnivLaw<int>
69{
70 public:
73 inline Categorical(): Base(_T("Categorical")), prob_(1,1) { computeCumProb();}
79 { computeCumProb();}
84 template <class OtherArray>
85 Categorical(OtherArray const& prob): Base(_T("Categorical"))
88 inline virtual ~Categorical() {}
89
91 inline Array2DVector<Real> const& prob() const { return prob_;}
93 inline Array2DVector<Real> const& cumProb() const { return cumProb_;}
94
96 template<class OtherArray>
97 inline void setProb(OtherArray const& prob )
99
101 virtual int rand() const;
107 virtual Real cdf(Real const& t) const;
113 virtual Real pdf(int const& x) const;
119 virtual Real lpdf(int const& x) const;
125 virtual int icdf(Real const& prob) const;
126
128 template<class OtherArray>
129 static int rand(OtherArray const& prob)
130 {
131 Real u = Law::generator.randUnif(), cum = 0.;
132 for(int k = prob.begin(); k< prob.lastIdx(); k++)
133 {
134 cum += prob[k];
135 if (u<=cum) return k;
136 }
137 return prob.lastIdx();
138 }
145 template<class OtherArray>
146 static Real lpdf(int const& x, OtherArray const& prob)
147 { return (prob[x] == 0) ? -Arithmetic<Real>::infinity() : std::log(prob[x]);}
148
149 protected:
154
155 private:
156 void computeCumProb();
157};
158
159/* @return a @c Type random variate . */
160inline int Categorical::rand() const
161{
162 Real u = Law::generator.randUnif();
163 int k;
164 for(k = cumProb_.begin(); k< cumProb_.end(); k++)
165 { if (u<=cumProb_[k]) return k;}
166 return k;
167}
168
169/* @brief compute the probability distribution function (density)
170 * Give the value of the pdf at the point x.
171 * @param x the value to compute the pdf.
172 * @return the value of the pdf
173 **/
174inline Real Categorical::pdf(int const& x) const
175{ return prob_[x];}
176/* @brief compute the log probability distribution function
177 * Give the value of the log-pdf at the point x.
178 * @param x the value to compute the lpdf.
179 * @return the value of the log-pdf
180 **/
181inline Real Categorical::lpdf(int const& x) const
182{ return (prob_[x] == 0) ? -Arithmetic<Real>::infinity() : std::log(prob_[x]);}
183/* @brief compute the cumulative distribution function
184 * Give the probability that a Categorical random variate is less or equal
185 * to t.
186 * @param t the value to compute the cdf.
187 * @return the value of the cdf
188 **/
189inline Real Categorical::cdf(Real const& t) const
190{ return (t<prob_.begin()) ? 0. : (t>=prob_.lastIdx()) ? 1. : cumProb_[std::floor(t)];}
191
192/* @brief inverse cumulative distribution function
193 * Compute the Real quantile t such that the probability of a random
194 * variate less to t is less or equal to p.
195 * @param p value of the probability giving the quantile
196 **/
197inline int Categorical::icdf(Real const& prob) const
198{
201 int k;
202 for (k = cumProb_.begin(); k< cumProb_.lastIdx(); ++k)
203 { if (cumProb_[k] >= prob) return k;}
204 return k;
205}
206
208{
209 cumProb_.resize(prob_.range());
210 Real sum=0.;
211 for (int k=prob_.begin(); k< prob_.end(); ++k)
212 { cumProb_[k] = (sum+=prob_[k]);}
213 // normalize
214 if (sum) {cumProb_/=sum; prob_ /=sum;}
216}
217
218} // namespace Law
219
220} // namespace STK
221
222#endif /* STK_LAW_CATEGORICAL_H */
A Array2DVector is a one dimensional horizontal container.
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_NO_ARG(Where, Error)
Definition STK_Macros.h:201
#define _T(x)
Let x unmodified.
Derived & resize(Range const &I, Range const &J)
resize the array.
Categorical probability law.
void setProb(OtherArray const &prob)
virtual Real pdf(int const &x) const
compute the probability distribution function (density) Give the value of the pdf at the point x.
Array2DVector< Real > const & cumProb() const
static Real lpdf(int const &x, OtherArray const &prob)
compute the log probability distribution function Give the value of the log-pdf at the point x.
virtual ~Categorical()
destructor
Array2DVector< Real > const & prob() const
virtual Real cdf(Real const &t) const
compute the cumulative distribution function Give the probability that a Categorical random variate i...
Categorical(OtherArray const &prob)
constructor with given probabilities.
virtual Real lpdf(int const &x) const
compute the log probability distribution function Give the value of the log-pdf at the point x.
Array2DVector< Real > cumProb_
cumulative probabilities in a Categorical trial
Categorical(Array2DVector< Real > const &prob)
constructor with given probabilities.
Categorical()
Default constructor.
virtual int rand() const
static int rand(OtherArray const &prob)
virtual int icdf(Real const &prob) const
inverse cumulative distribution function The quantile is defined as the smallest value x such that F...
Array2DVector< Real > prob_
probabilities in a Categorical trial
Interface base class for all the univariate distributions.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
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.