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