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.OutOfRangeException;
|
---|
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.util.FastMath;
|
---|
25 | import agents.anac.y2019.harddealer.math3.util.MathUtils;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This class implements the Logistic distribution.
|
---|
29 | *
|
---|
30 | * @see <a href="http://en.wikipedia.org/wiki/Logistic_distribution">Logistic Distribution (Wikipedia)</a>
|
---|
31 | * @see <a href="http://mathworld.wolfram.com/LogisticDistribution.html">Logistic Distribution (Mathworld)</a>
|
---|
32 | *
|
---|
33 | * @since 3.4
|
---|
34 | */
|
---|
35 | public class LogisticDistribution extends AbstractRealDistribution {
|
---|
36 |
|
---|
37 | /** Serializable version identifier. */
|
---|
38 | private static final long serialVersionUID = 20141003;
|
---|
39 |
|
---|
40 | /** The location parameter. */
|
---|
41 | private final double mu;
|
---|
42 | /** The scale parameter. */
|
---|
43 | private final double s;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Build a new instance.
|
---|
47 | * <p>
|
---|
48 | * <b>Note:</b> this constructor will implicitly create an instance of
|
---|
49 | * {@link Well19937c} as random generator to be used for sampling only (see
|
---|
50 | * {@link #sample()} and {@link #sample(int)}). In case no sampling is
|
---|
51 | * needed for the created distribution, it is advised to pass {@code null}
|
---|
52 | * as random generator via the appropriate constructors to avoid the
|
---|
53 | * additional initialisation overhead.
|
---|
54 | *
|
---|
55 | * @param mu location parameter
|
---|
56 | * @param s scale parameter (must be positive)
|
---|
57 | * @throws NotStrictlyPositiveException if {@code beta <= 0}
|
---|
58 | */
|
---|
59 | public LogisticDistribution(double mu, double s) {
|
---|
60 | this(new Well19937c(), mu, s);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Build a new instance.
|
---|
65 | *
|
---|
66 | * @param rng Random number generator
|
---|
67 | * @param mu location parameter
|
---|
68 | * @param s scale parameter (must be positive)
|
---|
69 | * @throws NotStrictlyPositiveException if {@code beta <= 0}
|
---|
70 | */
|
---|
71 | public LogisticDistribution(RandomGenerator rng, double mu, double s) {
|
---|
72 | super(rng);
|
---|
73 |
|
---|
74 | if (s <= 0.0) {
|
---|
75 | throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, s);
|
---|
76 | }
|
---|
77 |
|
---|
78 | this.mu = mu;
|
---|
79 | this.s = s;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Access the location parameter, {@code mu}.
|
---|
84 | *
|
---|
85 | * @return the location parameter.
|
---|
86 | */
|
---|
87 | public double getLocation() {
|
---|
88 | return mu;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Access the scale parameter, {@code s}.
|
---|
93 | *
|
---|
94 | * @return the scale parameter.
|
---|
95 | */
|
---|
96 | public double getScale() {
|
---|
97 | return s;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** {@inheritDoc} */
|
---|
101 | public double density(double x) {
|
---|
102 | double z = (x - mu) / s;
|
---|
103 | double v = FastMath.exp(-z);
|
---|
104 | return 1 / s * v / ((1.0 + v) * (1.0 + v));
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** {@inheritDoc} */
|
---|
108 | public double cumulativeProbability(double x) {
|
---|
109 | double z = 1 / s * (x - mu);
|
---|
110 | return 1.0 / (1.0 + FastMath.exp(-z));
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** {@inheritDoc} */
|
---|
114 | @Override
|
---|
115 | public double inverseCumulativeProbability(double p) throws OutOfRangeException {
|
---|
116 | if (p < 0.0 || p > 1.0) {
|
---|
117 | throw new OutOfRangeException(p, 0.0, 1.0);
|
---|
118 | } else if (p == 0) {
|
---|
119 | return 0.0;
|
---|
120 | } else if (p == 1) {
|
---|
121 | return Double.POSITIVE_INFINITY;
|
---|
122 | }
|
---|
123 | return s * Math.log(p / (1.0 - p)) + mu;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** {@inheritDoc} */
|
---|
127 | public double getNumericalMean() {
|
---|
128 | return mu;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** {@inheritDoc} */
|
---|
132 | public double getNumericalVariance() {
|
---|
133 | return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s));
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** {@inheritDoc} */
|
---|
137 | public double getSupportLowerBound() {
|
---|
138 | return Double.NEGATIVE_INFINITY;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** {@inheritDoc} */
|
---|
142 | public double getSupportUpperBound() {
|
---|
143 | return Double.POSITIVE_INFINITY;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** {@inheritDoc} */
|
---|
147 | public boolean isSupportLowerBoundInclusive() {
|
---|
148 | return false;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** {@inheritDoc} */
|
---|
152 | public boolean isSupportUpperBoundInclusive() {
|
---|
153 | return false;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** {@inheritDoc} */
|
---|
157 | public boolean isSupportConnected() {
|
---|
158 | return true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|