WeibullDistribution.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.special.Gamma;
  25. import org.hipparchus.util.FastMath;
  26. import org.hipparchus.util.MathUtils;

  27. /**
  28.  * Implementation of the Weibull distribution. This implementation uses the
  29.  * two parameter form of the distribution defined by
  30.  * <a href="http://mathworld.wolfram.com/WeibullDistribution.html">
  31.  * Weibull Distribution</a>, equations (1) and (2).
  32.  *
  33.  * @see <a href="http://en.wikipedia.org/wiki/Weibull_distribution">Weibull distribution (Wikipedia)</a>
  34.  * @see <a href="http://mathworld.wolfram.com/WeibullDistribution.html">Weibull distribution (MathWorld)</a>
  35.  */
  36. public class WeibullDistribution extends AbstractRealDistribution {
  37.     /** Serializable version identifier. */
  38.     private static final long serialVersionUID = 20160320L;
  39.     /** The shape parameter. */
  40.     private final double shape;
  41.     /** The scale parameter. */
  42.     private final double scale;

  43.     /**
  44.      * Create a Weibull distribution with the given shape and scale.
  45.      *
  46.      * @param alpha Shape parameter.
  47.      * @param beta Scale parameter.
  48.      * @throws MathIllegalArgumentException if {@code alpha <= 0} or {@code beta <= 0}.
  49.      */
  50.     public WeibullDistribution(double alpha, double beta)
  51.         throws MathIllegalArgumentException {
  52.         if (alpha <= 0) {
  53.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SHAPE,
  54.                                                    alpha);
  55.         }
  56.         if (beta <= 0) {
  57.             throw new MathIllegalArgumentException(LocalizedCoreFormats.SCALE,
  58.                                                    beta);
  59.         }
  60.         scale = beta;
  61.         shape = alpha;
  62.     }

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

  71.     /**
  72.      * Access the scale parameter, {@code beta}.
  73.      *
  74.      * @return the scale parameter, {@code beta}.
  75.      */
  76.     public double getScale() {
  77.         return scale;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public double density(double x) {
  82.         if (x < 0) {
  83.             return 0;
  84.         }

  85.         final double xscale = x / scale;
  86.         final double xscalepow = FastMath.pow(xscale, shape - 1);

  87.         /*
  88.          * FastMath.pow(x / scale, shape) =
  89.          * FastMath.pow(xscale, shape) =
  90.          * FastMath.pow(xscale, shape - 1) * xscale
  91.          */
  92.         final double xscalepowshape = xscalepow * xscale;

  93.         return (shape / scale) * xscalepow * FastMath.exp(-xscalepowshape);
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public double logDensity(double x) {
  98.         if (x < 0) {
  99.             return Double.NEGATIVE_INFINITY;
  100.         }

  101.         final double xscale = x / scale;
  102.         final double logxscalepow = FastMath.log(xscale) * (shape - 1);

  103.         /*
  104.          * FastMath.pow(x / scale, shape) =
  105.          * FastMath.pow(xscale, shape) =
  106.          * FastMath.pow(xscale, shape - 1) * xscale
  107.          */
  108.         final double xscalepowshape = FastMath.exp(logxscalepow) * xscale;

  109.         return FastMath.log(shape / scale) + logxscalepow - xscalepowshape;
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public double cumulativeProbability(double x) {
  114.         double ret;
  115.         if (x <= 0.0) {
  116.             ret = 0.0;
  117.         } else {
  118.             ret = 1.0 - FastMath.exp(-FastMath.pow(x / scale, shape));
  119.         }
  120.         return ret;
  121.     }

  122.     /**
  123.      * {@inheritDoc}
  124.      *
  125.      * Returns {@code 0} when {@code p == 0} and
  126.      * {@code Double.POSITIVE_INFINITY} when {@code p == 1}.
  127.      */
  128.     @Override
  129.     public double inverseCumulativeProbability(double p) {
  130.         MathUtils.checkRangeInclusive(p, 0, 1);

  131.         double ret;
  132.         if (p == 0) {
  133.             ret = 0.0;
  134.         } else  if (p == 1) {
  135.             ret = Double.POSITIVE_INFINITY;
  136.         } else {
  137.             ret = scale * FastMath.pow(-FastMath.log1p(-p), 1.0 / shape);
  138.         }
  139.         return ret;
  140.     }

  141.     /**
  142.      * {@inheritDoc}
  143.      *
  144.      * The mean is {@code scale * Gamma(1 + (1 / shape))}, where {@code Gamma()}
  145.      * is the Gamma-function.
  146.      */
  147.     @Override
  148.     public double getNumericalMean() {
  149.         final double sh = getShape();
  150.         final double sc = getScale();

  151.         return sc * FastMath.exp(Gamma.logGamma(1 + (1 / sh)));
  152.     }

  153.     /**
  154.      * {@inheritDoc}
  155.      *
  156.      * The variance is {@code scale^2 * Gamma(1 + (2 / shape)) - mean^2}
  157.      * where {@code Gamma()} is the Gamma-function.
  158.      */
  159.     @Override
  160.     public double getNumericalVariance() {
  161.         final double sh = getShape();
  162.         final double sc = getScale();
  163.         final double mn = getNumericalMean();

  164.         return (sc * sc) * FastMath.exp(Gamma.logGamma(1 + (2 / sh))) -
  165.                (mn * mn);
  166.     }

  167.     /**
  168.      * {@inheritDoc}
  169.      *
  170.      * The lower bound of the support is always 0 no matter the parameters.
  171.      *
  172.      * @return lower bound of the support (always 0)
  173.      */
  174.     @Override
  175.     public double getSupportLowerBound() {
  176.         return 0;
  177.     }

  178.     /**
  179.      * {@inheritDoc}
  180.      *
  181.      * The upper bound of the support is always positive infinity
  182.      * no matter the parameters.
  183.      *
  184.      * @return upper bound of the support (always
  185.      * {@code Double.POSITIVE_INFINITY})
  186.      */
  187.     @Override
  188.     public double getSupportUpperBound() {
  189.         return Double.POSITIVE_INFINITY;
  190.     }

  191.     /**
  192.      * {@inheritDoc}
  193.      *
  194.      * The support of this distribution is connected.
  195.      *
  196.      * @return {@code true}
  197.      */
  198.     @Override
  199.     public boolean isSupportConnected() {
  200.         return true;
  201.     }
  202. }