~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import java.io.Serializable;
20
20
 
21
21
import org.apache.commons.math.ConvergenceException;
22
 
import org.apache.commons.math.FunctionEvaluationException;
23
22
import org.apache.commons.math.MathException;
24
23
import org.apache.commons.math.MathRuntimeException;
25
24
import org.apache.commons.math.analysis.UnivariateRealFunction;
26
25
import org.apache.commons.math.analysis.solvers.BrentSolver;
27
26
import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils;
 
27
import org.apache.commons.math.FunctionEvaluationException;
 
28
import org.apache.commons.math.exception.util.LocalizedFormats;
 
29
import org.apache.commons.math.random.RandomDataImpl;
 
30
import org.apache.commons.math.util.FastMath;
28
31
 
29
32
/**
30
33
 * Base class for continuous distributions.  Default implementations are
31
34
 * provided for some of the methods that do not vary from distribution to
32
35
 * distribution.
33
36
 *
34
 
 * @version $Revision: 925812 $ $Date: 2010-03-21 11:49:31 -0400 (Sun, 21 Mar 2010) $
 
37
 * @version $Revision: 1073498 $ $Date: 2011-02-22 21:57:26 +0100 (mar. 22 févr. 2011) $
35
38
 */
36
39
public abstract class AbstractContinuousDistribution
37
40
    extends AbstractDistribution
41
44
    private static final long serialVersionUID = -38038050983108802L;
42
45
 
43
46
    /**
44
 
     * Solver absolute accuracy for inverse cum computation
 
47
     * RandomData instance used to generate samples from the distribution
 
48
     * @since 2.2
 
49
     */
 
50
    protected final RandomDataImpl randomData = new RandomDataImpl();
 
51
 
 
52
    /**
 
53
     * Solver absolute accuracy for inverse cumulative computation
45
54
     * @since 2.1
46
55
     */
47
56
    private double solverAbsoluteAccuracy = BrentSolver.DEFAULT_ABSOLUTE_ACCURACY;
62
71
     */
63
72
    public double density(double x) throws MathRuntimeException {
64
73
        throw new MathRuntimeException(new UnsupportedOperationException(),
65
 
                "This distribution does not have a density function implemented");
 
74
                LocalizedFormats.NO_DENSITY_FOR_THIS_DISTRIBUTION);
66
75
    }
67
76
 
68
77
    /**
80
89
        throws MathException {
81
90
        if (p < 0.0 || p > 1.0) {
82
91
            throw MathRuntimeException.createIllegalArgumentException(
83
 
                  "{0} out of [{1}, {2}] range", p, 0.0, 1.0);
 
92
                  LocalizedFormats.OUT_OF_RANGE_SIMPLE, p, 0.0, 1.0);
84
93
        }
85
94
 
86
95
        // by default, do simple root finding using bracketing and default solver.
92
101
                try {
93
102
                    ret = cumulativeProbability(x) - p;
94
103
                } catch (MathException ex) {
95
 
                    throw new FunctionEvaluationException(ex, x, ex.getPattern(), ex.getArguments());
 
104
                    throw new FunctionEvaluationException(x, ex.getSpecificPattern(), ex.getGeneralPattern(), ex.getArguments());
96
105
                }
97
106
                if (Double.isNaN(ret)) {
98
 
                    throw new FunctionEvaluationException(x,
99
 
                        "Cumulative probability function returned NaN for argument {0} p = {1}", x, p);
 
107
                    throw new FunctionEvaluationException(x, LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p);
100
108
                }
101
109
                return ret;
102
110
            }
103
111
        };
104
112
 
105
 
        // Try to bracket root, test domain endoints if this fails
 
113
        // Try to bracket root, test domain endpoints if this fails
106
114
        double lowerBound = getDomainLowerBound(p);
107
115
        double upperBound = getDomainUpperBound(p);
108
116
        double[] bracket = null;
116
124
             * the default solver's defaultAbsoluteAccuracy of 0 (will be the
117
125
             * case if density has bounded support and p is 0 or 1).
118
126
             */
119
 
            if (Math.abs(rootFindingFunction.value(lowerBound)) < getSolverAbsoluteAccuracy()) {
 
127
            if (FastMath.abs(rootFindingFunction.value(lowerBound)) < getSolverAbsoluteAccuracy()) {
120
128
                return lowerBound;
121
129
            }
122
 
            if (Math.abs(rootFindingFunction.value(upperBound)) < getSolverAbsoluteAccuracy()) {
 
130
            if (FastMath.abs(rootFindingFunction.value(upperBound)) < getSolverAbsoluteAccuracy()) {
123
131
                return upperBound;
124
132
            }
125
133
            // Failed bracket convergence was not because of corner solution
135
143
    }
136
144
 
137
145
    /**
 
146
     * Reseeds the random generator used to generate samples.
 
147
     *
 
148
     * @param seed the new seed
 
149
     * @since 2.2
 
150
     */
 
151
    public void reseedRandomGenerator(long seed) {
 
152
        randomData.reSeed(seed);
 
153
    }
 
154
 
 
155
    /**
 
156
     * Generates a random value sampled from this distribution. The default
 
157
     * implementation uses the
 
158
     * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> inversion method.</a>
 
159
     *
 
160
     * @return random value
 
161
     * @since 2.2
 
162
     * @throws MathException if an error occurs generating the random value
 
163
     */
 
164
    public double sample() throws MathException {
 
165
        return randomData.nextInversionDeviate(this);
 
166
    }
 
167
 
 
168
    /**
 
169
     * Generates a random sample from the distribution.  The default implementation
 
170
     * generates the sample by calling {@link #sample()} in a loop.
 
171
     *
 
172
     * @param sampleSize number of random values to generate
 
173
     * @since 2.2
 
174
     * @return an array representing the random sample
 
175
     * @throws MathException if an error occurs generating the sample
 
176
     * @throws IllegalArgumentException if sampleSize is not positive
 
177
     */
 
178
    public double[] sample(int sampleSize) throws MathException {
 
179
        if (sampleSize <= 0) {
 
180
            MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_POSITIVE_SAMPLE_SIZE, sampleSize);
 
181
        }
 
182
        double[] out = new double[sampleSize];
 
183
        for (int i = 0; i < sampleSize; i++) {
 
184
            out[i] = sample();
 
185
        }
 
186
        return out;
 
187
    }
 
188
 
 
189
    /**
138
190
     * Access the initial domain value, based on <code>p</code>, used to
139
191
     * bracket a CDF root.  This method is used by
140
192
     * {@link #inverseCumulativeProbability(double)} to find critical values.
167
219
    protected abstract double getDomainUpperBound(double p);
168
220
 
169
221
    /**
170
 
     * Returns the solver absolute accuracy for inverse cum computation.
 
222
     * Returns the solver absolute accuracy for inverse cumulative computation.
171
223
     *
172
224
     * @return the maximum absolute error in inverse cumulative probability estimates
173
225
     * @since 2.1
175
227
    protected double getSolverAbsoluteAccuracy() {
176
228
        return solverAbsoluteAccuracy;
177
229
    }
 
230
 
178
231
}