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.ContinuousDistribution;
22 import org.apache.commons.statistics.distribution.TrapezoidalDistribution;
23 import picocli.CommandLine.ArgGroup;
24 import picocli.CommandLine.Command;
25 import picocli.CommandLine.Option;
26
27
28
29
30 @Command(name = "trapezoidal",
31 aliases = {"trap"},
32 description = "Trapezoidal distribution.",
33 subcommands = {
34 TrapezoidalCommand.Check.class,
35 TrapezoidalCommand.PDF.class,
36 TrapezoidalCommand.LPDF.class,
37 TrapezoidalCommand.CDF.class,
38 TrapezoidalCommand.SF.class,
39 TrapezoidalCommand.ICDF.class,
40 TrapezoidalCommand.ISF.class,
41 })
42 class TrapezoidalCommand extends AbstractDistributionCommand {
43
44
45 private abstract static class BaseCommand extends AbstractContinuousDistributionCommand {
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 = {"-a", "--lower"},
54 paramLabel = "a",
55 arity = "1..*",
56 split = ",",
57 description = {"lower bound (default: ${DEFAULT-VALUE})."})
58 private double[] lower = {-4, 0, 0};
59
60
61 @Option(names = {"-b", "--shape1"},
62 paramLabel = "b",
63 arity = "1..*",
64 split = ",",
65 description = {"constant start (default: ${DEFAULT-VALUE})."})
66 private double[] start = {-1.5, 0, 1};
67
68
69 @Option(names = {"-c", "--shape2"},
70 paramLabel = "c",
71 arity = "1..*",
72 split = ",",
73 description = {"constant end (default: ${DEFAULT-VALUE})."})
74 private double[] end = {2.5, 2, 3};
75
76
77 @Option(names = {"-d", "--upper"},
78 paramLabel = "d",
79 arity = "1..*",
80 split = ",",
81 description = {"upper bound (default: ${DEFAULT-VALUE})."})
82 private double[] upper = {4, 3, 3};
83 }
84
85
86 static final class Options extends ContinuousDistributionOptions {
87
88 private Options() {
89 min = -5;
90 max = 5;
91 }
92 }
93
94 @Override
95 protected List<Distribution<ContinuousDistribution>> getDistributions() {
96 double[] a = params.lower;
97 double[] b = params.start;
98 double[] c = params.end;
99 double[] d = params.upper;
100 final int n = DistributionUtils.validateLengths(a.length, b.length, c.length, d.length);
101
102 a = DistributionUtils.expandToLength(a, n);
103 b = DistributionUtils.expandToLength(b, n);
104 c = DistributionUtils.expandToLength(c, n);
105 d = DistributionUtils.expandToLength(d, n);
106
107
108 final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
109 for (int i = 0; i < n; i++) {
110 final ContinuousDistribution dist = TrapezoidalDistribution.of(a[i], b[i], c[i], d[i]);
111 list.add(new Distribution<>(dist, "a=" + a[i] + ",b=" + b[i] + ",c=" + c[i] + ",d=" + d[i]));
112 }
113 return list;
114 }
115 }
116
117
118 private abstract static class ProbabilityCommand extends BaseCommand {
119
120 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
121 private Options distributionOptions = new Options();
122
123 @Override
124 protected DistributionOptions getDistributionOptions() {
125 return distributionOptions;
126 }
127 }
128
129
130 private abstract static class InverseProbabilityCommand extends BaseCommand {
131
132 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
133 private InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
134
135 @Override
136 protected DistributionOptions getDistributionOptions() {
137 return distributionOptions;
138 }
139 }
140
141
142 @Command(name = "check",
143 hidden = true,
144 description = "Trapezoidal distribution verification checks.")
145 static class Check extends ProbabilityCommand {}
146
147
148 @Command(name = "pdf",
149 description = "Trapezoidal distribution PDF.")
150 static class PDF extends ProbabilityCommand {}
151
152
153 @Command(name = "lpdf",
154 description = "Trapezoidal distribution natural logarithm of the PDF.")
155 static class LPDF extends ProbabilityCommand {}
156
157
158 @Command(name = "cdf",
159 description = "Trapezoidal distribution CDF.")
160 static class CDF extends ProbabilityCommand {}
161
162
163 @Command(name = "sf",
164 description = "Trapezoidal distribution survival probability.")
165 static class SF extends ProbabilityCommand {}
166
167
168 @Command(name = "icdf",
169 description = "Trapezoidal distribution inverse CDF.")
170 static class ICDF extends InverseProbabilityCommand {}
171
172
173 @Command(name = "isf",
174 description = "Trapezoidal distribution inverse SF.")
175 static class ISF extends InverseProbabilityCommand {}
176 }