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 agents.anac.y2019.harddealer.math3.distribution;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.exception.NotStrictlyPositiveException;
|
---|
20 | import agents.anac.y2019.harddealer.math3.exception.NumberIsTooSmallException;
|
---|
21 | import agents.anac.y2019.harddealer.math3.exception.util.LocalizedFormats;
|
---|
22 | import agents.anac.y2019.harddealer.math3.random.RandomGenerator;
|
---|
23 | import agents.anac.y2019.harddealer.math3.random.Well19937c;
|
---|
24 | import agents.anac.y2019.harddealer.math3.special.Gamma;
|
---|
25 | import agents.anac.y2019.harddealer.math3.util.FastMath;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This class implements the Nakagami distribution.
|
---|
29 | *
|
---|
30 | * @see <a href="http://en.wikipedia.org/wiki/Nakagami_distribution">Nakagami Distribution (Wikipedia)</a>
|
---|
31 | *
|
---|
32 | * @since 3.4
|
---|
33 | */
|
---|
34 | public class NakagamiDistribution extends AbstractRealDistribution {
|
---|
35 |
|
---|
36 | /** Default inverse cumulative probability accuracy. */
|
---|
37 | public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
|
---|
38 |
|
---|
39 | /** Serializable version identifier. */
|
---|
40 | private static final long serialVersionUID = 20141003;
|
---|
41 |
|
---|
42 | /** The shape parameter. */
|
---|
43 | private final double mu;
|
---|
44 | /** The scale parameter. */
|
---|
45 | private final double omega;
|
---|
46 | /** Inverse cumulative probability accuracy. */
|
---|
47 | private final double inverseAbsoluteAccuracy;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Build a new instance.
|
---|
51 | * <p>
|
---|
52 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
53 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
54 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
55 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
56 | * as random generator via the appropriate constructors to avoid the
|
---|
57 | * additional initialisation overhead.
|
---|
58 | *
|
---|
59 | * @param mu shape parameter
|
---|
60 | * @param omega scale parameter (must be positive)
|
---|
61 | * @throws NumberIsTooSmallException if {@code mu < 0.5}
|
---|
62 | * @throws NotStrictlyPositiveException if {@code omega <= 0}
|
---|
63 | */
|
---|
64 | public NakagamiDistribution(double mu, double omega) {
|
---|
65 | this(mu, omega, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Build a new instance.
|
---|
70 | * <p>
|
---|
71 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
72 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
73 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
74 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
75 | * as random generator via the appropriate constructors to avoid the
|
---|
76 | * additional initialisation overhead.
|
---|
77 | *
|
---|
78 | * @param mu shape parameter
|
---|
79 | * @param omega scale parameter (must be positive)
|
---|
80 | * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
|
---|
81 | * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
|
---|
82 | * @throws NumberIsTooSmallException if {@code mu < 0.5}
|
---|
83 | * @throws NotStrictlyPositiveException if {@code omega <= 0}
|
---|
84 | */
|
---|
85 | public NakagamiDistribution(double mu, double omega, double inverseAbsoluteAccuracy) {
|
---|
86 | this(new Well19937c(), mu, omega, inverseAbsoluteAccuracy);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Build a new instance.
|
---|
91 | *
|
---|
92 | * @param rng Random number generator
|
---|
93 | * @param mu shape parameter
|
---|
94 | * @param omega scale parameter (must be positive)
|
---|
95 | * @param inverseAbsoluteAccuracy the maximum absolute error in inverse
|
---|
96 | * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
|
---|
97 | * @throws NumberIsTooSmallException if {@code mu < 0.5}
|
---|
98 | * @throws NotStrictlyPositiveException if {@code omega <= 0}
|
---|
99 | */
|
---|
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 | }
|
---|