Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/binomialdistr.cs @ 3839

Last change on this file since 3839 was 3839, checked in by mkommend, 14 years ago

implemented first version of LR (ticket #1012)

File size: 7.8 KB
Line 
1/*************************************************************************
2Cephes Math Library Release 2.8:  June, 2000
3Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
4
5Contributors:
6    * Sergey Bochkanov (ALGLIB project). Translation from C to
7      pseudocode.
8
9See subroutines comments for additional copyrights.
10
11>>> SOURCE LICENSE >>>
12This program is free software; you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation (www.fsf.org); either version 2 of the
15License, or (at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20GNU General Public License for more details.
21
22A copy of the GNU General Public License is available at
23http://www.fsf.org/licensing/licenses
24
25>>> END OF LICENSE >>>
26*************************************************************************/
27
28using System;
29
30namespace alglib
31{
32    public class binomialdistr
33    {
34        /*************************************************************************
35        Binomial distribution
36
37        Returns the sum of the terms 0 through k of the Binomial
38        probability density:
39
40          k
41          --  ( n )   j      n-j
42          >   (   )  p  (1-p)
43          --  ( j )
44         j=0
45
46        The terms are not summed directly; instead the incomplete
47        beta integral is employed, according to the formula
48
49        y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
50
51        The arguments must be positive, with p ranging from 0 to 1.
52
53        ACCURACY:
54
55        Tested at random points (a,b,p), with p between 0 and 1.
56
57                      a,b                     Relative error:
58        arithmetic  domain     # trials      peak         rms
59         For p between 0.001 and 1:
60           IEEE     0,100       100000      4.3e-15     2.6e-16
61
62        Cephes Math Library Release 2.8:  June, 2000
63        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
64        *************************************************************************/
65        public static double binomialdistribution(int k,
66            int n,
67            double p)
68        {
69            double result = 0;
70            double dk = 0;
71            double dn = 0;
72
73            System.Diagnostics.Debug.Assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistribution");
74            System.Diagnostics.Debug.Assert(k>=-1 & k<=n, "Domain error in BinomialDistribution");
75            if( k==-1 )
76            {
77                result = 0;
78                return result;
79            }
80            if( k==n )
81            {
82                result = 1;
83                return result;
84            }
85            dn = n-k;
86            if( k==0 )
87            {
88                dk = Math.Pow(1.0-p, dn);
89            }
90            else
91            {
92                dk = k+1;
93                dk = ibetaf.incompletebeta(dn, dk, 1.0-p);
94            }
95            result = dk;
96            return result;
97        }
98
99
100        /*************************************************************************
101        Complemented binomial distribution
102
103        Returns the sum of the terms k+1 through n of the Binomial
104        probability density:
105
106          n
107          --  ( n )   j      n-j
108          >   (   )  p  (1-p)
109          --  ( j )
110         j=k+1
111
112        The terms are not summed directly; instead the incomplete
113        beta integral is employed, according to the formula
114
115        y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
116
117        The arguments must be positive, with p ranging from 0 to 1.
118
119        ACCURACY:
120
121        Tested at random points (a,b,p).
122
123                      a,b                     Relative error:
124        arithmetic  domain     # trials      peak         rms
125         For p between 0.001 and 1:
126           IEEE     0,100       100000      6.7e-15     8.2e-16
127         For p between 0 and .001:
128           IEEE     0,100       100000      1.5e-13     2.7e-15
129
130        Cephes Math Library Release 2.8:  June, 2000
131        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
132        *************************************************************************/
133        public static double binomialcdistribution(int k,
134            int n,
135            double p)
136        {
137            double result = 0;
138            double dk = 0;
139            double dn = 0;
140
141            System.Diagnostics.Debug.Assert((double)(p)>=(double)(0) & (double)(p)<=(double)(1), "Domain error in BinomialDistributionC");
142            System.Diagnostics.Debug.Assert(k>=-1 & k<=n, "Domain error in BinomialDistributionC");
143            if( k==-1 )
144            {
145                result = 1;
146                return result;
147            }
148            if( k==n )
149            {
150                result = 0;
151                return result;
152            }
153            dn = n-k;
154            if( k==0 )
155            {
156                if( (double)(p)<(double)(0.01) )
157                {
158                    dk = -nearunityunit.expm1(dn*nearunityunit.log1p(-p));
159                }
160                else
161                {
162                    dk = 1.0-Math.Pow(1.0-p, dn);
163                }
164            }
165            else
166            {
167                dk = k+1;
168                dk = ibetaf.incompletebeta(dk, dn, p);
169            }
170            result = dk;
171            return result;
172        }
173
174
175        /*************************************************************************
176        Inverse binomial distribution
177
178        Finds the event probability p such that the sum of the
179        terms 0 through k of the Binomial probability density
180        is equal to the given cumulative probability y.
181
182        This is accomplished using the inverse beta integral
183        function and the relation
184
185        1 - p = incbi( n-k, k+1, y ).
186
187        ACCURACY:
188
189        Tested at random points (a,b,p).
190
191                      a,b                     Relative error:
192        arithmetic  domain     # trials      peak         rms
193         For p between 0.001 and 1:
194           IEEE     0,100       100000      2.3e-14     6.4e-16
195           IEEE     0,10000     100000      6.6e-12     1.2e-13
196         For p between 10^-6 and 0.001:
197           IEEE     0,100       100000      2.0e-12     1.3e-14
198           IEEE     0,10000     100000      1.5e-12     3.2e-14
199
200        Cephes Math Library Release 2.8:  June, 2000
201        Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
202        *************************************************************************/
203        public static double invbinomialdistribution(int k,
204            int n,
205            double y)
206        {
207            double result = 0;
208            double dk = 0;
209            double dn = 0;
210            double p = 0;
211
212            System.Diagnostics.Debug.Assert(k>=0 & k<n, "Domain error in InvBinomialDistribution");
213            dn = n-k;
214            if( k==0 )
215            {
216                if( (double)(y)>(double)(0.8) )
217                {
218                    p = -nearunityunit.expm1(nearunityunit.log1p(y-1.0)/dn);
219                }
220                else
221                {
222                    p = 1.0-Math.Pow(y, 1.0/dn);
223                }
224            }
225            else
226            {
227                dk = k+1;
228                p = ibetaf.incompletebeta(dn, dk, 0.5);
229                if( (double)(p)>(double)(0.5) )
230                {
231                    p = ibetaf.invincompletebeta(dk, dn, 1.0-y);
232                }
233                else
234                {
235                    p = 1.0-ibetaf.invincompletebeta(dn, dk, y);
236                }
237            }
238            result = p;
239            return result;
240        }
241    }
242}
Note: See TracBrowser for help on using the repository browser.