    1   /*
    2    * Copyright 2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.math.distribution;
    18
    19  /***
    20   * Test cases for NormalDistribution.
    21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
    22   * ContinuousDistributionAbstractTest for details.
    23   *
    24   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
    25   */
    26  public class NormalDistributionTest extends ContinuousDistributionAbstractTest  {
    27
    28      /***
    29       * Constructor for NormalDistributionTest.
    30       * @param arg0
    31       */
    32      public NormalDistributionTest(String arg0) {
    33          super(arg0);
    34      }
    35
    36      public static void main(String[] args) {
    37          junit.swingui.TestRunner.run(NormalDistributionTest.class);
    38      }
    39
    40      //-------------- Implementations for abstract methods -----------------------
    41
    42      /*** Creates the default continuous distribution instance to use in tests. */
    43      public ContinuousDistribution makeDistribution() {
    44          return DistributionFactory.newInstance().createNormalDistribution(2.1, 1.4);
    45      }
    46
    47      /*** Creates the default cumulative probability distribution test input values */
    48      public double[] makeCumulativeTestPoints() {
    49          // quantiles computed using R
    50          return new double[] {-2.226325d, -1.156887d, -0.6439496d, -0.2027951d, 0.3058278d,
    51                  6.426325d, 5.356887d, 4.84395d, 4.402795d, 3.894172d};
    52      }
    53
    54      /*** Creates the default cumulative probability density test expected values */
    55      public double[] makeCumulativeTestValues() {
    56          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
    57                  0.990d, 0.975d, 0.950d, 0.900d};
    58      }
    59
    60      // --------------------- Override tolerance  --------------
    61      protected void setup() throws Exception {
    62          super.setUp();
    63          setTolerance(1E-6);
    64      }
    65
    66      //---------------------------- Additional test cases -------------------------
    67
    68      private void verifyQuantiles() throws Exception {
    69          NormalDistribution distribution = (NormalDistribution) getDistribution();
    70          double mu = distribution.getMean();
    71          double sigma = distribution.getStandardDeviation();
    72          setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
    73                  mu, mu + sigma, mu +2 * sigma,  mu +3 * sigma, mu + 4 * sigma,
    74                  mu + 5 * sigma});
    75          // Quantiles computed using R (same as Mathematica)
    76          setCumulativeTestValues(new double[] {0.02275013, 0.1586553, 0.5, 0.8413447,
    77                  0.9772499, 0.9986501, 0.9999683,  0.9999997});
    78          verifyCumulativeProbabilities();
    79      }
    80
    81      public void testQuantiles() throws Exception {
    82          verifyQuantiles();
    83          setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 1));
    84          verifyQuantiles();
    85          setDistribution(DistributionFactory.newInstance().createNormalDistribution(0, 0.1));
    86          verifyQuantiles();
    87      }
    88
    89      public void testInverseCumulativeProbabilityExtremes() throws Exception {
    90          setInverseCumulativeTestPoints(new double[] {0, 1});
    91          setInverseCumulativeTestValues(
    92                  new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
    93          verifyInverseCumulativeProbabilities();
    94      }
    95
    96      public void testGetMean() {
    97          NormalDistribution distribution = (NormalDistribution) getDistribution();
    98          assertEquals(2.1, distribution.getMean(), 0);
    99      }
    100
    101     public void testSetMean() throws Exception {
    102         double mu = Math.random();
    103         NormalDistribution distribution = (NormalDistribution) getDistribution();
    104         distribution.setMean(mu);
    105         verifyQuantiles();
    106     }
    107
    108     public void testGetStandardDeviation() {
    109         NormalDistribution distribution = (NormalDistribution) getDistribution();
    110         assertEquals(1.4, distribution.getStandardDeviation(), 0);
    111     }
    112
    113     public void testSetStandardDeviation() throws Exception {
    114         double sigma = 0.1d + Math.random();
    115         NormalDistribution distribution = (NormalDistribution) getDistribution();
    116         distribution.setStandardDeviation(sigma);
    117         assertEquals(sigma, distribution.getStandardDeviation(), 0);
    118         verifyQuantiles();
    119         try {
    120             distribution.setStandardDeviation(0);
    121             fail("Expecting IllegalArgumentException for sd = 0");
    122         } catch (IllegalArgumentException ex) {
    123             // Expected
    124         }
    125     }
    126 }