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
19 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
21
22 /**
23 * Represents an interval estimate of a population parameter.
24 *
25 * @since 3.3
26 */
27 public class ConfidenceInterval {
28
29 /** Lower endpoint of the interval. */
30 private double lowerBound;
31
32 /** Upper endpoint of the interval. */
33 private double upperBound;
34
35 /**
36 * The asserted probability that the interval contains the population.
37 * parameter
38 */
39 private double confidenceLevel;
40
41 /**
42 * Create a confidence interval with the given bounds and confidence level.
43 * <p>
44 * Preconditions:
45 * <ul>
46 * <li>{@code lower} must be strictly less than {@code upper}</li>
47 * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
48 * </ul>
49 *
50 * @param lowerBound lower endpoint of the interval
51 * @param upperBound upper endpoint of the interval
52 * @param confidenceLevel coverage probability
53 * @throws MathIllegalArgumentException if the preconditions are not met
54 */
55 public ConfidenceInterval(double lowerBound, double upperBound, double confidenceLevel) {
56 checkParameters(lowerBound, upperBound, confidenceLevel);
57 this.lowerBound = lowerBound;
58 this.upperBound = upperBound;
59 this.confidenceLevel = confidenceLevel;
60 }
61
62 /**
63 * @return the lower endpoint of the interval
64 */
65 public double getLowerBound() {
66 return lowerBound;
67 }
68
69 /**
70 * @return the upper endpoint of the interval
71 */
72 public double getUpperBound() {
73 return upperBound;
74 }
75
76 /**
77 * @return the asserted probability that the interval contains the
78 * population parameter
79 */
80 public double getConfidenceLevel() {
81 return confidenceLevel;
82 }
83
84 /**
85 * @return String representation of the confidence interval
86 */
87 @Override
88 public String toString() {
89 return "[" + lowerBound + ";" + upperBound + "] (confidence level:" + confidenceLevel + ")";
90 }
91
92 /**
93 * Verifies that (lower, upper) is a valid non-empty interval and confidence
94 * is strictly between 0 and 1.
95 *
96 * @param lower lower endpoint
97 * @param upper upper endpoint
98 * @param confidence confidence level
99 */
100 private void checkParameters(double lower, double upper, double confidence) {
101 if (lower >= upper) {
102 throw new MathIllegalArgumentException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper);
103 }
104 if (confidence <= 0 || confidence >= 1) {
105 throw new MathIllegalArgumentException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL, confidence, 0, 1);
106 }
107 }
108 }