You are viewing an archived page from the original site. Go to the new numericalmethod.com →

source: public/Examples/src/main/java/com/numericalmethod/suanshu/examples/Distribution.java @ 144

Revision 55, 4.5 KB checked in by kenyiu, 3 months ago (diff)

CHG: Made examples as a project by adding a maven POM.

Line 
1/*
2 * Copyright (c) Numerical Method Inc.
3 * http://www.numericalmethod.com/
4 *
5 * THIS SOFTWARE IS LICENSED, NOT SOLD.
6 *
7 * YOU MAY USE THIS SOFTWARE ONLY AS DESCRIBED IN THE LICENSE.
8 * IF YOU ARE NOT AWARE OF AND/OR DO NOT AGREE TO THE TERMS OF THE LICENSE,
9 * DO NOT USE THIS SOFTWARE.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITH NO WARRANTY WHATSOEVER,
12 * EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
13 * ANY WARRANTIES OF ACCURACY, ACCESSIBILITY, COMPLETENESS,
14 * FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, NON-INFRINGEMENT,
15 * TITLE AND USEFULNESS.
16 *
17 * IN NO EVENT AND UNDER NO LEGAL THEORY,
18 * WHETHER IN ACTION, CONTRACT, NEGLIGENCE, TORT, OR OTHERWISE,
19 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
20 * ANY CLAIMS, DAMAGES OR OTHER LIABILITIES,
21 * ARISING AS A RESULT OF USING OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23package com.numericalmethod.suanshu.examples;
24
25import com.numericalmethod.suanshu.stats.distribution.univariate.*;
26import com.numericalmethod.suanshu.stats.test.distribution.kolmogorov.KolmogorovDistribution;
27
28/**
29 * Demonstrates the distribution functions in Java using SuanShu.
30 *
31 * @author Haksun Li
32 */
33public final class Distribution {
34
35    private Distribution() {
36    }
37
38    public static void main(String args[]) {
39        System.out.println(
40                "This class demonstrates the distribution functions in Java using SuanShu.");
41        beta(0.5, 1.5, 0.1);
42        chiSquare(5.0, 0.1);
43        f(12, 11.5, 0.1);
44        kolmogorov(30, 0.1);
45    }
46
47    /**
48     * Prints the properties of a beta distribution with the given parameters, where the quantile
49     * and the density is printed for the given value.
50     *
51     * @param alpha the shape parameter alpha
52     * @param beta  the shape parameter beta
53     * @param x     the position at which to evaluate the quantile and the density of the
54     *              distribution
55     */
56    public static void beta(double alpha, double beta, double x) {
57        System.out.printf("Beta (alpha=%f, beta=%f):%n", alpha, beta);
58        ProbabilityDistribution F = new BetaDistribution(alpha, beta);
59        printProperties(F, x);
60    }
61
62    /**
63     * Prints the properties of the given probability distribution. The quantile and density is
64     * printed for the given value.
65     *
66     * @param F the probability distribution for which to print the properties
67     * @param x the value at which to evaluate the quantile and the density of the distribution
68     */
69    public static void printProperties(ProbabilityDistribution F, double x) {
70        System.out.printf("mean: %f, var: %f, skew: %f, kurtosis: %f%n",
71                          F.mean(),
72                          F.variance(),
73                          F.skew(),
74                          F.kurtosis());
75
76        System.out.printf("the quantile at %f = %f%n", x, F.quantile(x));
77        System.out.printf("the density at %f = %f%n", x, F.density(x));
78    }
79
80    /**
81     * Prints the properties of a Chi Square distribution with the given degree of freedom, where
82     * the quantile and the density is printed for the given value.
83     *
84     * @param k the degree of freedom of the distribution
85     * @param x the value at which to evaluate the quantile and the density of the distribution
86     */
87    public static void chiSquare(double k, double x) {
88        ProbabilityDistribution F = new ChiSquareDistribution(k);
89        System.out.printf("Chi Square (k=%f):%n", k);
90        printProperties(F, x);
91
92    }
93
94    /**
95     * Prints the properties of an F-distribution with the given parameters, where the quantile and
96     * the density is printed for the given value.
97     *
98     * @param df1 the first degree of freedom
99     * @param df2 the second degree of freedom
100     * @param x   the value at which to evaluate the quantile and the density of the distribution
101     */
102    public static void f(double df1, double df2, double x) {
103        ProbabilityDistribution F = new FDistribution(df1, df2);
104        System.out.printf("F (df1=%f, df2=%f)%n", df1, df2);
105        printProperties(F, x);
106    }
107
108    /**
109     * Prints the CDF of a Komogorov distribution with the given sample size at the given value.
110     *
111     * @param n the sample size
112     * @param x the value at which to evaluate the cdf
113     */
114    public static void kolmogorov(int n, double x) {
115        ProbabilityDistribution F = new KolmogorovDistribution(n);
116        System.out.printf("the cdf of the Kolmogorov distribution (sample size %d) at %f = %f%n",
117                          n, x, F.cdf(x));
118    }
119}
Note: See TracBrowser for help on using the repository browser.