001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.random;
019
020import java.io.BufferedReader;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.InputStreamReader;
026import java.net.URL;
027import java.nio.charset.Charset;
028import java.util.ArrayList;
029import java.util.List;
030
031import org.apache.commons.math3.distribution.AbstractRealDistribution;
032import org.apache.commons.math3.distribution.NormalDistribution;
033import org.apache.commons.math3.distribution.RealDistribution;
034import org.apache.commons.math3.exception.MathIllegalStateException;
035import org.apache.commons.math3.exception.MathInternalError;
036import org.apache.commons.math3.exception.NullArgumentException;
037import org.apache.commons.math3.exception.OutOfRangeException;
038import org.apache.commons.math3.exception.ZeroException;
039import org.apache.commons.math3.exception.util.LocalizedFormats;
040import org.apache.commons.math3.stat.descriptive.StatisticalSummary;
041import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
042import org.apache.commons.math3.util.FastMath;
043import org.apache.commons.math3.util.MathUtils;
044
045/**
046 * <p>Represents an <a href="http://http://en.wikipedia.org/wiki/Empirical_distribution_function">
047 * empirical probability distribution</a> -- a probability distribution derived
048 * from observed data without making any assumptions about the functional form
049 * of the population distribution that the data come from.</p>
050 *
051 * <p>An <code>EmpiricalDistribution</code> maintains data structures, called
052 * <i>distribution digests</i>, that describe empirical distributions and
053 * support the following operations: <ul>
054 * <li>loading the distribution from a file of observed data values</li>
055 * <li>dividing the input data into "bin ranges" and reporting bin frequency
056 *     counts (data for histogram)</li>
057 * <li>reporting univariate statistics describing the full set of data values
058 *     as well as the observations within each bin</li>
059 * <li>generating random values from the distribution</li>
060 * </ul>
061 * Applications can use <code>EmpiricalDistribution</code> to build grouped
062 * frequency histograms representing the input data or to generate random values
063 * "like" those in the input file -- i.e., the values generated will follow the
064 * distribution of the values in the file.</p>
065 *
066 * <p>The implementation uses what amounts to the
067 * <a href="http://nedwww.ipac.caltech.edu/level5/March02/Silverman/Silver2_6.html">
068 * Variable Kernel Method</a> with Gaussian smoothing:<p>
069 * <strong>Digesting the input file</strong>
070 * <ol><li>Pass the file once to compute min and max.</li>
071 * <li>Divide the range from min-max into <code>binCount</code> "bins."</li>
072 * <li>Pass the data file again, computing bin counts and univariate
073 *     statistics (mean, std dev.) for each of the bins </li>
074 * <li>Divide the interval (0,1) into subintervals associated with the bins,
075 *     with the length of a bin's subinterval proportional to its count.</li></ol>
076 * <strong>Generating random values from the distribution</strong><ol>
077 * <li>Generate a uniformly distributed value in (0,1) </li>
078 * <li>Select the subinterval to which the value belongs.
079 * <li>Generate a random Gaussian value with mean = mean of the associated
080 *     bin and std dev = std dev of associated bin.</li></ol></p>
081 *
082 * <p>EmpiricalDistribution implements the {@link RealDistribution} interface
083 * as follows.  Given x within the range of values in the dataset, let B
084 * be the bin containing x and let K be the within-bin kernel for B.  Let P(B-)
085 * be the sum of the probabilities of the bins below B and let K(B) be the
086 * mass of B under K (i.e., the integral of the kernel density over B).  Then
087 * set P(X < x) = P(B-) + P(B) * K(x) / K(B) where K(x) is the kernel distribution
088 * evaluated at x. This results in a cdf that matches the grouped frequency
089 * distribution at the bin endpoints and interpolates within bins using
090 * within-bin kernels.</p>
091 *
092 *<strong>USAGE NOTES:</strong><ul>
093 *<li>The <code>binCount</code> is set by default to 1000.  A good rule of thumb
094 *    is to set the bin count to approximately the length of the input file divided
095 *    by 10. </li>
096 *<li>The input file <i>must</i> be a plain text file containing one valid numeric
097 *    entry per line.</li>
098 * </ul></p>
099 *
100 * @version $Id: EmpiricalDistribution.java 1587494 2014-04-15 10:02:54Z erans $
101 */
102public 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.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        // 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     * <strong>Preconditions:</strong><ul>
560     * <li>the distribution must be loaded before invoking this method</li></ul>
561     *
562     * <p>In versions 1.0-2.0 of commons-math, this array was (incorrectly) returned
563     * by {@link #getUpperBounds()}.</p>
564     *
565     * @since 2.1
566     * @return array of upper bounds of subintervals used in data generation
567     * @throws NullPointerException unless a {@code load} method has been
568     * called beforehand.
569     */
570    public double[] getGeneratorUpperBounds() {
571        int len = upperBounds.length;
572        double[] out = new double[len];
573        System.arraycopy(upperBounds, 0, out, 0, len);
574        return out;
575    }
576
577    /**
578     * Property indicating whether or not the distribution has been loaded.
579     *
580     * @return true if the distribution has been loaded
581     */
582    public boolean isLoaded() {
583        return loaded;
584    }
585
586    /**
587     * Reseeds the random number generator used by {@link #getNextValue()}.
588     *
589     * @param seed random generator seed
590     * @since 3.0
591     */
592    public void reSeed(long seed) {
593        randomData.reSeed(seed);
594    }
595
596    // Distribution methods ---------------------------
597
598    /**
599     * {@inheritDoc}
600     * @since 3.1
601     */
602    @Override
603    public double probability(double x) {
604        return 0;
605    }
606
607    /**
608     * {@inheritDoc}
609     *
610     * <p>Returns the kernel density normalized so that its integral over each bin
611     * equals the bin mass.</p>
612     *
613     * <p>Algorithm description: <ol>
614     * <li>Find the bin B that x belongs to.</li>
615     * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
616     * integral of the kernel density over B).</li>
617     * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
618     * and P(B) is the mass of B.</li></ol></p>
619     * @since 3.1
620     */
621    public double density(double x) {
622        if (x < min || x > max) {
623            return 0d;
624        }
625        final int binIndex = findBin(x);
626        final RealDistribution kernel = getKernel(binStats.get(binIndex));
627        return kernel.density(x) * pB(binIndex) / kB(binIndex);
628    }
629
630    /**
631     * {@inheritDoc}
632     *
633     * <p>Algorithm description:<ol>
634     * <li>Find the bin B that x belongs to.</li>
635     * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
636     * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
637     * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
638     * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
639     * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
640     *
641     * @since 3.1
642     */
643    public double cumulativeProbability(double x) {
644        if (x < min) {
645            return 0d;
646        } else if (x >= max) {
647            return 1d;
648        }
649        final int binIndex = findBin(x);
650        final double pBminus = pBminus(binIndex);
651        final double pB = pB(binIndex);
652        final double[] binBounds = getUpperBounds();
653        final double kB = kB(binIndex);
654        final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
655        final RealDistribution kernel = k(x);
656        final double withinBinCum =
657            (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
658        return pBminus + pB * withinBinCum;
659    }
660
661    /**
662     * {@inheritDoc}
663     *
664     * <p>Algorithm description:<ol>
665     * <li>Find the smallest i such that the sum of the masses of the bins
666     *  through i is at least p.</li>
667     * <li>
668     *   Let K be the within-bin kernel distribution for bin i.</br>
669     *   Let K(B) be the mass of B under K. <br/>
670     *   Let K(B-) be K evaluated at the lower endpoint of B (the combined
671     *   mass of the bins below B under K).<br/>
672     *   Let P(B) be the probability of bin i.<br/>
673     *   Let P(B-) be the sum of the bin masses below bin i. <br/>
674     *   Let pCrit = p - P(B-)<br/>
675     * <li>Return the inverse of K evaluated at <br/>
676     *    K(B-) + pCrit * K(B) / P(B) </li>
677     *  </ol></p>
678     *
679     * @since 3.1
680     */
681    @Override
682    public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
683        if (p < 0.0 || p > 1.0) {
684            throw new OutOfRangeException(p, 0, 1);
685        }
686
687        if (p == 0.0) {
688            return getSupportLowerBound();
689        }
690
691        if (p == 1.0) {
692            return getSupportUpperBound();
693        }
694
695        int i = 0;
696        while (cumBinP(i) < p) {
697            i++;
698        }
699
700        final RealDistribution kernel = getKernel(binStats.get(i));
701        final double kB = kB(i);
702        final double[] binBounds = getUpperBounds();
703        final double lower = i == 0 ? min : binBounds[i - 1];
704        final double kBminus = kernel.cumulativeProbability(lower);
705        final double pB = pB(i);
706        final double pBminus = pBminus(i);
707        final double pCrit = p - pBminus;
708        if (pCrit <= 0) {
709            return lower;
710        }
711        return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB);
712    }
713
714    /**
715     * {@inheritDoc}
716     * @since 3.1
717     */
718    public double getNumericalMean() {
719       return sampleStats.getMean();
720    }
721
722    /**
723     * {@inheritDoc}
724     * @since 3.1
725     */
726    public double getNumericalVariance() {
727        return sampleStats.getVariance();
728    }
729
730    /**
731     * {@inheritDoc}
732     * @since 3.1
733     */
734    public double getSupportLowerBound() {
735       return min;
736    }
737
738    /**
739     * {@inheritDoc}
740     * @since 3.1
741     */
742    public double getSupportUpperBound() {
743        return max;
744    }
745
746    /**
747     * {@inheritDoc}
748     * @since 3.1
749     */
750    public boolean isSupportLowerBoundInclusive() {
751        return true;
752    }
753
754    /**
755     * {@inheritDoc}
756     * @since 3.1
757     */
758    public boolean isSupportUpperBoundInclusive() {
759        return true;
760    }
761
762    /**
763     * {@inheritDoc}
764     * @since 3.1
765     */
766    public boolean isSupportConnected() {
767        return true;
768    }
769
770    /**
771     * {@inheritDoc}
772     * @since 3.1
773     */
774    @Override
775    public double sample() {
776        return getNextValue();
777    }
778
779    /**
780     * {@inheritDoc}
781     * @since 3.1
782     */
783    @Override
784    public void reseedRandomGenerator(long seed) {
785        randomData.reSeed(seed);
786    }
787
788    /**
789     * The probability of bin i.
790     *
791     * @param i the index of the bin
792     * @return the probability that selection begins in bin i
793     */
794    private double pB(int i) {
795        return i == 0 ? upperBounds[0] :
796            upperBounds[i] - upperBounds[i - 1];
797    }
798
799    /**
800     * The combined probability of the bins up to but not including bin i.
801     *
802     * @param i the index of the bin
803     * @return the probability that selection begins in a bin below bin i.
804     */
805    private double pBminus(int i) {
806        return i == 0 ? 0 : upperBounds[i - 1];
807    }
808
809    /**
810     * Mass of bin i under the within-bin kernel of the bin.
811     *
812     * @param i index of the bin
813     * @return the difference in the within-bin kernel cdf between the
814     * upper and lower endpoints of bin i
815     */
816    @SuppressWarnings("deprecation")
817    private double kB(int i) {
818        final double[] binBounds = getUpperBounds();
819        final RealDistribution kernel = getKernel(binStats.get(i));
820        return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) :
821            kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]);
822    }
823
824    /**
825     * The within-bin kernel of the bin that x belongs to.
826     *
827     * @param x the value to locate within a bin
828     * @return the within-bin kernel of the bin containing x
829     */
830    private RealDistribution k(double x) {
831        final int binIndex = findBin(x);
832        return getKernel(binStats.get(binIndex));
833    }
834
835    /**
836     * The combined probability of the bins up to and including binIndex.
837     *
838     * @param binIndex maximum bin index
839     * @return sum of the probabilities of bins through binIndex
840     */
841    private double cumBinP(int binIndex) {
842        return upperBounds[binIndex];
843    }
844
845    /**
846     * The within-bin smoothing kernel.
847     *
848     * @param bStats summary statistics for the bin
849     * @return within-bin kernel parameterized by bStats
850     */
851    protected RealDistribution getKernel(SummaryStatistics bStats) {
852        // Default to Gaussian
853        return new NormalDistribution(randomData.getRandomGenerator(),
854                bStats.getMean(), bStats.getStandardDeviation(),
855                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
856    }
857}