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: package org.apache.commons.math.distribution;
017:
018: /**
019: * Test cases for FDistribution.
020: * Extends ContinuousDistributionAbstractTest. See class javadoc for
021: * ContinuousDistributionAbstractTest for details.
022: *
023: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
024: */
025: public class FDistributionTest extends
026: ContinuousDistributionAbstractTest {
027:
028: /**
029: * Constructor for FDistributionTest.
030: * @param name
031: */
032: public FDistributionTest(String name) {
033: super (name);
034: }
035:
036: //-------------- Implementations for abstract methods -----------------------
037:
038: /** Creates the default continuous distribution instance to use in tests. */
039: public ContinuousDistribution makeDistribution() {
040: return DistributionFactory.newInstance().createFDistribution(
041: 5.0, 6.0);
042: }
043:
044: /** Creates the default cumulative probability distribution test input values */
045: public double[] makeCumulativeTestPoints() {
046: // quantiles computed using R version 1.8.1 (linux version)
047: return new double[] { 0.03468084d, 0.09370091d, 0.1433137d,
048: 0.2020084d, 0.2937283d, 20.80266d, 8.745895d,
049: 5.987565d, 4.387374d, 3.107512d };
050: }
051:
052: /** Creates the default cumulative probability density test expected values */
053: public double[] makeCumulativeTestValues() {
054: return new double[] { 0.001d, 0.01d, 0.025d, 0.05d, 0.1d,
055: 0.999d, 0.990d, 0.975d, 0.950d, 0.900d };
056: }
057:
058: // --------------------- Override tolerance --------------
059: protected void setup() throws Exception {
060: super .setUp();
061: setTolerance(1E-6);
062: }
063:
064: //---------------------------- Additional test cases -------------------------
065:
066: public void testCumulativeProbabilityExtremes() throws Exception {
067: setCumulativeTestPoints(new double[] { -2, 0 });
068: setCumulativeTestValues(new double[] { 0, 0 });
069: verifyCumulativeProbabilities();
070: }
071:
072: public void testInverseCumulativeProbabilityExtremes()
073: throws Exception {
074: setInverseCumulativeTestPoints(new double[] { 0, 1 });
075: setInverseCumulativeTestValues(new double[] { 0,
076: Double.POSITIVE_INFINITY });
077: verifyInverseCumulativeProbabilities();
078: }
079:
080: public void testDfAccessors() {
081: FDistribution distribution = (FDistribution) getDistribution();
082: assertEquals(5d, distribution.getNumeratorDegreesOfFreedom(),
083: Double.MIN_VALUE);
084: distribution.setNumeratorDegreesOfFreedom(4d);
085: assertEquals(4d, distribution.getNumeratorDegreesOfFreedom(),
086: Double.MIN_VALUE);
087: assertEquals(6d, distribution.getDenominatorDegreesOfFreedom(),
088: Double.MIN_VALUE);
089: distribution.setDenominatorDegreesOfFreedom(4d);
090: assertEquals(4d, distribution.getDenominatorDegreesOfFreedom(),
091: Double.MIN_VALUE);
092: try {
093: distribution.setNumeratorDegreesOfFreedom(0d);
094: fail("Expecting IllegalArgumentException for df = 0");
095: } catch (IllegalArgumentException ex) {
096: // expected
097: }
098: try {
099: distribution.setDenominatorDegreesOfFreedom(0d);
100: fail("Expecting IllegalArgumentException for df = 0");
101: } catch (IllegalArgumentException ex) {
102: // expected
103: }
104: }
105:
106: public void testLargeDegreesOfFreedom() throws Exception {
107: org.apache.commons.math.distribution.FDistributionImpl fd = new org.apache.commons.math.distribution.FDistributionImpl(
108: 100000., 100000.);
109: double p = fd.cumulativeProbability(.999);
110: double x = fd.inverseCumulativeProbability(p);
111: assertEquals(.999, x, 1.0e-5);
112: }
113: }
|