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