STK++ 0.9.13
STK_Law_Logistic.cpp
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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 * Purpose: Logistic probability distribution.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
34
35#ifndef IS_RTKPP_LIB
38#endif
39
40namespace STK
41{
42namespace Law
43{
44
45#ifndef IS_RTKPP_LIB
46
47/* @brief Generate a pseudo logisticized Logistic random variate.
48 *
49 * Generate a pseudo logisticized Logistic random variate
50 * with location parameter @c mu_ and scale @c scale_.
51 * @return a pseudo logistic random variate
52 **/
54{
56 Real const u = generator.randDblExc();
57 return mu_ + scale_*std::log(u/(1. - u));
58}
59/* @param x a real value
60 * @return the value of the logistic pdf at @c x
61 **/
62Real Logistic::pdf( Real const& x) const
63{
64 return pdf(x, mu_, scale_);
65}
66/* @return Give the value of the log-pdf at x.
67 * @param x a real value
68 **/
69Real Logistic::lpdf( Real const& x) const
70{
71 return lpdf(x, mu_, scale_);
72}
73/* @brief Compute the cumulative distribution function at t of
74 * the standard logistic distribution.
75 *
76 * The cumulative distribution function of the logistic distribution is
77 * also a scaled version of the Hyperbolic function.
78 * \f[
79 * F(t; \mu, s) = \frac{1}{1+e^{-\frac{t-\mu}{s}}}
80 * = \frac{1}{2} + \frac{1}{2} \;\operatorname{tanh}\!\left(\frac{t-\mu}{2s}\right).
81 * \f]
82 *
83 * @param t a real value
84 * @return the cumulative distribution function value at t
85 **/
86Real Logistic::cdf( Real const& t) const
87{
88 return cdf(t, mu_, scale_);
89}
90
91/* @brief Compute the inverse cumulative distribution function at p
92 * of the standard logistic distribution.
93 *
94 * The inverse cumulative distribution function (quantile function) of the
95 * logistic distribution is a generalization of the logit function.
96 * It is defined as follows:
97 * \f[
98 * Q(p;\mu,s) = \mu + s\,\ln\left(\frac{p}{1-p}\right).
99 * \f]
100 * @param p a probability number.
101 * @return the inverse cumulative distribution function value at p.
102 **/
103Real Logistic::icdf( Real const& p) const
104{
105 return icdf(p, mu_, scale_);
106}
107
108/* @brief Generate a pseudo Logistic random variate.
109 *
110 * Generate a pseudo Logistic random variate with location @c mu and
111 * scale @c scale parameters.
112 * @param mu mean of the Logistic distribution
113 * @param scale scale of the Logistic distribution
114 * @return a pseudo logistic random variate, centered in @c mu and with
115 * scale @c scale
116 **/
118{
120 Real const u = generator.randDblExc();
121 return mu + scale*std::log(u/(1. - u));
122}
123/* @param x a real value
124 * @param mu mean of the logistic law
125 * @param scale scale of the logistic law
126 * @return the value of the logistic pdf at @c x
127 **/
128Real Logistic::pdf( Real const& x, Real const& mu, Real const& scale)
129{
131 // use the symmetry of the pdf around mu: compute with z = |x-mu|/scale
132 Real const z = std::abs(x - mu)/scale;
133 return std::exp(-z - 2.*std::log1p(std::exp(-z)))/scale;
134}
135/* @return Give the value of the log-pdf at x.
136 * @param x a real value
137 * @param mu mean of the logistic law
138 * @param scale scale of the logistic law
139 **/
140Real Logistic::lpdf( Real const& x, Real const& mu, Real const& scale)
141{
143 Real const z = std::abs(x - mu)/scale;
144 return -z - 2.*std::log1p(std::exp(-z)) - std::log(scale);
145}
146/* @brief Compute the cumulative distribution function at t of
147 * the standard logistic distribution.
148 *
149 * @param t a real value
150 * @return the cumulative distribution function value at t
151 **/
152Real Logistic::cdf( Real const& t, Real const& mu, Real const& scale)
153{
154 // check NA value
156 if (Arithmetic<Real>::isInfinite(t)) return (t < 0.) ? 0. : 1.;
157 // stable computation: cdf = 0.5*(1 + tanh(z/2))
158 return 0.5*(1. + std::tanh((t - mu)/(2.*scale)));
159}
160
161/* @brief Compute the inverse cumulative distribution function at p
162 * of the standard logistic distribution.
163 *
164 * @param p a probability number.
165 * @return the inverse cumulative distribution function value at p.
166 **/
167Real Logistic::icdf( Real const& p, Real const& mu, Real const& scale)
168{
169 // NA is encoded in the Arithmetic traits
171 if ((p > 1.) || (p < 0.))
172 STKDOMAIN_ERROR_1ARG(Logistic::icdf,p,argument outside [0;1]);
173 if (p == 0.) return -Arithmetic<Real>::infinity();
174 if (p == 1.) return Arithmetic<Real>::infinity();
175 // closed form: t = mu + scale * log(p/(1-p))
176 return mu + scale*std::log(p/(1. - p));
177}
178
179#endif
180
181} // namespace Law
182
183} // namespace STK
184
In this file we define the Logistic probability law class.
In this file we define the utilities constant and method for the Law namespace.
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
virtual Real pdf(Real const &x) const
Real rand() const
Generate a pseudo Logistic random variate.
Real scale_
The scale parameter.
Real mu_
The mu parameter.
Real const & scale() const
Real const & mu() const
virtual Real icdf(Real const &p) const
Compute the inverse cumulative distribution function at p of the standard logistic distribution.
virtual Real cdf(Real const &t) const
Compute the cumulative distribution function at t of the standard logistic distribution.
virtual Real lpdf(Real const &x) const
double Real
STK fundamental type of Real values.
This is the namespace enclosing the classes handling the usual probabilities Laws.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static bool isInfinite(Type const &x)
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)