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.PoissonDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link PoissonDistribution}.
29   */
30  @Command(name = "poisson",
31           aliases = {"poiss"},
32           description = "Poisson distribution.",
33           subcommands = {
34               PoissonCommand.Check.class,
35               PoissonCommand.PMF.class,
36               PoissonCommand.LPMF.class,
37               PoissonCommand.CDF.class,
38               PoissonCommand.SF.class,
39               PoissonCommand.ICDF.class,
40               PoissonCommand.ISF.class,
41           })
42  class PoissonCommand extends AbstractDistributionCommand {
43  
44      /** Base command for the distribution that defines the parameters. */
45      private abstract static class BaseCommand extends AbstractDiscreteDistributionCommand {
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 mean. */
53              @Option(names = {"-m", "--mean"},
54                      arity = "1..*",
55                      split = ",",
56                      description = {"mean (default: ${DEFAULT-VALUE})."})
57              private double[] mean = {1, 4, 10};
58          }
59  
60          /** Extend the options to set the default values for this distribution. */
61          static final class Options extends DiscreteDistributionOptions {
62              /** Set defaults. */
63              private Options() {
64                  min = 0;
65                  max = 20;
66              }
67          }
68  
69          @Override
70          protected List<Distribution<DiscreteDistribution>> getDistributions() {
71              // Create distributions
72              final ArrayList<Distribution<DiscreteDistribution>> list = new ArrayList<>();
73              for (final double m : params.mean) {
74                  final DiscreteDistribution d = PoissonDistribution.of(m);
75                  list.add(new Distribution<>(d, "mean=" + m));
76              }
77              return list;
78          }
79      }
80  
81      /** Base command for the distribution that defines the parameters. */
82      private abstract static class ProbabilityCommand extends BaseCommand {
83          /** The distribution options. */
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      /** Base command for the distribution that defines the parameters for inverse probability functions. */
94      private abstract static class InverseProbabilityCommand extends BaseCommand {
95          /** The distribution options. */
96          @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
97          private InverseDiscreteDistributionOptions distributionOptions = new InverseDiscreteDistributionOptions();
98  
99          @Override
100         protected DistributionOptions getDistributionOptions() {
101             return distributionOptions;
102         }
103     }
104 
105     /** Verification checks command. */
106     @Command(name = "check",
107              hidden = true,
108              description = "Poisson distribution verification checks.")
109     static class Check extends ProbabilityCommand {}
110 
111     /** PMF command. */
112     @Command(name = "pmf",
113              aliases = {"pdf"},
114              description = "Poisson distribution PMF.")
115     static class PMF extends ProbabilityCommand {}
116 
117     /** LPMF command. */
118     @Command(name = "lpmf",
119              aliases = {"lpdf"},
120              description = "Poisson distribution natural logarithm of the PMF.")
121     static class LPMF extends ProbabilityCommand {}
122 
123     /** CDF command. */
124     @Command(name = "cdf",
125              description = "Poisson distribution CDF.")
126     static class CDF extends ProbabilityCommand {}
127 
128     /** SF command. */
129     @Command(name = "sf",
130              description = "Poisson distribution survival probability.")
131     static class SF extends ProbabilityCommand {}
132 
133     /** ICDF command. */
134     @Command(name = "icdf",
135              description = "Poisson distribution inverse CDF.")
136     static class ICDF extends InverseProbabilityCommand {}
137 
138     /** ISF command. */
139     @Command(name = "isf",
140              description = "Poisson distribution inverse SF.")
141     static class ISF extends InverseProbabilityCommand {}
142 }