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.TrapezoidalDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link TrapezoidalDistribution}.
29   */
30  @Command(name = "trapezoidal",
31           aliases = {"trap"},
32           description = "Trapezoidal distribution.",
33           subcommands = {
34               TrapezoidalCommand.Check.class,
35               TrapezoidalCommand.PDF.class,
36               TrapezoidalCommand.LPDF.class,
37               TrapezoidalCommand.CDF.class,
38               TrapezoidalCommand.SF.class,
39               TrapezoidalCommand.ICDF.class,
40               TrapezoidalCommand.ISF.class,
41           })
42  class TrapezoidalCommand 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, 0, 0};
59  
60              /** The start of the density constant region. */
61              @Option(names = {"-b", "--shape1"},
62                      paramLabel = "b",
63                      arity = "1..*",
64                      split = ",",
65                      description = {"constant start (default: ${DEFAULT-VALUE})."})
66              private double[] start = {-1.5, 0, 1};
67  
68              /** The end of the density constant region. */
69              @Option(names = {"-c", "--shape2"},
70                      paramLabel = "c",
71                      arity = "1..*",
72                      split = ",",
73                      description = {"constant end (default: ${DEFAULT-VALUE})."})
74              private double[] end = {2.5, 2, 3};
75  
76              /** The distribution upper limit. */
77              @Option(names = {"-d", "--upper"},
78                      paramLabel = "d",
79                      arity = "1..*",
80                      split = ",",
81                      description = {"upper bound (default: ${DEFAULT-VALUE})."})
82              private double[] upper = {4, 3, 3};
83          }
84  
85          /** Extend the options to set the default values for this distribution. */
86          static final class Options extends ContinuousDistributionOptions {
87              /** Set defaults. */
88              private Options() {
89                  min = -5;
90                  max = 5;
91              }
92          }
93  
94          @Override
95          protected List<Distribution<ContinuousDistribution>> getDistributions() {
96              double[] a = params.lower;
97              double[] b = params.start;
98              double[] c = params.end;
99              double[] d = params.upper;
100             final int n = DistributionUtils.validateLengths(a.length, b.length, c.length, d.length);
101 
102             a = DistributionUtils.expandToLength(a, n);
103             b = DistributionUtils.expandToLength(b, n);
104             c = DistributionUtils.expandToLength(c, n);
105             d = DistributionUtils.expandToLength(d, n);
106 
107             // Create distributions
108             final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
109             for (int i = 0; i < n; i++) {
110                 final ContinuousDistribution dist = TrapezoidalDistribution.of(a[i], b[i], c[i], d[i]);
111                 list.add(new Distribution<>(dist, "a=" + a[i] + ",b=" + b[i] + ",c=" + c[i] + ",d=" + d[i]));
112             }
113             return list;
114         }
115     }
116 
117     /** Base command for the distribution that defines the parameters. */
118     private abstract static class ProbabilityCommand extends BaseCommand {
119         /** The distribution options. */
120         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
121         private Options distributionOptions = new Options();
122 
123         @Override
124         protected DistributionOptions getDistributionOptions() {
125             return distributionOptions;
126         }
127     }
128 
129     /** Base command for the distribution that defines the parameters for inverse probability functions. */
130     private abstract static class InverseProbabilityCommand extends BaseCommand {
131         /** The distribution options. */
132         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
133         private InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
134 
135         @Override
136         protected DistributionOptions getDistributionOptions() {
137             return distributionOptions;
138         }
139     }
140 
141     /** Verification checks command. */
142     @Command(name = "check",
143              hidden = true,
144              description = "Trapezoidal distribution verification checks.")
145     static class Check extends ProbabilityCommand {}
146 
147     /** PDF command. */
148     @Command(name = "pdf",
149              description = "Trapezoidal distribution PDF.")
150     static class PDF extends ProbabilityCommand {}
151 
152     /** LPDF command. */
153     @Command(name = "lpdf",
154              description = "Trapezoidal distribution natural logarithm of the PDF.")
155     static class LPDF extends ProbabilityCommand {}
156 
157     /** CDF command. */
158     @Command(name = "cdf",
159              description = "Trapezoidal distribution CDF.")
160     static class CDF extends ProbabilityCommand {}
161 
162     /** SF command. */
163     @Command(name = "sf",
164              description = "Trapezoidal distribution survival probability.")
165     static class SF extends ProbabilityCommand {}
166 
167     /** ICDF command. */
168     @Command(name = "icdf",
169              description = "Trapezoidal distribution inverse CDF.")
170     static class ICDF extends InverseProbabilityCommand {}
171 
172     /** ISF command. */
173     @Command(name = "isf",
174              description = "Trapezoidal distribution inverse SF.")
175     static class ISF extends InverseProbabilityCommand {}
176 }