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.DiscreteDistribution;
22  import org.apache.commons.statistics.distribution.UniformDiscreteDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link UniformDiscreteDistribution}.
29   */
30  @Command(name = "unid",
31           description = "Discrete uniform distribution.",
32           subcommands = {
33               UniformDiscreteCommand.Check.class,
34               UniformDiscreteCommand.PMF.class,
35               UniformDiscreteCommand.LPMF.class,
36               UniformDiscreteCommand.CDF.class,
37               UniformDiscreteCommand.SF.class,
38               UniformDiscreteCommand.ICDF.class,
39               UniformDiscreteCommand.ISF.class,
40           })
41  class UniformDiscreteCommand extends AbstractDistributionCommand {
42  
43      /** Base command for the distribution that defines the parameters. */
44      private abstract static class BaseCommand extends AbstractDiscreteDistributionCommand {
45          /** Distribution parameters. */
46          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
47          private Params params = new Params();
48  
49          /** Parameters class. */
50          static class Params {
51              /** The distribution lower limit. */
52              @Option(names = {"-a", "--lower"},
53                      paramLabel = "a",
54                      arity = "1..*",
55                      split = ",",
56                      description = {"lower bound (default: ${DEFAULT-VALUE})."})
57              private int[] lower = {0, -3};
58  
59              /** The distribution upper limit. */
60              @Option(names = {"-b", "--upper"},
61                      paramLabel = "b",
62                      arity = "1..*",
63                      split = ",",
64                      description = {"upper bound (default: ${DEFAULT-VALUE})."})
65              private int[] upper = {5, 4};
66          }
67  
68          /** Extend the options to set the default values for this distribution. */
69          static final class Options extends DiscreteDistributionOptions {
70              /** Set defaults. */
71              private Options() {
72                  min = -5;
73                  max = 5;
74              }
75          }
76  
77          @Override
78          protected List<Distribution<DiscreteDistribution>> getDistributions() {
79              int[] lower = params.lower;
80              int[] upper = params.upper;
81              final int n = DistributionUtils.validateLengths(lower.length, upper.length);
82  
83              lower = DistributionUtils.expandToLength(lower, n);
84              upper = DistributionUtils.expandToLength(upper, n);
85  
86              // Create distributions
87              final ArrayList<Distribution<DiscreteDistribution>> list = new ArrayList<>();
88              for (int i = 0; i < n; i++) {
89                  final DiscreteDistribution d = UniformDiscreteDistribution.of(lower[i], upper[i]);
90                  list.add(new Distribution<>(d, "a=" + lower[i] + ",b=" + upper[i]));
91              }
92              return list;
93          }
94      }
95  
96      /** Base command for the distribution that defines the parameters. */
97      private abstract static class ProbabilityCommand extends BaseCommand {
98          /** The distribution options. */
99          @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
100         private Options distributionOptions = new Options();
101 
102         @Override
103         protected DistributionOptions getDistributionOptions() {
104             return distributionOptions;
105         }
106     }
107 
108     /** Base command for the distribution that defines the parameters for inverse probability functions. */
109     private abstract static class InverseProbabilityCommand extends BaseCommand {
110         /** The distribution options. */
111         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
112         private InverseDiscreteDistributionOptions distributionOptions = new InverseDiscreteDistributionOptions();
113 
114         @Override
115         protected DistributionOptions getDistributionOptions() {
116             return distributionOptions;
117         }
118     }
119 
120     /** Verification checks command. */
121     @Command(name = "check",
122              hidden = true,
123              description = "Discrete uniform distribution verification checks.")
124     static class Check extends ProbabilityCommand {}
125 
126     /** PMF command. */
127     @Command(name = "pmf",
128              aliases = {"pdf"},
129              description = "Discrete uniform distribution PMF.")
130     static class PMF extends ProbabilityCommand {}
131 
132     /** LPMF command. */
133     @Command(name = "lpmf",
134              aliases = {"lpdf"},
135              description = "Discrete distribution natural logarithm of the PMF.")
136     static class LPMF extends ProbabilityCommand {}
137 
138     /** CDF command. */
139     @Command(name = "cdf",
140              description = "Discrete uniform distribution CDF.")
141     static class CDF extends ProbabilityCommand {}
142 
143     /** SF command. */
144     @Command(name = "sf",
145              description = "Discrete uniform distribution survival probability.")
146     static class SF extends ProbabilityCommand {}
147 
148     /** ICDF command. */
149     @Command(name = "icdf",
150              description = "Discrete uniform distribution inverse CDF.")
151     static class ICDF extends InverseProbabilityCommand {}
152 
153     /** ISF command. */
154     @Command(name = "isf",
155              description = "Discrete uniform distribution inverse SF.")
156     static class ISF extends InverseProbabilityCommand {}
157 }