LaplaceDistribution.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.distribution.continuous;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.util.FastMath;
  25. import org.hipparchus.util.MathUtils;

  26. /**
  27.  * This class implements the Laplace distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Laplace_distribution">Laplace distribution (Wikipedia)</a>
  30.  */
  31. public class LaplaceDistribution extends AbstractRealDistribution {

  32.     /** Serializable version identifier. */
  33.     private static final long serialVersionUID = 20141003L;

  34.     /** The location parameter. */
  35.     private final double mu;
  36.     /** The scale parameter. */
  37.     private final double beta;

  38.     /**
  39.      * Build a new instance.
  40.      *
  41.      * @param mu location parameter
  42.      * @param beta scale parameter (must be positive)
  43.      * @throws MathIllegalArgumentException if {@code beta <= 0}
  44.      */
  45.     public LaplaceDistribution(double mu, double beta)
  46.         throws MathIllegalArgumentException {
  47.         if (beta <= 0.0) {
  48.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_POSITIVE_SCALE, beta);
  49.         }

  50.         this.mu   = mu;
  51.         this.beta = beta;
  52.     }

  53.     /**
  54.      * Access the location parameter, {@code mu}.
  55.      *
  56.      * @return the location parameter.
  57.      */
  58.     public double getLocation() {
  59.         return mu;
  60.     }

  61.     /**
  62.      * Access the scale parameter, {@code beta}.
  63.      *
  64.      * @return the scale parameter.
  65.      */
  66.     public double getScale() {
  67.         return beta;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public double density(double x) {
  72.         return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public double cumulativeProbability(double x) {
  77.         if (x <= mu) {
  78.             return FastMath.exp((x - mu) / beta) / 2.0;
  79.         } else {
  80.             return 1.0 - FastMath.exp((mu - x) / beta) / 2.0;
  81.         }
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
  86.         MathUtils.checkRangeInclusive(p, 0, 1);

  87.         if (p == 0) {
  88.             return Double.NEGATIVE_INFINITY;
  89.         } else if (p == 1) {
  90.             return Double.POSITIVE_INFINITY;
  91.         }
  92.         double x = (p > 0.5) ? -Math.log(2.0 - 2.0 * p) : Math.log(2.0 * p);
  93.         return mu + beta * x;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public double getNumericalMean() {
  98.         return mu;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public double getNumericalVariance() {
  103.         return 2.0 * beta * beta;
  104.     }

  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public double getSupportLowerBound() {
  108.         return Double.NEGATIVE_INFINITY;
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public double getSupportUpperBound() {
  113.         return Double.POSITIVE_INFINITY;
  114.     }

  115.     /** {@inheritDoc} */
  116.     @Override
  117.     public boolean isSupportConnected() {
  118.         return true;
  119.     }

  120. }