LogNormalSampler.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.  *      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. import org.apache.commons.rng.UniformRandomProvider;

  19. /**
  20.  * Sampling from a log-normal distribution.
  21.  *
  22.  * @since 1.1
  23.  */
  24. public class LogNormalSampler implements SharedStateContinuousSampler {
  25.     /** Mean of the natural logarithm of the distribution values. */
  26.     private final double mu;
  27.     /** Standard deviation of the natural logarithm of the distribution values. */
  28.     private final double sigma;
  29.     /** Gaussian sampling. */
  30.     private final NormalizedGaussianSampler gaussian;

  31.     /**
  32.      * Create an instance.
  33.      *
  34.      * @param gaussian N(0,1) generator.
  35.      * @param mu Mean of the natural logarithm of the distribution values.
  36.      * @param sigma Standard deviation of the natural logarithm of the distribution values.
  37.      * @throws IllegalArgumentException if {@code sigma <= 0}.
  38.      */
  39.     public LogNormalSampler(NormalizedGaussianSampler gaussian,
  40.                             double mu,
  41.                             double sigma) {
  42.         // Validation before java.lang.Object constructor exits prevents partially initialized object
  43.         this(mu, InternalUtils.requireStrictlyPositive(sigma, "sigma"), gaussian);
  44.     }

  45.     /**
  46.      * @param mu Mean of the natural logarithm of the distribution values.
  47.      * @param sigma Standard deviation of the natural logarithm of the distribution values.
  48.      * @param gaussian N(0,1) generator.
  49.      */
  50.     private LogNormalSampler(double mu,
  51.                              double sigma,
  52.                              NormalizedGaussianSampler gaussian) {
  53.         this.mu = mu;
  54.         this.sigma = sigma;
  55.         this.gaussian = gaussian;
  56.     }

  57.     /**
  58.      * @param rng Generator of uniformly distributed random numbers.
  59.      * @param source Source to copy.
  60.      */
  61.     private LogNormalSampler(UniformRandomProvider rng,
  62.                              LogNormalSampler source) {
  63.         this.mu = source.mu;
  64.         this.sigma = source.sigma;
  65.         this.gaussian = InternalUtils.newNormalizedGaussianSampler(source.gaussian, rng);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public double sample() {
  70.         return Math.exp(mu + sigma * gaussian.sample());
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public String toString() {
  75.         return "Log-normal deviate [" + gaussian.toString() + "]";
  76.     }

  77.     /**
  78.      * {@inheritDoc}
  79.      *
  80.      * <p>Note: This function is available if the underlying {@link NormalizedGaussianSampler}
  81.      * is a {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler}.
  82.      * Otherwise a run-time exception is thrown.</p>
  83.      *
  84.      * @throws UnsupportedOperationException if the underlying sampler is not a
  85.      * {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler} or
  86.      * does not return a {@link NormalizedGaussianSampler} when sharing state.
  87.      *
  88.      * @since 1.3
  89.      */
  90.     @Override
  91.     public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) {
  92.         return new LogNormalSampler(rng, this);
  93.     }

  94.     /**
  95.      * Create a new log-normal distribution sampler.
  96.      *
  97.      * <p>Note: The shared-state functionality is available if the {@link NormalizedGaussianSampler}
  98.      * is a {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler}.
  99.      * Otherwise a run-time exception will be thrown when the sampler is used to share state.</p>
  100.      *
  101.      * @param gaussian N(0,1) generator.
  102.      * @param mu Mean of the natural logarithm of the distribution values.
  103.      * @param sigma Standard deviation of the natural logarithm of the distribution values.
  104.      * @return the sampler
  105.      * @throws IllegalArgumentException if {@code sigma <= 0}.
  106.      * @see #withUniformRandomProvider(UniformRandomProvider)
  107.      * @since 1.3
  108.      */
  109.     public static SharedStateContinuousSampler of(NormalizedGaussianSampler gaussian,
  110.                                                   double mu,
  111.                                                   double sigma) {
  112.         return new LogNormalSampler(gaussian, mu, sigma);
  113.     }
  114. }