NakagamiDistribution.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.math3.distribution;

  18. import org.apache.commons.math3.exception.NotStrictlyPositiveException;
  19. import org.apache.commons.math3.exception.NumberIsTooSmallException;
  20. import org.apache.commons.math3.exception.util.LocalizedFormats;
  21. import org.apache.commons.math3.random.RandomGenerator;
  22. import org.apache.commons.math3.random.Well19937c;
  23. import org.apache.commons.math3.special.Gamma;
  24. import org.apache.commons.math3.util.FastMath;

  25. /**
  26.  * This class implements the Nakagami distribution.
  27.  *
  28.  * @see <a href="http://en.wikipedia.org/wiki/Nakagami_distribution">Nakagami Distribution (Wikipedia)</a>
  29.  *
  30.  * @since 3.4
  31.  */
  32. public class NakagamiDistribution extends AbstractRealDistribution {

  33.     /** Default inverse cumulative probability accuracy. */
  34.     public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;

  35.     /** Serializable version identifier. */
  36.     private static final long serialVersionUID = 20141003;

  37.     /** The shape parameter. */
  38.     private final double mu;
  39.     /** The scale parameter. */
  40.     private final double omega;
  41.     /** Inverse cumulative probability accuracy. */
  42.     private final double inverseAbsoluteAccuracy;

  43.     /**
  44.      * Build a new instance.
  45.      * <p>
  46.      * <b>Note:</b> this constructor will implicitly create an instance of
  47.      * {@link Well19937c} as random generator to be used for sampling only (see
  48.      * {@link #sample()} and {@link #sample(int)}). In case no sampling is
  49.      * needed for the created distribution, it is advised to pass {@code null}
  50.      * as random generator via the appropriate constructors to avoid the
  51.      * additional initialisation overhead.
  52.      *
  53.      * @param mu shape parameter
  54.      * @param omega scale parameter (must be positive)
  55.      * @throws NumberIsTooSmallException if {@code mu < 0.5}
  56.      * @throws NotStrictlyPositiveException if {@code omega <= 0}
  57.      */
  58.     public NakagamiDistribution(double mu, double omega) {
  59.         this(mu, omega, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
  60.     }

  61.     /**
  62.      * Build a new instance.
  63.      * <p>
  64.      * <b>Note:</b> this constructor will implicitly create an instance of
  65.      * {@link Well19937c} as random generator to be used for sampling only (see
  66.      * {@link #sample()} and {@link #sample(int)}). In case no sampling is
  67.      * needed for the created distribution, it is advised to pass {@code null}
  68.      * as random generator via the appropriate constructors to avoid the
  69.      * additional initialisation overhead.
  70.      *
  71.      * @param mu shape parameter
  72.      * @param omega scale parameter (must be positive)
  73.      * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
  74.      * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
  75.      * @throws NumberIsTooSmallException if {@code mu < 0.5}
  76.      * @throws NotStrictlyPositiveException if {@code omega <= 0}
  77.      */
  78.     public NakagamiDistribution(double mu, double omega, double inverseAbsoluteAccuracy) {
  79.         this(new Well19937c(), mu, omega, inverseAbsoluteAccuracy);
  80.     }

  81.     /**
  82.      * Build a new instance.
  83.      *
  84.      * @param rng Random number generator
  85.      * @param mu shape parameter
  86.      * @param omega scale parameter (must be positive)
  87.      * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
  88.      * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
  89.      * @throws NumberIsTooSmallException if {@code mu < 0.5}
  90.      * @throws NotStrictlyPositiveException if {@code omega <= 0}
  91.      */
  92.     public NakagamiDistribution(RandomGenerator rng, double mu, double omega, double inverseAbsoluteAccuracy) {
  93.         super(rng);

  94.         if (mu < 0.5) {
  95.             throw new NumberIsTooSmallException(mu, 0.5, true);
  96.         }
  97.         if (omega <= 0) {
  98.             throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, omega);
  99.         }

  100.         this.mu = mu;
  101.         this.omega = omega;
  102.         this.inverseAbsoluteAccuracy = inverseAbsoluteAccuracy;
  103.     }

  104.     /**
  105.      * Access the shape parameter, {@code mu}.
  106.      *
  107.      * @return the shape parameter.
  108.      */
  109.     public double getShape() {
  110.         return mu;
  111.     }

  112.     /**
  113.      * Access the scale parameter, {@code omega}.
  114.      *
  115.      * @return the scale parameter.
  116.      */
  117.     public double getScale() {
  118.         return omega;
  119.     }

  120.     @Override
  121.     protected double getSolverAbsoluteAccuracy() {
  122.         return inverseAbsoluteAccuracy;
  123.     }

  124.     /** {@inheritDoc} */
  125.     public double density(double x) {
  126.         if (x <= 0) {
  127.             return 0.0;
  128.         }
  129.         return 2.0 * FastMath.pow(mu, mu) / (Gamma.gamma(mu) * FastMath.pow(omega, mu)) *
  130.                      FastMath.pow(x, 2 * mu - 1) * FastMath.exp(-mu * x * x / omega);
  131.     }

  132.     /** {@inheritDoc} */
  133.     public double cumulativeProbability(double x) {
  134.         return Gamma.regularizedGammaP(mu, mu * x * x / omega);
  135.     }

  136.     /** {@inheritDoc} */
  137.     public double getNumericalMean() {
  138.         return Gamma.gamma(mu + 0.5) / Gamma.gamma(mu) * FastMath.sqrt(omega / mu);
  139.     }

  140.     /** {@inheritDoc} */
  141.     public double getNumericalVariance() {
  142.         double v = Gamma.gamma(mu + 0.5) / Gamma.gamma(mu);
  143.         return omega * (1 - 1 / mu * v * v);
  144.     }

  145.     /** {@inheritDoc} */
  146.     public double getSupportLowerBound() {
  147.         return 0;
  148.     }

  149.     /** {@inheritDoc} */
  150.     public double getSupportUpperBound() {
  151.         return Double.POSITIVE_INFINITY;
  152.     }

  153.     /** {@inheritDoc} */
  154.     public boolean isSupportLowerBoundInclusive() {
  155.         return true;
  156.     }

  157.     /** {@inheritDoc} */
  158.     public boolean isSupportUpperBoundInclusive() {
  159.         return false;
  160.     }

  161.     /** {@inheritDoc} */
  162.     public boolean isSupportConnected() {
  163.         return true;
  164.     }

  165. }