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