org.apache.commons.statistics.distribution.ParetoDistribution Maven / Gradle / Ivy
Show all versions of virtdata-lib-curves4 Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.statistics.distribution;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
import org.apache.commons.rng.sampling.distribution.InverseTransformParetoSampler;
/**
* Implementation of the Pareto distribution.
*
*
* Parameters:
* The probability distribution function of {@code X} is given by (for {@code x >= k}):
*
* α * k^α / x^(α + 1)
*
*
* - {@code k} is the scale parameter: this is the minimum possible value of {@code X},
* - {@code α} is the shape parameter: this is the Pareto index
*
*/
public class ParetoDistribution extends AbstractContinuousDistribution {
/** The scale parameter of this distribution. */
private final double scale;
/** The shape parameter of this distribution. */
private final double shape;
/**
* Creates a Pareto distribution.
*
* @param scale Scale parameter of this distribution.
* @param shape Shape parameter of this distribution.
* @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0}.
*/
public ParetoDistribution(double scale,
double shape) {
if (scale <= 0) {
throw new DistributionException(DistributionException.NEGATIVE, scale);
}
if (shape <= 0) {
throw new DistributionException(DistributionException.NEGATIVE, shape);
}
this.scale = scale;
this.shape = shape;
}
/**
* Returns the scale parameter of this distribution.
*
* @return the scale parameter
*/
public double getScale() {
return scale;
}
/**
* Returns the shape parameter of this distribution.
*
* @return the shape parameter
*/
public double getShape() {
return shape;
}
/**
* {@inheritDoc}
*
* For scale {@code k}, and shape {@code α} of this distribution, the PDF
* is given by
*
* - {@code 0} if {@code x < k},
* - {@code α * k^α / x^(α + 1)} otherwise.
*
*/
@Override
public double density(double x) {
if (x < scale) {
return 0;
}
return Math.pow(scale, shape) / Math.pow(x, shape + 1) * shape;
}
/** {@inheritDoc}
*
* See documentation of {@link #density(double)} for computation details.
*/
@Override
public double logDensity(double x) {
if (x < scale) {
return Double.NEGATIVE_INFINITY;
}
return Math.log(scale) * shape - Math.log(x) * (shape + 1) + Math.log(shape);
}
/**
* {@inheritDoc}
*
* For scale {@code k}, and shape {@code α} of this distribution, the CDF is given by
*
* - {@code 0} if {@code x < k},
* - {@code 1 - (k / x)^α} otherwise.
*
*/
@Override
public double cumulativeProbability(double x) {
if (x <= scale) {
return 0;
}
return 1 - Math.pow(scale / x, shape);
}
/**
* {@inheritDoc}
*
* For scale {@code k} and shape {@code α}, the mean is given by
*
* - {@code ∞} if {@code α <= 1},
* - {@code α * k / (α - 1)} otherwise.
*
*/
@Override
public double getMean() {
if (shape <= 1) {
return Double.POSITIVE_INFINITY;
}
return shape * scale / (shape - 1);
}
/**
* {@inheritDoc}
*
* For scale {@code k} and shape {@code α}, the variance is given by
*
* - {@code ∞} if {@code 1 < α <= 2},
* - {@code k^2 * α / ((α - 1)^2 * (α - 2))} otherwise.
*
*/
@Override
public double getVariance() {
if (shape <= 2) {
return Double.POSITIVE_INFINITY;
}
double s = shape - 1;
return scale * scale * shape / (s * s) / (shape - 2);
}
/**
* {@inheritDoc}
*
* The lower bound of the support is equal to the scale parameter {@code k}.
*
* @return lower bound of the support
*/
@Override
public double getSupportLowerBound() {
return scale;
}
/**
* {@inheritDoc}
*
* The upper bound of the support is always positive infinity no matter the parameters.
*
* @return upper bound of the support (always {@code Double.POSITIVE_INFINITY})
*/
@Override
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
}
/**
* {@inheritDoc}
*
* The support of this distribution is connected.
*
* @return {@code true}
*/
@Override
public boolean isSupportConnected() {
return true;
}
/** {@inheritDoc} */
@Override
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
return new ContinuousDistribution.Sampler() {
/**
* Pareto distribution sampler.
*/
private final ContinuousSampler sampler =
new InverseTransformParetoSampler(rng, scale, shape);
/**{@inheritDoc} */
@Override
public double sample() {
return sampler.sample();
}
};
}
}