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