ConfidenceInterval.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.stat.interval;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;

  20. /**
  21.  * Represents an interval estimate of a population parameter.
  22.  *
  23.  * @since 3.3
  24.  */
  25. public class ConfidenceInterval {

  26.     /** Lower endpoint of the interval. */
  27.     private double lowerBound;

  28.     /** Upper endpoint of the interval. */
  29.     private double upperBound;

  30.     /**
  31.      * The asserted probability that the interval contains the population.
  32.      * parameter
  33.      */
  34.     private double confidenceLevel;

  35.     /**
  36.      * Create a confidence interval with the given bounds and confidence level.
  37.      * <p>
  38.      * Preconditions:
  39.      * <ul>
  40.      * <li>{@code lower} must be strictly less than {@code upper}</li>
  41.      * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
  42.      * </ul>
  43.      *
  44.      * @param lowerBound lower endpoint of the interval
  45.      * @param upperBound upper endpoint of the interval
  46.      * @param confidenceLevel coverage probability
  47.      * @throws MathIllegalArgumentException if the preconditions are not met
  48.      */
  49.     public ConfidenceInterval(double lowerBound, double upperBound, double confidenceLevel) {
  50.         checkParameters(lowerBound, upperBound, confidenceLevel);
  51.         this.lowerBound = lowerBound;
  52.         this.upperBound = upperBound;
  53.         this.confidenceLevel = confidenceLevel;
  54.     }

  55.     /**
  56.      * @return the lower endpoint of the interval
  57.      */
  58.     public double getLowerBound() {
  59.         return lowerBound;
  60.     }

  61.     /**
  62.      * @return the upper endpoint of the interval
  63.      */
  64.     public double getUpperBound() {
  65.         return upperBound;
  66.     }

  67.     /**
  68.      * @return the asserted probability that the interval contains the
  69.      *         population parameter
  70.      */
  71.     public double getConfidenceLevel() {
  72.         return confidenceLevel;
  73.     }

  74.     /**
  75.      * @return String representation of the confidence interval
  76.      */
  77.     @Override
  78.     public String toString() {
  79.         return "[" + lowerBound + ";" + upperBound + "] (confidence level:" + confidenceLevel + ")";
  80.     }

  81.     /**
  82.      * Verifies that (lower, upper) is a valid non-empty interval and confidence
  83.      * is strictly between 0 and 1.
  84.      *
  85.      * @param lower lower endpoint
  86.      * @param upper upper endpoint
  87.      * @param confidence confidence level
  88.      */
  89.     private void checkParameters(double lower, double upper, double confidence) {
  90.         if (lower >= upper) {
  91.             throw new MathIllegalArgumentException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper);
  92.         }
  93.         if (confidence <= 0 || confidence >= 1) {
  94.             throw new MathIllegalArgumentException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL, confidence, 0, 1);
  95.         }
  96.     }
  97. }