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.BinomialDistribution;
22 import org.apache.commons.statistics.distribution.DiscreteDistribution;
23 import picocli.CommandLine.ArgGroup;
24 import picocli.CommandLine.Command;
25 import picocli.CommandLine.Option;
26
27
28
29
30 @Command(name = "binomial",
31 aliases = {"bino"},
32 description = "Binomial distribution.",
33 subcommands = {
34 BinomialCommand.Check.class,
35 BinomialCommand.PMF.class,
36 BinomialCommand.LPMF.class,
37 BinomialCommand.CDF.class,
38 BinomialCommand.SF.class,
39 BinomialCommand.ICDF.class,
40 BinomialCommand.ISF.class,
41 })
42 class BinomialCommand 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", "--trials"},
54 arity = "1..*",
55 split = ",",
56 description = {"number of trials (default: ${DEFAULT-VALUE})."})
57 private int[] trials = {20, 20, 40};
58
59
60 @Option(names = {"-p", "--probability"},
61 arity = "1..*",
62 split = ",",
63 description = {"probability of success (default: ${DEFAULT-VALUE})."})
64 private double[] p = {0.5, 0.7, 0.5};
65 }
66
67
68 static final class Options extends DiscreteDistributionOptions {
69
70 private Options() {
71 min = 0;
72 max = 40;
73 }
74 }
75
76 @Override
77 protected List<Distribution<DiscreteDistribution>> getDistributions() {
78 int[] trials = params.trials;
79 double[] p = params.p;
80 final int n = DistributionUtils.validateLengths(trials.length, p.length);
81
82 trials = DistributionUtils.expandToLength(trials, n);
83 p = DistributionUtils.expandToLength(p, n);
84
85
86 final ArrayList<Distribution<DiscreteDistribution>> list = new ArrayList<>();
87 for (int i = 0; i < n; i++) {
88 final DiscreteDistribution d = BinomialDistribution.of(trials[i], p[i]);
89 list.add(new Distribution<>(d, "trials=" + trials[i] + ",p=" + p[i]));
90 }
91 return list;
92 }
93 }
94
95
96 private abstract static class ProbabilityCommand extends BaseCommand {
97
98 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
99 private Options distributionOptions = new Options();
100
101 @Override
102 protected DistributionOptions getDistributionOptions() {
103 return distributionOptions;
104 }
105 }
106
107
108 private abstract static class InverseProbabilityCommand extends BaseCommand {
109
110 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
111 private InverseDiscreteDistributionOptions distributionOptions = new InverseDiscreteDistributionOptions();
112
113 @Override
114 protected DistributionOptions getDistributionOptions() {
115 return distributionOptions;
116 }
117 }
118
119
120 @Command(name = "check",
121 hidden = true,
122 description = "Binomial distribution verification checks.")
123 static class Check extends ProbabilityCommand {}
124
125
126 @Command(name = "pmf",
127 aliases = {"pdf"},
128 description = "Binomial distribution PMF.")
129 static class PMF extends ProbabilityCommand {}
130
131
132 @Command(name = "lpmf",
133 aliases = {"lpdf"},
134 description = "Binomial distribution natural logarithm of the PMF.")
135 static class LPMF extends ProbabilityCommand {}
136
137
138 @Command(name = "cdf",
139 description = "Binomial distribution CDF.")
140 static class CDF extends ProbabilityCommand {}
141
142
143 @Command(name = "sf",
144 description = "Binomial distribution survival probability.")
145 static class SF extends ProbabilityCommand {}
146
147
148 @Command(name = "icdf",
149 description = "Binomial distribution inverse CDF.")
150 static class ICDF extends InverseProbabilityCommand {}
151
152
153 @Command(name = "isf",
154 description = "Binomial distribution inverse SF.")
155 static class ISF extends InverseProbabilityCommand {}
156 }