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