    1   package baseCode.math;
    2
    3   import junit.framework.TestCase;
    4   import baseCode.math.DescriptiveWithMissing;
    5   import baseCode.util.RegressionTesting;
    6   import cern.colt.list.DoubleArrayList;
    7   import cern.jet.stat.Descriptive;
    8
    9   /***
    10   * <p>
    11   * Title:
    12   * </p>
    13   * <p>
    14   * Description:
    15   * </p>
    16   * <p>
    17   * Copyright: Copyright (c) 2004
    18   * </p>
    19   * <p>
    20   * Institution:: Columbia University
    21   * </p>
    22   *
    23   * @author Paul Pavlidis
    24   * @version $Id: TestDescriptiveWithMissing.java,v 1.1 2005/03/17 13:58:41 pavlidis Exp $
    25   */
    26
    27  public class TestDescriptiveWithMissing extends TestCase {
    28
    29     private DoubleArrayList data1missing;
    30     private DoubleArrayList data1Nomissing;
    31     private DoubleArrayList data2missing;
    32     private DoubleArrayList data2Nomissing;
    33     private DoubleArrayList data3shortmissing;
    34     private DoubleArrayList data3shortNomissing;
    35
    36     private DoubleArrayList datacortest1Nomissing;
    37     private DoubleArrayList datacortest2Nomissing;
    38     private DoubleArrayList dataAcortest1Nomissing;
    39
    40     protected void setUp() throws Exception {
    41        super.setUp();
    42        data1missing = new DoubleArrayList( new double[] {
    43              1.0, Double.NaN, 3.0, 4.0, 5.0, 6.0
    44        } );
    45        data2missing = new DoubleArrayList( new double[] {
    46              Double.NaN, Double.NaN, 3.0, Double.NaN, 3.5, 4.0
    47        } );
    48        data3shortmissing = new DoubleArrayList( new double[] {
    49              Double.NaN, Double.NaN, 3.0
    50        } );
    51
    52        /* versions of the above, but without the NaNs */
    53        data1Nomissing = new DoubleArrayList( new double[] {
    54              1.0, 3.0, 4.0, 5.0, 6.0
    55        } );
    56        data2Nomissing = new DoubleArrayList( new double[] {
    57              3.0, 3.5, 4.0
    58        } );
    59        data3shortNomissing = new DoubleArrayList( new double[] {
    60           3.0
    61        } );
    62
    63        datacortest1Nomissing = new DoubleArrayList( new double[] {
    64              3.0, 5.0, 6.0
    65        } );
    66        dataAcortest1Nomissing = new DoubleArrayList( new double[] {
    67              1.0, 3.0, 5.0, 6.0
    68        } );
    69        datacortest2Nomissing = new DoubleArrayList( new double[] {
    70              3.0, 3.5, 4.0
    71        } );
    72
    73     }
    74
    75     protected void tearDown() throws Exception {
    76        super.tearDown();
    77     }
    78
    79     public void testCorrelationA() {
    80        double s1 = Math.sqrt( Descriptive.sampleVariance(
    81              datacortest1Nomissing.size(), Descriptive
    82                    .sum( datacortest1Nomissing ), Descriptive
    83                    .sumOfSquares( datacortest1Nomissing ) ) );
    84        double s2 = Math.sqrt( Descriptive.sampleVariance(
    85              datacortest2Nomissing.size(), Descriptive
    86                    .sum( datacortest2Nomissing ), Descriptive
    87                    .sumOfSquares( datacortest2Nomissing ) ) );
    88
    89        double expectedReturn = Descriptive.correlation( datacortest1Nomissing,
    90              s1, datacortest2Nomissing, s2 );
    91        double actualReturn = DescriptiveWithMissing.correlation( data1missing,
    92              data2missing );
    93        assertEquals( "return value", expectedReturn, actualReturn,
    94              1e-15 );
    95     }
    96
    97     public void testCorrelationB() {
    98        double s1 = Math.sqrt( Descriptive.sampleVariance(
    99              datacortest1Nomissing.size(), Descriptive
    100                   .sum( datacortest1Nomissing ), Descriptive
    101                   .sumOfSquares( datacortest1Nomissing ) ) );
    102       double s2 = Math.sqrt(  Descriptive.sampleVariance(
    103             datacortest2Nomissing.size(), Descriptive
    104                   .sum( datacortest2Nomissing ), Descriptive
    105                   .sumOfSquares( datacortest2Nomissing ) ) );
    106
    107       double s1m = 0;
    108       double s2m = 0;
    109       double expectedReturn = Descriptive.correlation( datacortest1Nomissing,
    110             s1, datacortest2Nomissing, s2 );
    111       double actualReturn = DescriptiveWithMissing.correlation( data1missing,
    112             s1m, data2missing, s2m );
    113
    114       assertEquals( "return value", expectedReturn, actualReturn,
    115             1e-15 );
    116    }
    117
    118    public void testCovariance() {
    119       double expectedReturn = Descriptive.covariance( datacortest1Nomissing,
    120             datacortest2Nomissing );
    121       double actualReturn = DescriptiveWithMissing.covariance( data1missing,
    122             data2missing );
    123       assertEquals( "return value", expectedReturn, actualReturn,
    124             Double.MIN_VALUE );
    125    }
    126
    127    public void testDurbinWatson() {
    128       double expectedReturn = Descriptive.durbinWatson( data1Nomissing );
    129       double actualReturn = DescriptiveWithMissing.durbinWatson( data1missing );
    130       assertEquals( "return value", expectedReturn, actualReturn,
    131             Double.MIN_VALUE );
    132    }
    133
    134    public void testDurbinWatsonTwo() {
    135       double expectedReturn = Descriptive.durbinWatson( data2Nomissing );
    136       double actualReturn = DescriptiveWithMissing.durbinWatson( data2missing );
    137       assertEquals( "return value", expectedReturn, actualReturn,
    138             Double.MIN_VALUE );
    139    }
    140
    141    public void testDurbinWatsonShort() {
    142
    143       try {
    144          double expectedReturn = Descriptive.durbinWatson( data3shortNomissing );
    145          double actualReturn = DescriptiveWithMissing
    146                .durbinWatson( data3shortmissing );
    147          assertEquals( "Short array failure.", expectedReturn, actualReturn,
    148                Double.MIN_VALUE );
    149          fail( "Should have thrown an IllegalArgumentException" );
    150       } catch ( IllegalArgumentException e ) {
    151          return;
    152       } catch ( Exception e ) {
    153          fail( "Threw wrong exception: " + e );
    154       }
    155    }
    156
    157    public void testGeometricMean() {
    158       double expectedReturn = Descriptive.geometricMean( data1Nomissing );
    159       double actualReturn = DescriptiveWithMissing.geometricMean( data1missing );
    160       assertEquals( "Excercises sumOfLogarithms too; return value",
    161             expectedReturn, actualReturn, Double.MIN_VALUE );
    162
    163    }
    164
    165    public void testMean() {
    166       double expectedReturn = Descriptive.mean( data1Nomissing );
    167       double actualReturn = DescriptiveWithMissing.mean( data1missing );
    168       assertEquals( "return value", expectedReturn, actualReturn,
    169             Double.MIN_VALUE );
    170
    171    }
    172
    173    public void testMin() {
    174       double expectedReturn = Descriptive.min( data1Nomissing );
    175       double actualReturn = DescriptiveWithMissing.min( data1missing );
    176       assertEquals( "return value", expectedReturn, actualReturn,
    177             Double.MIN_VALUE );
    178    }
    179
    180    public void testMax() {
    181       double expectedReturn = Descriptive.max( data1Nomissing );
    182       double actualReturn = DescriptiveWithMissing.max( data1missing );
    183       assertEquals( "return value", expectedReturn, actualReturn,
    184             Double.MIN_VALUE );
    185    }
    186
    187    public void testMedian() {
    188       data1missing.sort();
    189       data1Nomissing.sort();
    190       double expectedReturn = Descriptive.median( data1Nomissing );
    191       double actualReturn = DescriptiveWithMissing.median( data1missing );
    192       assertEquals( "return value", expectedReturn, actualReturn,
    193             Double.MIN_VALUE );
    194    }
    195
    196    public void testProduct() {
    197       double expectedReturn = Descriptive.product( data1Nomissing );
    198       double actualReturn = DescriptiveWithMissing.product( data1missing );
    199       assertEquals( "return value", expectedReturn, actualReturn,
    200             Double.MIN_VALUE );
    201    }
    202
    203    public void testSampleKurtosis() {
    204       double expectedReturn = Descriptive.sampleKurtosis( data1Nomissing,
    205             Descriptive.mean( data1Nomissing ), Descriptive.sampleVariance(
    206                   data1Nomissing, Descriptive.mean( data1Nomissing ) ) );
    207       double actualReturn = DescriptiveWithMissing.sampleKurtosis(
    208             data1missing, DescriptiveWithMissing.mean( data1missing ),
    209             DescriptiveWithMissing.sampleVariance( data1missing,
    210                   DescriptiveWithMissing.mean( data1missing ) ) );
    211       assertEquals( "Exercises sampleVariance, mean as well; return value",
    212             expectedReturn, actualReturn, Double.MIN_VALUE );
    213    }
    214
    215    public void testQuantile() {
    216       data1missing.sort();
    217       data1Nomissing.sort();
    218       double expectedReturn = Descriptive.quantile( data1Nomissing, 0.10 );
    219       double actualReturn = DescriptiveWithMissing
    220             .quantile( data1missing, 0.10 );
    221       assertEquals( "return value", expectedReturn, actualReturn,
    222             Double.MIN_VALUE );
    223
    224    }
    225
    226    public void testSum() {
    227       double expectedReturn = Descriptive.sum( data1Nomissing );
    228       double actualReturn = DescriptiveWithMissing.sum( data1missing );
    229       assertEquals( "return value", expectedReturn, actualReturn,
    230             Double.MIN_VALUE );
    231    }
    232
    233    public void testSumOfSquares() {
    234       double expectedReturn = Descriptive.sumOfSquares( data1Nomissing );
    235       double actualReturn = DescriptiveWithMissing.sumOfSquares( data1missing );
    236       assertEquals( "return value", expectedReturn, actualReturn,
    237             Double.MIN_VALUE );
    238    }
    239
    240    public void testSampleVariance() {
    241       double expectedReturn = Descriptive.sampleVariance( data1Nomissing,
    242             Descriptive.mean( data1Nomissing ) );
    243       double actualReturn = DescriptiveWithMissing.sampleVariance(
    244             data1missing, DescriptiveWithMissing.mean( data1missing ) );
    245       assertEquals( "return value", expectedReturn, actualReturn,
    246             Double.MIN_VALUE );
    247
    248    }
    249
    250    public void testStandardize() {
    251
    252       // we use this because Descriptive.standardize does not do exactly the
    253       // same thing - there is a correction applied.
    254       DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
    255             -1.4556506857481, Double.NaN, -0.415900195928029,
    256             0.103975048982007, 0.623850293892044, 1.14372553880208
    257       } );
    258
    259       DescriptiveWithMissing.standardize( data1missing );
    260       assertEquals( true, RegressionTesting.closeEnough( data1missing,
    261             expectedReturn, 0.0001 ) );
    262    }
    263
    264    public void testTrimmedMean() {
    265       data1Nomissing.sort();
    266       data1missing.sort();
    267       double expectedReturn = Descriptive.trimmedMean( data1Nomissing,
    268             Descriptive.mean( data1Nomissing ), 1, 1 );
    269       double actualReturn = DescriptiveWithMissing.trimmedMean( data1missing,
    270             DescriptiveWithMissing.mean( data1missing ), 1, 1 );
    271       assertEquals( "return value", expectedReturn, actualReturn,
    272             Double.MIN_VALUE );
    273
    274    }
    275
    276    public void testVariance() {
    277       double expectedReturn = Descriptive.variance( data1Nomissing.size(),
    278             Descriptive.sum( data1Nomissing ), Descriptive
    279                   .sumOfSquares( data1Nomissing ) );
    280       double actualReturn = DescriptiveWithMissing.variance(
    281             DescriptiveWithMissing.sizeWithoutMissingValues( data1missing ),
    282             DescriptiveWithMissing.sum( data1missing ), DescriptiveWithMissing
    283                   .sumOfSquares( data1missing ) );
    284       assertEquals( "return value", expectedReturn, actualReturn, 0.000001 );
    285    }
    286
    287 }