STK++ 0.9.13
STK_Law_Cauchy.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: STatistiK
27 * Purpose: Implementation of the Cauchy Distribution
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef IS_RTKPP_LIB
36#include "../include/STK_Law_Cauchy.h"
37#endif
38
39
40namespace STK
41{
42
43namespace Law
44{
45
46#ifndef IS_RTKPP_LIB
47
48/* Generate a pseudo Cauchy random variate. */
50{ return mu_ + scale_ * Real(std::tan( double(Const::_PI_ * generator.randUnif())));}
51
52/*
53 * Generate a pseudo Cauchy random variate with the specified parameters.
54 * (static)
55 */
56Real Cauchy::rand( Real const& mu, Real const& scale)
57{
58#ifdef STK_DEBUG
59 // check parameters
62#endif
63 return mu + scale * Real(std::tan( double(Const::_PI_ * generator.randUnif()) ));
64}
65
66/* Give the value of the pdf at x.*/
67Real Cauchy::pdf( Real const& x) const
68{
69 // check NA value
70 if (isNA(x)) return Arithmetic<Real>::NA();
71 // trivial case
72 if (Arithmetic<Real>::isInfinite(x)) return 0.0;
73
74 // general case
75 Real y = (x - mu_) / scale_;
76 return 1. / (Const::_PI_ * scale_ * (1. + y * y));
77}
78
79/* Give the value of the pdf at x.*/
80Real Cauchy::pdf( Real const& x, Real const& mu, Real const& scale)
81{
82#ifdef STK_DEBUG
83 // check parameters
86#endif
87 // check NA value
88 if (isNA(x)) return Arithmetic<Real>::NA();
89 // trivial case
90 if (Arithmetic<Real>::isInfinite(x)) return 0.0;
91
92 // general case
93 Real y = (x - mu) / scale;
94 return 1. / (Const::_PI_ * scale * (1. + y * y));
95}
96
97/* Give the value of the log-pdf at x. */
98Real Cauchy::lpdf( Real const& x) const
99{
100 // check NA value
101 if (isNA(x)) return Arithmetic<Real>::NA();
102 // trivial case
104
105 // general case
106 Real y = (x - mu_) / scale_;
107 return -Real(std::log( double(Const::_PI_ * scale_ * (1. + y * y)) ));
108}
109
110/* Give the value of the log-pdf at x. */
111Real Cauchy::lpdf( Real const& x, Real const& mu, Real const& scale)
112{
113#ifdef STK_DEBUG
114 // check parameters
117#endif
118 // check NA value
119 if (isNA(x)) return Arithmetic<Real>::NA();
120 // trivial case
123
124 // general case
125 Real y = (x - mu) / scale;
126 return - Real(std::log( double(Const::_PI_ * scale * (1. + y * y)) ));
127}
128
129/* The cumulative distribution function at t.
130 */
131Real Cauchy::cdf( Real const& t) const
132{
133 // check NA value
134 if (isNA(t)) return Arithmetic<Real>::NA();
135 // check parameter
137 return (t < 0.) ? 0.0 : 1.0;
138
139 /* http://www.faqs.org/faqs/fr/maths/maths-faq-3/
140 * arctan on [0, 1[:
141 * if x<0 atan(x)= -atan(-x)
142 * elseif x>1 atan(x)= Pi/2-atan(1/x).
143 */
144 Real td = (t - mu_)/scale_;
145 if (std::abs(td) > 1)
146 {
147 Real y = Real(std::atan( 1./double(td))) / Const::_PI_;
148 return (td > 0) ? (1. - y) : (-y);
149 }
150 return 0.5 + Real(std::atan( double(td))) / Const::_PI_;
151}
152
153/*
154 * The inverse cumulative distribution function at p.
155 */
156Real Cauchy::icdf( Real const& p) const
157{
158 // check NA value
159 if (isNA(p)) return Arithmetic<Real>::NA();
160 // check parameter
161 if ((p > 1.) || (p < 0.))
163 // trivial cases
164 if (p == 0.) return -Arithmetic<Real>::infinity();
165 if (p == 1.) return Arithmetic<Real>::infinity();
166
167 // general case
168 // tan(pi * (p - 1/2)) = -cot(pi * p) = -1/tan(pi * p)
169 return mu_ - scale_ / Real(std::tan( double(Const::_PI_ * p) ));
170}
171
172/* The cumulative distribution function at t.
173 */
174Real Cauchy::cdf( Real const& t, Real const& mu, Real const& scale)
175{
176 // check NA value
177 if (isNA(t)) return Arithmetic<Real>::NA();
178 // check parameter
180 return (t < 0.) ? 0.0 : 1.0;
181
182 /* http://www.faqs.org/faqs/fr/maths/maths-faq-3/
183 * arctan on [0, 1[:
184 * if x<0 atan(x)= -atan(-x)
185 * elseif x>1 atan(x)= Pi/2-atan(1/x).
186 */
187 Real td = (t - mu)/scale;
188 if (std::abs(td) > 1)
189 {
190 Real y = Real(std::atan( 1./double(td))) / Const::_PI_;
191 return (td > 0) ? (1. - y) : (-y);
192 }
193 return 0.5 + Real(std::atan( double(td))) / Const::_PI_;
194}
195
196/*
197 * The inverse cumulative distribution function at p.
198 */
199Real Cauchy::icdf( Real const& p, Real const& mu, Real const& scale)
200{
201 // check NA value
202 if (isNA(p)) return Arithmetic<Real>::NA();
203 // check parameter
204 if ((p > 1.) || (p < 0.))
206 // trivial cases
207 if (p == 0.) return -Arithmetic<Real>::infinity();
208 if (p == 1.) return Arithmetic<Real>::infinity();
209
210 // general case
211 // tan(pi * (p - 1/2)) = -cot(pi * p) = -1/tan(pi * p)
212 return mu - scale / Real(std::tan( double(Const::_PI_ * p) ));
213}
214
215#endif
216
217} // namespace Law
218
219} // namespace STK
220
#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
virtual Real pdf(Real const &x) const
Cauchy(Real const &mu=0, Real const &scale=1)
Default constructor.
Real scale_
The scale parameter.
virtual Real rand() const
Generate a pseudo Cauchy random variate.
Real const & scale() const
virtual Real lpdf(Real const &x) const
virtual Real cdf(Real const &t) const
The cumulative distribution function of the Cauchy distribution at t is.
Real const & mu() const
Real mu_
The mu parameter.
virtual Real icdf(Real const &p) const
The inverse cumulative distribution function at p is.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
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.