STK++ 0.9.13
STK_Law_FisherSnedecor.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: stkpp::STatistiK::Law
27 * Purpose: FisherSnedecor probability distribution.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
34
35
36#ifndef IS_RTKPP_LIB
40#include <random>
41#endif
42
43
44namespace STK
45{
46
47namespace Law
48{
49
50#ifndef IS_RTKPP_LIB
51
52/* @return a pseudo FisherSnedecor random variate. */
54{
56 std::fisher_f_distribution<Real> dist(static_cast<Real>(df1_), static_cast<Real>(df2_));
57 return dist(generator.engine());
58}
59/* @return the value of the pdf
60 * @param x a positive real value
61 **/
63{
64 return pdf(x, df1_, df2_);
65}
66/* @return the value of the log-pdf
67 * @param x a positive real value
68 **/
70{
71 return lpdf(x, df1_, df2_);
72}
73/* @return the cumulative distribution function
74 * @param t a positive real value
75 **/
77{
78 return cdf(t, df1_, df2_);
79}
80/* @return the inverse cumulative distribution function
81 * @param p a probability number
82 **/
84{
85 return icdf(p, df1_, df2_);
86}
87
88/* @return a pseudo FisherSnedecor random variate with the specified parameters.
89 * @param df1, df2 degree of freedom parameters
90 **/
92{
94 std::fisher_f_distribution<Real> dist(static_cast<Real>(df1), static_cast<Real>(df2));
95 return dist(generator.engine());
96}
97/* @return the value of the pdf
98 * @param x a positive real value
99 * @param df1, df2 degree of freedom parameters
100 **/
102{
103 return std::exp(lpdf(x, df1, df2));
104}
105/* @return the value of the log-pdf
106 * @param x a positive real value
107 * @param df1, df2 degree of freedom parameters
108 **/
110{
112 if (x <= 0.) return -Arithmetic<Real>::infinity();
113 Real const a = static_cast<Real>(df1)/2., b = static_cast<Real>(df2)/2.;
114 Real const d1x = static_cast<Real>(df1)*x;
115 // log of the sqrt term, written in a stable way
116 return ( a*std::log(d1x) + b*std::log(static_cast<Real>(df2))
117 - (a + b)*std::log(d1x + static_cast<Real>(df2))
118 - std::log(x)
119 - (std::lgamma(a) + std::lgamma(b) - std::lgamma(a + b)));
120}
121/* @return the cumulative distribution function
122 * @param t a positive real value
123 * @param df1, df2 degree of freedom parameters
124 **/
126{
127 // check NA value
129 if (Arithmetic<Real>::isInfinite(t)) return (t < 0.) ? 0. : 1.;
130 if (t <= 0.) return 0.;
131 // cdf(t) = I_{d1 t/(d1 t + d2)}(d1/2, d2/2)
132 Real const x = static_cast<Real>(df1)*t/(static_cast<Real>(df1)*t + static_cast<Real>(df2));
133 return Funct::betaRatio(static_cast<Real>(df1)/2., static_cast<Real>(df2)/2., x);
134}
135
136/* @return the inverse cumulative distribution function
137 * @param p a probability number
138 * @param df1, df2 degree of freedom parameters
139 **/
141{
142 // NA is encoded in the Arithmetic traits
144 if ((p > 1.) || (p < 0.))
145 STKDOMAIN_ERROR_1ARG(FisherSnedecor::icdf,p,argument outside [0;1]);
146 if (p == 0.) return 0.;
147 if (p == 1.) return Arithmetic<Real>::infinity();
148 // quantile by bisection on our own cdf
149 return hidden::quantile_bisection([df1, df2](Real const& t){ return cdf(t, df1, df2); }, p, 0., 1.);
150}
151
152#endif
153
154} // namespace Law
155
156} // namespace STK
157
In this file we declare functions around the beta function ratio.
In this file we define the FisherSnedecor probability distribution.
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 cdf(Real const &t) const
virtual Real pdf(Real const &x) const
Integer df2_
Second degree of freedom.
virtual Real icdf(Real const &p) const
virtual Real lpdf(Real const &x) const
Integer df1_
First degree of freedom.
Real betaRatio(Real const &a, Real const &b, Real const &x, bool lower_tail=true)
Compute the incomplete beta function ratio Compute the beta ratio function.
Real dist(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y)
Compute the distance between two vectors.
double Real
STK fundamental type of Real values.
int Integer
STK fundamental type of integer values.
Real quantile_bisection(Cdf const &cdf, Real const &p, Real lo, Real hi)
Compute the quantile of a continuous distribution by bisection on its cumulative distribution functio...
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)