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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.interval;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.stat.LocalizedStatFormats;

  25. /**
  26.  * Represents an interval estimate of a population parameter.
  27.  */
  28. public class ConfidenceInterval {

  29.     /** Lower endpoint of the interval */
  30.     private double lowerBound;

  31.     /** Upper endpoint of the interval */
  32.     private double upperBound;

  33.     /**
  34.      * The asserted probability that the interval contains the population
  35.      * parameter
  36.      */
  37.     private double confidenceLevel;

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

  59.     /** Get lower endpoint of the interval.
  60.      * @return the lower endpoint of the interval
  61.      */
  62.     public double getLowerBound() {
  63.         return lowerBound;
  64.     }

  65.     /** Get upper endpoint of the interval.
  66.      * @return the upper endpoint of the interval
  67.      */
  68.     public double getUpperBound() {
  69.         return upperBound;
  70.     }

  71.     /** Get asserted probability that the interval contains the population parameter.
  72.      * @return the asserted probability that the interval contains the
  73.      *         population parameter
  74.      */
  75.     public double getConfidenceLevel() {
  76.         return confidenceLevel;
  77.     }

  78.     /** Get String representation of the confidence interval.
  79.      * @return String representation of the confidence interval
  80.      */
  81.     @Override
  82.     public String toString() {
  83.         return "[" + lowerBound + ";" + upperBound + "] (confidence level:" + confidenceLevel + ")";
  84.     }

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