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