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