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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.distribution.continuous;
24  
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.special.Gamma;
27  import org.hipparchus.util.FastMath;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.fail;
32  
33  /**
34   * Test cases for WeibullDistribution.
35   */
36  public class WeibullDistributionTest extends RealDistributionAbstractTest {
37  
38      //-------------- Implementations for abstract methods -----------------------
39  
40      /** Creates the default continuous distribution instance to use in tests. */
41      @Override
42      public WeibullDistribution makeDistribution() {
43          return new WeibullDistribution(1.2, 2.1);
44      }
45  
46      /** Creates the default cumulative probability distribution test input values */
47      @Override
48      public double[] makeCumulativeTestPoints() {
49          // quantiles computed using R version 2.9.2
50          return new double[] {0.00664355180993, 0.0454328283309, 0.0981162737374, 0.176713524579, 0.321946865392,
51                  10.5115496887, 7.4976304671, 6.23205600701, 5.23968436955, 4.2079028257};
52      }
53  
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900};
58      }
59  
60      /** Creates the default probability density test expected values */
61      @Override
62      public double[] makeDensityTestValues() {
63          return new double[] {0.180535929306, 0.262801138133, 0.301905425199, 0.330899152971,
64            0.353441418887, 0.000788590320203, 0.00737060094841, 0.0177576041516, 0.0343043442574, 0.065664589369};
65      }
66  
67      //---------------------------- Additional test cases -------------------------
68  
69      @Test
70      void testInverseCumulativeProbabilitySmallPAccuracy() {
71          WeibullDistribution dist = new WeibullDistribution(2, 3);
72          double t = dist.inverseCumulativeProbability(1e-17);
73          // Analytically, answer is solution to 1e-17 = 1-exp(-(x/3)^2)
74          // x = sqrt(-9*log(1-1e-17))
75          // If we're not careful, answer will be 0. Answer below is computed with care in Octave:
76          assertEquals(9.48683298050514e-9, t, 1e-17);
77      }
78  
79      @Test
80      void testInverseCumulativeProbabilityExtremes() {
81          setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
82          setInverseCumulativeTestValues(
83                  new double[] {0.0, Double.POSITIVE_INFINITY});
84          verifyInverseCumulativeProbabilities();
85      }
86  
87      @Test
88      void testAlpha() {
89          WeibullDistribution dist = new WeibullDistribution(1, 2);
90          assertEquals(1, dist.getShape(), 0);
91          try {
92              new WeibullDistribution(0, 2);
93              fail("MathIllegalArgumentException expected");
94          } catch (MathIllegalArgumentException e) {
95              // Expected.
96          }
97      }
98  
99      @Test
100     void testBeta() {
101         WeibullDistribution dist = new WeibullDistribution(1, 2);
102         assertEquals(2, dist.getScale(), 0);
103         try {
104             new WeibullDistribution(1, 0);
105             fail("MathIllegalArgumentException expected");
106         } catch (MathIllegalArgumentException e) {
107             // Expected.
108         }
109     }
110 
111     @Test
112     void testMoments() {
113         final double tol = 1e-9;
114         WeibullDistribution dist;
115 
116         dist = new WeibullDistribution(2.5, 3.5);
117         // In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5)))
118         assertEquals(dist.getNumericalMean(), 3.5 * FastMath.exp(Gamma.logGamma(1 + (1 / 2.5))), tol);
119         assertEquals(dist.getNumericalVariance(), (3.5 * 3.5) *
120                 FastMath.exp(Gamma.logGamma(1 + (2 / 2.5))) -
121                 (dist.getNumericalMean() * dist.getNumericalMean()), tol);
122 
123         dist = new WeibullDistribution(10.4, 2.222);
124         assertEquals(dist.getNumericalMean(), 2.222 * FastMath.exp(Gamma.logGamma(1 + (1 / 10.4))), tol);
125         assertEquals(dist.getNumericalVariance(), (2.222 * 2.222) *
126                 FastMath.exp(Gamma.logGamma(1 + (2 / 10.4))) -
127                 (dist.getNumericalMean() * dist.getNumericalMean()), tol);
128     }
129 }