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.TruncatedNormalDistribution;
23  import picocli.CommandLine.ArgGroup;
24  import picocli.CommandLine.Command;
25  import picocli.CommandLine.Option;
26  
27  /**
28   * Command for the {@link TruncatedNormalDistribution}.
29   */
30  @Command(name = "truncatednormal",
31           aliases = {"tnorm"},
32           description = "Truncated normal distribution.",
33           subcommands = {
34               TruncatedNormalCommand.Check.class,
35               TruncatedNormalCommand.PDF.class,
36               TruncatedNormalCommand.LPDF.class,
37               TruncatedNormalCommand.CDF.class,
38               TruncatedNormalCommand.SF.class,
39               TruncatedNormalCommand.ICDF.class,
40               TruncatedNormalCommand.ISF.class,
41           })
42  class TruncatedNormalCommand extends AbstractDistributionCommand {
43  
44      /** Base command for the distribution that defines the parameters. */
45      private abstract static class BaseCommand extends AbstractContinuousDistributionCommand {
46          /** The default minimum for x. */
47          static final double MIN = -10;
48          /** The default maximum for x. */
49          static final double MAX = 10;
50  
51          /** Distribution parameters. */
52          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
53          private Params params = new Params();
54  
55          /** Parameters class. */
56          static class Params {
57              /** The distribution mean. */
58              @Option(names = {"-m", "--mu", "--mean"},
59                      arity = "1..*",
60                      split = ",",
61                      description = {"mean (default: ${DEFAULT-VALUE})."})
62              private double[] mu = {-8, 0, 9, 0};
63  
64              /** The distribution sigma. */
65              @Option(names = {"-s", "--sigma"},
66                      arity = "1..*",
67                      split = ",",
68                      description = {"standard deviation (default: ${DEFAULT-VALUE})."})
69              private double[] sigma = {2, 2, 10, 10};
70  
71              /** The distribution lower limit. */
72              @Option(names = {"-l", "--lower"},
73                      arity = "1..*",
74                      split = ",",
75                      description = {"lower bound (default: ${DEFAULT-VALUE})."})
76              private double[] lower = {MIN};
77  
78              /** The distribution upper limit. */
79              @Option(names = {"-u", "--upper"},
80                      arity = "1..*",
81                      split = ",",
82                      description = {"upper bound (default: ${DEFAULT-VALUE})."})
83              private double[] upper = {MAX};
84          }
85  
86          /** Extend the options to set the default values for this distribution. */
87          static final class Options extends ContinuousDistributionOptions {
88              /** Set defaults. */
89              private Options() {
90                  min = MIN;
91                  max = MAX;
92              }
93          }
94  
95          @Override
96          protected List<Distribution<ContinuousDistribution>> getDistributions() {
97              double[] mean = params.mu;
98              double[] sigma = params.sigma;
99              double[] lower = params.lower;
100             double[] upper = params.upper;
101             final int n = DistributionUtils.validateLengths(mean.length, sigma.length, lower.length, upper.length);
102 
103             mean = DistributionUtils.expandToLength(mean, n);
104             sigma = DistributionUtils.expandToLength(sigma, n);
105             lower = DistributionUtils.expandToLength(lower, n);
106             upper = DistributionUtils.expandToLength(upper, n);
107 
108             // Create distributions
109             final ArrayList<Distribution<ContinuousDistribution>> list = new ArrayList<>();
110             for (int i = 0; i < n; i++) {
111                 final ContinuousDistribution d = TruncatedNormalDistribution.of(mean[i], sigma[i], lower[i], upper[i]);
112                 list.add(new Distribution<>(d, "mu=" + mean[i] + ",sigma=" + sigma[i] +
113                                                ",lower=" + lower[i] + ",upper=" + upper[i]));
114             }
115             return list;
116         }
117     }
118 
119     /** Base command for the distribution that defines the parameters. */
120     private abstract static class ProbabilityCommand extends BaseCommand {
121         /** The distribution options. */
122         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
123         private Options distributionOptions = new Options();
124 
125         @Override
126         protected DistributionOptions getDistributionOptions() {
127             return distributionOptions;
128         }
129     }
130 
131     /** Base command for the distribution that defines the parameters for inverse probability functions. */
132     private abstract static class InverseProbabilityCommand extends BaseCommand {
133         /** The distribution options. */
134         @ArgGroup(validate = false, heading = "Evaluation options:%n", order = 2)
135         private InverseContinuousDistributionOptions distributionOptions = new InverseContinuousDistributionOptions();
136 
137         @Override
138         protected DistributionOptions getDistributionOptions() {
139             return distributionOptions;
140         }
141     }
142 
143     /** Verification checks command. */
144     @Command(name = "check",
145              hidden = true,
146              description = "Truncated normal distribution verification checks.")
147     static class Check extends ProbabilityCommand {}
148 
149     /** PDF command. */
150     @Command(name = "pdf",
151              description = "Truncated normal distribution PDF.")
152     static class PDF extends ProbabilityCommand {}
153 
154     /** LPDF command. */
155     @Command(name = "lpdf",
156              description = "Truncated normal distribution natural logarithm of the PDF.")
157     static class LPDF extends ProbabilityCommand {}
158 
159     /** CDF command. */
160     @Command(name = "cdf",
161              description = "Truncated normal distribution CDF.")
162     static class CDF extends ProbabilityCommand {}
163 
164     /** SF command. */
165     @Command(name = "sf",
166              description = "Truncated normal distribution survival probability.")
167     static class SF extends ProbabilityCommand {}
168 
169     /** ICDF command. */
170     @Command(name = "icdf",
171              description = "Truncated normal distribution inverse CDF.")
172     static class ICDF extends InverseProbabilityCommand {}
173 
174     /** ISF command. */
175     @Command(name = "isf",
176              description = "Truncated normal distribution inverse SF.")
177     static class ISF extends InverseProbabilityCommand {}
178 }