001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.stat.inference; 018 019 import junit.framework.Test; 020 import junit.framework.TestCase; 021 import junit.framework.TestSuite; 022 023 import java.util.ArrayList; 024 import java.util.List; 025 026 /** 027 * Test cases for the OneWayAnovaImpl class. 028 * 029 * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $ 030 */ 031 032 public class OneWayAnovaTest extends TestCase { 033 034 protected OneWayAnova testStatistic = new OneWayAnovaImpl(); 035 036 private double[] emptyArray = {}; 037 038 private double[] classA = 039 {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 }; 040 private double[] classB = 041 {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 }; 042 private double[] classC = 043 {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 }; 044 045 public OneWayAnovaTest(String name) { 046 super(name); 047 } 048 049 public static Test suite() { 050 TestSuite suite = new TestSuite(OneWayAnovaTest.class); 051 suite.setName("TestStatistic Tests"); 052 return suite; 053 } 054 055 public void testAnovaFValue() throws Exception { 056 // Target comparison values computed using R version 2.6.0 (Linux version) 057 List<double[]> threeClasses = new ArrayList<double[]>(); 058 threeClasses.add(classA); 059 threeClasses.add(classB); 060 threeClasses.add(classC); 061 062 assertEquals("ANOVA F-value", 24.67361709460624, 063 testStatistic.anovaFValue(threeClasses), 1E-12); 064 065 List<double[]> twoClasses = new ArrayList<double[]>(); 066 twoClasses.add(classA); 067 twoClasses.add(classB); 068 069 assertEquals("ANOVA F-value", 0.0150579150579, 070 testStatistic.anovaFValue(twoClasses), 1E-12); 071 072 List<double[]> emptyContents = new ArrayList<double[]>(); 073 emptyContents.add(emptyArray); 074 emptyContents.add(classC); 075 try { 076 testStatistic.anovaFValue(emptyContents); 077 fail("empty array for key classX, IllegalArgumentException expected"); 078 } catch (IllegalArgumentException ex) { 079 // expected 080 } 081 082 List<double[]> tooFew = new ArrayList<double[]>(); 083 tooFew.add(classA); 084 try { 085 testStatistic.anovaFValue(tooFew); 086 fail("less than two classes, IllegalArgumentException expected"); 087 } catch (IllegalArgumentException ex) { 088 // expected 089 } 090 } 091 092 093 public void testAnovaPValue() throws Exception { 094 // Target comparison values computed using R version 2.6.0 (Linux version) 095 List<double[]> threeClasses = new ArrayList<double[]>(); 096 threeClasses.add(classA); 097 threeClasses.add(classB); 098 threeClasses.add(classC); 099 100 assertEquals("ANOVA P-value", 6.959446E-06, 101 testStatistic.anovaPValue(threeClasses), 1E-12); 102 103 List<double[]> twoClasses = new ArrayList<double[]>(); 104 twoClasses.add(classA); 105 twoClasses.add(classB); 106 107 assertEquals("ANOVA P-value", 0.904212960464, 108 testStatistic.anovaPValue(twoClasses), 1E-12); 109 110 } 111 112 public void testAnovaTest() throws Exception { 113 // Target comparison values computed using R version 2.3.1 (Linux version) 114 List<double[]> threeClasses = new ArrayList<double[]>(); 115 threeClasses.add(classA); 116 threeClasses.add(classB); 117 threeClasses.add(classC); 118 119 assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01)); 120 121 List<double[]> twoClasses = new ArrayList<double[]>(); 122 twoClasses.add(classA); 123 twoClasses.add(classB); 124 125 assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01)); 126 } 127 128 }