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