-
Notifications
You must be signed in to change notification settings - Fork 0
/
Distribution.java
525 lines (457 loc) · 16.5 KB
/
Distribution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package statlib;
import java.util.Arrays;
import java.util.Vector;
public interface Distribution {
public interface WeightedLazyFit {
public void Initialize();
public Distribution Finalize();
public void AddPoint(double weight, double point);
}
public interface WeightedLazyFitFactory {
public WeightedLazyFit CreateInstance();
}
public static class TruncatedDistribution implements Distribution {
private Double min, max;
private boolean redistribute;
private double minFreq, maxFreq;
private Vector moments;
private Distribution srcDist;
public TruncatedDistribution(Distribution srcDist, Double min, Double max,
boolean redistribute)
{
this.srcDist = srcDist;
this.min = min;
this.max = max;
this.redistribute = redistribute;
minFreq = srcDist.getCumulativeProb(min.doubleValue()).doubleValue();
maxFreq = 1 - srcDist.getCumulativeProb(max.doubleValue()).doubleValue();
moments = new Vector();
}
public Double getCumulativeProb(double v) {
double m, M;
m = (min == null) ? Double.NEGATIVE_INFINITY : min.doubleValue();
M = (max == null) ? Double.POSITIVE_INFINITY : max.doubleValue();
if(v < m)
return new Double(0.0);
else if(v > M)
return new Double(1.0);
else if(redistribute)
return new Double((srcDist.getCumulativeProb(v).doubleValue() - minFreq)
/ (1 - maxFreq - minFreq));
else
return srcDist.getCumulativeProb(v);
}
/**
* Get the value probabilility function (p.d.f.) evaluated at the given point.
*/
public Double getProbability(double v) {
double m, M;
m = (min == null) ? Double.NEGATIVE_INFINITY : min.doubleValue();
M = (max == null) ? Double.POSITIVE_INFINITY : max.doubleValue();
if(v < m || v > M)
return new Double(0);
else if(!redistribute)
return srcDist.getProbability(v);
else {
Double d = srcDist.getProbability(v);
if(d != null)
return new Double(d.doubleValue() / (1 - maxFreq - minFreq));
else
return null;
}
}
/** Get the specified central moment from the distribution.
*/
public Double getCentralMoment(int i) {
return null;
}
/** Get an instance string for the distribution (this would be for example
* "Normal(0, 1)" for the standard normal distribution).
*/
public String getDistributionInstance() {
return "Truncated(" + min + ", " + max + ") "
+ srcDist.getDistributionInstance();
}
/** Get the standard name of the distribution.
*/
public String getDistributionName() {
return "Truncated " + srcDist.getDistributionName();
}
/** Get the mean of the distribution.
*/
public Double getMean() {
return getRawMoment(1);
}
/** Get the number of parameters required by the distribution.
*/
public int getParameterCount() {
return srcDist.getParameterCount();
}
/** Get the name of the ith parameter required by the distribution.
*/
public String getParameterName(int i) {
return srcDist.getParameterName(i);
}
/** Get the allowed parameter value ranges for the ith parameter.
*/
public Double[] getParameterRange(int i) {
return srcDist.getParameterRange(i);
}
/** Get the value of the ith parameter of this instance of the distribution.
*/
public double getParameterValue(int i) {
return srcDist.getParameterValue(i);
}
/** Get the parameter values of this instance of the distribution.
*/
public double[] getParameterValues() {
return srcDist.getParameterValues();
}
/** Get the specified raw moment from the distribution.
*/
public Double getRawMoment(int i) {
int sz = moments.size();
if(sz >= i)
return (Double)moments.get(i-1);
else {
for(int r=sz; r < i; r++) {
double m = srcDist.getLimitedRawMoment(i, min, max).doubleValue();
if(redistribute) m /= (1 - maxFreq - minFreq);
moments.add(new Double(m));
}
return (Double)moments.get(i-1);
}
}
/** Get the standard deviation of the distribution.
*/
public Double getStdDev() {
Double m = getRawMoment(1);
Double m2 = getRawMoment(2);
if(m == null || m2 == null) return null;
double mv = m.doubleValue();
return new Double(m2.doubleValue() - mv * mv);
}
/** Sample the distribution at the points specified.
*/
public Distribution sample(double[] points) {
Distribution rv = srcDist.sample(points);
if(rv != null)
return rv.truncate(min, max, redistribute);
else
return null;
}
/** Sample the distribution at evenly spaced intervals, using the min value
* and max value specified to make the correct number of buckets.
*/
public Distribution sampleBuckets(double minValue, double maxValue,
int nBuckets)
{
Distribution rv = srcDist.sampleBuckets(minValue, maxValue, nBuckets);
if(rv != null)
return rv.truncate(min, max, redistribute);
else
return null;
}
/** Sample the distribution at evenly spaced intervals.
*/
public Distribution sampleStepped(double minValue, double stepSize,
int nSteps)
{
Distribution rv = srcDist.sampleStepped(minValue, stepSize, nSteps);
if(rv != null)
return rv.truncate(min, max, redistribute);
else
return null;
}
/** Generate random sample from distribution (using Uniform random number
* generator supplied, or java built-in (Math.random()) if null supplied).
* @param n The number of values to be simulated (optimized for > 1)
* @param rand The random number generator to use (if non-null) or use
* Math.random() if not supplied (null).
* @return An array of length n with the simulated values */
public double[] simulateValues(int n, IUniformRandom rand) {
double m, M;
m = (min == null) ? Double.NEGATIVE_INFINITY : min.doubleValue();
M = (max == null) ? Double.POSITIVE_INFINITY : max.doubleValue();
double[] rv = srcDist.simulateValues(n, rand);
if(rv != null)
if(redistribute)
for(int i=0; i<rv.length; i++)
while(rv[i] < m || rv[i] > M)
rv[i] = srcDist.simulateValues(1, rand)[0];
else
for(int i=0; i<rv.length; i++)
if(rv[i] < m)
rv[i] = m;
else if(rv[i] > M)
rv[i] = M;
return rv;
}
/** Truncate the distribution using the min and max values specified (pass null
* for unbounded).
* @param minValue the minimum value to use for truncation (or null for none)
* @param maxValue the maximum value to use for truncation (or null for none)
* @param redistribute if true, redistribute the probability on the truncated
* tails to the distribution evenly (as if these
* observations aren't possible), otherwise allocate the
* probability to the tails as point masses.
* @return Null if the distribution does not have a truncated form
* (or it's not implemented).
*/
public Distribution truncate(Double minValue, Double maxValue,
boolean redistribute)
{
return new TruncatedDistribution(this, minValue, maxValue, redistribute);
}
/** Get the mth raw moment of the distribution limited to the range
* between a and b.
*/
public Double getLimitedRawMoment(int m, Double a, Double b) {
return null;
}
/** Get the first m raw moments of the distribution limited to the range
* between a and b.
*/
public Double[] getLimitedRawMoments(int m, Double a, Double b) {
return null;
}
/** Get the mth central moment of the distribution limited to the range
* between a and b
*/
public Double[] getLimitedCentralMoments(int m, Double a, Double b) {
return null;
}
/** Get the mth central moment of the distribution limited to the range
* between a and b
*/
public Double getLimitedCentralMoment(int m, Double a, Double b) {
return null;
}
/** Convolve the distribution N times with itself (if implemented)
*/
public Distribution convolve(int N) {
return null;
}
/** Calculate the quantile of the distribution at probability p
*/
public Double getQuantile(double p) {
return null;
}
}
public interface IUniformRandom {
public void setSeed(double seed);
public double getNext();
public void reset();
}
/**
* This is a class that implements some functionality usable for most
* distributions in a generic way, so as to limit code duplication.
**/
public static class StdImpl {
/**
* A standard implementation of the IUniformRandom interface that uses Java's
* Math.random() to generate the random numbers
**/
private static class JavaUniformRandom implements IUniformRandom {
public void setSeed(double seed) {
// this can't be implemented for java.Math
}
public double getNext() {
return Math.random();
}
public void reset() {
// this can't be implemented for java.Math
}
}
/**
* The Java standard Math.random() based random number generator for use
* in calls to simulateValues()
**/
public static IUniformRandom rand = new JavaUniformRandom();
/**
* A standard implementation of the sample() method for all distributions
* to use in lieu of a special method for doing sampling.
**/
public static Distribution sample(Distribution src, double[] points) {
int nSteps = points.length;
double[] prob = new double[nSteps];
double cumprob = 0.0;
double[] mypoints = (double[])points.clone();
Arrays.sort(mypoints);
for(int i=0; i<nSteps-1; i++) {
prob[i] = src.getCumulativeProb(points[i]).doubleValue() - cumprob;
cumprob += prob[i];
}
prob[nSteps-1] = 1 - cumprob;
return new FrequencyDist(mypoints, prob);
}
/**
* A standard implementation of the sampleStepped() method for all
* distributions to use in lieu of a special method for doing sampling.
**/
public static Distribution sampleStepped(Distribution src, double minValue,
double stepSize, int nSteps)
{
double pts[] = new double[nSteps];
double left = minValue;
for(int i=0; i<nSteps; i++, left += stepSize)
pts[i] = left;
return sample(src, pts);
}
/**
* A standard implementation of the sampleBuckets() method for all
* distributions to use in lieu of a special method for doing sampling.
**/
public static Distribution sampleBuckets(Distribution src, double minValue,
double maxValue, int nBuckets)
{
double sz = (maxValue - minValue) / (nBuckets - 1);
return sampleStepped(src, minValue, sz, nBuckets);
}
/**
* A standard implementation of the algorithm to convert from raw moments
* to central moments given a list of raw moments is stored up through
* the moment requested
**/
public static Double centralMoment(Vector moments, int n) {
// this is based on the formula relating raw moments to central moments
// see for example Abramowitz, Stegun: Handbook of Math. functions
// ISBN 0-486-61272-4 formula #26.1.14 (page 928).
if(moments.size() < n) return null;
double mean = ((Double)moments.get(0)).doubleValue();
double mul = 1.0, s = 0.0;
for(int k = 0; k < n; k++) {
s += mul * ((Double)moments.get(n - k - 1)).doubleValue();
mul *= -1.0 * mean * (n - k) / (k + 1.0);
}
s += mul;
return new Double(s);
}
/**
* Convolve the distribution specified by v with itself N times and return
* the result. The points implied in v must be equally spaced. The
* parameter v is the frequencies of the points and should be normalized.
**/
public static double[] convolve(double[] v, int N) {
return null;
}
}
/**
* Get the number of parameters required by the distribution.
*/
public int getParameterCount();
/**
* Get the name of the ith parameter required by the distribution.
*/
public String getParameterName(int i);
/**
* Get the allowed parameter value ranges for the ith parameter.
*/
public Double[] getParameterRange(int i);
/**
* Get the standard name of the distribution.
*/
public String getDistributionName();
/**
* Get an instance string for the distribution (this would be for example
* "Normal(0, 1)" for the standard normal distribution).
*/
public String getDistributionInstance();
/**
* Get the value of the ith parameter of this instance of the distribution.
*/
public double getParameterValue(int i);
/**
* Get the parameter values of this instance of the distribution.
*/
public double[] getParameterValues();
/**
* Truncate the distribution using the min and max values specified (pass null
* for unbounded).
* @param minValue the minimum value to use for truncation (or null for none)
* @param maxValue the maximum value to use for truncation (or null for none)
* @param redistribute if true, redistribute the probability on the truncated
* tails to the distribution evenly (as if these
* observations aren't possible), otherwise allocate the
* probability to the tails as point masses.
* @return Null if the distribution does not have a truncated form
* (or it's not implemented).
*/
public Distribution truncate(Double minValue, Double maxValue,
boolean redistribute);
/**
* Sample the distribution at evenly spaced intervals.
*/
public Distribution sampleStepped(double minValue, double stepSize,
int nSteps);
/**
* Sample the distribution at evenly spaced intervals, using the min value
* and max value specified to make the correct number of buckets.
*/
public Distribution sampleBuckets(double minValue, double maxValue,
int nBuckets);
/**
* Sample the distribution at the points specified.
*/
public Distribution sample(double[] points);
/**
* Get the probability that a variable with this distribution lies below or
* equal to the specified value.
*/
public Double getCumulativeProb(double v);
/**
* Get the value probabilility function (p.d.f.) evaluated at the given point.
*/
public Double getProbability(double v);
/**
* Get the mean of the distribution.
*/
public Double getMean();
/**
* Get the standard deviation of the distribution.
*/
public Double getStdDev();
/**
* Get the specified raw moment from the distribution.
*/
public Double getRawMoment(int i);
/**
* Get the specified central moment from the distribution.
*/
public Double getCentralMoment(int i);
/** Generate random sample from distribution (using Uniform random number
* generator supplied, or java built-in (Math.random()) if null supplied).
* @param n The number of values to be simulated (optimized for > 1)
* @param rand The random number generator to use (if non-null) or use
* Math.random() if not supplied (null).
* @return An array of length n with the simulated values */
public double[] simulateValues(int n, IUniformRandom rand);
/**
* Get the first m raw moments of the distribution limited to the range
* between a and b.
*/
public Double[] getLimitedRawMoments(int m, Double a, Double b);
/**
* Get the mth raw moment of the distribution limited to the range
* between a and b.
*/
public Double getLimitedRawMoment(int m, Double a, Double b);
/**
* Get the first m central moments of the distribution limited to the range
* between a and b
*/
public Double[] getLimitedCentralMoments(int m, Double a, Double b);
/**
* Get the mth central moment of the distribution limited to the range
* between a and b
*/
public Double getLimitedCentralMoment(int m, Double a, Double b);
/**
* Convolve the distribution N times with itself (if implemented)
*/
public Distribution convolve(int N);
/**
* Calculate the quantile of the distribution at probability p
*/
public Double getQuantile(double p);
}