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