001: /*
002: * Copyright 2003-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 ChiSquareDistribution.
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 ChiSquareDistributionTest extends
027: ContinuousDistributionAbstractTest {
028:
029: /**
030: * Constructor for ChiSquareDistributionTest.
031: * @param name
032: */
033: public ChiSquareDistributionTest(String name) {
034: super (name);
035: }
036:
037: //-------------- Implementations for abstract methods -----------------------
038:
039: /** Creates the default continuous distribution instance to use in tests. */
040: public ContinuousDistribution makeDistribution() {
041: return DistributionFactory.newInstance()
042: .createChiSquareDistribution(5.0);
043: }
044:
045: /** Creates the default cumulative probability distribution test input values */
046: public double[] makeCumulativeTestPoints() {
047: // quantiles computed using R version 1.8.1 (linux version)
048: return new double[] { 0.210216d, 0.5542981d, 0.8312116d,
049: 1.145476d, 1.610308d, 20.51501d, 15.08627d, 12.83250d,
050: 11.07050d, 9.236357d };
051: }
052:
053: /** Creates the default cumulative probability density test expected values */
054: public double[] makeCumulativeTestValues() {
055: return new double[] { 0.001d, 0.01d, 0.025d, 0.05d, 0.1d,
056: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d };
057: }
058:
059: /** Creates the default inverse cumulative probability test input values */
060: public double[] makeInverseCumulativeTestPoints() {
061: return new double[] { 0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d,
062: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 };
063: }
064:
065: /** Creates the default inverse cumulative probability density test expected values */
066: public double[] makeInverseCumulativeTestValues() {
067: return new double[] { 0, 0.210216d, 0.5542981d, 0.8312116d,
068: 1.145476d, 1.610308d, 20.51501d, 15.08627d, 12.83250d,
069: 11.07050d, 9.236357d, Double.POSITIVE_INFINITY };
070: }
071:
072: // --------------------- Override tolerance --------------
073: protected void setup() throws Exception {
074: super .setUp();
075: setTolerance(1E-6);
076: }
077:
078: //---------------------------- Additional test cases -------------------------
079:
080: public void testSmallDf() throws Exception {
081: setDistribution(DistributionFactory.newInstance()
082: .createChiSquareDistribution(0.1d));
083: setTolerance(1E-4);
084: // quantiles computed using R version 1.8.1 (linux version)
085: setCumulativeTestPoints(new double[] { 1.168926E-60,
086: 1.168926E-40, 1.063132E-32, 1.144775E-26, 1.168926E-20,
087: 5.472917, 2.175255, 1.13438, 0.5318646, 0.1526342 });
088: setInverseCumulativeTestValues(getCumulativeTestPoints());
089: setInverseCumulativeTestPoints(getCumulativeTestValues());
090: verifyCumulativeProbabilities();
091: verifyInverseCumulativeProbabilities();
092: }
093:
094: public void testDfAccessors() {
095: ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
096: assertEquals(5d, distribution.getDegreesOfFreedom(),
097: Double.MIN_VALUE);
098: distribution.setDegreesOfFreedom(4d);
099: assertEquals(4d, distribution.getDegreesOfFreedom(),
100: Double.MIN_VALUE);
101: try {
102: distribution.setDegreesOfFreedom(0d);
103: fail("Expecting IllegalArgumentException for df = 0");
104: } catch (IllegalArgumentException ex) {
105: // expected
106: }
107: }
108:
109: }
|