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