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.ChiSquaredDistribution;
22 import org.apache.commons.statistics.distribution.ContinuousDistribution;
23 import picocli.CommandLine.ArgGroup;
24 import picocli.CommandLine.Command;
25 import picocli.CommandLine.Option;
26
27
28
29
30 @Command(name = "chisq",
31 aliases = {"chi2"},
32 description = "Chi-squared distribution.",
33 subcommands = {
34 ChiSquaredCommand.Check.class,
35 ChiSquaredCommand.PDF.class,
36 ChiSquaredCommand.LPDF.class,
37 ChiSquaredCommand.CDF.class,
38 ChiSquaredCommand.SF.class,
39 ChiSquaredCommand.ICDF.class,
40 ChiSquaredCommand.ISF.class,
41 })
42 class ChiSquaredCommand 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 = {"--df", "--degrees-of-freedom"},
54 arity = "1..*",
55 split = ",",
56 description = {"degrees of freedom (default: ${DEFAULT-VALUE})."})
57 private double[] df = {1, 2, 3, 4, 6, 9};
58 }
59
60
61 static final class Options extends ContinuousDistributionOptions {
62
63 private Options() {
64 min = 0;
65 max = 8;
66 }
67 }
68
69 @Override
70 protected List<Distribution<ContinuousDistribution>> getDistributions() {
71
72 final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
73 for (final double degreesOfFreedom : params.df) {
74 final ContinuousDistribution d = ChiSquaredDistribution.of(degreesOfFreedom);
75 list.add(new Distribution<>(d, "df=" + degreesOfFreedom));
76 }
77 return list;
78 }
79 }
80
81
82 private abstract static class ProbabilityCommand extends BaseCommand {
83
84 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
85 private Options distributionOptions = new Options();
86
87 @Override
88 protected DistributionOptions getDistributionOptions() {
89 return distributionOptions;
90 }
91 }
92
93
94 private abstract static class InverseProbabilityCommand extends BaseCommand {
95
96 @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
97 private InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
98
99 @Override
100 protected DistributionOptions getDistributionOptions() {
101 return distributionOptions;
102 }
103 }
104
105
106 @Command(name = "check",
107 hidden = true,
108 description = "Chi-squared distribution verification checks.")
109 static class Check extends ProbabilityCommand {}
110
111
112 @Command(name = "pdf",
113 description = "Chi-squared distribution PDF.")
114 static class PDF extends ProbabilityCommand {}
115
116
117 @Command(name = "lpdf",
118 description = "Chi-squared distribution natural logarithm of the PDF.")
119 static class LPDF extends ProbabilityCommand {}
120
121
122 @Command(name = "cdf",
123 description = "Chi-squared distribution CDF.")
124 static class CDF extends ProbabilityCommand {}
125
126
127 @Command(name = "sf",
128 description = "Chi-squared distribution survival probability.")
129 static class SF extends ProbabilityCommand {}
130
131
132 @Command(name = "icdf",
133 description = "Chi-squared distribution inverse CDF.")
134 static class ICDF extends InverseProbabilityCommand {}
135
136
137 @Command(name = "isf",
138 description = "Chi-squared distribution inverse SF.")
139 static class ISF extends InverseProbabilityCommand {}
140 }