1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
45 private abstract static class BaseCommand extends AbstractDiscreteDistributionCommand {
46
47 @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
48 private Params params = new Params();
49
50
51 static class Params {
52
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
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
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
77 static final class Options extends DiscreteDistributionOptions {
78
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
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
107 private abstract static class ProbabilityCommand extends BaseCommand {
108
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
119 private abstract static class InverseProbabilityCommand extends BaseCommand {
120
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
131 @Command(name = "check",
132 hidden = true,
133 description = "Hypergeometric distribution verification checks.")
134 static class Check extends ProbabilityCommand {}
135
136
137 @Command(name = "pmf",
138 aliases = {"pdf"},
139 description = "Hypergeometric distribution PMF.")
140 static class PMF extends ProbabilityCommand {}
141
142
143 @Command(name = "lpmf",
144 aliases = {"lpdf"},
145 description = "Hypergeometric distribution natural logarithm of the PMF.")
146 static class LPMF extends ProbabilityCommand {}
147
148
149 @Command(name = "cdf",
150 description = "Hypergeometric distribution CDF.")
151 static class CDF extends ProbabilityCommand {}
152
153
154 @Command(name = "sf",
155 description = "Hypergeometric distribution survival probability.")
156 static class SF extends ProbabilityCommand {}
157
158
159 @Command(name = "icdf",
160 description = "Hypergeometric distribution inverse CDF.")
161 static class ICDF extends InverseProbabilityCommand {}
162
163
164 @Command(name = "isf",
165 description = "Hypergeometric distribution inverse SF.")
166 static class ISF extends InverseProbabilityCommand {}
167 }