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 */
017package org.apache.commons.rng.sampling.distribution;
018
019import org.apache.commons.rng.UniformRandomProvider;
020
021/**
022 * Sampling from a <a href="https://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a>.
023 *
024 * @since 1.0
025 */
026public class InverseTransformParetoSampler
027    extends SamplerBase
028    implements ContinuousSampler {
029    /** Scale. */
030    private final double scale;
031    /** Shape. */
032    private final double shape;
033    /** Underlying source of randomness. */
034    private final UniformRandomProvider rng;
035
036    /**
037     * @param rng Generator of uniformly distributed random numbers.
038     * @param scale Scale of the distribution.
039     * @param shape Shape of the distribution.
040     */
041    public InverseTransformParetoSampler(UniformRandomProvider rng,
042                                         double scale,
043                                         double shape) {
044        super(null);
045        this.rng = rng;
046        this.scale = scale;
047        this.shape = shape;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public double sample() {
053        return scale / Math.pow(rng.nextDouble(), 1 / shape);
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public String toString() {
059        return "[Inverse method for Pareto distribution " + rng.toString() + "]";
060    }
061}