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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.distribution.continuous;
24  
25  import org.hipparchus.exception.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.special.Erf;
28  import org.hipparchus.util.FastMath;
29  import org.hipparchus.util.MathUtils;
30  
31  /**
32   * Implementation of the normal (gaussian) distribution.
33   *
34   * @see <a href="http://en.wikipedia.org/wiki/Normal_distribution">Normal distribution (Wikipedia)</a>
35   * @see <a href="http://mathworld.wolfram.com/NormalDistribution.html">Normal distribution (MathWorld)</a>
36   */
37  public class NormalDistribution extends AbstractRealDistribution {
38      /** Serializable version identifier. */
39      private static final long serialVersionUID = 20160320L;
40      /** &radic;(2) */
41      private static final double SQRT2 = FastMath.sqrt(2.0);
42      /** Mean of this distribution. */
43      private final double mean;
44      /** Standard deviation of this distribution. */
45      private final double standardDeviation;
46      /** The value of {@code log(sd) + 0.5*log(2*pi)} stored for faster computation. */
47      private final double logStandardDeviationPlusHalfLog2Pi;
48  
49      /**
50       * Create a normal distribution with mean equal to zero and standard
51       * deviation equal to one.
52       */
53      public NormalDistribution() {
54          this(0, 1);
55      }
56  
57      /**
58       * Create a normal distribution using the given mean, standard deviation.
59       *
60       * @param mean Mean for this distribution.
61       * @param sd Standard deviation for this distribution.
62       * @throws MathIllegalArgumentException if {@code sd <= 0}.
63       */
64      public NormalDistribution(double mean, double sd)
65          throws MathIllegalArgumentException {
66          if (sd <= 0) {
67              throw new MathIllegalArgumentException(LocalizedCoreFormats.STANDARD_DEVIATION, sd);
68          }
69  
70          this.mean = mean;
71          this.standardDeviation = sd;
72          this.logStandardDeviationPlusHalfLog2Pi =
73                  FastMath.log(sd) + 0.5 * FastMath.log(2 * FastMath.PI);
74      }
75  
76      /**
77       * Access the mean.
78       *
79       * @return the mean for this distribution.
80       */
81      public double getMean() {
82          return mean;
83      }
84  
85      /**
86       * Access the standard deviation.
87       *
88       * @return the standard deviation for this distribution.
89       */
90      public double getStandardDeviation() {
91          return standardDeviation;
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public double density(double x) {
97          return FastMath.exp(logDensity(x));
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public double logDensity(double x) {
103         final double x0 = x - mean;
104         final double x1 = x0 / standardDeviation;
105         return -0.5 * x1 * x1 - logStandardDeviationPlusHalfLog2Pi;
106     }
107 
108     /**
109      * {@inheritDoc}
110      *
111      * If {@code x} is more than 40 standard deviations from the mean, 0 or 1
112      * is returned, as in these cases the actual value is within
113      * {@code Double.MIN_VALUE} of 0 or 1.
114      */
115     @Override
116     public double cumulativeProbability(double x)  {
117         final double dev = x - mean;
118         if (FastMath.abs(dev) > 40 * standardDeviation) {
119             return dev < 0 ? 0.0d : 1.0d;
120         }
121         return 0.5 * Erf.erfc(-dev / (standardDeviation * SQRT2));
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public double inverseCumulativeProbability(final double p) throws MathIllegalArgumentException {
127         MathUtils.checkRangeInclusive(p, 0, 1);
128         return mean + standardDeviation * SQRT2 * Erf.erfInv(2 * p - 1);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public double probability(double x0,
134                               double x1)
135         throws MathIllegalArgumentException {
136         if (x0 > x1) {
137             throw new MathIllegalArgumentException(LocalizedCoreFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
138                                                 x0, x1, true);
139         }
140         final double denom = standardDeviation * SQRT2;
141         final double v0 = (x0 - mean) / denom;
142         final double v1 = (x1 - mean) / denom;
143         return 0.5 * Erf.erf(v0, v1);
144     }
145 
146     /**
147      * {@inheritDoc}
148      *
149      * For mean parameter {@code mu}, the mean is {@code mu}.
150      */
151     @Override
152     public double getNumericalMean() {
153         return getMean();
154     }
155 
156     /**
157      * {@inheritDoc}
158      *
159      * For standard deviation parameter {@code s}, the variance is {@code s^2}.
160      */
161     @Override
162     public double getNumericalVariance() {
163         final double s = getStandardDeviation();
164         return s * s;
165     }
166 
167     /**
168      * {@inheritDoc}
169      *
170      * The lower bound of the support is always negative infinity
171      * no matter the parameters.
172      *
173      * @return lower bound of the support (always
174      * {@code Double.NEGATIVE_INFINITY})
175      */
176     @Override
177     public double getSupportLowerBound() {
178         return Double.NEGATIVE_INFINITY;
179     }
180 
181     /**
182      * {@inheritDoc}
183      *
184      * The upper bound of the support is always positive infinity
185      * no matter the parameters.
186      *
187      * @return upper bound of the support (always
188      * {@code Double.POSITIVE_INFINITY})
189      */
190     @Override
191     public double getSupportUpperBound() {
192         return Double.POSITIVE_INFINITY;
193     }
194 
195     /**
196      * {@inheritDoc}
197      *
198      * The support of this distribution is connected.
199      *
200      * @return {@code true}
201      */
202     @Override
203     public boolean isSupportConnected() {
204         return true;
205     }
206 }