1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
33
34 public class WeibullDistributionTest extends RealDistributionAbstractTest {
35
36
37
38
39 @Override
40 public WeibullDistribution makeDistribution() {
41 return new WeibullDistribution(1.2, 2.1);
42 }
43
44
45 @Override
46 public double[] makeCumulativeTestPoints() {
47
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
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
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
66
67 @Test
68 public void testInverseCumulativeProbabilitySmallPAccuracy() {
69 WeibullDistribution dist = new WeibullDistribution(2, 3);
70 double t = dist.inverseCumulativeProbability(1e-17);
71
72
73
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
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
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
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 }