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