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.FoldedNormalDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link FoldedNormalDistribution}.
29   *
30   * @since 1.1
31   */
32  @Command(name = "foldednormal",
33           aliases = {"foldnorm"},
34           description = "Folded normal distribution.",
35           subcommands = {
36               FoldedNormalCommand.Check.class,
37               FoldedNormalCommand.PDF.class,
38               FoldedNormalCommand.LPDF.class,
39               FoldedNormalCommand.CDF.class,
40               FoldedNormalCommand.SF.class,
41               FoldedNormalCommand.ICDF.class,
42               FoldedNormalCommand.ISF.class,
43           })
44  class FoldedNormalCommand extends AbstractDistributionCommand {
45  
46      /** Base command for the distribution that defines the parameters. */
47      private abstract static class BaseCommand extends AbstractContinuousDistributionCommand {
48          /** Distribution parameters. */
49          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
50          private Params params = new Params();
51  
52          /** Parameters class. */
53          static class Params {
54              /** The distribution mean. */
55              @Option(names = {"-m", "--mu", "--mean"},
56                      arity = "1..*",
57                      split = ",",
58                      description = {"mean (default: ${DEFAULT-VALUE})."})
59              private double[] mu = {0, 0, 1, -1};
60  
61              /** The distribution sigma. */
62              @Option(names = {"-s", "--sigma"},
63                      arity = "1..*",
64                      split = ",",
65                      description = {"standard deviation (default: ${DEFAULT-VALUE})."})
66              private double[] sigma = {1, 2, 1, 1};
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[] mean = params.mu;
81              double[] sigma = params.sigma;
82              final int n = DistributionUtils.validateLengths(mean.length, sigma.length);
83  
84              mean = DistributionUtils.expandToLength(mean, n);
85              sigma = DistributionUtils.expandToLength(sigma, 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 = FoldedNormalDistribution.of(mean[i], sigma[i]);
91                  list.add(new Distribution<>(d, "mu=" + mean[i] + ",sigma=" + sigma[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 = "Folded normal distribution verification checks.")
125     static class Check extends ProbabilityCommand {}
126 
127     /** PDF command. */
128     @Command(name = "pdf",
129              description = "Folded normal distribution PDF.")
130     static class PDF extends ProbabilityCommand {}
131 
132     /** LPDF command. */
133     @Command(name = "lpdf",
134              description = "Folded normal distribution natural logarithm of the PDF.")
135     static class LPDF extends ProbabilityCommand {}
136 
137     /** CDF command. */
138     @Command(name = "cdf",
139              description = "Folded normal distribution CDF.")
140     static class CDF extends ProbabilityCommand {}
141 
142     /** SF command. */
143     @Command(name = "sf",
144              description = "Folded normal distribution survival probability.")
145     static class SF extends ProbabilityCommand {}
146 
147     /** ICDF command. */
148     @Command(name = "icdf",
149              description = "Folded normal distribution inverse CDF.")
150     static class ICDF extends InverseProbabilityCommand {}
151 
152     /** ISF command. */
153     @Command(name = "isf",
154              description = "Folded normal distribution inverse SF.")
155     static class ISF extends InverseProbabilityCommand {}
156 }