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.NormalDistribution;
23 import picocli.CommandLine.ArgGroup;
24 import picocli.CommandLine.Command;
25 import picocli.CommandLine.Option;
26
27
28
29
30 @Command(name = "normal",
31 aliases = {"norm"},
32 description = "Normal distribution.",
33 subcommands = {
34 NormalCommand.Check.class,
35 NormalCommand.PDF.class,
36 NormalCommand.LPDF.class,
37 NormalCommand.CDF.class,
38 NormalCommand.SF.class,
39 NormalCommand.ICDF.class,
40 NormalCommand.ISF.class,
41 })
42 class NormalCommand 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 = {"-m", "--mu", "--mean"},
54 arity = "1..*",
55 split = ",",
56 description = {"mean (default: ${DEFAULT-VALUE})."})
57 private double[] mu = {0, 0, 0, -2};
58
59
60 @Option(names = {"-s", "--sigma"},
61 arity = "1..*",
62 split = ",",
63 description = {"standard deviation (default: ${DEFAULT-VALUE})."})
64
65 private double[] sigma = {Math.sqrt(0.2), 1, Math.sqrt(5), Math.sqrt(0.5)};
66 }
67
68
69 static final class Options extends ContinuousDistributionOptions {
70
71 private Options() {
72 min = -5;
73 max = 5;
74 }
75 }
76
77 @Override
78 protected List<Distribution<ContinuousDistribution>> getDistributions() {
79 double[] mean = params.mu;
80 double[] sigma = params.sigma;
81 final int n = DistributionUtils.validateLengths(mean.length, sigma.length);
82
83 mean = DistributionUtils.expandToLength(mean, n);
84 sigma = DistributionUtils.expandToLength(sigma, n);
85
86
87 final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
88 for (int i = 0; i < n; i++) {
89 final ContinuousDistribution d = NormalDistribution.of(mean[i], sigma[i]);
90 list.add(new Distribution<>(d, "mu=" + mean[i] + ",sigma=" + sigma[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 InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
113
114 @Override
115 protected DistributionOptions getDistributionOptions() {
116 return distributionOptions;
117 }
118 }
119
120
121 @Command(name = "check",
122 hidden = true,
123 description = "Normal distribution verification checks.")
124 static class Check extends ProbabilityCommand {}
125
126
127 @Command(name = "pdf",
128 description = "Normal distribution PDF.")
129 static class PDF extends ProbabilityCommand {}
130
131
132 @Command(name = "lpdf",
133 description = "Normal distribution natural logarithm of the PDF.")
134 static class LPDF extends ProbabilityCommand {}
135
136
137 @Command(name = "cdf",
138 description = "Normal distribution CDF.")
139 static class CDF extends ProbabilityCommand {}
140
141
142 @Command(name = "sf",
143 description = "Normal distribution survival probability.")
144 static class SF extends ProbabilityCommand {}
145
146
147 @Command(name = "icdf",
148 description = "Normal distribution inverse CDF.")
149 static class ICDF extends InverseProbabilityCommand {}
150
151
152 @Command(name = "isf",
153 description = "Normal distribution inverse SF.")
154 static class ISF extends InverseProbabilityCommand {}
155 }