View Javadoc
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    *      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  
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   * Test cases for {@link FoldedNormalDistribution}.
28   * Extends {@link BaseContinuousDistributionTest}. See javadoc of that class for details.
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      //-------------------- Additional test cases -------------------------------
57  
58      /**
59       * Test the mean. This is performed using the folding together of two truncated
60       * normal distributions, with the truncation at the origin.
61       *
62       * <p>This test cross-validates the mean computation.
63       */
64      @ParameterizedTest
65      @MethodSource
66      void testMean(double mu, double sigma) {
67          // Expected mean is the weighted means of each truncated distribution.
68          // The mean of the distribution below the origin must be negated.
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          // Use a small shape parameter so that we can exceed 40 * shape
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         // Use a small shape parameter so that we can exceed 40 * shape
101         testSurvivalProbability(FoldedNormalDistribution.of(1, 0.0001),
102                                 new double[] {0, 10},
103                                 new double[] {1.0, 0.0},
104                                 DoubleTolerances.equals());
105     }
106 }