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  package org.hipparchus.distribution.continuous;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.MathUtils;
28  
29  /**
30   * This class implements the Laplace distribution.
31   *
32   * @see <a href="http://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a>
33   */
34  public class LaplaceDistribution extends AbstractRealDistribution {
35  
36      /** Serializable version identifier. */
37      private static final long serialVersionUID = 20141003L;
38  
39      /** The location parameter. */
40      private final double mu;
41      /** The scale parameter. */
42      private final double beta;
43  
44      /**
45       * Build a new instance.
46       *
47       * @param mu location parameter
48       * @param beta scale parameter (must be positive)
49       * @throws MathIllegalArgumentException if {@code beta <= 0}
50       */
51      public LaplaceDistribution(double mu, double beta)
52          throws MathIllegalArgumentException {
53          if (beta <= 0.0) {
54              throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_POSITIVE_SCALE, beta);
55          }
56  
57          this.mu   = mu;
58          this.beta = beta;
59      }
60  
61      /**
62       * Access the location parameter, {@code mu}.
63       *
64       * @return the location parameter.
65       */
66      public double getLocation() {
67          return mu;
68      }
69  
70      /**
71       * Access the scale parameter, {@code beta}.
72       *
73       * @return the scale parameter.
74       */
75      public double getScale() {
76          return beta;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public double density(double x) {
82          return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public double cumulativeProbability(double x) {
88          if (x <= mu) {
89              return FastMath.exp((x - mu) / beta) / 2.0;
90          } else {
91              return 1.0 - FastMath.exp((mu - x) / beta) / 2.0;
92          }
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
98          MathUtils.checkRangeInclusive(p, 0, 1);
99  
100         if (p == 0) {
101             return Double.NEGATIVE_INFINITY;
102         } else if (p == 1) {
103             return Double.POSITIVE_INFINITY;
104         }
105         double x = (p > 0.5) ? -Math.log(2.0 - 2.0 * p) : Math.log(2.0 * p);
106         return mu + beta * x;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public double getNumericalMean() {
112         return mu;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public double getNumericalVariance() {
118         return 2.0 * beta * beta;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public double getSupportLowerBound() {
124         return Double.NEGATIVE_INFINITY;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public double getSupportUpperBound() {
130         return Double.POSITIVE_INFINITY;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public boolean isSupportConnected() {
136         return true;
137     }
138 
139 }