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.  *      http://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. package org.apache.commons.math3.distribution;

  18. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  19. import org.apache.commons.math3.exception.OutOfRangeException;
  20. import org.apache.commons.math3.exception.util.LocalizedFormats;
  21. import org.apache.commons.math3.random.RandomGenerator;
  22. import org.apache.commons.math3.random.Well19937c;
  23. import org.apache.commons.math3.util.FastMath;
  24. import org.apache.commons.math3.util.MathUtils;

  25. /**
  26.  * This class implements the Logistic distribution.
  27.  *
  28.  * @see <a href="http://en.wikipedia.org/wiki/Logistic_distribution">Logistic Distribution (Wikipedia)</a>
  29.  * @see <a href="http://mathworld.wolfram.com/LogisticDistribution.html">Logistic Distribution (Mathworld)</a>
  30.  *
  31.  * @since 3.4
  32.  */
  33. public class LogisticDistribution extends AbstractRealDistribution {

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

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

  40.     /**
  41.      * Build a new instance.
  42.      * <p>
  43.      * <b>Note:</b> this constructor will implicitly create an instance of
  44.      * {@link Well19937c} as random generator to be used for sampling only (see
  45.      * {@link #sample()} and {@link #sample(int)}). In case no sampling is
  46.      * needed for the created distribution, it is advised to pass {@code null}
  47.      * as random generator via the appropriate constructors to avoid the
  48.      * additional initialisation overhead.
  49.      *
  50.      * @param mu location parameter
  51.      * @param s scale parameter (must be positive)
  52.      * @throws NotStrictlyPositiveException if {@code beta <= 0}
  53.      */
  54.     public LogisticDistribution(double mu, double s) {
  55.         this(new Well19937c(), mu, s);
  56.     }

  57.     /**
  58.      * Build a new instance.
  59.      *
  60.      * @param rng Random number generator
  61.      * @param mu location parameter
  62.      * @param s scale parameter (must be positive)
  63.      * @throws NotStrictlyPositiveException if {@code beta <= 0}
  64.      */
  65.     public LogisticDistribution(RandomGenerator rng, double mu, double s) {
  66.         super(rng);

  67.         if (s <= 0.0) {
  68.             throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, s);
  69.         }

  70.         this.mu = mu;
  71.         this.s = s;
  72.     }

  73.     /**
  74.      * Access the location parameter, {@code mu}.
  75.      *
  76.      * @return the location parameter.
  77.      */
  78.     public double getLocation() {
  79.         return mu;
  80.     }

  81.     /**
  82.      * Access the scale parameter, {@code s}.
  83.      *
  84.      * @return the scale parameter.
  85.      */
  86.     public double getScale() {
  87.         return s;
  88.     }

  89.     /** {@inheritDoc} */
  90.     public double density(double x) {
  91.         double z = (x - mu) / s;
  92.         double v = FastMath.exp(-z);
  93.         return 1 / s * v / ((1.0 + v) * (1.0 + v));
  94.     }

  95.     /** {@inheritDoc} */
  96.     public double cumulativeProbability(double x) {
  97.         double z = 1 / s * (x - mu);
  98.         return 1.0 / (1.0 + FastMath.exp(-z));
  99.     }

  100.     @Override
  101.     public double inverseCumulativeProbability(double p) throws OutOfRangeException {
  102.         if (p < 0.0 || p > 1.0) {
  103.             throw new OutOfRangeException(p, 0.0, 1.0);
  104.         } else if (p == 0) {
  105.             return 0.0;
  106.         } else if (p == 1) {
  107.             return Double.POSITIVE_INFINITY;
  108.         }
  109.         return s * Math.log(p / (1.0 - p)) + mu;
  110.     }

  111.     /** {@inheritDoc} */
  112.     public double getNumericalMean() {
  113.         return mu;
  114.     }

  115.     /** {@inheritDoc} */
  116.     public double getNumericalVariance() {
  117.         return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s));
  118.     }

  119.     /** {@inheritDoc} */
  120.     public double getSupportLowerBound() {
  121.         return Double.NEGATIVE_INFINITY;
  122.     }

  123.     /** {@inheritDoc} */
  124.     public double getSupportUpperBound() {
  125.         return Double.POSITIVE_INFINITY;
  126.     }

  127.     /** {@inheritDoc} */
  128.     public boolean isSupportLowerBoundInclusive() {
  129.         return false;
  130.     }

  131.     /** {@inheritDoc} */
  132.     public boolean isSupportUpperBoundInclusive() {
  133.         return false;
  134.     }

  135.     /** {@inheritDoc} */
  136.     public boolean isSupportConnected() {
  137.         return true;
  138.     }

  139. }