1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
29  
30  
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      
47      private abstract static class BaseCommand extends AbstractContinuousDistributionCommand {
48          
49          @ArgGroup(validate = false, heading = "Distribution parameters:%n", order = 1)
50          private Params params = new Params();
51  
52          
53          static class Params {
54              
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              
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          
70          static final class Options extends ContinuousDistributionOptions {
71              
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              
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      
98      private abstract static class ProbabilityCommand extends BaseCommand {
99          
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     
110     private abstract static class InverseProbabilityCommand extends BaseCommand {
111         
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     
122     @Command(name = "check",
123              hidden = true,
124              description = "Folded normal distribution verification checks.")
125     static class Check extends ProbabilityCommand {}
126 
127     
128     @Command(name = "pdf",
129              description = "Folded normal distribution PDF.")
130     static class PDF extends ProbabilityCommand {}
131 
132     
133     @Command(name = "lpdf",
134              description = "Folded normal distribution natural logarithm of the PDF.")
135     static class LPDF extends ProbabilityCommand {}
136 
137     
138     @Command(name = "cdf",
139              description = "Folded normal distribution CDF.")
140     static class CDF extends ProbabilityCommand {}
141 
142     
143     @Command(name = "sf",
144              description = "Folded normal distribution survival probability.")
145     static class SF extends ProbabilityCommand {}
146 
147     
148     @Command(name = "icdf",
149              description = "Folded normal distribution inverse CDF.")
150     static class ICDF extends InverseProbabilityCommand {}
151 
152     
153     @Command(name = "isf",
154              description = "Folded normal distribution inverse SF.")
155     static class ISF extends InverseProbabilityCommand {}
156 }