View Javadoc
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 log-normal distribution.
23   *
24   * @since 1.1
25   */
26  public class LogNormalSampler implements SharedStateContinuousSampler {
27      /** Mean of the natural logarithm of the distribution values. */
28      private final double mu;
29      /** Standard deviation of the natural logarithm of the distribution values. */
30      private final double sigma;
31      /** Gaussian sampling. */
32      private final NormalizedGaussianSampler gaussian;
33  
34      /**
35       * Create an instance.
36       *
37       * @param gaussian N(0,1) generator.
38       * @param mu Mean of the natural logarithm of the distribution values.
39       * @param sigma Standard deviation of the natural logarithm of the distribution values.
40       * @throws IllegalArgumentException if {@code sigma <= 0}.
41       */
42      public LogNormalSampler(NormalizedGaussianSampler gaussian,
43                              double mu,
44                              double sigma) {
45          // Validation before java.lang.Object constructor exits prevents partially initialized object
46          this(mu, InternalUtils.requireStrictlyPositive(sigma, "sigma"), gaussian);
47      }
48  
49      /**
50       * @param mu Mean of the natural logarithm of the distribution values.
51       * @param sigma Standard deviation of the natural logarithm of the distribution values.
52       * @param gaussian N(0,1) generator.
53       */
54      private LogNormalSampler(double mu,
55                               double sigma,
56                               NormalizedGaussianSampler gaussian) {
57          this.mu = mu;
58          this.sigma = sigma;
59          this.gaussian = gaussian;
60      }
61  
62      /**
63       * @param rng Generator of uniformly distributed random numbers.
64       * @param source Source to copy.
65       */
66      private LogNormalSampler(UniformRandomProvider rng,
67                               LogNormalSampler source) {
68          this.mu = source.mu;
69          this.sigma = source.sigma;
70          this.gaussian = InternalUtils.newNormalizedGaussianSampler(source.gaussian, rng);
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public double sample() {
76          return Math.exp(mu + sigma * gaussian.sample());
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public String toString() {
82          return "Log-normal deviate [" + gaussian.toString() + "]";
83      }
84  
85      /**
86       * {@inheritDoc}
87       *
88       * <p>Note: This function is available if the underlying {@link NormalizedGaussianSampler}
89       * is a {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler}.
90       * Otherwise a run-time exception is thrown.</p>
91       *
92       * @throws UnsupportedOperationException if the underlying sampler is not a
93       * {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler} or
94       * does not return a {@link NormalizedGaussianSampler} when sharing state.
95       *
96       * @since 1.3
97       */
98      @Override
99      public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) {
100         return new LogNormalSampler(rng, this);
101     }
102 
103     /**
104      * Create a new log-normal distribution sampler.
105      *
106      * <p>Note: The shared-state functionality is available if the {@link NormalizedGaussianSampler}
107      * is a {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler}.
108      * Otherwise a run-time exception will be thrown when the sampler is used to share state.</p>
109      *
110      * @param gaussian N(0,1) generator.
111      * @param mu Mean of the natural logarithm of the distribution values.
112      * @param sigma Standard deviation of the natural logarithm of the distribution values.
113      * @return the sampler
114      * @throws IllegalArgumentException if {@code sigma <= 0}.
115      * @see #withUniformRandomProvider(UniformRandomProvider)
116      * @since 1.3
117      */
118     public static SharedStateContinuousSampler of(NormalizedGaussianSampler gaussian,
119                                                   double mu,
120                                                   double sigma) {
121         return new LogNormalSampler(gaussian, mu, sigma);
122     }
123 }