View Javadoc
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.statistics.examples.distribution;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import org.apache.commons.statistics.distribution.DiscreteDistribution;
22  import org.apache.commons.statistics.distribution.HypergeometricDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link HypergeometricDistribution}.
29   */
30  @Command(name = "hypergeometric",
31           aliases = {"hyge"},
32           description = "Hypergeometric distribution.",
33           subcommands = {
34               HypergeometricCommand.Check.class,
35               HypergeometricCommand.PMF.class,
36               HypergeometricCommand.LPMF.class,
37               HypergeometricCommand.CDF.class,
38               HypergeometricCommand.SF.class,
39               HypergeometricCommand.ICDF.class,
40               HypergeometricCommand.ISF.class,
41           })
42  class HypergeometricCommand extends AbstractDistributionCommand {
43  
44      /** Base command for the distribution that defines the parameters. */
45      private abstract static class BaseCommand extends AbstractDiscreteDistributionCommand {
46          /** Distribution parameters. */
47          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
48          private Params params = new Params();
49  
50          /** Parameters class. */
51          static class Params {
52              /** The distribution N. */
53              @Option(names = {"-N", "--population-size"},
54                      paramLabel = "N",
55                      arity = "1..*",
56                      split = ",",
57                      description = {"population size (default: ${DEFAULT-VALUE})."})
58              private int[] popSize = {500};
59  
60              /** The distribution successes. */
61              @Option(names = {"-K", "--number-of-successes"},
62                      paramLabel = "K",
63                      arity = "1..*",
64                      split = ",",
65                      description = {"number of successes (default: ${DEFAULT-VALUE})."})
66              private int[] successes = {50, 60, 70};
67  
68              /** The distribution n. */
69              @Option(names = {"-n", "--sample-size"},
70                      arity = "1..*",
71                      split = ",",
72                      description = {"sample size (default: ${DEFAULT-VALUE})."})
73              private int[] n = {100, 200, 300};
74          }
75  
76          /** Extend the options to set the default values for this distribution. */
77          static final class Options extends DiscreteDistributionOptions {
78              /** Set defaults. */
79              private Options() {
80                  min = 0;
81                  max = 60;
82              }
83          }
84  
85          @Override
86          protected List<Distribution<DiscreteDistribution>> getDistributions() {
87              int[] popSize = params.popSize;
88              int[] successes = params.successes;
89              int[] n = params.n;
90              final int max = DistributionUtils.validateLengths(popSize.length, successes.length);
91  
92              popSize = DistributionUtils.expandToLength(popSize, max);
93              successes = DistributionUtils.expandToLength(successes, max);
94              n = DistributionUtils.expandToLength(n, max);
95  
96              // Create distributions
97              final ArrayList<Distribution<DiscreteDistribution>> list = new ArrayList<>();
98              for (int i = 0; i < max; i++) {
99                  final DiscreteDistribution d = HypergeometricDistribution.of(popSize[i], successes[i], n[i]);
100                 list.add(new Distribution<>(d, "N=" + popSize[i] + ",K=" + successes[i] + ",n=" + n[i]));
101             }
102             return list;
103         }
104     }
105 
106     /** Base command for the distribution that defines the parameters. */
107     private abstract static class ProbabilityCommand extends BaseCommand {
108         /** The distribution options. */
109         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
110         private Options distributionOptions = new Options();
111 
112         @Override
113         protected DistributionOptions getDistributionOptions() {
114             return distributionOptions;
115         }
116     }
117 
118     /** Base command for the distribution that defines the parameters for inverse probability functions. */
119     private abstract static class InverseProbabilityCommand extends BaseCommand {
120         /** The distribution options. */
121         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
122         private InverseDiscreteDistributionOptions distributionOptions = new InverseDiscreteDistributionOptions();
123 
124         @Override
125         protected DistributionOptions getDistributionOptions() {
126             return distributionOptions;
127         }
128     }
129 
130     /** Verification checks command. */
131     @Command(name = "check",
132              hidden = true,
133              description = "Hypergeometric distribution verification checks.")
134     static class Check extends ProbabilityCommand {}
135 
136     /** PMF command. */
137     @Command(name = "pmf",
138              aliases = {"pdf"},
139              description = "Hypergeometric distribution PMF.")
140     static class PMF extends ProbabilityCommand {}
141 
142     /** LPMF command. */
143     @Command(name = "lpmf",
144              aliases = {"lpdf"},
145              description = "Hypergeometric distribution natural logarithm of the PMF.")
146     static class LPMF extends ProbabilityCommand {}
147 
148     /** CDF command. */
149     @Command(name = "cdf",
150              description = "Hypergeometric distribution CDF.")
151     static class CDF extends ProbabilityCommand {}
152 
153     /** SF command. */
154     @Command(name = "sf",
155              description = "Hypergeometric distribution survival probability.")
156     static class SF extends ProbabilityCommand {}
157 
158     /** ICDF command. */
159     @Command(name = "icdf",
160              description = "Hypergeometric distribution inverse CDF.")
161     static class ICDF extends InverseProbabilityCommand {}
162 
163     /** ISF command. */
164     @Command(name = "isf",
165              description = "Hypergeometric distribution inverse SF.")
166     static class ISF extends InverseProbabilityCommand {}
167 }