View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.TriangularDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link TriangularDistribution}.
29   */
30  @Command(name = "triangular",
31           aliases = {"tri"},
32           description = "Triangular distribution.",
33           subcommands = {
34               TriangularCommand.Check.class,
35               TriangularCommand.PDF.class,
36               TriangularCommand.LPDF.class,
37               TriangularCommand.CDF.class,
38               TriangularCommand.SF.class,
39               TriangularCommand.ICDF.class,
40               TriangularCommand.ISF.class,
41           })
42  class TriangularCommand extends AbstractDistributionCommand {
43  
44      /** Base command for the distribution that defines the parameters. */
45      private abstract static class BaseCommand extends AbstractContinuousDistributionCommand {
46          /** Distribution parameters. */
47          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
48          private Params params = new Params();
49  
50          /** Parameters class. */
51          static class Params {
52              /** The distribution lower limit. */
53              @Option(names = {"-a", "--lower"},
54                      paramLabel = "a",
55                      arity = "1..*",
56                      split = ",",
57                      description = {"lower bound (default: ${DEFAULT-VALUE})."})
58              private double[] lower = {-4, -1};
59  
60              /** The distribution mode. */
61              @Option(names = {"-c", "--mode"},
62                      paramLabel = "c",
63                      arity = "1..*",
64                      split = ",",
65                      description = {"mode (default: ${DEFAULT-VALUE})."})
66              private double[] mode = {2.5, 2};
67  
68              /** The distribution upper limit. */
69              @Option(names = {"-b", "--upper"},
70                      paramLabel = "b",
71                      arity = "1..*",
72                      split = ",",
73                      description = {"upper bound (default: ${DEFAULT-VALUE})."})
74              private double[] upper = {4, 5};
75          }
76  
77          /** Extend the options to set the default values for this distribution. */
78          static final class Options extends ContinuousDistributionOptions {
79              /** Set defaults. */
80              private Options() {
81                  min = -5;
82                  max = 5;
83              }
84          }
85  
86          @Override
87          protected List<Distribution<ContinuousDistribution>> getDistributions() {
88              double[] lower = params.lower;
89              double[] mode = params.mode;
90              double[] upper = params.upper;
91              final int n = DistributionUtils.validateLengths(lower.length, mode.length, upper.length);
92  
93              lower = DistributionUtils.expandToLength(lower, n);
94              mode = DistributionUtils.expandToLength(mode, n);
95              upper = DistributionUtils.expandToLength(upper, n);
96  
97              // Create distributions
98              final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
99              for (int i = 0; i < n; i++) {
100                 final ContinuousDistribution d = TriangularDistribution.of(lower[i], mode[i], upper[i]);
101                 list.add(new Distribution<>(d, "a=" + lower[i] + ",b=" + upper[i] + ",c=" + mode[i]));
102             }
103             return list;
104         }
105     }
106 
107     /** Base command for the distribution that defines the parameters. */
108     private abstract static class ProbabilityCommand extends BaseCommand {
109         /** The distribution options. */
110         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
111         private Options distributionOptions = new Options();
112 
113         @Override
114         protected DistributionOptions getDistributionOptions() {
115             return distributionOptions;
116         }
117     }
118 
119     /** Base command for the distribution that defines the parameters for inverse probability functions. */
120     private abstract static class InverseProbabilityCommand extends BaseCommand {
121         /** The distribution options. */
122         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
123         private InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
124 
125         @Override
126         protected DistributionOptions getDistributionOptions() {
127             return distributionOptions;
128         }
129     }
130 
131     /** Verification checks command. */
132     @Command(name = "check",
133              hidden = true,
134              description = "Triangular distribution verification checks.")
135     static class Check extends ProbabilityCommand {}
136 
137     /** PDF command. */
138     @Command(name = "pdf",
139              description = "Triangular distribution PDF.")
140     static class PDF extends ProbabilityCommand {}
141 
142     /** LPDF command. */
143     @Command(name = "lpdf",
144              description = "Triangular distribution natural logarithm of the PDF.")
145     static class LPDF extends ProbabilityCommand {}
146 
147     /** CDF command. */
148     @Command(name = "cdf",
149              description = "Triangular distribution CDF.")
150     static class CDF extends ProbabilityCommand {}
151 
152     /** SF command. */
153     @Command(name = "sf",
154              description = "Triangular distribution survival probability.")
155     static class SF extends ProbabilityCommand {}
156 
157     /** ICDF command. */
158     @Command(name = "icdf",
159              description = "Triangular distribution inverse CDF.")
160     static class ICDF extends InverseProbabilityCommand {}
161 
162     /** ISF command. */
163     @Command(name = "isf",
164              description = "Triangular distribution inverse SF.")
165     static class ISF extends InverseProbabilityCommand {}
166 }