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