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.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.special.Gamma;
28 import org.hipparchus.util.FastMath;
29 import org.hipparchus.util.MathUtils;
30
31
32
33
34
35
36
37
38
39
40 public class WeibullDistribution extends AbstractRealDistribution {
41
42 private static final long serialVersionUID = 20160320L;
43
44 private final double shape;
45
46 private final double scale;
47
48
49
50
51
52
53
54
55 public WeibullDistribution(double alpha, double beta)
56 throws MathIllegalArgumentException {
57 if (alpha <= 0) {
58 throw new MathIllegalArgumentException(LocalizedCoreFormats.SHAPE,
59 alpha);
60 }
61 if (beta <= 0) {
62 throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE,
63 beta);
64 }
65 scale = beta;
66 shape = alpha;
67 }
68
69
70
71
72
73
74 public double getShape() {
75 return shape;
76 }
77
78
79
80
81
82
83 public double getScale() {
84 return scale;
85 }
86
87
88 @Override
89 public double density(double x) {
90 if (x < 0) {
91 return 0;
92 }
93
94 final double xscale = x / scale;
95 final double xscalepow = FastMath.pow(xscale, shape - 1);
96
97
98
99
100
101
102 final double xscalepowshape = xscalepow * xscale;
103
104 return (shape / scale) * xscalepow * FastMath.exp(-xscalepowshape);
105 }
106
107
108 @Override
109 public double logDensity(double x) {
110 if (x < 0) {
111 return Double.NEGATIVE_INFINITY;
112 }
113
114 final double xscale = x / scale;
115 final double logxscalepow = FastMath.log(xscale) * (shape - 1);
116
117
118
119
120
121
122 final double xscalepowshape = FastMath.exp(logxscalepow) * xscale;
123
124 return FastMath.log(shape / scale) + logxscalepow - xscalepowshape;
125 }
126
127
128 @Override
129 public double cumulativeProbability(double x) {
130 double ret;
131 if (x <= 0.0) {
132 ret = 0.0;
133 } else {
134 ret = 1.0 - FastMath.exp(-FastMath.pow(x / scale, shape));
135 }
136 return ret;
137 }
138
139
140
141
142
143
144
145 @Override
146 public double inverseCumulativeProbability(double p) {
147 MathUtils.checkRangeInclusive(p, 0, 1);
148
149 double ret;
150 if (p == 0) {
151 ret = 0.0;
152 } else if (p == 1) {
153 ret = Double.POSITIVE_INFINITY;
154 } else {
155 ret = scale * FastMath.pow(-FastMath.log1p(-p), 1.0 / shape);
156 }
157 return ret;
158 }
159
160
161
162
163
164
165
166 @Override
167 public double getNumericalMean() {
168 final double sh = getShape();
169 final double sc = getScale();
170
171 return sc * FastMath.exp(Gamma.logGamma(1 + (1 / sh)));
172 }
173
174
175
176
177
178
179
180 @Override
181 public double getNumericalVariance() {
182 final double sh = getShape();
183 final double sc = getScale();
184 final double mn = getNumericalMean();
185
186 return (sc * sc) * FastMath.exp(Gamma.logGamma(1 + (2 / sh))) -
187 (mn * mn);
188 }
189
190
191
192
193
194
195
196
197 @Override
198 public double getSupportLowerBound() {
199 return 0;
200 }
201
202
203
204
205
206
207
208
209
210
211 @Override
212 public double getSupportUpperBound() {
213 return Double.POSITIVE_INFINITY;
214 }
215
216
217
218
219
220
221
222
223 @Override
224 public boolean isSupportConnected() {
225 return true;
226 }
227 }
228