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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.continuous;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.special.Gamma;
27  import org.hipparchus.util.FastMath;
28  
29  /**
30   * This class implements the Nakagami distribution.
31   *
32   * @see <a href="http://en.wikipedia.org/wiki/Nakagami_distribution">Nakagami Distribution (Wikipedia)</a>
33   */
34  public class NakagamiDistribution extends AbstractRealDistribution {
35  
36      /** Serializable version identifier. */
37      private static final long serialVersionUID = 20141003;
38  
39      /** The shape parameter. */
40      private final double mu;
41      /** The scale parameter. */
42      private final double omega;
43  
44      /**
45       * Build a new instance.
46       *
47       * @param mu shape parameter
48       * @param omega scale parameter (must be positive)
49       * @throws MathIllegalArgumentException if {@code mu < 0.5}
50       * @throws MathIllegalArgumentException if {@code omega <= 0}
51       */
52      public NakagamiDistribution(double mu, double omega)
53          throws MathIllegalArgumentException {
54          this(mu, omega, DEFAULT_SOLVER_ABSOLUTE_ACCURACY);
55      }
56  
57      /**
58       * Build a new instance.
59       *
60       * @param mu shape parameter
61       * @param omega scale parameter (must be positive)
62       * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
63       * cumulative probability estimates (defaults to {@link #DEFAULT_SOLVER_ABSOLUTE_ACCURACY}).
64       * @throws MathIllegalArgumentException if {@code mu < 0.5}
65       * @throws MathIllegalArgumentException if {@code omega <= 0}
66       */
67      public NakagamiDistribution(double mu,
68                                  double omega,
69                                  double inverseAbsoluteAccuracy)
70          throws MathIllegalArgumentException {
71          super(inverseAbsoluteAccuracy);
72  
73          if (mu < 0.5) {
74              throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
75                                                     mu, 0.5);
76          }
77          if (omega <= 0) {
78              throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_POSITIVE_SCALE, omega);
79          }
80  
81          this.mu = mu;
82          this.omega = omega;
83      }
84  
85      /**
86       * Access the shape parameter, {@code mu}.
87       *
88       * @return the shape parameter.
89       */
90      public double getShape() {
91          return mu;
92      }
93  
94      /**
95       * Access the scale parameter, {@code omega}.
96       *
97       * @return the scale parameter.
98       */
99      public double getScale() {
100         return omega;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public double density(double x) {
106         if (x <= 0) {
107             return 0.0;
108         }
109         return 2.0 * FastMath.pow(mu, mu) / (Gamma.gamma(mu) * FastMath.pow(omega, mu)) *
110                      FastMath.pow(x, 2 * mu - 1) * FastMath.exp(-mu * x * x / omega);
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public double cumulativeProbability(double x) {
116         return Gamma.regularizedGammaP(mu, mu * x * x / omega);
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public double getNumericalMean() {
122         return Gamma.gamma(mu + 0.5) / Gamma.gamma(mu) * FastMath.sqrt(omega / mu);
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public double getNumericalVariance() {
128         double v = Gamma.gamma(mu + 0.5) / Gamma.gamma(mu);
129         return omega * (1 - 1 / mu * v * v);
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public double getSupportLowerBound() {
135         return 0;
136     }
137 
138     /** {@inheritDoc} */
139     @Override
140     public double getSupportUpperBound() {
141         return Double.POSITIVE_INFINITY;
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public boolean isSupportConnected() {
147         return true;
148     }
149 
150 }