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 * http://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 package org.apache.commons.math3.random;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.net.URL;
27 import java.nio.charset.Charset;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.math3.distribution.AbstractRealDistribution;
32 import org.apache.commons.math3.distribution.NormalDistribution;
33 import org.apache.commons.math3.distribution.RealDistribution;
34 import org.apache.commons.math3.exception.MathIllegalStateException;
35 import org.apache.commons.math3.exception.MathInternalError;
36 import org.apache.commons.math3.exception.NullArgumentException;
37 import org.apache.commons.math3.exception.OutOfRangeException;
38 import org.apache.commons.math3.exception.ZeroException;
39 import org.apache.commons.math3.exception.util.LocalizedFormats;
40 import org.apache.commons.math3.stat.descriptive.StatisticalSummary;
41 import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
42 import org.apache.commons.math3.util.FastMath;
43 import org.apache.commons.math3.util.MathUtils;
44
45 /**
46 * <p>Represents an <a href="http://http://en.wikipedia.org/wiki/Empirical_distribution_function">
47 * empirical probability distribution</a> -- a probability distribution derived
48 * from observed data without making any assumptions about the functional form
49 * of the population distribution that the data come from.</p>
50 *
51 * <p>An <code>EmpiricalDistribution</code> maintains data structures, called
52 * <i>distribution digests</i>, that describe empirical distributions and
53 * support the following operations: <ul>
54 * <li>loading the distribution from a file of observed data values</li>
55 * <li>dividing the input data into "bin ranges" and reporting bin frequency
56 * counts (data for histogram)</li>
57 * <li>reporting univariate statistics describing the full set of data values
58 * as well as the observations within each bin</li>
59 * <li>generating random values from the distribution</li>
60 * </ul>
61 * Applications can use <code>EmpiricalDistribution</code> to build grouped
62 * frequency histograms representing the input data or to generate random values
63 * "like" those in the input file -- i.e., the values generated will follow the
64 * distribution of the values in the file.</p>
65 *
66 * <p>The implementation uses what amounts to the
67 * <a href="http://nedwww.ipac.caltech.edu/level5/March02/Silverman/Silver2_6.html">
68 * Variable Kernel Method</a> with Gaussian smoothing:<p>
69 * <strong>Digesting the input file</strong>
70 * <ol><li>Pass the file once to compute min and max.</li>
71 * <li>Divide the range from min-max into <code>binCount</code> "bins."</li>
72 * <li>Pass the data file again, computing bin counts and univariate
73 * statistics (mean, std dev.) for each of the bins </li>
74 * <li>Divide the interval (0,1) into subintervals associated with the bins,
75 * with the length of a bin's subinterval proportional to its count.</li></ol>
76 * <strong>Generating random values from the distribution</strong><ol>
77 * <li>Generate a uniformly distributed value in (0,1) </li>
78 * <li>Select the subinterval to which the value belongs.
79 * <li>Generate a random Gaussian value with mean = mean of the associated
80 * bin and std dev = std dev of associated bin.</li></ol></p>
81 *
82 * <p>EmpiricalDistribution implements the {@link RealDistribution} interface
83 * as follows. Given x within the range of values in the dataset, let B
84 * be the bin containing x and let K be the within-bin kernel for B. Let P(B-)
85 * be the sum of the probabilities of the bins below B and let K(B) be the
86 * mass of B under K (i.e., the integral of the kernel density over B). Then
87 * set P(X < x) = P(B-) + P(B) * K(x) / K(B) where K(x) is the kernel distribution
88 * evaluated at x. This results in a cdf that matches the grouped frequency
89 * distribution at the bin endpoints and interpolates within bins using
90 * within-bin kernels.</p>
91 *
92 *<strong>USAGE NOTES:</strong><ul>
93 *<li>The <code>binCount</code> is set by default to 1000. A good rule of thumb
94 * is to set the bin count to approximately the length of the input file divided
95 * by 10. </li>
96 *<li>The input file <i>must</i> be a plain text file containing one valid numeric
97 * entry per line.</li>
98 * </ul></p>
99 *
100 * @version $Id: EmpiricalDistribution.java 1457372 2013-03-17 04:28:04Z psteitz $
101 */
102 public class EmpiricalDistribution extends AbstractRealDistribution {
103
104 /** Default bin count */
105 public static final int DEFAULT_BIN_COUNT = 1000;
106
107 /** Character set for file input */
108 private static final String FILE_CHARSET = "US-ASCII";
109
110 /** Serializable version identifier */
111 private static final long serialVersionUID = 5729073523949762654L;
112
113 /** RandomDataGenerator instance to use in repeated calls to getNext() */
114 protected final RandomDataGenerator randomData;
115
116 /** List of SummaryStatistics objects characterizing the bins */
117 private final List<SummaryStatistics> binStats;
118
119 /** Sample statistics */
120 private SummaryStatistics sampleStats = null;
121
122 /** Max loaded value */
123 private double max = Double.NEGATIVE_INFINITY;
124
125 /** Min loaded value */
126 private double min = Double.POSITIVE_INFINITY;
127
128 /** Grid size */
129 private double delta = 0d;
130
131 /** number of bins */
132 private final int binCount;
133
134 /** is the distribution loaded? */
135 private boolean loaded = false;
136
137 /** upper bounds of subintervals in (0,1) "belonging" to the bins */
138 private double[] upperBounds = null;
139
140 /**
141 * Creates a new EmpiricalDistribution with the default bin count.
142 */
143 public EmpiricalDistribution() {
144 this(DEFAULT_BIN_COUNT);
145 }
146
147 /**
148 * Creates a new EmpiricalDistribution with the specified bin count.
149 *
150 * @param binCount number of bins
151 */
152 public EmpiricalDistribution(int binCount) {
153 this(binCount, new RandomDataGenerator());
154 }
155
156 /**
157 * Creates a new EmpiricalDistribution with the specified bin count using the
158 * provided {@link RandomGenerator} as the source of random data.
159 *
160 * @param binCount number of bins
161 * @param generator random data generator (may be null, resulting in default JDK generator)
162 * @since 3.0
163 */
164 public EmpiricalDistribution(int binCount, RandomGenerator generator) {
165 this(binCount, new RandomDataGenerator(generator));
166 }
167
168 /**
169 * Creates a new EmpiricalDistribution with default bin count using the
170 * provided {@link RandomGenerator} as the source of random data.
171 *
172 * @param generator random data generator (may be null, resulting in default JDK generator)
173 * @since 3.0
174 */
175 public EmpiricalDistribution(RandomGenerator generator) {
176 this(DEFAULT_BIN_COUNT, generator);
177 }
178
179 /**
180 * Creates a new EmpiricalDistribution with the specified bin count using the
181 * provided {@link RandomDataImpl} instance as the source of random data.
182 *
183 * @param binCount number of bins
184 * @param randomData random data generator (may be null, resulting in default JDK generator)
185 * @since 3.0
186 * @deprecated As of 3.1. Please use {@link #EmpiricalDistribution(int,RandomGenerator)} instead.
187 */
188 @Deprecated
189 public EmpiricalDistribution(int binCount, RandomDataImpl randomData) {
190 this(binCount, randomData.getDelegate());
191 }
192
193 /**
194 * Creates a new EmpiricalDistribution with default bin count using the
195 * provided {@link RandomDataImpl} as the source of random data.
196 *
197 * @param randomData random data generator (may be null, resulting in default JDK generator)
198 * @since 3.0
199 * @deprecated As of 3.1. Please use {@link #EmpiricalDistribution(RandomGenerator)} instead.
200 */
201 @Deprecated
202 public EmpiricalDistribution(RandomDataImpl randomData) {
203 this(DEFAULT_BIN_COUNT, randomData);
204 }
205
206 /**
207 * Private constructor to allow lazy initialisation of the RNG contained
208 * in the {@link #randomData} instance variable.
209 *
210 * @param binCount number of bins
211 * @param randomData Random data generator.
212 */
213 private EmpiricalDistribution(int binCount,
214 RandomDataGenerator randomData) {
215 super(null);
216 this.binCount = binCount;
217 this.randomData = randomData;
218 binStats = new ArrayList<SummaryStatistics>();
219 }
220
221 /**
222 * Computes the empirical distribution from the provided
223 * array of numbers.
224 *
225 * @param in the input data array
226 * @exception NullArgumentException if in is null
227 */
228 public void load(double[] in) throws NullArgumentException {
229 DataAdapter da = new ArrayDataAdapter(in);
230 try {
231 da.computeStats();
232 // new adapter for the second pass
233 fillBinStats(new ArrayDataAdapter(in));
234 } catch (IOException ex) {
235 // Can't happen
236 throw new MathInternalError();
237 }
238 loaded = true;
239
240 }
241
242 /**
243 * Computes the empirical distribution using data read from a URL.
244 *
245 * <p>The input file <i>must</i> be an ASCII text file containing one
246 * valid numeric entry per line.</p>
247 *
248 * @param url url of the input file
249 *
250 * @throws IOException if an IO error occurs
251 * @throws NullArgumentException if url is null
252 * @throws ZeroException if URL contains no data
253 */
254 public void load(URL url) throws IOException, NullArgumentException, ZeroException {
255 MathUtils.checkNotNull(url);
256 Charset charset = Charset.forName(FILE_CHARSET);
257 BufferedReader in =
258 new BufferedReader(new InputStreamReader(url.openStream(), charset));
259 try {
260 DataAdapter da = new StreamDataAdapter(in);
261 da.computeStats();
262 if (sampleStats.getN() == 0) {
263 throw new ZeroException(LocalizedFormats.URL_CONTAINS_NO_DATA, url);
264 }
265 // new adapter for the second pass
266 in = new BufferedReader(new InputStreamReader(url.openStream(), charset));
267 fillBinStats(new StreamDataAdapter(in));
268 loaded = true;
269 } finally {
270 try {
271 in.close();
272 } catch (IOException ex) { //NOPMD
273 // ignore
274 }
275 }
276 }
277
278 /**
279 * Computes the empirical distribution from the input file.
280 *
281 * <p>The input file <i>must</i> be an ASCII text file containing one
282 * valid numeric entry per line.</p>
283 *
284 * @param file the input file
285 * @throws IOException if an IO error occurs
286 * @throws NullArgumentException if file is null
287 */
288 public void load(File file) throws IOException, NullArgumentException {
289 MathUtils.checkNotNull(file);
290 Charset charset = Charset.forName(FILE_CHARSET);
291 InputStream is = new FileInputStream(file);
292 BufferedReader in = new BufferedReader(new InputStreamReader(is, charset));
293 try {
294 DataAdapter da = new StreamDataAdapter(in);
295 da.computeStats();
296 // new adapter for second pass
297 is = new FileInputStream(file);
298 in = new BufferedReader(new InputStreamReader(is, charset));
299 fillBinStats(new StreamDataAdapter(in));
300 loaded = true;
301 } finally {
302 try {
303 in.close();
304 } catch (IOException ex) { //NOPMD
305 // ignore
306 }
307 }
308 }
309
310 /**
311 * Provides methods for computing <code>sampleStats</code> and
312 * <code>beanStats</code> abstracting the source of data.
313 */
314 private abstract class DataAdapter{
315
316 /**
317 * Compute bin stats.
318 *
319 * @throws IOException if an error occurs computing bin stats
320 */
321 public abstract void computeBinStats() throws IOException;
322
323 /**
324 * Compute sample statistics.
325 *
326 * @throws IOException if an error occurs computing sample stats
327 */
328 public abstract void computeStats() throws IOException;
329
330 }
331
332 /**
333 * <code>DataAdapter</code> for data provided through some input stream
334 */
335 private class StreamDataAdapter extends DataAdapter{
336
337 /** Input stream providing access to the data */
338 private BufferedReader inputStream;
339
340 /**
341 * Create a StreamDataAdapter from a BufferedReader
342 *
343 * @param in BufferedReader input stream
344 */
345 public StreamDataAdapter(BufferedReader in){
346 super();
347 inputStream = in;
348 }
349
350 /** {@inheritDoc} */
351 @Override
352 public void computeBinStats() throws IOException {
353 String str = null;
354 double val = 0.0d;
355 while ((str = inputStream.readLine()) != null) {
356 val = Double.parseDouble(str);
357 SummaryStatistics stats = binStats.get(findBin(val));
358 stats.addValue(val);
359 }
360
361 inputStream.close();
362 inputStream = null;
363 }
364
365 /** {@inheritDoc} */
366 @Override
367 public void computeStats() throws IOException {
368 String str = null;
369 double val = 0.0;
370 sampleStats = new SummaryStatistics();
371 while ((str = inputStream.readLine()) != null) {
372 val = Double.valueOf(str).doubleValue();
373 sampleStats.addValue(val);
374 }
375 inputStream.close();
376 inputStream = null;
377 }
378 }
379
380 /**
381 * <code>DataAdapter</code> for data provided as array of doubles.
382 */
383 private class ArrayDataAdapter extends DataAdapter {
384
385 /** Array of input data values */
386 private double[] inputArray;
387
388 /**
389 * Construct an ArrayDataAdapter from a double[] array
390 *
391 * @param in double[] array holding the data
392 * @throws NullArgumentException if in is null
393 */
394 public ArrayDataAdapter(double[] in) throws NullArgumentException {
395 super();
396 MathUtils.checkNotNull(in);
397 inputArray = in;
398 }
399
400 /** {@inheritDoc} */
401 @Override
402 public void computeStats() throws IOException {
403 sampleStats = new SummaryStatistics();
404 for (int i = 0; i < inputArray.length; i++) {
405 sampleStats.addValue(inputArray[i]);
406 }
407 }
408
409 /** {@inheritDoc} */
410 @Override
411 public void computeBinStats() throws IOException {
412 for (int i = 0; i < inputArray.length; i++) {
413 SummaryStatistics stats =
414 binStats.get(findBin(inputArray[i]));
415 stats.addValue(inputArray[i]);
416 }
417 }
418 }
419
420 /**
421 * Fills binStats array (second pass through data file).
422 *
423 * @param da object providing access to the data
424 * @throws IOException if an IO error occurs
425 */
426 private void fillBinStats(final DataAdapter da)
427 throws IOException {
428 // Set up grid
429 min = sampleStats.getMin();
430 max = sampleStats.getMax();
431 delta = (max - min)/(Double.valueOf(binCount)).doubleValue();
432
433 // Initialize binStats ArrayList
434 if (!binStats.isEmpty()) {
435 binStats.clear();
436 }
437 for (int i = 0; i < binCount; i++) {
438 SummaryStatistics stats = new SummaryStatistics();
439 binStats.add(i,stats);
440 }
441
442 // Filling data in binStats Array
443 da.computeBinStats();
444
445 // Assign upperBounds based on bin counts
446 upperBounds = new double[binCount];
447 upperBounds[0] =
448 ((double) binStats.get(0).getN()) / (double) sampleStats.getN();
449 for (int i = 1; i < binCount-1; i++) {
450 upperBounds[i] = upperBounds[i-1] +
451 ((double) binStats.get(i).getN()) / (double) sampleStats.getN();
452 }
453 upperBounds[binCount-1] = 1.0d;
454 }
455
456 /**
457 * Returns the index of the bin to which the given value belongs
458 *
459 * @param value the value whose bin we are trying to find
460 * @return the index of the bin containing the value
461 */
462 private int findBin(double value) {
463 return FastMath.min(
464 FastMath.max((int) FastMath.ceil((value - min) / delta) - 1, 0),
465 binCount - 1);
466 }
467
468 /**
469 * Generates a random value from this distribution.
470 * <strong>Preconditions:</strong><ul>
471 * <li>the distribution must be loaded before invoking this method</li></ul>
472 * @return the random value.
473 * @throws MathIllegalStateException if the distribution has not been loaded
474 */
475 public double getNextValue() throws MathIllegalStateException {
476
477 if (!loaded) {
478 throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED);
479 }
480
481 // Start with a uniformly distributed random number in (0,1)
482 final double x = randomData.nextUniform(0,1);
483
484 // Use this to select the bin and generate a Gaussian within the bin
485 for (int i = 0; i < binCount; i++) {
486 if (x <= upperBounds[i]) {
487 SummaryStatistics stats = binStats.get(i);
488 if (stats.getN() > 0) {
489 if (stats.getStandardDeviation() > 0) { // more than one obs
490 return getKernel(stats).sample();
491 } else {
492 return stats.getMean(); // only one obs in bin
493 }
494 }
495 }
496 }
497 throw new MathIllegalStateException(LocalizedFormats.NO_BIN_SELECTED);
498 }
499
500 /**
501 * Returns a {@link StatisticalSummary} describing this distribution.
502 * <strong>Preconditions:</strong><ul>
503 * <li>the distribution must be loaded before invoking this method</li></ul>
504 *
505 * @return the sample statistics
506 * @throws IllegalStateException if the distribution has not been loaded
507 */
508 public StatisticalSummary getSampleStats() {
509 return sampleStats;
510 }
511
512 /**
513 * Returns the number of bins.
514 *
515 * @return the number of bins.
516 */
517 public int getBinCount() {
518 return binCount;
519 }
520
521 /**
522 * Returns a List of {@link SummaryStatistics} instances containing
523 * statistics describing the values in each of the bins. The list is
524 * indexed on the bin number.
525 *
526 * @return List of bin statistics.
527 */
528 public List<SummaryStatistics> getBinStats() {
529 return binStats;
530 }
531
532 /**
533 * <p>Returns a fresh copy of the array of upper bounds for the bins.
534 * Bins are: <br/>
535 * [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],...,
536 * (upperBounds[binCount-2], upperBounds[binCount-1] = max].</p>
537 *
538 * <p>Note: In versions 1.0-2.0 of commons-math, this method
539 * incorrectly returned the array of probability generator upper
540 * bounds now returned by {@link #getGeneratorUpperBounds()}.</p>
541 *
542 * @return array of bin upper bounds
543 * @since 2.1
544 */
545 public double[] getUpperBounds() {
546 double[] binUpperBounds = new double[binCount];
547 for (int i = 0; i < binCount - 1; i++) {
548 binUpperBounds[i] = min + delta * (i + 1);
549 }
550 binUpperBounds[binCount - 1] = max;
551 return binUpperBounds;
552 }
553
554 /**
555 * <p>Returns a fresh copy of the array of upper bounds of the subintervals
556 * of [0,1] used in generating data from the empirical distribution.
557 * Subintervals correspond to bins with lengths proportional to bin counts.</p>
558 *
559 * <p>In versions 1.0-2.0 of commons-math, this array was (incorrectly) returned
560 * by {@link #getUpperBounds()}.</p>
561 *
562 * @since 2.1
563 * @return array of upper bounds of subintervals used in data generation
564 */
565 public double[] getGeneratorUpperBounds() {
566 int len = upperBounds.length;
567 double[] out = new double[len];
568 System.arraycopy(upperBounds, 0, out, 0, len);
569 return out;
570 }
571
572 /**
573 * Property indicating whether or not the distribution has been loaded.
574 *
575 * @return true if the distribution has been loaded
576 */
577 public boolean isLoaded() {
578 return loaded;
579 }
580
581 /**
582 * Reseeds the random number generator used by {@link #getNextValue()}.
583 *
584 * @param seed random generator seed
585 * @since 3.0
586 */
587 public void reSeed(long seed) {
588 randomData.reSeed(seed);
589 }
590
591 // Distribution methods ---------------------------
592
593 /**
594 * {@inheritDoc}
595 * @since 3.1
596 */
597 public double probability(double x) {
598 return 0;
599 }
600
601 /**
602 * {@inheritDoc}
603 *
604 * <p>Returns the kernel density normalized so that its integral over each bin
605 * equals the bin mass.</p>
606 *
607 * <p>Algorithm description: <ol>
608 * <li>Find the bin B that x belongs to.</li>
609 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
610 * integral of the kernel density over B).</li>
611 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
612 * and P(B) is the mass of B.</li></ol></p>
613 * @since 3.1
614 */
615 public double density(double x) {
616 if (x < min || x > max) {
617 return 0d;
618 }
619 final int binIndex = findBin(x);
620 final RealDistribution kernel = getKernel(binStats.get(binIndex));
621 return kernel.density(x) * pB(binIndex) / kB(binIndex);
622 }
623
624 /**
625 * {@inheritDoc}
626 *
627 * <p>Algorithm description:<ol>
628 * <li>Find the bin B that x belongs to.</li>
629 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
630 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
631 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
632 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
633 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
634 *
635 * @since 3.1
636 */
637 public double cumulativeProbability(double x) {
638 if (x < min) {
639 return 0d;
640 } else if (x >= max) {
641 return 1d;
642 }
643 final int binIndex = findBin(x);
644 final double pBminus = pBminus(binIndex);
645 final double pB = pB(binIndex);
646 final double[] binBounds = getUpperBounds();
647 final double kB = kB(binIndex);
648 final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
649 final RealDistribution kernel = k(x);
650 final double withinBinCum =
651 (kernel.cumulativeProbability(x) - kernel.cumulativeProbability(lower)) / kB;
652 return pBminus + pB * withinBinCum;
653 }
654
655 /**
656 * {@inheritDoc}
657 *
658 * <p>Algorithm description:<ol>
659 * <li>Find the smallest i such that the sum of the masses of the bins
660 * through i is at least p.</li>
661 * <li>
662 * Let K be the within-bin kernel distribution for bin i.</br>
663 * Let K(B) be the mass of B under K. <br/>
664 * Let K(B-) be K evaluated at the lower endpoint of B (the combined
665 * mass of the bins below B under K).<br/>
666 * Let P(B) be the probability of bin i.<br/>
667 * Let P(B-) be the sum of the bin masses below bin i. <br/>
668 * Let pCrit = p - P(B-)<br/>
669 * <li>Return the inverse of K evaluated at <br/>
670 * K(B-) + pCrit * K(B) / P(B) </li>
671 * </ol></p>
672 *
673 * @since 3.1
674 */
675 public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
676 if (p < 0.0 || p > 1.0) {
677 throw new OutOfRangeException(p, 0, 1);
678 }
679
680 if (p == 0.0) {
681 return getSupportLowerBound();
682 }
683
684 if (p == 1.0) {
685 return getSupportUpperBound();
686 }
687
688 int i = 0;
689 while (cumBinP(i) < p) {
690 i++;
691 }
692
693 final RealDistribution kernel = getKernel(binStats.get(i));
694 final double kB = kB(i);
695 final double[] binBounds = getUpperBounds();
696 final double lower = i == 0 ? min : binBounds[i - 1];
697 final double kBminus = kernel.cumulativeProbability(lower);
698 final double pB = pB(i);
699 final double pBminus = pBminus(i);
700 final double pCrit = p - pBminus;
701 if (pCrit <= 0) {
702 return lower;
703 }
704 return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
705 }
706
707 /**
708 * {@inheritDoc}
709 * @since 3.1
710 */
711 public double getNumericalMean() {
712 return sampleStats.getMean();
713 }
714
715 /**
716 * {@inheritDoc}
717 * @since 3.1
718 */
719 public double getNumericalVariance() {
720 return sampleStats.getVariance();
721 }
722
723 /**
724 * {@inheritDoc}
725 * @since 3.1
726 */
727 public double getSupportLowerBound() {
728 return min;
729 }
730
731 /**
732 * {@inheritDoc}
733 * @since 3.1
734 */
735 public double getSupportUpperBound() {
736 return max;
737 }
738
739 /**
740 * {@inheritDoc}
741 * @since 3.1
742 */
743 public boolean isSupportLowerBoundInclusive() {
744 return true;
745 }
746
747 /**
748 * {@inheritDoc}
749 * @since 3.1
750 */
751 public boolean isSupportUpperBoundInclusive() {
752 return true;
753 }
754
755 /**
756 * {@inheritDoc}
757 * @since 3.1
758 */
759 public boolean isSupportConnected() {
760 return true;
761 }
762
763 /**
764 * {@inheritDoc}
765 * @since 3.1
766 */
767 @Override
768 public double sample() {
769 return getNextValue();
770 }
771
772 /**
773 * {@inheritDoc}
774 * @since 3.1
775 */
776 @Override
777 public void reseedRandomGenerator(long seed) {
778 randomData.reSeed(seed);
779 }
780
781 /**
782 * The probability of bin i.
783 *
784 * @param i the index of the bin
785 * @return the probability that selection begins in bin i
786 */
787 private double pB(int i) {
788 return i == 0 ? upperBounds[0] :
789 upperBounds[i] - upperBounds[i - 1];
790 }
791
792 /**
793 * The combined probability of the bins up to but not including bin i.
794 *
795 * @param i the index of the bin
796 * @return the probability that selection begins in a bin below bin i.
797 */
798 private double pBminus(int i) {
799 return i == 0 ? 0 : upperBounds[i - 1];
800 }
801
802 /**
803 * Mass of bin i under the within-bin kernel of the bin.
804 *
805 * @param i index of the bin
806 * @return the difference in the within-bin kernel cdf between the
807 * upper and lower endpoints of bin i
808 */
809 @SuppressWarnings("deprecation")
810 private double kB(int i) {
811 final double[] binBounds = getUpperBounds();
812 final RealDistribution kernel = getKernel(binStats.get(i));
813 return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
814 kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
815 }
816
817 /**
818 * The within-bin kernel of the bin that x belongs to.
819 *
820 * @param x the value to locate within a bin
821 * @return the within-bin kernel of the bin containing x
822 */
823 private RealDistribution k(double x) {
824 final int binIndex = findBin(x);
825 return getKernel(binStats.get(binIndex));
826 }
827
828 /**
829 * The combined probability of the bins up to and including binIndex.
830 *
831 * @param binIndex maximum bin index
832 * @return sum of the probabilities of bins through binIndex
833 */
834 private double cumBinP(int binIndex) {
835 return upperBounds[binIndex];
836 }
837
838 /**
839 * The within-bin smoothing kernel.
840 *
841 * @param bStats summary statistics for the bin
842 * @return within-bin kernel parameterized by bStats
843 */
844 protected RealDistribution getKernel(SummaryStatistics bStats) {
845 // Default to Gaussian
846 return new NormalDistribution(randomData.getRandomGenerator(),
847 bStats.getMean(), bStats.getStandardDeviation(),
848 NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
849 }
850 }