1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.statistics.distribution;
18
19 import org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.Test;
21
22
23
24
25
26 class LogisticDistributionTest extends BaseContinuousDistributionTest {
27 @Override
28 ContinuousDistribution makeDistribution(Object... parameters) {
29 final double location = (Double) parameters[0];
30 final double scale = (Double) parameters[1];
31 return LogisticDistribution.of(location, scale);
32 }
33
34 @Override
35 Object[][] makeInvalidParameters() {
36 return new Object[][] {
37 {0.0, 0.0},
38 {0.0, -0.1}
39 };
40 }
41
42 @Override
43 String[] getParameterNames() {
44 return new String[] {"Location", "Scale"};
45 }
46
47 @Override
48 protected double getRelativeTolerance() {
49 return 5e-15;
50 }
51
52
53
54 @Test
55 void testExtremeDensity() {
56 final LogisticDistribution dist = LogisticDistribution.of(0, 1.0);
57
58
59
60
61 final double x0 = -710;
62 Assertions.assertEquals(Double.POSITIVE_INFINITY, Math.exp(-x0));
63 Assertions.assertEquals(Double.NaN, Math.exp(-x0) / Math.pow(1 + Math.exp(-x0), 2.0));
64
65
66
67 final double[] x = {710, 720, 730, 740, 750};
68 final double[] values = {4.47628622567513e-309,
69 2.03223080241836e-313, 9.22631526816382e-318,
70 4.19955798965060e-322, 0.00000000000000e+000};
71 testDensity(dist, x, values, DoubleTolerances.equals());
72
73
74 for (final double xi : x) {
75 Assertions.assertEquals(dist.density(xi), dist.density(-xi), () -> Double.toString(xi));
76 }
77 }
78
79
80
81
82 @Test
83 void testExtremeLogDensity() {
84 final double scale = 2.5;
85 final LogisticDistribution dist = LogisticDistribution.of(0, scale);
86
87
88 final double x = 1e160;
89 Assertions.assertEquals(0.0, dist.density(x));
90
91 final double expected = -x / scale - 2 * Math.log1p(Math.exp(-x / scale));
92 Assertions.assertNotEquals(Double.NEGATIVE_INFINITY, expected, "Density is zero but log density should not be -infinity");
93 Assertions.assertEquals(expected, dist.logDensity(x), Math.abs(expected) * 1e-15);
94 }
95 }