summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/distribution/IntegerDistribution.java
blob: c188a78f64992ddb981d883e92032d3e6d501675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math3.distribution;

import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;

/** Interface for distributions on the integers. */
public interface IntegerDistribution {
    /**
     * For a random variable {@code X} whose values are distributed according to this distribution,
     * this method returns {@code P(X = x)}. In other words, this method represents the probability
     * mass function (PMF) for the distribution.
     *
     * @param x the point at which the PMF is evaluated
     * @return the value of the probability mass function at {@code x}
     */
    double probability(int x);

    /**
     * For a random variable {@code X} whose values are distributed according to this distribution,
     * this method returns {@code P(X <= x)}. In other words, this method represents the
     * (cumulative) distribution function (CDF) for this distribution.
     *
     * @param x the point at which the CDF is evaluated
     * @return the probability that a random variable with this distribution takes a value less than
     *     or equal to {@code x}
     */
    double cumulativeProbability(int x);

    /**
     * For a random variable {@code X} whose values are distributed according to this distribution,
     * this method returns {@code P(x0 < X <= x1)}.
     *
     * @param x0 the exclusive lower bound
     * @param x1 the inclusive upper bound
     * @return the probability that a random variable with this distribution will take a value
     *     between {@code x0} and {@code x1}, excluding the lower and including the upper endpoint
     * @throws NumberIsTooLargeException if {@code x0 > x1}
     */
    double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException;

    /**
     * Computes the quantile function of this distribution. For a random variable {@code X}
     * distributed according to this distribution, the returned value is
     *
     * <ul>
     *   <li><code>inf{x in Z | P(X<=x) >= p}</code> for {@code 0 < p <= 1},
     *   <li><code>inf{x in Z | P(X<=x) > 0}</code> for {@code p = 0}.
     * </ul>
     *
     * If the result exceeds the range of the data type {@code int}, then {@code Integer.MIN_VALUE}
     * or {@code Integer.MAX_VALUE} is returned.
     *
     * @param p the cumulative probability
     * @return the smallest {@code p}-quantile of this distribution (largest 0-quantile for {@code p
     *     = 0})
     * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}
     */
    int inverseCumulativeProbability(double p) throws OutOfRangeException;

    /**
     * Use this method to get the numerical value of the mean of this distribution.
     *
     * @return the mean or {@code Double.NaN} if it is not defined
     */
    double getNumericalMean();

    /**
     * Use this method to get the numerical value of the variance of this distribution.
     *
     * @return the variance (possibly {@code Double.POSITIVE_INFINITY} or {@code Double.NaN} if it
     *     is not defined)
     */
    double getNumericalVariance();

    /**
     * Access the lower bound of the support. This method must return the same value as {@code
     * inverseCumulativeProbability(0)}. In other words, this method must return
     *
     * <p><code>inf {x in Z | P(X <= x) > 0}</code>.
     *
     * @return lower bound of the support ({@code Integer.MIN_VALUE} for negative infinity)
     */
    int getSupportLowerBound();

    /**
     * Access the upper bound of the support. This method must return the same value as {@code
     * inverseCumulativeProbability(1)}. In other words, this method must return
     *
     * <p><code>inf {x in R | P(X <= x) = 1}</code>.
     *
     * @return upper bound of the support ({@code Integer.MAX_VALUE} for positive infinity)
     */
    int getSupportUpperBound();

    /**
     * Use this method to get information about whether the support is connected, i.e. whether all
     * integers between the lower and upper bound of the support are included in the support.
     *
     * @return whether the support is connected or not
     */
    boolean isSupportConnected();

    /**
     * Reseed the random generator used to generate samples.
     *
     * @param seed the new seed
     * @since 3.0
     */
    void reseedRandomGenerator(long seed);

    /**
     * Generate a random value sampled from this distribution.
     *
     * @return a random value
     * @since 3.0
     */
    int sample();

    /**
     * Generate a random sample from the distribution.
     *
     * @param sampleSize the number of random values to generate
     * @return an array representing the random sample
     * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException if {@code sampleSize}
     *     is not positive
     * @since 3.0
     */
    int[] sample(int sampleSize);
}