Class NormalDistribution

  • All Implemented Interfaces:
    Distribution

    @Alias({"GaussianDistribution","normal","gauss"})
    public class NormalDistribution
    extends java.lang.Object
    implements Distribution
    Gaussian distribution aka normal distribution
    Since:
    0.5.0
    Author:
    Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double[] ERF_COEFF1
      T.
      private static double[] ERF_COEFF2
      T.
      private double mean
      Mean value for the generator
      static double ONEBYPHIINV075
      1 / CDFINV(0.75)
      (package private) static double P_HIGH
      Treshold for switching nethods for erfinv approximation
      (package private) static double P_LOW
      Treshold for switching nethods for erfinv approximation
      static double PHIINV075
      CDFINV(0.75)
      private double stddev
      Standard deviation
    • Constructor Summary

      Constructors 
      Constructor Description
      NormalDistribution​(double mean, double stddev)
      Constructor for Gaussian distribution
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double cdf​(double val)
      Return the cumulative density function at the given value.
      static double cdf​(double x, double mu, double sigma)
      Cumulative probability density function (CDF) of a normal distribution.
      static double erf​(double x)
      Error function for Gaussian distributions = Normal distributions.
      static double erfc​(double x)
      Complementary error function for Gaussian distributions = Normal distributions.
      static double erfcinv​(double y)
      Inverse error function.
      double getMean()  
      double getStddev()  
      double logpdf​(double val)
      Return the log density of an existing value
      static double logpdf​(double x, double mu, double sigma)
      Log probability density function of the normal distribution.
      double nextRandom​(java.util.Random random)
      Generate a new random value
      double pdf​(double val)
      Return the density of an existing value
      static double pdf​(double x, double mu, double sigma)
      Probability density function of the normal distribution.
      double quantile​(double q)
      Quantile aka probit (for normal) aka inverse CDF (invcdf, cdf^-1) function.
      static double quantile​(double x, double mu, double sigma)
      Inverse cumulative probability density function (probit) of a normal distribution.
      static double standardNormalCDF​(double x)
      Cumulative probability density function (CDF) of a normal distribution.
      static double standardNormalLogPDF​(double x)
      Log probability density function of the standard normal distribution.
      static double standardNormalPDF​(double x)
      Probability density function of the standard normal distribution.
      static double standardNormalQuantile​(double d)
      Approximate the inverse error function for normal distributions.
      java.lang.String toString()
      Describe the distribution
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • P_LOW

        static final double P_LOW
        Treshold for switching nethods for erfinv approximation
        See Also:
        Constant Field Values
      • P_HIGH

        static final double P_HIGH
        Treshold for switching nethods for erfinv approximation
        See Also:
        Constant Field Values
      • ONEBYPHIINV075

        public static final double ONEBYPHIINV075
        1 / CDFINV(0.75)
        See Also:
        Constant Field Values
      • mean

        private double mean
        Mean value for the generator
      • stddev

        private double stddev
        Standard deviation
      • ERF_COEFF1

        private static final double[] ERF_COEFF1
        T. Ooura, http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html
      • ERF_COEFF2

        private static final double[] ERF_COEFF2
        T. Ooura, http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html
    • Constructor Detail

      • NormalDistribution

        public NormalDistribution​(double mean,
                                  double stddev)
        Constructor for Gaussian distribution
        Parameters:
        mean - Mean
        stddev - Standard Deviation
    • Method Detail

      • pdf

        public double pdf​(double val)
        Description copied from interface: Distribution
        Return the density of an existing value
        Specified by:
        pdf in interface Distribution
        Parameters:
        val - existing value
        Returns:
        distribution density
      • logpdf

        public double logpdf​(double val)
        Description copied from interface: Distribution
        Return the log density of an existing value
        Specified by:
        logpdf in interface Distribution
        Parameters:
        val - existing value
        Returns:
        log distribution density
      • cdf

        public double cdf​(double val)
        Description copied from interface: Distribution
        Return the cumulative density function at the given value.
        Specified by:
        cdf in interface Distribution
        Parameters:
        val - existing value
        Returns:
        cumulative density
      • quantile

        public double quantile​(double q)
        Description copied from interface: Distribution
        Quantile aka probit (for normal) aka inverse CDF (invcdf, cdf^-1) function.
        Specified by:
        quantile in interface Distribution
        Parameters:
        q - Quantile to find
        Returns:
        Quantile position
      • nextRandom

        public double nextRandom​(java.util.Random random)
        Description copied from interface: Distribution
        Generate a new random value
        Specified by:
        nextRandom in interface Distribution
        Parameters:
        random - Random number generator
        Returns:
        new random value
      • toString

        public java.lang.String toString()
        Description copied from interface: Distribution
        Describe the distribution
        Specified by:
        toString in interface Distribution
        Overrides:
        toString in class java.lang.Object
        Returns:
        description
      • getMean

        public double getMean()
        Returns:
        the mean
      • getStddev

        public double getStddev()
        Returns:
        the standard deviation
      • erfc

        @Reference(authors="T. Ooura",
                   title="Gamma / Error Functions",
                   booktitle="",
                   url="http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html",
                   bibkey="web/Ooura96")
        public static double erfc​(double x)
        Complementary error function for Gaussian distributions = Normal distributions.

        Based on:
        Takuya Ooura, http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html
        Copyright (C) 1996 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
        "You may use, copy, modify this code for any purpose and without fee."

        Parameters:
        x - parameter value
        Returns:
        erfc(x)
      • erf

        public static double erf​(double x)
        Error function for Gaussian distributions = Normal distributions.
        Parameters:
        x - parameter value
        Returns:
        erf(x)
      • erfcinv

        @Reference(authors="T. Ooura",
                   title="Gamma / Error Functions",
                   booktitle="",
                   url="http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html",
                   bibkey="web/Ooura96")
        public static double erfcinv​(double y)
        Inverse error function.

        Based on:
        T. Ooura, http://www.kurims.kyoto-u.ac.jp/~ooura/gamerf.html
        Copyright (C) 1996 Takuya OOURA (email: ooura@mmm.t.u-tokyo.ac.jp).
        "You may use, copy, modify this code for any purpose and without fee."

        Parameters:
        y - parameter value
        Returns:
        erfcinv(y)
      • pdf

        public static double pdf​(double x,
                                 double mu,
                                 double sigma)
        Probability density function of the normal distribution.

        \[ \frac{1}{\sqrt{2\pi\sigma^2}} \exp(-\frac{(x-\mu)^2}{2\sigma^2}) \]

        Parameters:
        x - The value.
        mu - The mean.
        sigma - The standard deviation.
        Returns:
        PDF of the given normal distribution at x.
      • logpdf

        public static double logpdf​(double x,
                                    double mu,
                                    double sigma)
        Log probability density function of the normal distribution.

        \[\log\frac{1}{\sqrt{2\pi}} - \log\sigma - \tfrac{(x-\mu)^2}{2\sigma^2}\]

        Parameters:
        x - The value.
        mu - The mean.
        sigma - The standard deviation.
        Returns:
        PDF of the given normal distribution at x.
      • standardNormalLogPDF

        public static double standardNormalLogPDF​(double x)
        Log probability density function of the standard normal distribution.

        \[ \log\frac{1}{\sqrt{2\pi}} -\frac{x^2}{2}) \]

        Parameters:
        x - The value.
        Returns:
        PDF of the given normal distribution at x.
      • standardNormalPDF

        public static double standardNormalPDF​(double x)
        Probability density function of the standard normal distribution.

        \[ \frac{1}{\sqrt{2\pi}} \exp(-\frac{x^2}{2}) \]

        Parameters:
        x - The value.
        Returns:
        PDF of the given normal distribution at x.
      • cdf

        @Reference(authors="G. Marsaglia",
                   title="Evaluating the Normal Distribution",
                   booktitle="Journal of Statistical Software 11(4)",
                   url="https://doi.org/10.18637/jss.v011.i04",
                   bibkey="doi:10.18637/jss.v011.i04")
        public static double cdf​(double x,
                                 double mu,
                                 double sigma)
        Cumulative probability density function (CDF) of a normal distribution.

        Reference:

        G. Marsaglia
        Evaluating the Normal Distribution
        Journal of Statistical Software 11(4)

        Parameters:
        x - value to evaluate CDF at
        mu - Mean value
        sigma - Standard deviation.
        Returns:
        The CDF of the given normal distribution at x.
      • standardNormalCDF

        @Reference(authors="G. Marsaglia",
                   title="Evaluating the Normal Distribution",
                   booktitle="Journal of Statistical Software 11(4)",
                   url="https://doi.org/10.18637/jss.v011.i04",
                   bibkey="doi:10.18637/jss.v011.i04")
        public static double standardNormalCDF​(double x)
        Cumulative probability density function (CDF) of a normal distribution.

        Reference:

        G. Marsaglia
        Evaluating the Normal Distribution
        Journal of Statistical Software 11(4)

        Parameters:
        x - value to evaluate CDF at
        Returns:
        The CDF of the given normal distribution at x.
      • quantile

        public static double quantile​(double x,
                                      double mu,
                                      double sigma)
        Inverse cumulative probability density function (probit) of a normal distribution.
        Parameters:
        x - value to evaluate probit function at
        mu - Mean value
        sigma - Standard deviation.
        Returns:
        The probit of the given normal distribution at x.
      • standardNormalQuantile

        public static double standardNormalQuantile​(double d)
        Approximate the inverse error function for normal distributions.
        Parameters:
        d - Quantile. Must be in [0:1], obviously.
        Returns:
        Inverse erf.