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