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