1 | /*************************************************************************
|
---|
2 | Cephes Math Library Release 2.8: June, 2000
|
---|
3 | Copyright 1984, 1987, 2000 by Stephen L. Moshier
|
---|
4 |
|
---|
5 | Contributors:
|
---|
6 | * Sergey Bochkanov (ALGLIB project). Translation from C to
|
---|
7 | pseudocode.
|
---|
8 |
|
---|
9 | See subroutines comments for additional copyrights.
|
---|
10 |
|
---|
11 | >>> SOURCE LICENSE >>>
|
---|
12 | This program is free software; you can redistribute it and/or modify
|
---|
13 | it under the terms of the GNU General Public License as published by
|
---|
14 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
15 | License, or (at your option) any later version.
|
---|
16 |
|
---|
17 | This program is distributed in the hope that it will be useful,
|
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | GNU General Public License for more details.
|
---|
21 |
|
---|
22 | A copy of the GNU General Public License is available at
|
---|
23 | http://www.fsf.org/licensing/licenses
|
---|
24 |
|
---|
25 | >>> END OF LICENSE >>>
|
---|
26 | *************************************************************************/
|
---|
27 |
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace alglib
|
---|
31 | {
|
---|
32 | public class chisquaredistr
|
---|
33 | {
|
---|
34 | /*************************************************************************
|
---|
35 | Chi-square distribution
|
---|
36 |
|
---|
37 | Returns the area under the left hand tail (from 0 to x)
|
---|
38 | of the Chi square probability density function with
|
---|
39 | v degrees of freedom.
|
---|
40 |
|
---|
41 |
|
---|
42 | x
|
---|
43 | -
|
---|
44 | 1 | | v/2-1 -t/2
|
---|
45 | P( x | v ) = ----------- | t e dt
|
---|
46 | v/2 - | |
|
---|
47 | 2 | (v/2) -
|
---|
48 | 0
|
---|
49 |
|
---|
50 | where x is the Chi-square variable.
|
---|
51 |
|
---|
52 | The incomplete gamma integral is used, according to the
|
---|
53 | formula
|
---|
54 |
|
---|
55 | y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
|
---|
56 |
|
---|
57 | The arguments must both be positive.
|
---|
58 |
|
---|
59 | ACCURACY:
|
---|
60 |
|
---|
61 | See incomplete gamma function
|
---|
62 |
|
---|
63 |
|
---|
64 | Cephes Math Library Release 2.8: June, 2000
|
---|
65 | Copyright 1984, 1987, 2000 by Stephen L. Moshier
|
---|
66 | *************************************************************************/
|
---|
67 | public static double chisquaredistribution(double v,
|
---|
68 | double x)
|
---|
69 | {
|
---|
70 | double result = 0;
|
---|
71 |
|
---|
72 | System.Diagnostics.Debug.Assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistribution");
|
---|
73 | result = igammaf.incompletegamma(v/2.0, x/2.0);
|
---|
74 | return result;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /*************************************************************************
|
---|
79 | Complemented Chi-square distribution
|
---|
80 |
|
---|
81 | Returns the area under the right hand tail (from x to
|
---|
82 | infinity) of the Chi square probability density function
|
---|
83 | with v degrees of freedom:
|
---|
84 |
|
---|
85 | inf.
|
---|
86 | -
|
---|
87 | 1 | | v/2-1 -t/2
|
---|
88 | P( x | v ) = ----------- | t e dt
|
---|
89 | v/2 - | |
|
---|
90 | 2 | (v/2) -
|
---|
91 | x
|
---|
92 |
|
---|
93 | where x is the Chi-square variable.
|
---|
94 |
|
---|
95 | The incomplete gamma integral is used, according to the
|
---|
96 | formula
|
---|
97 |
|
---|
98 | y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
|
---|
99 |
|
---|
100 | The arguments must both be positive.
|
---|
101 |
|
---|
102 | ACCURACY:
|
---|
103 |
|
---|
104 | See incomplete gamma function
|
---|
105 |
|
---|
106 | Cephes Math Library Release 2.8: June, 2000
|
---|
107 | Copyright 1984, 1987, 2000 by Stephen L. Moshier
|
---|
108 | *************************************************************************/
|
---|
109 | public static double chisquarecdistribution(double v,
|
---|
110 | double x)
|
---|
111 | {
|
---|
112 | double result = 0;
|
---|
113 |
|
---|
114 | System.Diagnostics.Debug.Assert((double)(x)>=(double)(0) & (double)(v)>=(double)(1), "Domain error in ChiSquareDistributionC");
|
---|
115 | result = igammaf.incompletegammac(v/2.0, x/2.0);
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /*************************************************************************
|
---|
121 | Inverse of complemented Chi-square distribution
|
---|
122 |
|
---|
123 | Finds the Chi-square argument x such that the integral
|
---|
124 | from x to infinity of the Chi-square density is equal
|
---|
125 | to the given cumulative probability y.
|
---|
126 |
|
---|
127 | This is accomplished using the inverse gamma integral
|
---|
128 | function and the relation
|
---|
129 |
|
---|
130 | x/2 = igami( df/2, y );
|
---|
131 |
|
---|
132 | ACCURACY:
|
---|
133 |
|
---|
134 | See inverse incomplete gamma function
|
---|
135 |
|
---|
136 |
|
---|
137 | Cephes Math Library Release 2.8: June, 2000
|
---|
138 | Copyright 1984, 1987, 2000 by Stephen L. Moshier
|
---|
139 | *************************************************************************/
|
---|
140 | public static double invchisquaredistribution(double v,
|
---|
141 | double y)
|
---|
142 | {
|
---|
143 | double result = 0;
|
---|
144 |
|
---|
145 | System.Diagnostics.Debug.Assert((double)(y)>=(double)(0) & (double)(y)<=(double)(1) & (double)(v)>=(double)(1), "Domain error in InvChiSquareDistribution");
|
---|
146 | result = 2*igammaf.invincompletegammac(0.5*v, y);
|
---|
147 | return result;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|