EnumeratedRealDistribution.java

  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.  *      https://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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.distribution.continuous;

  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;

  27. import org.hipparchus.distribution.EnumeratedDistribution;
  28. import org.hipparchus.exception.MathIllegalArgumentException;
  29. import org.hipparchus.util.MathArrays;
  30. import org.hipparchus.util.MathUtils;
  31. import org.hipparchus.util.Pair;

  32. /**
  33.  * Implementation of a real-valued {@link EnumeratedDistribution}.
  34.  * <p>
  35.  * Values with zero-probability are allowed but they do not extend the
  36.  * support.
  37.  * <p>
  38.  * Duplicate values are allowed. Probabilities of duplicate values are
  39.  * combined when computing cumulative probabilities and statistics.
  40.  */
  41. public class EnumeratedRealDistribution extends AbstractRealDistribution {

  42.     /** Serializable UID. */
  43.     private static final long serialVersionUID = 20130308L;

  44.     /**
  45.      * {@link EnumeratedDistribution} (using the {@link Double} wrapper)
  46.      * used to generate the pmf.
  47.      */
  48.     private final EnumeratedDistribution<Double> innerDistribution;

  49.     /**
  50.      * Create a discrete real-valued distribution from the input data.  Values are assigned
  51.      * mass based on their frequency.  For example, [0,1,1,2] as input creates a distribution
  52.      * with values 0, 1 and 2 having probability masses 0.25, 0.5 and 0.25 respectively,
  53.      *
  54.      * @param data input dataset
  55.      */
  56.     public EnumeratedRealDistribution(final double[] data) {
  57.         super();
  58.         final Map<Double, Integer> dataMap = new HashMap<>();
  59.         for (double value : data) {
  60.             Integer count = dataMap.get(value);
  61.             if (count == null) {
  62.                 count = 0;
  63.             }
  64.             dataMap.put(value, ++count);
  65.         }
  66.         final int massPoints = dataMap.size();
  67.         final double denom = data.length;
  68.         final double[] values = new double[massPoints];
  69.         final double[] probabilities = new double[massPoints];
  70.         int index = 0;
  71.         for (Entry<Double, Integer> entry : dataMap.entrySet()) {
  72.             values[index] = entry.getKey();
  73.             probabilities[index] = entry.getValue() / denom;
  74.             index++;
  75.         }
  76.         innerDistribution =
  77.                 new EnumeratedDistribution<>(createDistribution(values, probabilities));
  78.     }

  79.     /**
  80.      * Create a discrete real-valued distribution using the given probability mass function
  81.      * enumeration.
  82.      *
  83.      * @param singletons array of random variable values.
  84.      * @param probabilities array of probabilities.
  85.      * @throws MathIllegalArgumentException if
  86.      * {@code singletons.length != probabilities.length}
  87.      * @throws MathIllegalArgumentException if any of the probabilities are negative.
  88.      * @throws MathIllegalArgumentException if any of the probabilities are NaN.
  89.      * @throws MathIllegalArgumentException if any of the probabilities are infinite.
  90.      */
  91.     public EnumeratedRealDistribution(final double[] singletons, final double[] probabilities)
  92.         throws MathIllegalArgumentException {
  93.         super();
  94.         innerDistribution =
  95.                 new EnumeratedDistribution<>(createDistribution(singletons, probabilities));
  96.     }


  97.     /**
  98.      * Create the list of Pairs representing the distribution from singletons and probabilities.
  99.      *
  100.      * @param singletons values
  101.      * @param probabilities probabilities
  102.      * @return list of value/probability pairs
  103.      * @throws MathIllegalArgumentException if probabilities contains negative, infinite or NaN values or only 0's
  104.      */
  105.     private static List<Pair<Double, Double>> createDistribution(double[] singletons,
  106.                                                                  double[] probabilities) {
  107.         MathArrays.checkEqualLength(singletons, probabilities);
  108.         final List<Pair<Double, Double>> samples = new ArrayList<>(singletons.length);

  109.         final double[] normalizedProbabilities = EnumeratedDistribution.checkAndNormalize(probabilities);
  110.         for (int i = 0; i < singletons.length; i++) {
  111.             samples.add(new Pair<>(singletons[i], normalizedProbabilities[i]));
  112.         }
  113.         return samples;
  114.     }

  115.     /**
  116.      * For a random variable {@code X} whose values are distributed according to
  117.      * this distribution, this method returns {@code P(X = x)}. In other words,
  118.      * this method represents the probability mass function (PMF) for the
  119.      * distribution.
  120.      * <p>
  121.      * Note that if {@code x1} and {@code x2} satisfy {@code x1.equals(x2)},
  122.      * or both are null, then {@code probability(x1) = probability(x2)}.
  123.      *
  124.      * @param x the point at which the PMF is evaluated
  125.      * @return the value of the probability mass function at {@code x}
  126.      */
  127.     public double probability(final double x) {
  128.         return innerDistribution.probability(x);
  129.     }

  130.     /**
  131.      * For a random variable {@code X} whose values are distributed according to
  132.      * this distribution, this method returns {@code P(X = x)}. In other words,
  133.      * this method represents the probability mass function (PMF) for the
  134.      * distribution.
  135.      *
  136.      * @param x the point at which the PMF is evaluated
  137.      * @return the value of the probability mass function at point {@code x}
  138.      */
  139.     @Override
  140.     public double density(final double x) {
  141.         return probability(x);
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public double cumulativeProbability(final double x) {
  148.         double probability = 0;

  149.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  150.             if (sample.getKey() <= x) {
  151.                 probability += sample.getValue();
  152.             }
  153.         }

  154.         return probability;
  155.     }

  156.     /**
  157.      * {@inheritDoc}
  158.      */
  159.     @Override
  160.     public double inverseCumulativeProbability(final double p) throws MathIllegalArgumentException {
  161.         MathUtils.checkRangeInclusive(p, 0, 1);

  162.         double probability = 0;
  163.         double x = getSupportLowerBound();
  164.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  165.             if (sample.getValue() == 0.0) {
  166.                 continue;
  167.             }

  168.             probability += sample.getValue();
  169.             x = sample.getKey();

  170.             if (probability >= p) {
  171.                 break;
  172.             }
  173.         }

  174.         return x;
  175.     }

  176.     /**
  177.      * {@inheritDoc}
  178.      *
  179.      * @return {@code sum(singletons[i] * probabilities[i])}
  180.      */
  181.     @Override
  182.     public double getNumericalMean() {
  183.         double mean = 0;

  184.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  185.             mean += sample.getValue() * sample.getKey();
  186.         }

  187.         return mean;
  188.     }

  189.     /**
  190.      * {@inheritDoc}
  191.      *
  192.      * @return {@code sum((singletons[i] - mean) ^ 2 * probabilities[i])}
  193.      */
  194.     @Override
  195.     public double getNumericalVariance() {
  196.         double mean = 0;
  197.         double meanOfSquares = 0;

  198.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  199.             mean += sample.getValue() * sample.getKey();
  200.             meanOfSquares += sample.getValue() * sample.getKey() * sample.getKey();
  201.         }

  202.         return meanOfSquares - mean * mean;
  203.     }

  204.     /**
  205.      * {@inheritDoc}
  206.      *
  207.      * Returns the lowest value with non-zero probability.
  208.      *
  209.      * @return the lowest value with non-zero probability.
  210.      */
  211.     @Override
  212.     public double getSupportLowerBound() {
  213.         double min = Double.POSITIVE_INFINITY;
  214.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  215.             if (sample.getKey() < min && sample.getValue() > 0) {
  216.                 min = sample.getKey();
  217.             }
  218.         }

  219.         return min;
  220.     }

  221.     /**
  222.      * {@inheritDoc}
  223.      *
  224.      * Returns the highest value with non-zero probability.
  225.      *
  226.      * @return the highest value with non-zero probability.
  227.      */
  228.     @Override
  229.     public double getSupportUpperBound() {
  230.         double max = Double.NEGATIVE_INFINITY;
  231.         for (final Pair<Double, Double> sample : innerDistribution.getPmf()) {
  232.             if (sample.getKey() > max && sample.getValue() > 0) {
  233.                 max = sample.getKey();
  234.             }
  235.         }

  236.         return max;
  237.     }

  238.     /**
  239.      * {@inheritDoc}
  240.      *
  241.      * The support of this distribution is connected.
  242.      *
  243.      * @return {@code true}
  244.      */
  245.     @Override
  246.     public boolean isSupportConnected() {
  247.         return true;
  248.     }

  249.     /**
  250.      * Return the probability mass function as a list of (value, probability) pairs.
  251.      *
  252.      * @return the probability mass function.
  253.      */
  254.     public List<Pair<Double, Double>> getPmf() {
  255.         return innerDistribution.getPmf();
  256.     }
  257. }