1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.statistics.distribution;
19
20 import java.util.stream.Stream;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.params.ParameterizedTest;
23 import org.junit.jupiter.params.provider.Arguments;
24 import org.junit.jupiter.params.provider.MethodSource;
25
26
27
28
29
30 class FoldedNormalDistributionTest extends BaseContinuousDistributionTest {
31 @Override
32 ContinuousDistribution makeDistribution(Object... parameters) {
33 final double mu = (Double) parameters[0];
34 final double sigma = (Double) parameters[1];
35 return FoldedNormalDistribution.of(mu, sigma);
36 }
37
38 @Override
39 Object[][] makeInvalidParameters() {
40 return new Object[][] {
41 {0.0, 0.0},
42 {0.0, -0.1}
43 };
44 }
45
46 @Override
47 String[] getParameterNames() {
48 return new String[] {"Mu", "Sigma"};
49 }
50
51 @Override
52 protected double getRelativeTolerance() {
53 return 5e-15;
54 }
55
56
57
58
59
60
61
62
63
64 @ParameterizedTest
65 @MethodSource
66 void testMean(double mu, double sigma) {
67
68
69 final TruncatedNormalDistribution t1 = TruncatedNormalDistribution.of(mu, sigma, Double.NEGATIVE_INFINITY, 0);
70 final TruncatedNormalDistribution t2 = TruncatedNormalDistribution.of(mu, sigma, 0, Double.POSITIVE_INFINITY);
71 final NormalDistribution n = NormalDistribution.of(mu, sigma);
72 final double p1 = n.cumulativeProbability(0);
73 final double p2 = 1 - p1;
74 final double expected = p2 * t2.getMean() - p1 * t1.getMean();
75 TestUtils.assertEquals(expected, FoldedNormalDistribution.of(mu, sigma).getMean(),
76 DoubleTolerances.relative(1e-14));
77 }
78
79 static Stream<Arguments> testMean() {
80 final Stream.Builder<Arguments> builder = Stream.builder();
81 for (final double mu : new double[] {-3, -2, -1, 0, 1, 2, 3}) {
82 for (final double sigma : new double[] {0.75, 1, 1.5}) {
83 builder.add(Arguments.of(mu, sigma));
84 }
85 }
86 return builder.build();
87 }
88
89 @Test
90 void testCumulativeProbabilityExtremes() {
91
92 testCumulativeProbability(FoldedNormalDistribution.of(1, 0.0001),
93 new double[] {0, 10},
94 new double[] {0, 1.0},
95 DoubleTolerances.equals());
96 }
97
98 @Test
99 void testSurvivalProbabilityExtremes() {
100
101 testSurvivalProbability(FoldedNormalDistribution.of(1, 0.0001),
102 new double[] {0, 10},
103 new double[] {1.0, 0.0},
104 DoubleTolerances.equals());
105 }
106 }