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   * Base interface for distributions on the reals.
24   *
25   * @since 3.0
26   */
27  public interface RealDistribution {
28      /**
29       * For a random variable {@code X} whose values are distributed according
30       * to this distribution, this method returns {@code P(X = x)}. In other
31       * words, this method represents the probability mass function (PMF)
32       * for the distribution.
33       *
34       * @param x the point at which the PMF is evaluated
35       * @return the value of the probability mass function at point {@code x}
36       */
37      double probability(double x);
38  
39      /**
40       * Returns the probability density function (PDF) of this distribution
41       * evaluated at the specified point {@code x}. In general, the PDF is
42       * the derivative of the {@link #cumulativeProbability(double) CDF}.
43       * If the derivative does not exist at {@code x}, then an appropriate
44       * replacement should be returned, e.g. {@code Double.POSITIVE_INFINITY},
45       * {@code Double.NaN}, or  the limit inferior or limit superior of the
46       * difference quotient.
47       *
48       * @param x the point at which the PDF is evaluated
49       * @return the value of the probability density function at point {@code x}
50       */
51      double density(double x);
52  
53      /**
54       * For a random variable {@code X} whose values are distributed according
55       * to this distribution, this method returns {@code P(X <= x)}. In other
56       * words, this method represents the (cumulative) distribution function
57       * (CDF) for this distribution.
58       *
59       * @param x the point at which the CDF is evaluated
60       * @return the probability that a random variable with this
61       * distribution takes a value less than or equal to {@code x}
62       */
63      double cumulativeProbability(double x);
64  
65      /**
66       * For a random variable {@code X} whose values are distributed according
67       * to this distribution, this method returns {@code P(x0 < X <= x1)}.
68       *
69       * @param x0 the exclusive lower bound
70       * @param x1 the inclusive upper bound
71       * @return the probability that a random variable with this distribution
72       * takes a value between {@code x0} and {@code x1},
73       * excluding the lower and including the upper endpoint
74       * @throws NumberIsTooLargeException if {@code x0 > x1}
75       *
76       * @deprecated As of 3.1. In 4.0, this method will be renamed
77       * {@code probability(double x0, double x1)}.
78       */
79      @Deprecated
80      double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException;
81  
82      /**
83       * Computes the quantile function of this distribution. For a random
84       * variable {@code X} distributed according to this distribution, the
85       * returned value is
86       * <ul>
87       * <li><code>inf{x in R | P(X<=x) >= p}</code> for {@code 0 < p <= 1},</li>
88       * <li><code>inf{x in R | P(X<=x) > 0}</code> for {@code p = 0}.</li>
89       * </ul>
90       *
91       * @param p the cumulative probability
92       * @return the smallest {@code p}-quantile of this distribution
93       * (largest 0-quantile for {@code p = 0})
94       * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}
95       */
96      double inverseCumulativeProbability(double p) throws OutOfRangeException;
97  
98      /**
99       * Use this method to get the numerical value of the mean of this
100      * distribution.
101      *
102      * @return the mean or {@code Double.NaN} if it is not defined
103      */
104     double getNumericalMean();
105 
106     /**
107      * Use this method to get the numerical value of the variance of this
108      * distribution.
109      *
110      * @return the variance (possibly {@code Double.POSITIVE_INFINITY} as
111      * for certain cases in {@link TDistribution}) or {@code Double.NaN} if it
112      * is not defined
113      */
114     double getNumericalVariance();
115 
116     /**
117      * Access the lower bound of the support. This method must return the same
118      * value as {@code inverseCumulativeProbability(0)}. In other words, this
119      * method must return
120      * <p><code>inf {x in R | P(X <= x) > 0}</code>.</p>
121      *
122      * @return lower bound of the support (might be
123      * {@code Double.NEGATIVE_INFINITY})
124      */
125     double getSupportLowerBound();
126 
127     /**
128      * Access the upper bound of the support. This method must return the same
129      * value as {@code inverseCumulativeProbability(1)}. In other words, this
130      * method must return
131      * <p><code>inf {x in R | P(X <= x) = 1}</code>.</p>
132      *
133      * @return upper bound of the support (might be
134      * {@code Double.POSITIVE_INFINITY})
135      */
136     double getSupportUpperBound();
137 
138     /**
139      * Whether or not the lower bound of support is in the domain of the density
140      * function.  Returns true iff {@code getSupporLowerBound()} is finite and
141      * {@code density(getSupportLowerBound())} returns a non-NaN, non-infinite
142      * value.
143      *
144      * @return true if the lower bound of support is finite and the density
145      * function returns a non-NaN, non-infinite value there
146      * @deprecated to be removed in 4.0
147      */
148     @Deprecated
149     boolean isSupportLowerBoundInclusive();
150 
151     /**
152      * Whether or not the upper bound of support is in the domain of the density
153      * function.  Returns true iff {@code getSupportUpperBound()} is finite and
154      * {@code density(getSupportUpperBound())} returns a non-NaN, non-infinite
155      * value.
156      *
157      * @return true if the upper bound of support is finite and the density
158      * function returns a non-NaN, non-infinite value there
159      * @deprecated to be removed in 4.0
160      */
161     @Deprecated
162     boolean isSupportUpperBoundInclusive();
163 
164     /**
165      * Use this method to get information about whether the support is connected,
166      * i.e. whether all values between the lower and upper bound of the support
167      * are included in the support.
168      *
169      * @return whether the support is connected or not
170      */
171     boolean isSupportConnected();
172 
173     /**
174      * Reseed the random generator used to generate samples.
175      *
176      * @param seed the new seed
177      */
178     void reseedRandomGenerator(long seed);
179 
180     /**
181      * Generate a random value sampled from this distribution.
182      *
183      * @return a random value.
184      */
185     double sample();
186 
187     /**
188      * Generate a random sample from the distribution.
189      *
190      * @param sampleSize the number of random values to generate
191      * @return an array representing the random sample
192      * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
193      * if {@code sampleSize} is not positive
194      */
195     double[] sample(int sampleSize);
196 }