source: src/main/java/agents/anac/y2019/harddealer/math3/stat/interval/IntervalUtils.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 8.6 KB
Line 
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 */
17package agents.anac.y2019.harddealer.math3.stat.interval;
18
19import agents.anac.y2019.harddealer.math3.exception.NotPositiveException;
20import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
21import agents.anac.y2019.harddealer.math3.exception.NumberIsTooLargeException;
22import agents.anac.y2019.harddealer.math3.exception.OutOfRangeException;
23import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
24
25/**
26 * Factory methods to generate confidence intervals for a binomial proportion.
27 * The supported methods are:
28 * <ul>
29 * <li>Agresti-Coull interval</li>
30 * <li>Clopper-Pearson method (exact method)</li>
31 * <li>Normal approximation (based on central limit theorem)</li>
32 * <li>Wilson score interval</li>
33 * </ul>
34 *
35 * @since 3.3
36 */
37public final class IntervalUtils {
38
39 /** Singleton Agresti-Coull instance. */
40 private static final BinomialConfidenceInterval AGRESTI_COULL = new AgrestiCoullInterval();
41
42 /** Singleton Clopper-Pearson instance. */
43 private static final BinomialConfidenceInterval CLOPPER_PEARSON = new ClopperPearsonInterval();
44
45 /** Singleton NormalApproximation instance. */
46 private static final BinomialConfidenceInterval NORMAL_APPROXIMATION = new NormalApproximationInterval();
47
48 /** Singleton Wilson score instance. */
49 private static final BinomialConfidenceInterval WILSON_SCORE = new WilsonScoreInterval();
50
51 /**
52 * Prevent instantiation.
53 */
54 private IntervalUtils() {
55 }
56
57 /**
58 * Create an Agresti-Coull binomial confidence interval for the true
59 * probability of success of an unknown binomial distribution with the given
60 * observed number of trials, successes and confidence level.
61 *
62 * @param numberOfTrials number of trials
63 * @param numberOfSuccesses number of successes
64 * @param confidenceLevel desired probability that the true probability of
65 * success falls within the returned interval
66 * @return Confidence interval containing the probability of success with
67 * probability {@code confidenceLevel}
68 * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
69 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
70 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
71 * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
72 */
73 public static ConfidenceInterval getAgrestiCoullInterval(int numberOfTrials, int numberOfSuccesses,
74 double confidenceLevel) {
75 return AGRESTI_COULL.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
76 }
77
78 /**
79 * Create a Clopper-Pearson binomial confidence interval for the true
80 * probability of success of an unknown binomial distribution with the given
81 * observed number of trials, successes and confidence level.
82 * <p>
83 * Preconditions:
84 * <ul>
85 * <li>{@code numberOfTrials} must be positive</li>
86 * <li>{@code numberOfSuccesses} may not exceed {@code numberOfTrials}</li>
87 * <li>{@code confidenceLevel} must be strictly between 0 and 1 (exclusive)</li>
88 * </ul>
89 * </p>
90 *
91 * @param numberOfTrials number of trials
92 * @param numberOfSuccesses number of successes
93 * @param confidenceLevel desired probability that the true probability of
94 * success falls within the returned interval
95 * @return Confidence interval containing the probability of success with
96 * probability {@code confidenceLevel}
97 * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
98 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
99 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
100 * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
101 */
102 public static ConfidenceInterval getClopperPearsonInterval(int numberOfTrials, int numberOfSuccesses,
103 double confidenceLevel) {
104 return CLOPPER_PEARSON.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
105 }
106
107 /**
108 * Create a binomial confidence interval for the true probability of success
109 * of an unknown binomial distribution with the given observed number of
110 * trials, successes and confidence level using the Normal approximation to
111 * the binomial distribution.
112 *
113 * @param numberOfTrials number of trials
114 * @param numberOfSuccesses number of successes
115 * @param confidenceLevel desired probability that the true probability of
116 * success falls within the interval
117 * @return Confidence interval containing the probability of success with
118 * probability {@code confidenceLevel}
119 */
120 public static ConfidenceInterval getNormalApproximationInterval(int numberOfTrials, int numberOfSuccesses,
121 double confidenceLevel) {
122 return NORMAL_APPROXIMATION.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
123 }
124
125 /**
126 * Create a Wilson score binomial confidence interval for the true
127 * probability of success of an unknown binomial distribution with the given
128 * observed number of trials, successes and confidence level.
129 *
130 * @param numberOfTrials number of trials
131 * @param numberOfSuccesses number of successes
132 * @param confidenceLevel desired probability that the true probability of
133 * success falls within the returned interval
134 * @return Confidence interval containing the probability of success with
135 * probability {@code confidenceLevel}
136 * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
137 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
138 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
139 * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
140 */
141 public static ConfidenceInterval getWilsonScoreInterval(int numberOfTrials, int numberOfSuccesses,
142 double confidenceLevel) {
143 return WILSON_SCORE.createInterval(numberOfTrials, numberOfSuccesses, confidenceLevel);
144 }
145
146 /**
147 * Verifies that parameters satisfy preconditions.
148 *
149 * @param numberOfTrials number of trials (must be positive)
150 * @param numberOfSuccesses number of successes (must not exceed numberOfTrials)
151 * @param confidenceLevel confidence level (must be strictly between 0 and 1)
152 * @throws NotStrictlyPositiveException if {@code numberOfTrials <= 0}.
153 * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
154 * @throws NumberIsTooLargeException if {@code numberOfSuccesses > numberOfTrials}.
155 * @throws OutOfRangeException if {@code confidenceLevel} is not in the interval {@code (0, 1)}.
156 */
157 static void checkParameters(int numberOfTrials, int numberOfSuccesses, double confidenceLevel) {
158 if (numberOfTrials <= 0) {
159 throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, numberOfTrials);
160 }
161 if (numberOfSuccesses < 0) {
162 throw new NotPositiveException(LocalizedFormats.NEGATIVE_NUMBER_OF_SUCCESSES, numberOfSuccesses);
163 }
164 if (numberOfSuccesses > numberOfTrials) {
165 throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE,
166 numberOfSuccesses, numberOfTrials, true);
167 }
168 if (confidenceLevel <= 0 || confidenceLevel >= 1) {
169 throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUNDS_CONFIDENCE_LEVEL,
170 confidenceLevel, 0, 1);
171 }
172 }
173
174}
Note: See TracBrowser for help on using the repository browser.