View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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    *      https://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.hipparchus.distribution.discrete;
18  
19  import org.junit.jupiter.api.Test;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  class SaddlePointExpansionTest {
24  
25      @Test
26      void testSmallInteger() {
27          for (int n = 4; n < 16; ++n) {
28               assertEquals(alternateStirlingErrorImplementation(n, 8),
29                                  SaddlePointExpansion.getStirlingError(n),
30                                  1.0e-10);
31          }
32      }
33  
34      @Test
35      void testSmallNonInteger() {
36          for (double z = 3.75; z < 14.8; z += 1.0) {
37              assertEquals(alternateStirlingErrorImplementation(z, 12),
38                                  SaddlePointExpansion.getStirlingError(z),
39                                  1.0e-10);
40          }
41      }
42  
43      @Test
44      void testLargeValues() {
45          for (double z = 15.25; z < 25.5; z += 1.0) {
46              assertEquals(alternateStirlingErrorImplementation(z, 21),
47                                  SaddlePointExpansion.getStirlingError(z),
48                                  1.0e-15);
49          }
50      }
51  
52      @Test
53      void testSpecialValues() {
54          assertEquals(0.0, SaddlePointExpansion.logBinomialProbability(0, 0, 0.6, 0.4), 1.0e-15);
55          assertEquals(Double.NEGATIVE_INFINITY, SaddlePointExpansion.logBinomialProbability(1, 0, 0.6, 0.4), 1.0e-15);
56      }
57  
58      private double alternateStirlingErrorImplementation(final double z, final int kMax) {
59          // numerators of coefficients in Stirling's expansion for log(Γ(z)) from https://oeis.org/A046968
60          final double[] A046968 = {
61              1.0, -1.0, 1.0, -1.0, 1.0, -691.0, 1.0,
62              -3617.0, 43867.0, -174611.0, 77683.0, -236364091.0, 657931.0, -3392780147.0,
63              1723168255201.0, -7709321041217.0, 151628697551.0, -26315271553053477373.0,
64              154210205991661.0, -261082718496449122051.0, 1520097643918070802691.0
65          };
66          // denominators of coefficients in Stirling's expansion for log(Γ(z)) from https://oeis.org/A046969
67          final double[] A046969 = {
68              12.0, 360.0, 1260.0, 1680.0, 1188.0, 360360.0, 156.0,
69              122400.0, 244188.0, 125400.0, 5796.0, 1506960.0, 300.0, 93960.0,
70              2492028.0, 505920.0, 396.0, 2418179400.0, 444.0, 21106800.0, 3109932.0
71          };
72          double zk   = z;
73          double expansion = 0.0;
74          for (int i = 0; i < kMax; ++i) {
75              expansion += A046968[i] / (zk * A046969[i]);
76              zk *= z * z;
77          }
78          return expansion;
79      }
80  }