001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.statistics.distribution;
019
020import org.apache.commons.rng.UniformRandomProvider;
021import org.apache.commons.rng.sampling.distribution.InverseTransformParetoSampler;
022
023/**
024 * Implementation of the <a href="http://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a>.
025 *
026 * <p>
027 * <strong>Parameters:</strong>
028 * The probability distribution function of {@code X} is given by (for {@code x >= k}):
029 * <pre>
030 *  α * k^α / x^(α + 1)
031 * </pre>
032 * <ul>
033 * <li>{@code k} is the <em>scale</em> parameter: this is the minimum possible value of {@code X},</li>
034 * <li>{@code α} is the <em>shape</em> parameter: this is the Pareto index</li>
035 * </ul>
036 */
037public class ParetoDistribution extends AbstractContinuousDistribution {
038    /** The minimum value for the shape parameter when computing when computing the variance. */
039    private static final double MIN_SHAPE_FOR_VARIANCE = 2.0;
040
041    /** The scale parameter of this distribution. */
042    private final double scale;
043    /** The shape parameter of this distribution. */
044    private final double shape;
045
046    /**
047     * Creates a Pareto distribution.
048     *
049     * @param scale Scale parameter of this distribution.
050     * @param shape Shape parameter of this distribution.
051     * @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0}.
052     */
053    public ParetoDistribution(double scale,
054                              double shape) {
055        if (scale <= 0) {
056            throw new DistributionException(DistributionException.NEGATIVE, scale);
057        }
058
059        if (shape <= 0) {
060            throw new DistributionException(DistributionException.NEGATIVE, shape);
061        }
062
063        this.scale = scale;
064        this.shape = shape;
065    }
066
067    /**
068     * Returns the scale parameter of this distribution.
069     *
070     * @return the scale parameter
071     */
072    public double getScale() {
073        return scale;
074    }
075
076    /**
077     * Returns the shape parameter of this distribution.
078     *
079     * @return the shape parameter
080     */
081    public double getShape() {
082        return shape;
083    }
084
085    /**
086     * {@inheritDoc}
087     * <p>
088     * For scale {@code k}, and shape {@code α} of this distribution, the PDF
089     * is given by
090     * <ul>
091     * <li>{@code 0} if {@code x < k},</li>
092     * <li>{@code α * k^α / x^(α + 1)} otherwise.</li>
093     * </ul>
094     */
095    @Override
096    public double density(double x) {
097        if (x < scale) {
098            return 0;
099        }
100        return Math.pow(scale, shape) / Math.pow(x, shape + 1) * shape;
101    }
102
103    /** {@inheritDoc}
104     *
105     * See documentation of {@link #density(double)} for computation details.
106     */
107    @Override
108    public double logDensity(double x) {
109        if (x < scale) {
110            return Double.NEGATIVE_INFINITY;
111        }
112        return Math.log(scale) * shape - Math.log(x) * (shape + 1) + Math.log(shape);
113    }
114
115    /**
116     * {@inheritDoc}
117     * <p>
118     * For scale {@code k}, and shape {@code α} of this distribution, the CDF is given by
119     * <ul>
120     * <li>{@code 0} if {@code x < k},</li>
121     * <li>{@code 1 - (k / x)^α} otherwise.</li>
122     * </ul>
123     */
124    @Override
125    public double cumulativeProbability(double x)  {
126        if (x <= scale) {
127            return 0;
128        }
129        return 1 - Math.pow(scale / x, shape);
130    }
131
132    /**
133     * {@inheritDoc}
134     * <p>
135     * For scale {@code k} and shape {@code α}, the mean is given by
136     * <ul>
137     * <li>{@code ∞} if {@code α <= 1},</li>
138     * <li>{@code α * k / (α - 1)} otherwise.</li>
139     * </ul>
140     */
141    @Override
142    public double getMean() {
143        if (shape <= 1) {
144            return Double.POSITIVE_INFINITY;
145        }
146        return shape * scale / (shape - 1);
147    }
148
149    /**
150     * {@inheritDoc}
151     * <p>
152     * For scale {@code k} and shape {@code α}, the variance is given by
153     * <ul>
154     * <li>{@code ∞} if {@code 1 < α <= 2},</li>
155     * <li>{@code k^2 * α / ((α - 1)^2 * (α - 2))} otherwise.</li>
156     * </ul>
157     */
158    @Override
159    public double getVariance() {
160        if (shape <= MIN_SHAPE_FOR_VARIANCE) {
161            return Double.POSITIVE_INFINITY;
162        }
163        final double s = shape - 1;
164        return scale * scale * shape / (s * s) / (shape - 2);
165    }
166
167    /**
168     * {@inheritDoc}
169     * <p>
170     * The lower bound of the support is equal to the scale parameter {@code k}.
171     *
172     * @return lower bound of the support
173     */
174    @Override
175    public double getSupportLowerBound() {
176        return getScale();
177    }
178
179    /**
180     * {@inheritDoc}
181     * <p>
182     * The upper bound of the support is always positive infinity no matter the parameters.
183     *
184     * @return upper bound of the support (always {@code Double.POSITIVE_INFINITY})
185     */
186    @Override
187    public double getSupportUpperBound() {
188        return Double.POSITIVE_INFINITY;
189    }
190
191    /**
192     * {@inheritDoc}
193     * <p>
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
203    /** {@inheritDoc} */
204    @Override
205    public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
206        // Pareto distribution sampler.
207        return new InverseTransformParetoSampler(rng, scale, shape)::sample;
208    }
209}