001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.statistics.examples.distribution;
018
019import picocli.CommandLine;
020import picocli.CommandLine.Command;
021
022/**
023 * Construct instances of probability distributions using the Commons Statistics library.
024 * Output values from the distribution functions: PDF; CDF; SF Function; and Inverse CDF.
025 */
026public final class DistributionsApplication {
027
028    // Implementation Notes
029    //
030    // This application constructs distribution instances of the Commons Statistics library
031    // and calls the standard distribution functions:
032    //
033    // PDF(x)          Probability Density Function (continuous distributions)
034    // PMF(x)          Probability Mass Function (discrete distributions)
035    // CDF(x)          Cumulative Probability Function
036    // SF(x)           Survival Probability Function
037    // InverseCDF(p)   Inverse Cumulative Probability Function
038    // InverseSF(p)    Inverse Survival Probability Function
039    //
040    // There is very little logic in the application. All the classes serve to provide
041    // commands for annotation with the PicoCLI library. Each command must have an annotated
042    // instance. Since the functions (pdf, pmf, icdf) require different arguments
043    // (x real, x int, p-value) this implementation
044    // avoids using the function as an option to avoid specifying a generic set of parameters
045    // covering both input x as real or integer and input probability as real in [0, 1].
046    // This creates many classes with the same structure. Maintenance is possible using
047    // carefully constructed regex replacements across the Command classes.
048    //
049    // Note that PicoCLI constructs classes using the no arguments constructor. Thus any
050    // defaults for arguments must be declared inline (for primitives) or set in the no
051    // arguments constructor for objects. Reusing the same options class instantiated with
052    // arguments to the constructor to set default values is ignored when the option is
053    // changed by a command line argument. In this case PicoCLI will create
054    // a new instance with the no arguments constructor and the overridden values are lost.
055    //
056    // Discrete distributions have a PMF. For convenience this has an alias
057    // as pdf so all distributions can be called using the same syntax.
058    // Aliases have been used for the distribution names to shorten commands.
059
060    /** No public constructor. */
061    private DistributionsApplication() {}
062
063    /**
064     * Specification for the top-level command in the examples distributions application.
065     *
066     * <p>This command will print the top-level help message.</p>
067     */
068    @Command(name = "distributions",
069             description = "Apache Commons Statistics Distribution Utilities.",
070             subcommands = {
071                 BetaCommand.class,
072                 BinomialCommand.class,
073                 CauchyCommand.class,
074                 ChiSquaredCommand.class,
075                 ExpCommand.class,
076                 FCommand.class,
077                 FoldedNormalCommand.class,
078                 GammaCommand.class,
079                 GeometricCommand.class,
080                 GumbelCommand.class,
081                 HypergeometricCommand.class,
082                 LaplaceCommand.class,
083                 LevyCommand.class,
084                 LogisticCommand.class,
085                 LogNormalCommand.class,
086                 LogUniformCommand.class,
087                 NakagamiCommand.class,
088                 NormalCommand.class,
089                 ParetoCommand.class,
090                 PascalCommand.class,
091                 PoissonCommand.class,
092                 TCommand.class,
093                 TrapezoidalCommand.class,
094                 TriangularCommand.class,
095                 TruncatedNormalCommand.class,
096                 UniformContinuousCommand.class,
097                 UniformDiscreteCommand.class,
098                 WeibullCommand.class,
099                 ZipfCommand.class,
100             })
101    static class DistributionsCommand extends AbstractDistributionCommand {
102        // Nothing to do
103    }
104
105    /**
106     * Run the Statistics examples command line application.
107     *
108     * @param args Application's arguments.
109     */
110    public static void main(String[] args) {
111        // The command line is built using annotations
112        final CommandLine cmd = new CommandLine(new DistributionsCommand());
113        System.exit(cmd.execute(args));
114    }
115}