LogisticDistribution.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 Logistic distribution.
  28.  *
  29.  * @see <a href="http://en.wikipedia.org/wiki/Logistic_distribution">Logistic Distribution (Wikipedia)</a>
  30.  * @see <a href="http://mathworld.wolfram.com/LogisticDistribution.html">Logistic Distribution (Mathworld)</a>
  31.  */
  32. public class LogisticDistribution extends AbstractRealDistribution {

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

  35.     /** The location parameter. */
  36.     private final double mu;
  37.     /** The scale parameter. */
  38.     private final double s;

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

  52.         this.mu = mu;
  53.         this.s = s;
  54.     }

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

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

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public double density(double x) {
  74.         double z = (x - mu) / s;
  75.         double v = FastMath.exp(-z);
  76.         return 1 / s * v / ((1.0 + v) * (1.0 + v));
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public double cumulativeProbability(double x) {
  81.         double z = 1 / s * (x - mu);
  82.         return 1.0 / (1.0 + FastMath.exp(-z));
  83.     }

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

  88.         if (p == 0) {
  89.             return 0.0;
  90.         } else if (p == 1) {
  91.             return Double.POSITIVE_INFINITY;
  92.         }
  93.         return s * Math.log(p / (1.0 - p)) + mu;
  94.     }

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

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public double getNumericalVariance() {
  103.         return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s));
  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. }