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.rng.sampling.distribution; 18 19 import org.apache.commons.rng.UniformRandomProvider; 20 21 /** 22 * Sampling from a <a href="https://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a>. 23 * 24 * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p> 25 * 26 * @since 1.0 27 */ 28 public class InverseTransformParetoSampler 29 extends SamplerBase 30 implements SharedStateContinuousSampler { 31 /** Scale. */ 32 private final double scale; 33 /** 1 / Shape. */ 34 private final double oneOverShape; 35 /** Underlying source of randomness. */ 36 private final UniformRandomProvider rng; 37 38 /** 39 * @param rng Generator of uniformly distributed random numbers. 40 * @param scale Scale of the distribution. 41 * @param shape Shape of the distribution. 42 * @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0} 43 */ 44 public InverseTransformParetoSampler(UniformRandomProvider rng, 45 double scale, 46 double shape) { 47 super(null); 48 if (scale <= 0) { 49 throw new IllegalArgumentException("scale is not strictly positive: " + scale); 50 } 51 if (shape <= 0) { 52 throw new IllegalArgumentException("shape is not strictly positive: " + shape); 53 } 54 this.rng = rng; 55 this.scale = scale; 56 this.oneOverShape = 1 / shape; 57 } 58 59 /** 60 * @param rng Generator of uniformly distributed random numbers. 61 * @param source Source to copy. 62 */ 63 private InverseTransformParetoSampler(UniformRandomProvider rng, 64 InverseTransformParetoSampler source) { 65 super(null); 66 this.rng = rng; 67 scale = source.scale; 68 oneOverShape = source.oneOverShape; 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 public double sample() { 74 return scale / Math.pow(rng.nextDouble(), oneOverShape); 75 } 76 77 /** {@inheritDoc} */ 78 @Override 79 public String toString() { 80 return "[Inverse method for Pareto distribution " + rng.toString() + "]"; 81 } 82 83 /** 84 * {@inheritDoc} 85 * 86 * @since 1.3 87 */ 88 @Override 89 public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { 90 return new InverseTransformParetoSampler(rng, this); 91 } 92 93 /** 94 * Creates a new Pareto distribution sampler. 95 * 96 * @param rng Generator of uniformly distributed random numbers. 97 * @param scale Scale of the distribution. 98 * @param shape Shape of the distribution. 99 * @return the sampler 100 * @throws IllegalArgumentException if {@code scale <= 0} or {@code shape <= 0} 101 * @since 1.3 102 */ 103 public static SharedStateContinuousSampler of(UniformRandomProvider rng, 104 double scale, 105 double shape) { 106 return new InverseTransformParetoSampler(rng, scale, shape); 107 } 108 }