1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.distribution;
18  
19  import junit.framework.TestCase;
20  import org.apache.commons.math.MathException;
21  
22  public class BetaDistributionTest extends TestCase {
23      public void testCumulative() throws MathException {
24          double[] x = new double[]{-0.1, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1};
25          // all test data computed using R 2.5
26          checkCumulative(0.1, 0.1,
27                  x, new double[]{
28                  0.0000000000, 0.0000000000, 0.4063850939, 0.4397091902, 0.4628041861,
29                  0.4821200456, 0.5000000000, 0.5178799544, 0.5371958139, 0.5602908098,
30                  0.5936149061, 1.0000000000, 1.0000000000});
31          checkCumulative(0.1, 0.5,
32                  x, new double[]{
33                  0.0000000000, 0.0000000000, 0.7048336221, 0.7593042194, 0.7951765304,
34                  0.8234948385, 0.8480017124, 0.8706034370, 0.8926585878, 0.9156406404,
35                  0.9423662883, 1.0000000000, 1.0000000000});
36          checkCumulative(0.1, 1.0,
37                  x, new double[]{
38                  0.0000000000, 0.0000000000, 0.7943282347, 0.8513399225, 0.8865681506,
39                  0.9124435366, 0.9330329915, 0.9502002165, 0.9649610951, 0.9779327685,
40                  0.9895192582, 1.0000000000, 1.0000000000});
41          checkCumulative(0.1, 2.0,
42                  x, new double[]{
43                  0.0000000000, 0.0000000000, 0.8658177758, 0.9194471163, 0.9486279211,
44                  0.9671901487, 0.9796846411, 0.9882082252, 0.9939099280, 0.9974914239,
45                  0.9994144508, 1.0000000000, 1.0000000000});
46          checkCumulative(0.1, 4.0,
47                  x, new double[]{
48                  0.0000000000, 0.0000000000, 0.9234991121, 0.9661958941, 0.9842285085,
49                  0.9928444112, 0.9970040660, 0.9989112804, 0.9996895625, 0.9999440793,
50                  0.9999967829, 1.0000000000, 1.0000000000});
51          checkCumulative(0.5, 0.1,
52                  x, new double[]{
53                  0.00000000000, 0.00000000000, 0.05763371168, 0.08435935962,
54                  0.10734141216, 0.12939656302, 0.15199828760, 0.17650516146,
55                  0.20482346963, 0.24069578055, 0.29516637795, 1.00000000000, 1.00000000000});
56  
57          checkCumulative(0.5, 0.5,
58                  x, new double[]{
59                  0.0000000000, 0.0000000000, 0.2048327647, 0.2951672353, 0.3690101196,
60                  0.4359057832, 0.5000000000, 0.5640942168, 0.6309898804, 0.7048327647,
61                  0.7951672353, 1.0000000000, 1.0000000000});
62          checkCumulative(0.5, 1.0,
63                  x, new double[]{
64                  0.0000000000, 0.0000000000, 0.3162277660, 0.4472135955, 0.5477225575,
65                  0.6324555320, 0.7071067812, 0.7745966692, 0.8366600265, 0.8944271910,
66                  0.9486832981, 1.0000000000, 1.0000000000});
67          checkCumulative(0.5, 2.0,
68                  x, new double[]{
69                  0.0000000000, 0.0000000000, 0.4585302607, 0.6260990337, 0.7394254526,
70                  0.8221921916, 0.8838834765, 0.9295160031, 0.9621590305, 0.9838699101,
71                  0.9961174630, 1.0000000000, 1.0000000000});
72          checkCumulative(0.5, 4.0,
73                  x, new double[]{
74                  0.0000000000, 0.0000000000, 0.6266250826, 0.8049844719, 0.8987784842,
75                  0.9502644369, 0.9777960959, 0.9914837366, 0.9974556254, 0.9995223859,
76                  0.9999714889, 1.0000000000, 1.0000000000});
77          checkCumulative(1.0, 0.1,
78                  x, new double[]{
79                  0.00000000000, 0.00000000000, 0.01048074179, 0.02206723146,
80                  0.03503890488, 0.04979978349, 0.06696700846, 0.08755646344,
81                  0.11343184943, 0.14866007748, 0.20567176528, 1.00000000000, 1.00000000000});
82          checkCumulative(1.0, 0.5,
83                  x, new double[]{
84                  0.00000000000, 0.00000000000, 0.05131670195, 0.10557280900,
85                  0.16333997347, 0.22540333076, 0.29289321881, 0.36754446797,
86                  0.45227744249, 0.55278640450, 0.68377223398, 1.00000000000, 1.00000000000});
87          checkCumulative(1, 1,
88                  x, new double[]{
89                  0.0, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.0});
90          checkCumulative(1, 2,
91                  x, new double[]{
92                  0.00, 0.00, 0.19, 0.36, 0.51, 0.64, 0.75, 0.84, 0.91, 0.96, 0.99, 1.00, 1.00});
93          checkCumulative(1, 4,
94                  x, new double[]{
95                  0.0000, 0.0000, 0.3439, 0.5904, 0.7599, 0.8704, 0.9375, 0.9744, 0.9919,
96                  0.9984, 0.9999, 1.0000, 1.0000});
97          checkCumulative(2.0, 0.1,
98                  x, new double[]{
99                  0.0000000000000, 0.0000000000000, 0.0005855492117, 0.0025085760862,
100                 0.0060900720266, 0.0117917748341, 0.0203153588864, 0.0328098512512,
101                 0.0513720788952, 0.0805528836776, 0.1341822241505, 1.0000000000000, 1.0000000000000});
102         checkCumulative(2, 1,
103                 x, new double[]{
104                 0.00, 0.00, 0.01, 0.04, 0.09, 0.16, 0.25, 0.36, 0.49, 0.64, 0.81, 1.00, 1.00});
105         checkCumulative(2.0, 0.5,
106                 x, new double[]{
107                 0.000000000000, 0.000000000000, 0.003882537047, 0.016130089900,
108                 0.037840969486, 0.070483996910, 0.116116523517, 0.177807808356,
109                 0.260574547368, 0.373900966300, 0.541469739276, 1.000000000000, 1.000000000000});
110         checkCumulative(2, 2,
111                 x, new double[]{
112                 0.000, 0.000, 0.028, 0.104, 0.216, 0.352, 0.500, 0.648, 0.784, 0.896, 0.972, 1.000, 1.000});
113         checkCumulative(2, 4,
114                 x, new double[]{
115                 0.00000, 0.00000, 0.08146, 0.26272, 0.47178, 0.66304, 0.81250, 0.91296,
116                 0.96922, 0.99328, 0.99954, 1.00000, 1.00000});
117         checkCumulative(4.0, 0.1,
118                 x, new double[]{
119                 0.000000000e+00, 0.000000000e+00, 3.217128269e-06, 5.592070271e-05,
120                 3.104375474e-04, 1.088719595e-03, 2.995933981e-03, 7.155588777e-03,
121                 1.577149153e-02, 3.380410585e-02, 7.650088789e-02, 1.000000000e+00, 1.000000000e+00});
122         checkCumulative(4.0, 0.5,
123                 x, new double[]{
124                 0.000000000e+00, 0.000000000e+00, 2.851114863e-05, 4.776140576e-04,
125                 2.544374616e-03, 8.516263371e-03, 2.220390414e-02, 4.973556312e-02,
126                 1.012215158e-01, 1.950155281e-01, 3.733749174e-01, 1.000000000e+00, 1.000000000e+00});
127         checkCumulative(4, 1,
128                 x, new double[]{
129                 0.0000, 0.0000, 0.0001, 0.0016, 0.0081, 0.0256, 0.0625, 0.1296, 0.2401,
130                 0.4096, 0.6561, 1.0000, 1.0000});
131         checkCumulative(4, 2,
132                 x, new double[]{
133                 0.00000, 0.00000, 0.00046, 0.00672, 0.03078, 0.08704, 0.18750, 0.33696,
134                 0.52822, 0.73728, 0.91854, 1.00000, 1.00000});
135         checkCumulative(4, 4,
136                 x, new double[]{
137                 0.000000, 0.000000, 0.002728, 0.033344, 0.126036, 0.289792, 0.500000,
138                 0.710208, 0.873964, 0.966656, 0.997272, 1.000000, 1.000000});
139 
140     }
141 
142     private void checkCumulative(double alpha, double beta, double[] x, double[] cumes) throws MathException {
143         BetaDistribution d = new BetaDistributionImpl(alpha, beta);
144         for (int i = 0; i < x.length; i++) {
145             assertEquals(cumes[i], d.cumulativeProbability(x[i]), 1e-8);
146         }
147 
148         for (int i = 1; i < x.length - 1; i++) {
149             assertEquals(x[i], d.inverseCumulativeProbability(cumes[i]), 1e-5);
150         }
151     }
152 
153     public void testDensity() throws MathException {
154         double[] x = new double[]{1e-6, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
155         checkDensity(0.1, 0.1,
156                 x, new double[]{
157                 12741.2357380649, 0.4429889586665234, 2.639378715e-01, 2.066393611e-01,
158                 1.832401831e-01, 1.766302780e-01, 1.832404579e-01, 2.066400696e-01,
159                 2.639396531e-01, 4.429925026e-01});
160         checkDensity(0.1, 0.5,
161                 x, new double[]{
162                 2.218377102e+04, 7.394524202e-01, 4.203020268e-01, 3.119435533e-01,
163                 2.600787829e-01, 2.330648626e-01, 2.211408259e-01, 2.222728708e-01,
164                 2.414013907e-01, 3.070567405e-01});
165         checkDensity(0.1, 1.0,
166                 x, new double[]{
167                 2.511886432e+04, 7.943210858e-01, 4.256680458e-01, 2.955218303e-01,
168                 2.281103709e-01, 1.866062624e-01, 1.583664652e-01, 1.378514078e-01,
169                 1.222414585e-01, 1.099464743e-01});
170         checkDensity(0.1, 2.0,
171                 x, new double[]{
172                 2.763072312e+04, 7.863770012e-01, 3.745874120e-01, 2.275514842e-01,
173                 1.505525939e-01, 1.026332391e-01, 6.968107049e-02, 4.549081293e-02,
174                 2.689298641e-02, 1.209399123e-02});
175         checkDensity(0.1, 4.0,
176                 x, new double[]{
177                 2.997927462e+04, 6.911058917e-01, 2.601128486e-01, 1.209774010e-01,
178                 5.880564714e-02, 2.783915474e-02, 1.209657335e-02, 4.442148268e-03,
179                 1.167143939e-03, 1.312171805e-04});
180         checkDensity(0.5, 0.1,
181                 x, new double[]{
182                 88.3152184726, 0.3070542841, 0.2414007269, 0.2222727015,
183                 0.2211409364, 0.2330652355, 0.2600795198, 0.3119449793,
184                 0.4203052841, 0.7394649088});
185         checkDensity(0.5, 0.5,
186                 x, new double[]{
187                 318.3100453389, 1.0610282383, 0.7957732234, 0.6946084565,
188                 0.6497470636, 0.6366197724, 0.6497476051, 0.6946097796,
189                 0.7957762075, 1.0610376697});
190         checkDensity(0.5, 1.0,
191                 x, new double[]{
192                 500.0000000000, 1.5811309244, 1.1180311937, 0.9128694077,
193                 0.7905684268, 0.7071060741, 0.6454966865, 0.5976138778,
194                 0.5590166450, 0.5270459839});
195         checkDensity(0.5, 2.0,
196                 x, new double[]{
197                 749.99925000000, 2.134537420613655, 1.34163575536, 0.95851150881,
198                 0.71151039830, 0.53032849490, 0.38729704363, 0.26892534859,
199                 0.16770415497, 0.07905610701});
200         checkDensity(0.5, 4.0,
201                 x, new double[]{
202                 1.093746719e+03, 2.52142232809988, 1.252190241e+00, 6.849343920e-01,
203                 3.735417140e-01, 1.933481570e-01, 9.036885833e-02, 3.529621669e-02,
204                 9.782644546e-03, 1.152878503e-03});
205         checkDensity(1.0, 0.1,
206                 x, new double[]{
207                 0.1000000900, 0.1099466942, 0.1222417336, 0.1378517623, 0.1583669403,
208                 0.1866069342, 0.2281113974, 0.2955236034, 0.4256718768,
209                 0.7943353837});
210         checkDensity(1.0, 0.5,
211                 x, new double[]{
212                 0.5000002500, 0.5270465695, 0.5590173438, 0.5976147315, 0.6454977623,
213                 0.7071074883, 0.7905704033, 0.9128724506,
214                 1.1180367838, 1.5811467358});
215         checkDensity(1, 1,
216                 x, new double[]{
217                 1, 1, 1,
218                 1, 1, 1, 1,
219                 1, 1, 1});
220         checkDensity(1, 2,
221                 x, new double[]{
222                 1.999998, 1.799998, 1.599998, 1.399998, 1.199998, 0.999998, 0.799998,
223                 0.599998, 0.399998,
224                 0.199998});
225         checkDensity(1, 4,
226                 x, new double[]{
227                 3.999988000012, 2.915990280011, 2.047992320010, 1.371994120008,
228                 0.863995680007, 0.499997000006, 0.255998080005, 0.107998920004,
229                 0.031999520002, 0.003999880001});
230         checkDensity(2.0, 0.1,
231                 x, new double[]{
232                 1.100000990e-07, 1.209425730e-02, 2.689331586e-02, 4.549123318e-02,
233                 6.968162794e-02, 1.026340191e-01, 1.505537732e-01, 2.275534997e-01,
234                 3.745917198e-01, 7.863929037e-01});
235         checkDensity(2.0, 0.5,
236                 x, new double[]{
237                 7.500003750e-07, 7.905777599e-02, 1.677060417e-01, 2.689275256e-01,
238                 3.872996256e-01, 5.303316769e-01, 7.115145488e-01, 9.585174425e-01,
239                 1.341645818e+00, 2.134537420613655});
240         checkDensity(2, 1,
241                 x, new double[]{
242                 0.000002, 0.200002, 0.400002, 0.600002, 0.800002, 1.000002, 1.200002,
243                 1.400002, 1.600002,
244                 1.800002});
245         checkDensity(2, 2,
246                 x, new double[]{
247                 5.9999940e-06, 5.4000480e-01, 9.6000360e-01, 1.2600024e+00,
248                 1.4400012e+00, 1.5000000e+00, 1.4399988e+00, 1.2599976e+00,
249                 9.5999640e-01, 5.3999520e-01});
250         checkDensity(2, 4,
251                 x, new double[]{
252                 0.00001999994, 1.45800971996, 2.04800255997, 2.05799803998,
253                 1.72799567999, 1.24999500000, 0.76799552000, 0.37799676001,
254                 0.12799824001, 0.01799948000});
255         checkDensity(4.0, 0.1,
256                 x, new double[]{
257                 1.193501074e-19, 1.312253162e-04, 1.167181580e-03, 4.442248535e-03,
258                 1.209679109e-02, 2.783958903e-02, 5.880649983e-02, 1.209791638e-01,
259                 2.601171405e-01, 6.911229392e-01});
260         checkDensity(4.0, 0.5,
261                 x, new double[]{
262                 1.093750547e-18, 1.152948959e-03, 9.782950259e-03, 3.529697305e-02,
263                 9.037036449e-02, 1.933508639e-01, 3.735463833e-01, 6.849425461e-01,
264                 1.252205894e+00, 2.52142232809988});
265         checkDensity(4, 1,
266                 x, new double[]{
267                 4.000000000e-18, 4.000120001e-03, 3.200048000e-02, 1.080010800e-01,
268                 2.560019200e-01, 5.000030000e-01, 8.640043200e-01, 1.372005880e+00,
269                 2.048007680e+00, 2.916009720e+00});
270         checkDensity(4, 2,
271                 x, new double[]{
272                 1.999998000e-17, 1.800052000e-02, 1.280017600e-01, 3.780032400e-01,
273                 7.680044800e-01, 1.250005000e+00, 1.728004320e+00, 2.058001960e+00,
274                 2.047997440e+00, 1.457990280e+00});
275         checkDensity(4, 4,
276                 x, new double[]{
277                 1.399995800e-16, 1.020627216e-01, 5.734464512e-01, 1.296547409e+00,
278                 1.935364838e+00, 2.187500000e+00, 1.935355162e+00, 1.296532591e+00,
279                 5.734335488e-01, 1.020572784e-01});
280 
281     }
282 
283     private void checkDensity(double alpha, double beta, double[] x, double[] expected) throws MathException {
284         BetaDistribution d = new BetaDistributionImpl(alpha, beta);
285         for (int i = 0; i < x.length; i++) {
286             assertEquals(String.format("density at x=%.1f for alpha=%.1f, beta=%.1f", x[i], alpha, beta), expected[i], d.density(x[i]), 1e-5);
287         }
288     }
289 }