GeometricDistribution.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.  *      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. package org.apache.commons.math3.distribution;

  18. import org.apache.commons.math3.exception.OutOfRangeException;
  19. import org.apache.commons.math3.exception.util.LocalizedFormats;
  20. import org.apache.commons.math3.random.RandomGenerator;
  21. import org.apache.commons.math3.random.Well19937c;
  22. import org.apache.commons.math3.util.FastMath;

  23. /**
  24.  * Implementation of the geometric distribution.
  25.  *
  26.  * @see <a href="http://en.wikipedia.org/wiki/Geometric_distribution">Geometric distribution (Wikipedia)</a>
  27.  * @see <a href="http://mathworld.wolfram.com/GeometricDistribution.html">Geometric Distribution (MathWorld)</a>
  28.  * @since 3.3
  29.  */
  30. public class GeometricDistribution extends AbstractIntegerDistribution {

  31.     /** Serializable version identifier. */
  32.     private static final long serialVersionUID = 20130507L;
  33.     /** The probability of success. */
  34.     private final double probabilityOfSuccess;

  35.     /**
  36.      * Create a geometric distribution with the given probability of success.
  37.      * <p>
  38.      * <b>Note:</b> this constructor will implicitly create an instance of
  39.      * {@link Well19937c} as random generator to be used for sampling only (see
  40.      * {@link #sample()} and {@link #sample(int)}). In case no sampling is
  41.      * needed for the created distribution, it is advised to pass {@code null}
  42.      * as random generator via the appropriate constructors to avoid the
  43.      * additional initialisation overhead.
  44.      *
  45.      * @param p probability of success.
  46.      * @throws OutOfRangeException if {@code p <= 0} or {@code p > 1}.
  47.      */
  48.     public GeometricDistribution(double p) {
  49.         this(new Well19937c(), p);
  50.     }

  51.     /**
  52.      * Creates a geometric distribution.
  53.      *
  54.      * @param rng Random number generator.
  55.      * @param p Probability of success.
  56.      * @throws OutOfRangeException if {@code p <= 0} or {@code p > 1}.
  57.      */
  58.     public GeometricDistribution(RandomGenerator rng, double p) {
  59.         super(rng);

  60.         if (p <= 0 || p > 1) {
  61.             throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_LEFT, p, 0, 1);
  62.         }

  63.         probabilityOfSuccess = p;
  64.     }

  65.     /**
  66.      * Access the probability of success for this distribution.
  67.      *
  68.      * @return the probability of success.
  69.      */
  70.     public double getProbabilityOfSuccess() {
  71.         return probabilityOfSuccess;
  72.     }

  73.     /** {@inheritDoc} */
  74.     public double probability(int x) {
  75.         double ret;
  76.         if (x < 0) {
  77.             ret = 0.0;
  78.         } else {
  79.             final double p = probabilityOfSuccess;
  80.             ret = FastMath.pow(1 - p, x) * p;
  81.         }
  82.         return ret;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public double logProbability(int x) {
  87.         double ret;
  88.         if (x < 0) {
  89.             ret = Double.NEGATIVE_INFINITY;
  90.         } else {
  91.             final double p = probabilityOfSuccess;
  92.             ret = x * FastMath.log1p(-p) + FastMath.log(p);
  93.         }
  94.         return ret;
  95.     }

  96.     /** {@inheritDoc} */
  97.     public double cumulativeProbability(int x) {
  98.         double ret;
  99.         if (x < 0) {
  100.             ret = 0.0;
  101.         } else {
  102.             final double p = probabilityOfSuccess;
  103.             ret = 1.0 - FastMath.pow(1 - p, x + 1);
  104.         }
  105.         return ret;
  106.     }

  107.     /**
  108.      * {@inheritDoc}
  109.      *
  110.      * For probability parameter {@code p}, the mean is {@code (1 - p) / p}.
  111.      */
  112.     public double getNumericalMean() {
  113.         final double p = probabilityOfSuccess;
  114.         return (1 - p) / p;
  115.     }

  116.     /**
  117.      * {@inheritDoc}
  118.      *
  119.      * For probability parameter {@code p}, the variance is
  120.      * {@code (1 - p) / (p * p)}.
  121.      */
  122.     public double getNumericalVariance() {
  123.         final double p = probabilityOfSuccess;
  124.         return (1 - p) / (p * p);
  125.     }

  126.     /**
  127.      * {@inheritDoc}
  128.      *
  129.      * The lower bound of the support is always 0.
  130.      *
  131.      * @return lower bound of the support (always 0)
  132.      */
  133.     public int getSupportLowerBound() {
  134.         return 0;
  135.     }

  136.     /**
  137.      * {@inheritDoc}
  138.      *
  139.      * The upper bound of the support is infinite (which we approximate as
  140.      * {@code Integer.MAX_VALUE}).
  141.      *
  142.      * @return upper bound of the support (always Integer.MAX_VALUE)
  143.      */
  144.     public int getSupportUpperBound() {
  145.         return Integer.MAX_VALUE;
  146.     }

  147.     /**
  148.      * {@inheritDoc}
  149.      *
  150.      * The support of this distribution is connected.
  151.      *
  152.      * @return {@code true}
  153.      */
  154.     public boolean isSupportConnected() {
  155.         return true;
  156.     }
  157. }