001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
003 * Copyright (c) 2007-2014 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics.stat;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 import static org.jenetics.internal.util.object.eq;
025
026 import java.io.Serializable;
027 import java.util.Locale;
028
029 import org.jscience.mathematics.number.Float64;
030
031 import org.jenetics.internal.util.HashBuilder;
032
033 import org.jenetics.util.Function;
034 import org.jenetics.util.Range;
035
036
037 /**
038 * <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29">
039 * Uniform distribution</a> class.
040 *
041 * @see LinearDistribution
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
044 * @since 1.0
045 * @version 1.0 — <em>$Date: 2014-03-01 $</em>
046 */
047 public class UniformDistribution<
048 N extends Number & Comparable<? super N>
049 >
050 implements Distribution<N>
051 {
052
053 /**
054 * <p>
055 * <img
056 * src="doc-files/uniform-pdf.gif"
057 * alt="f(x)=\left\{\begin{matrix}
058 * \frac{1}{max-min} & for & x \in [min, max] \\
059 * 0 & & otherwise \\
060 * \end{matrix}\right."
061 * />
062 * </p>
063 *
064 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
065 * @since 1.0
066 * @version 1.0 — <em>$Date: 2014-03-01 $</em>
067 */
068 static final class PDF<N extends Number & Comparable<? super N>>
069 implements
070 Function<N, Float64>,
071 Serializable
072 {
073 private static final long serialVersionUID = 1L;
074
075 private final double _min;
076 private final double _max;
077 private final Float64 _probability;
078
079 public PDF(final Range<N> domain) {
080 _min = domain.getMin().doubleValue();
081 _max = domain.getMax().doubleValue();
082 _probability = Float64.valueOf(1.0/(_max - _min));
083 }
084
085 @Override
086 public Float64 apply(final N value) {
087 final double x = value.doubleValue();
088
089 Float64 result = Float64.ZERO;
090 if (x >= _min && x <= _max) {
091 result = _probability;
092 }
093
094 return result;
095 }
096
097 @Override
098 public String toString() {
099 return format(Locale.ENGLISH, "p(x) = %s", _probability);
100 }
101
102 }
103
104 /**
105 * <p>
106 * <img
107 * src="doc-files/uniform-cdf.gif"
108 * alt="f(x)=\left\{\begin{matrix}
109 * 0 & for & x < min \\
110 * \frac{x-min}{max-min} & for & x \in [min, max] \\
111 * 1 & for & x > max \\
112 * \end{matrix}\right."
113 * />
114 * </p>
115 *
116 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
117 * @since 1.0
118 * @version 1.0 — <em>$Date: 2014-03-01 $</em>
119 */
120 static final class CDF<N extends Number & Comparable<? super N>>
121 implements
122 Function<N, Float64>,
123 Serializable
124 {
125 private static final long serialVersionUID = 1L;
126
127
128 private final double _min;
129 private final double _max;
130 private final double _divisor;
131
132 public CDF(final Range<N> domain) {
133 _min = domain.getMin().doubleValue();
134 _max = domain.getMax().doubleValue();
135 _divisor = _max - _min;
136 assert (_divisor > 0);
137 }
138
139 @Override
140 public Float64 apply(final N value) {
141 final double x = value.doubleValue();
142
143 Float64 result = Float64.ZERO;
144 if (x < _min) {
145 result = Float64.ZERO;
146 } else if (x > _max) {
147 result = Float64.ONE;
148 } else {
149 result = Float64.valueOf((x - _min)/_divisor);
150 }
151
152 return result;
153 }
154
155 @Override
156 public String toString() {
157 return format(
158 Locale.ENGLISH,
159 "P(x) = (x - %1$s)/(%2$s - %1$s)", _min, _max
160 );
161 }
162
163 }
164
165
166 private final Range<N> _domain;
167 private final Function<N, Float64> _cdf;
168 private final Function<N, Float64> _pdf;
169
170 /**
171 * Create a new uniform distribution with the given {@code domain}.
172 *
173 * @param domain the domain of the distribution.
174 * @throws NullPointerException if the {@code domain} is {@code null}.
175 */
176 public UniformDistribution(final Range<N> domain) {
177 _domain = requireNonNull(domain, "Domain");
178 _cdf = new CDF<>(_domain);
179 _pdf = new PDF<>(_domain);
180 }
181
182 /**
183 * Create a new uniform distribution with the given min and max values.
184 *
185 * @param min the minimum value of the domain.
186 * @param max the maximum value of the domain.
187 * @throws IllegalArgumentException if {@code min >= max}
188 * @throws NullPointerException if one of the arguments is {@code null}.
189 */
190 public UniformDistribution(final N min, final N max) {
191 this(new Range<>(min, max));
192 }
193
194 @Override
195 public Range<N> getDomain() {
196 return _domain;
197 }
198
199 /**
200 * Return a new PDF object.
201 *
202 * <p>
203 * <img
204 * src="doc-files/uniform-pdf.gif"
205 * alt="f(x)=\left\{\begin{matrix}
206 * \frac{1}{max-min} & for & x \in [min, max] \\
207 * 0 & & otherwise \\
208 * \end{matrix}\right."
209 * />
210 * </p>
211 *
212 */
213 @Override
214 public Function<N, Float64> getPDF() {
215 return _pdf;
216 }
217
218 /**
219 * Return a new CDF object.
220 *
221 * <p>
222 * <img
223 * src="doc-files/uniform-cdf.gif"
224 * alt="f(x)=\left\{\begin{matrix}
225 * 0 & for & x < min \\
226 * \frac{x-min}{max-min} & for & x \in [min, max] \\
227 * 1 & for & x > max \\
228 * \end{matrix}\right."
229 * />
230 * </p>
231 *
232 */
233 @Override
234 public Function<N, Float64> getCDF() {
235 return _cdf;
236 }
237
238 @Override
239 public int hashCode() {
240 return HashBuilder.of(getClass()).and(_domain).value();
241 }
242
243 @Override
244 public boolean equals(final Object obj) {
245 if (obj == this) {
246 return true;
247 }
248 if (obj == null || getClass() != obj.getClass()) {
249 return false;
250 }
251
252 final UniformDistribution<?> dist = (UniformDistribution<?>)obj;
253 return eq(_domain, dist._domain);
254 }
255
256 @Override
257 public String toString() {
258 return format("UniformDistribution[%s]", _domain);
259 }
260
261 }
|