GaussianSampler.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 Gaussian distribution with given mean and
  21.  * standard deviation.
  22.  *
  23.  * <h2>Note</h2>
  24.  *
  25.  * <p>The mean and standard deviation are validated to ensure they are finite. This prevents
  26.  * generation of NaN samples by avoiding invalid arithmetic (inf * 0 or inf - inf).
  27.  * However use of an extremely large standard deviation and/or mean may result in samples that are
  28.  * infinite; that is the parameters are not validated to prevent truncation of the output
  29.  * distribution.
  30.  *
  31.  * @since 1.1
  32.  */
  33. public class GaussianSampler implements SharedStateContinuousSampler {
  34.     /** Mean. */
  35.     private final double mean;
  36.     /** standardDeviation. */
  37.     private final double standardDeviation;
  38.     /** Normalized Gaussian sampler. */
  39.     private final NormalizedGaussianSampler normalized;

  40.     /**
  41.      * Create an instance.
  42.      *
  43.      * @param normalized Generator of N(0,1) Gaussian distributed random numbers.
  44.      * @param mean Mean of the Gaussian distribution.
  45.      * @param standardDeviation Standard deviation of the Gaussian distribution.
  46.      * @throws IllegalArgumentException if {@code standardDeviation <= 0} or is infinite;
  47.      * or {@code mean} is infinite
  48.      */
  49.     public GaussianSampler(NormalizedGaussianSampler normalized,
  50.                            double mean,
  51.                            double standardDeviation) {
  52.         // Validation before java.lang.Object constructor exits prevents partially initialized object
  53.         this(InternalUtils.requireFinite(mean, "mean"),
  54.              InternalUtils.requireStrictlyPositiveFinite(standardDeviation, "standardDeviation"),
  55.              normalized);
  56.     }

  57.     /**
  58.      * @param mean Mean of the Gaussian distribution.
  59.      * @param standardDeviation Standard deviation of the Gaussian distribution.
  60.      * @param normalized Generator of N(0,1) Gaussian distributed random numbers.
  61.      */
  62.     private GaussianSampler(double mean,
  63.                             double standardDeviation,
  64.                             NormalizedGaussianSampler normalized) {
  65.         this.normalized = normalized;
  66.         this.mean = mean;
  67.         this.standardDeviation = standardDeviation;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public double sample() {
  72.         return standardDeviation * normalized.sample() + mean;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public String toString() {
  77.         return "Gaussian deviate [" + normalized.toString() + "]";
  78.     }

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

  97.     /**
  98.      * Create a new normalised Gaussian sampler.
  99.      *
  100.      * <p>Note: The shared-state functionality is available if the {@link NormalizedGaussianSampler}
  101.      * is a {@link org.apache.commons.rng.sampling.SharedStateSampler SharedStateSampler}.
  102.      * Otherwise a run-time exception will be thrown when the sampler is used to share state.</p>
  103.      *
  104.      * @param normalized Generator of N(0,1) Gaussian distributed random numbers.
  105.      * @param mean Mean of the Gaussian distribution.
  106.      * @param standardDeviation Standard deviation of the Gaussian distribution.
  107.      * @return the sampler
  108.      * @throws IllegalArgumentException if {@code standardDeviation <= 0} or is infinite;
  109.      * or {@code mean} is infinite
  110.      * @see #withUniformRandomProvider(UniformRandomProvider)
  111.      * @since 1.3
  112.      */
  113.     public static SharedStateContinuousSampler of(NormalizedGaussianSampler normalized,
  114.                                                   double mean,
  115.                                                   double standardDeviation) {
  116.         return new GaussianSampler(normalized, mean, standardDeviation);
  117.     }
  118. }