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    *      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.ConstantRealDistribution;
33  import org.apache.commons.math3.distribution.NormalDistribution;
34  import org.apache.commons.math3.distribution.RealDistribution;
35  import org.apache.commons.math3.exception.MathIllegalStateException;
36  import org.apache.commons.math3.exception.MathInternalError;
37  import org.apache.commons.math3.exception.NullArgumentException;
38  import org.apache.commons.math3.exception.OutOfRangeException;
39  import org.apache.commons.math3.exception.ZeroException;
40  import org.apache.commons.math3.exception.util.LocalizedFormats;
41  import org.apache.commons.math3.stat.descriptive.StatisticalSummary;
42  import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
43  import org.apache.commons.math3.util.FastMath;
44  import org.apache.commons.math3.util.MathUtils;
45  
46  /**
47   * <p>Represents an <a href="http://http://en.wikipedia.org/wiki/Empirical_distribution_function">
48   * empirical probability distribution</a> -- a probability distribution derived
49   * from observed data without making any assumptions about the functional form
50   * of the population distribution that the data come from.</p>
51   *
52   * <p>An <code>EmpiricalDistribution</code> maintains data structures, called
53   * <i>distribution digests</i>, that describe empirical distributions and
54   * support the following operations: <ul>
55   * <li>loading the distribution from a file of observed data values</li>
56   * <li>dividing the input data into "bin ranges" and reporting bin frequency
57   *     counts (data for histogram)</li>
58   * <li>reporting univariate statistics describing the full set of data values
59   *     as well as the observations within each bin</li>
60   * <li>generating random values from the distribution</li>
61   * </ul>
62   * Applications can use <code>EmpiricalDistribution</code> to build grouped
63   * frequency histograms representing the input data or to generate random values
64   * "like" those in the input file -- i.e., the values generated will follow the
65   * distribution of the values in the file.</p>
66   *
67   * <p>The implementation uses what amounts to the
68   * <a href="http://nedwww.ipac.caltech.edu/level5/March02/Silverman/Silver2_6.html">
69   * Variable Kernel Method</a> with Gaussian smoothing:<p>
70   * <strong>Digesting the input file</strong>
71   * <ol><li>Pass the file once to compute min and max.</li>
72   * <li>Divide the range from min-max into <code>binCount</code> "bins."</li>
73   * <li>Pass the data file again, computing bin counts and univariate
74   *     statistics (mean, std dev.) for each of the bins </li>
75   * <li>Divide the interval (0,1) into subintervals associated with the bins,
76   *     with the length of a bin's subinterval proportional to its count.</li></ol>
77   * <strong>Generating random values from the distribution</strong><ol>
78   * <li>Generate a uniformly distributed value in (0,1) </li>
79   * <li>Select the subinterval to which the value belongs.
80   * <li>Generate a random Gaussian value with mean = mean of the associated
81   *     bin and std dev = std dev of associated bin.</li></ol></p>
82   *
83   * <p>EmpiricalDistribution implements the {@link RealDistribution} interface
84   * as follows.  Given x within the range of values in the dataset, let B
85   * be the bin containing x and let K be the within-bin kernel for B.  Let P(B-)
86   * be the sum of the probabilities of the bins below B and let K(B) be the
87   * mass of B under K (i.e., the integral of the kernel density over B).  Then
88   * set P(X < x) = P(B-) + P(B) * K(x) / K(B) where K(x) is the kernel distribution
89   * evaluated at x. This results in a cdf that matches the grouped frequency
90   * distribution at the bin endpoints and interpolates within bins using
91   * within-bin kernels.</p>
92   *
93   *<strong>USAGE NOTES:</strong><ul>
94   *<li>The <code>binCount</code> is set by default to 1000.  A good rule of thumb
95   *    is to set the bin count to approximately the length of the input file divided
96   *    by 10. </li>
97   *<li>The input file <i>must</i> be a plain text file containing one valid numeric
98   *    entry per line.</li>
99   * </ul></p>
100  *
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(randomData.getRandomGenerator());
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.parseDouble(str);
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) binCount);
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         return sample();
482     }
483 
484     /**
485      * Returns a {@link StatisticalSummary} describing this distribution.
486      * <strong>Preconditions:</strong><ul>
487      * <li>the distribution must be loaded before invoking this method</li></ul>
488      *
489      * @return the sample statistics
490      * @throws IllegalStateException if the distribution has not been loaded
491      */
492     public StatisticalSummary getSampleStats() {
493         return sampleStats;
494     }
495 
496     /**
497      * Returns the number of bins.
498      *
499      * @return the number of bins.
500      */
501     public int getBinCount() {
502         return binCount;
503     }
504 
505     /**
506      * Returns a List of {@link SummaryStatistics} instances containing
507      * statistics describing the values in each of the bins.  The list is
508      * indexed on the bin number.
509      *
510      * @return List of bin statistics.
511      */
512     public List<SummaryStatistics> getBinStats() {
513         return binStats;
514     }
515 
516     /**
517      * <p>Returns a fresh copy of the array of upper bounds for the bins.
518      * Bins are: <br/>
519      * [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],...,
520      *  (upperBounds[binCount-2], upperBounds[binCount-1] = max].</p>
521      *
522      * <p>Note: In versions 1.0-2.0 of commons-math, this method
523      * incorrectly returned the array of probability generator upper
524      * bounds now returned by {@link #getGeneratorUpperBounds()}.</p>
525      *
526      * @return array of bin upper bounds
527      * @since 2.1
528      */
529     public double[] getUpperBounds() {
530         double[] binUpperBounds = new double[binCount];
531         for (int i = 0; i < binCount - 1; i++) {
532             binUpperBounds[i] = min + delta * (i + 1);
533         }
534         binUpperBounds[binCount - 1] = max;
535         return binUpperBounds;
536     }
537 
538     /**
539      * <p>Returns a fresh copy of the array of upper bounds of the subintervals
540      * of [0,1] used in generating data from the empirical distribution.
541      * Subintervals correspond to bins with lengths proportional to bin counts.</p>
542      *
543      * <strong>Preconditions:</strong><ul>
544      * <li>the distribution must be loaded before invoking this method</li></ul>
545      *
546      * <p>In versions 1.0-2.0 of commons-math, this array was (incorrectly) returned
547      * by {@link #getUpperBounds()}.</p>
548      *
549      * @since 2.1
550      * @return array of upper bounds of subintervals used in data generation
551      * @throws NullPointerException unless a {@code load} method has been
552      * called beforehand.
553      */
554     public double[] getGeneratorUpperBounds() {
555         int len = upperBounds.length;
556         double[] out = new double[len];
557         System.arraycopy(upperBounds, 0, out, 0, len);
558         return out;
559     }
560 
561     /**
562      * Property indicating whether or not the distribution has been loaded.
563      *
564      * @return true if the distribution has been loaded
565      */
566     public boolean isLoaded() {
567         return loaded;
568     }
569 
570     /**
571      * Reseeds the random number generator used by {@link #getNextValue()}.
572      *
573      * @param seed random generator seed
574      * @since 3.0
575      */
576     public void reSeed(long seed) {
577         randomData.reSeed(seed);
578     }
579 
580     // Distribution methods ---------------------------
581 
582     /**
583      * {@inheritDoc}
584      * @since 3.1
585      */
586     @Override
587     public double probability(double x) {
588         return 0;
589     }
590 
591     /**
592      * {@inheritDoc}
593      *
594      * <p>Returns the kernel density normalized so that its integral over each bin
595      * equals the bin mass.</p>
596      *
597      * <p>Algorithm description: <ol>
598      * <li>Find the bin B that x belongs to.</li>
599      * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
600      * integral of the kernel density over B).</li>
601      * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
602      * and P(B) is the mass of B.</li></ol></p>
603      * @since 3.1
604      */
605     public double density(double x) {
606         if (x < min || x > max) {
607             return 0d;
608         }
609         final int binIndex = findBin(x);
610         final RealDistribution kernel = getKernel(binStats.get(binIndex));
611         return kernel.density(x) * pB(binIndex) / kB(binIndex);
612     }
613 
614     /**
615      * {@inheritDoc}
616      *
617      * <p>Algorithm description:<ol>
618      * <li>Find the bin B that x belongs to.</li>
619      * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
620      * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
621      * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
622      * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
623      * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
624      *
625      * @since 3.1
626      */
627     public double cumulativeProbability(double x) {
628         if (x < min) {
629             return 0d;
630         } else if (x >= max) {
631             return 1d;
632         }
633         final int binIndex = findBin(x);
634         final double pBminus = pBminus(binIndex);
635         final double pB = pB(binIndex);
636         final double[] binBounds = getUpperBounds();
637         final double kB = kB(binIndex);
638         final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
639         final RealDistribution kernel = k(x);
640         final double withinBinCum =
641             (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
642         return pBminus + pB * withinBinCum;
643     }
644 
645     /**
646      * {@inheritDoc}
647      *
648      * <p>Algorithm description:<ol>
649      * <li>Find the smallest i such that the sum of the masses of the bins
650      *  through i is at least p.</li>
651      * <li>
652      *   Let K be the within-bin kernel distribution for bin i.</br>
653      *   Let K(B) be the mass of B under K. <br/>
654      *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
655      *   mass of the bins below B under K).<br/>
656      *   Let P(B) be the probability of bin i.<br/>
657      *   Let P(B-) be the sum of the bin masses below bin i. <br/>
658      *   Let pCrit = p - P(B-)<br/>
659      * <li>Return the inverse of K evaluated at <br/>
660      *    K(B-) + pCrit * K(B) / P(B) </li>
661      *  </ol></p>
662      *
663      * @since 3.1
664      */
665     @Override
666     public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
667         if (p < 0.0 || p > 1.0) {
668             throw new OutOfRangeException(p, 0, 1);
669         }
670 
671         if (p == 0.0) {
672             return getSupportLowerBound();
673         }
674 
675         if (p == 1.0) {
676             return getSupportUpperBound();
677         }
678 
679         int i = 0;
680         while (cumBinP(i) < p) {
681             i++;
682         }
683 
684         final RealDistribution kernel = getKernel(binStats.get(i));
685         final double kB = kB(i);
686         final double[] binBounds = getUpperBounds();
687         final double lower = i == 0 ? min : binBounds[i - 1];
688         final double kBminus = kernel.cumulativeProbability(lower);
689         final double pB = pB(i);
690         final double pBminus = pBminus(i);
691         final double pCrit = p - pBminus;
692         if (pCrit <= 0) {
693             return lower;
694         }
695         return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
696     }
697 
698     /**
699      * {@inheritDoc}
700      * @since 3.1
701      */
702     public double getNumericalMean() {
703        return sampleStats.getMean();
704     }
705 
706     /**
707      * {@inheritDoc}
708      * @since 3.1
709      */
710     public double getNumericalVariance() {
711         return sampleStats.getVariance();
712     }
713 
714     /**
715      * {@inheritDoc}
716      * @since 3.1
717      */
718     public double getSupportLowerBound() {
719        return min;
720     }
721 
722     /**
723      * {@inheritDoc}
724      * @since 3.1
725      */
726     public double getSupportUpperBound() {
727         return max;
728     }
729 
730     /**
731      * {@inheritDoc}
732      * @since 3.1
733      */
734     public boolean isSupportLowerBoundInclusive() {
735         return true;
736     }
737 
738     /**
739      * {@inheritDoc}
740      * @since 3.1
741      */
742     public boolean isSupportUpperBoundInclusive() {
743         return true;
744     }
745 
746     /**
747      * {@inheritDoc}
748      * @since 3.1
749      */
750     public boolean isSupportConnected() {
751         return true;
752     }
753 
754     /**
755      * {@inheritDoc}
756      * @since 3.1
757      */
758     @Override
759     public void reseedRandomGenerator(long seed) {
760         randomData.reSeed(seed);
761     }
762 
763     /**
764      * The probability of bin i.
765      *
766      * @param i the index of the bin
767      * @return the probability that selection begins in bin i
768      */
769     private double pB(int i) {
770         return i == 0 ? upperBounds[0] :
771             upperBounds[i] - upperBounds[i - 1];
772     }
773 
774     /**
775      * The combined probability of the bins up to but not including bin i.
776      *
777      * @param i the index of the bin
778      * @return the probability that selection begins in a bin below bin i.
779      */
780     private double pBminus(int i) {
781         return i == 0 ? 0 : upperBounds[i - 1];
782     }
783 
784     /**
785      * Mass of bin i under the within-bin kernel of the bin.
786      *
787      * @param i index of the bin
788      * @return the difference in the within-bin kernel cdf between the
789      * upper and lower endpoints of bin i
790      */
791     @SuppressWarnings("deprecation")
792     private double kB(int i) {
793         final double[] binBounds = getUpperBounds();
794         final RealDistribution kernel = getKernel(binStats.get(i));
795         return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
796             kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
797     }
798 
799     /**
800      * The within-bin kernel of the bin that x belongs to.
801      *
802      * @param x the value to locate within a bin
803      * @return the within-bin kernel of the bin containing x
804      */
805     private RealDistribution k(double x) {
806         final int binIndex = findBin(x);
807         return getKernel(binStats.get(binIndex));
808     }
809 
810     /**
811      * The combined probability of the bins up to and including binIndex.
812      *
813      * @param binIndex maximum bin index
814      * @return sum of the probabilities of bins through binIndex
815      */
816     private double cumBinP(int binIndex) {
817         return upperBounds[binIndex];
818     }
819 
820     /**
821      * The within-bin smoothing kernel. Returns a Gaussian distribution
822      * parameterized by {@code bStats}, unless the bin contains only one
823      * observation, in which case a constant distribution is returned.
824      *
825      * @param bStats summary statistics for the bin
826      * @return within-bin kernel parameterized by bStats
827      */
828     protected RealDistribution getKernel(SummaryStatistics bStats) {
829         if (bStats.getN() == 1) {
830             return new ConstantRealDistribution(bStats.getMean());
831         } else {
832             return new NormalDistribution(randomData.getRandomGenerator(),
833                 bStats.getMean(), bStats.getStandardDeviation(),
834                 NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
835         }
836     }
837 }