001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.distribution;
018
019import org.apache.commons.math3.exception.NotStrictlyPositiveException;
020import org.apache.commons.math3.exception.NumberIsTooSmallException;
021import org.apache.commons.math3.exception.util.LocalizedFormats;
022import org.apache.commons.math3.random.RandomGenerator;
023import org.apache.commons.math3.random.Well19937c;
024import org.apache.commons.math3.special.Gamma;
025import org.apache.commons.math3.util.FastMath;
026
027/**
028 * This class implements the Nakagami distribution.
029 *
030 * @see <a href="http://en.wikipedia.org/wiki/Nakagami_distribution">Nakagami Distribution (Wikipedia)</a>
031 *
032 * @since 3.4
033 */
034public class NakagamiDistribution extends AbstractRealDistribution {
035
036    /** Default inverse cumulative probability accuracy. */
037    public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
038
039    /** Serializable version identifier. */
040    private static final long serialVersionUID = 20141003;
041
042    /** The shape parameter. */
043    private final double mu;
044    /** The scale parameter. */
045    private final double omega;
046    /** Inverse cumulative probability accuracy. */
047    private final double inverseAbsoluteAccuracy;
048
049    /**
050     * Build a new instance.
051     * <p>
052     * <b>Note:</b> this constructor will implicitly create an instance of
053     * {@link Well19937c} as random generator to be used for sampling only (see
054     * {@link #sample()} and {@link #sample(int)}). In case no sampling is
055     * needed for the created distribution, it is advised to pass {@code null}
056     * as random generator via the appropriate constructors to avoid the
057     * additional initialisation overhead.
058     *
059     * @param mu shape parameter
060     * @param omega scale parameter (must be positive)
061     * @throws NumberIsTooSmallException if {@code mu < 0.5}
062     * @throws NotStrictlyPositiveException if {@code omega <= 0}
063     */
064    public NakagamiDistribution(double mu, double omega) {
065        this(mu, omega, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
066    }
067
068    /**
069     * Build a new instance.
070     * <p>
071     * <b>Note:</b> this constructor will implicitly create an instance of
072     * {@link Well19937c} as random generator to be used for sampling only (see
073     * {@link #sample()} and {@link #sample(int)}). In case no sampling is
074     * needed for the created distribution, it is advised to pass {@code null}
075     * as random generator via the appropriate constructors to avoid the
076     * additional initialisation overhead.
077     *
078     * @param mu shape parameter
079     * @param omega scale parameter (must be positive)
080     * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
081     * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
082     * @throws NumberIsTooSmallException if {@code mu < 0.5}
083     * @throws NotStrictlyPositiveException if {@code omega <= 0}
084     */
085    public NakagamiDistribution(double mu, double omega, double inverseAbsoluteAccuracy) {
086        this(new Well19937c(), mu, omega, inverseAbsoluteAccuracy);
087    }
088
089    /**
090     * Build a new instance.
091     *
092     * @param rng Random number generator
093     * @param mu shape parameter
094     * @param omega scale parameter (must be positive)
095     * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
096     * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
097     * @throws NumberIsTooSmallException if {@code mu < 0.5}
098     * @throws NotStrictlyPositiveException if {@code omega <= 0}
099     */
100    public NakagamiDistribution(RandomGenerator rng, double mu, double omega, double inverseAbsoluteAccuracy) {
101        super(rng);
102
103        if (mu < 0.5) {
104            throw new NumberIsTooSmallException(mu, 0.5, true);
105        }
106        if (omega <= 0) {
107            throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, omega);
108        }
109
110        this.mu = mu;
111        this.omega = omega;
112        this.inverseAbsoluteAccuracy = inverseAbsoluteAccuracy;
113    }
114
115    /**
116     * Access the shape parameter, {@code mu}.
117     *
118     * @return the shape parameter.
119     */
120    public double getShape() {
121        return mu;
122    }
123
124    /**
125     * Access the scale parameter, {@code omega}.
126     *
127     * @return the scale parameter.
128     */
129    public double getScale() {
130        return omega;
131    }
132
133    /** {@inheritDoc} */
134    @Override
135    protected double getSolverAbsoluteAccuracy() {
136        return inverseAbsoluteAccuracy;
137    }
138
139    /** {@inheritDoc} */
140    public double density(double x) {
141        if (x <= 0) {
142            return 0.0;
143        }
144        return 2.0 * FastMath.pow(mu, mu) / (Gamma.gamma(mu) * FastMath.pow(omega, mu)) *
145                     FastMath.pow(x, 2 * mu - 1) * FastMath.exp(-mu * x * x / omega);
146    }
147
148    /** {@inheritDoc} */
149    public double cumulativeProbability(double x) {
150        return Gamma.regularizedGammaP(mu, mu * x * x / omega);
151    }
152
153    /** {@inheritDoc} */
154    public double getNumericalMean() {
155        return Gamma.gamma(mu + 0.5) / Gamma.gamma(mu) * FastMath.sqrt(omega / mu);
156    }
157
158    /** {@inheritDoc} */
159    public double getNumericalVariance() {
160        double v = Gamma.gamma(mu + 0.5) / Gamma.gamma(mu);
161        return omega * (1 - 1 / mu * v * v);
162    }
163
164    /** {@inheritDoc} */
165    public double getSupportLowerBound() {
166        return 0;
167    }
168
169    /** {@inheritDoc} */
170    public double getSupportUpperBound() {
171        return Double.POSITIVE_INFINITY;
172    }
173
174    /** {@inheritDoc} */
175    public boolean isSupportLowerBoundInclusive() {
176        return true;
177    }
178
179    /** {@inheritDoc} */
180    public boolean isSupportUpperBoundInclusive() {
181        return false;
182    }
183
184    /** {@inheritDoc} */
185    public boolean isSupportConnected() {
186        return true;
187    }
188
189}