001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.distribution;
018:
019: /**
020: * Test cases for NormalDistribution.
021: * Extends ContinuousDistributionAbstractTest. See class javadoc for
022: * ContinuousDistributionAbstractTest for details.
023: *
024: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
025: */
026: public class NormalDistributionTest extends
027: ContinuousDistributionAbstractTest {
028:
029: /**
030: * Constructor for NormalDistributionTest.
031: * @param arg0
032: */
033: public NormalDistributionTest(String arg0) {
034: super (arg0);
035: }
036:
037: public static void main(String[] args) {
038: junit.swingui.TestRunner.run(NormalDistributionTest.class);
039: }
040:
041: //-------------- Implementations for abstract methods -----------------------
042:
043: /** Creates the default continuous distribution instance to use in tests. */
044: public ContinuousDistribution makeDistribution() {
045: return DistributionFactory.newInstance()
046: .createNormalDistribution(2.1, 1.4);
047: }
048:
049: /** Creates the default cumulative probability distribution test input values */
050: public double[] makeCumulativeTestPoints() {
051: // quantiles computed using R
052: return new double[] { -2.226325d, -1.156887d, -0.6439496d,
053: -0.2027951d, 0.3058278d, 6.426325d, 5.356887d,
054: 4.84395d, 4.402795d, 3.894172d };
055: }
056:
057: /** Creates the default cumulative probability density test expected values */
058: public double[] makeCumulativeTestValues() {
059: return new double[] { 0.001d, 0.01d, 0.025d, 0.05d, 0.1d,
060: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d };
061: }
062:
063: // --------------------- Override tolerance --------------
064: protected void setup() throws Exception {
065: super .setUp();
066: setTolerance(1E-6);
067: }
068:
069: //---------------------------- Additional test cases -------------------------
070:
071: private void verifyQuantiles() throws Exception {
072: NormalDistribution distribution = (NormalDistribution) getDistribution();
073: double mu = distribution.getMean();
074: double sigma = distribution.getStandardDeviation();
075: setCumulativeTestPoints(new double[] { mu - 2 * sigma,
076: mu - sigma, mu, mu + sigma, mu + 2 * sigma,
077: mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma });
078: // Quantiles computed using R (same as Mathematica)
079: setCumulativeTestValues(new double[] { 0.02275013, 0.1586553,
080: 0.5, 0.8413447, 0.9772499, 0.9986501, 0.9999683,
081: 0.9999997 });
082: verifyCumulativeProbabilities();
083: }
084:
085: public void testQuantiles() throws Exception {
086: verifyQuantiles();
087: setDistribution(DistributionFactory.newInstance()
088: .createNormalDistribution(0, 1));
089: verifyQuantiles();
090: setDistribution(DistributionFactory.newInstance()
091: .createNormalDistribution(0, 0.1));
092: verifyQuantiles();
093: }
094:
095: public void testInverseCumulativeProbabilityExtremes()
096: throws Exception {
097: setInverseCumulativeTestPoints(new double[] { 0, 1 });
098: setInverseCumulativeTestValues(new double[] {
099: Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY });
100: verifyInverseCumulativeProbabilities();
101: }
102:
103: public void testGetMean() {
104: NormalDistribution distribution = (NormalDistribution) getDistribution();
105: assertEquals(2.1, distribution.getMean(), 0);
106: }
107:
108: public void testSetMean() throws Exception {
109: double mu = Math.random();
110: NormalDistribution distribution = (NormalDistribution) getDistribution();
111: distribution.setMean(mu);
112: verifyQuantiles();
113: }
114:
115: public void testGetStandardDeviation() {
116: NormalDistribution distribution = (NormalDistribution) getDistribution();
117: assertEquals(1.4, distribution.getStandardDeviation(), 0);
118: }
119:
120: public void testSetStandardDeviation() throws Exception {
121: double sigma = 0.1d + Math.random();
122: NormalDistribution distribution = (NormalDistribution) getDistribution();
123: distribution.setStandardDeviation(sigma);
124: assertEquals(sigma, distribution.getStandardDeviation(), 0);
125: verifyQuantiles();
126: try {
127: distribution.setStandardDeviation(0);
128: fail("Expecting IllegalArgumentException for sd = 0");
129: } catch (IllegalArgumentException ex) {
130: // Expected
131: }
132: }
133: }
|