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  package org.apache.commons.math3.distribution;
18  
19  import org.apache.commons.math3.exception.NumberIsTooLargeException;
20  import org.apache.commons.math3.exception.OutOfRangeException;
21  
22  /**
23   * Interface for distributions on the integers.
24   *
25   */
26  public interface IntegerDistribution {
27      /**
28       * For a random variable {@code X} whose values are distributed according
29       * to this distribution, this method returns {@code P(X = x)}. In other
30       * words, this method represents the probability mass function (PMF)
31       * for the distribution.
32       *
33       * @param x the point at which the PMF is evaluated
34       * @return the value of the probability mass function at {@code x}
35       */
36      double probability(int x);
37  
38      /**
39       * For a random variable {@code X} whose values are distributed according
40       * to this distribution, this method returns {@code P(X <= x)}.  In other
41       * words, this method represents the (cumulative) distribution function
42       * (CDF) for this distribution.
43       *
44       * @param x the point at which the CDF is evaluated
45       * @return the probability that a random variable with this
46       * distribution takes a value less than or equal to {@code x}
47       */
48      double cumulativeProbability(int x);
49  
50      /**
51       * For a random variable {@code X} whose values are distributed according
52       * to this distribution, this method returns {@code P(x0 < X <= x1)}.
53       *
54       * @param x0 the exclusive lower bound
55       * @param x1 the inclusive upper bound
56       * @return the probability that a random variable with this distribution
57       * will take a value between {@code x0} and {@code x1},
58       * excluding the lower and including the upper endpoint
59       * @throws NumberIsTooLargeException if {@code x0 > x1}
60       */
61      double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException;
62  
63      /**
64       * Computes the quantile function of this distribution.
65       * For a random variable {@code X} distributed according to this distribution,
66       * the returned value is
67       * <ul>
68       * <li><code>inf{x in Z | P(X<=x) >= p}</code> for {@code 0 < p <= 1},</li>
69       * <li><code>inf{x in Z | P(X<=x) > 0}</code> for {@code p = 0}.</li>
70       * </ul>
71       * If the result exceeds the range of the data type {@code int},
72       * then {@code Integer.MIN_VALUE} or {@code Integer.MAX_VALUE} is returned.
73       *
74       * @param p the cumulative probability
75       * @return the smallest {@code p}-quantile of this distribution
76       * (largest 0-quantile for {@code p = 0})
77       * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}
78       */
79      int inverseCumulativeProbability(double p) throws OutOfRangeException;
80  
81      /**
82       * Use this method to get the numerical value of the mean of this
83       * distribution.
84       *
85       * @return the mean or {@code Double.NaN} if it is not defined
86       */
87      double getNumericalMean();
88  
89      /**
90       * Use this method to get the numerical value of the variance of this
91       * distribution.
92       *
93       * @return the variance (possibly {@code Double.POSITIVE_INFINITY} or
94       * {@code Double.NaN} if it is not defined)
95       */
96      double getNumericalVariance();
97  
98      /**
99       * Access the lower bound of the support. This method must return the same
100      * value as {@code inverseCumulativeProbability(0)}. In other words, this
101      * method must return
102      * <p><code>inf {x in Z | P(X <= x) > 0}</code>.</p>
103      *
104      * @return lower bound of the support ({@code Integer.MIN_VALUE}
105      * for negative infinity)
106      */
107     int getSupportLowerBound();
108 
109     /**
110      * Access the upper bound of the support. This method must return the same
111      * value as {@code inverseCumulativeProbability(1)}. In other words, this
112      * method must return
113      * <p><code>inf {x in R | P(X <= x) = 1}</code>.</p>
114      *
115      * @return upper bound of the support ({@code Integer.MAX_VALUE}
116      * for positive infinity)
117      */
118     int getSupportUpperBound();
119 
120     /**
121      * Use this method to get information about whether the support is
122      * connected, i.e. whether all integers between the lower and upper bound of
123      * the support are included in the support.
124      *
125      * @return whether the support is connected or not
126      */
127     boolean isSupportConnected();
128 
129     /**
130      * Reseed the random generator used to generate samples.
131      *
132      * @param seed the new seed
133      * @since 3.0
134      */
135     void reseedRandomGenerator(long seed);
136 
137     /**
138      * Generate a random value sampled from this distribution.
139      *
140      * @return a random value
141      * @since 3.0
142      */
143     int sample();
144 
145     /**
146      * Generate a random sample from the distribution.
147      *
148      * @param sampleSize the number of random values to generate
149      * @return an array representing the random sample
150      * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
151      * if {@code sampleSize} is not positive
152      * @since 3.0
153      */
154     int[] sample(int sampleSize);
155 }