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