Variance.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.Double.NaN;
023 import static java.lang.String.format;
024 import static org.jenetics.util.object.eq;
025 import static org.jenetics.util.object.hashCodeOf;
026 
027 
028 /**
029  <p>Calculate the variance from a finite sample of <i>N</i> observations.</p>
030  <p><img src="doc-files/variance.gif"
031  *         alt="s^2_{N-1}=\frac{1}{N-1}\sum_{i=1}^{N}\left ( x_i - \bar{x} \right )^2"
032  *    />
033  </p>
034  *
035  <p/>
036  <strong>Note that this implementation is not synchronized.</strong> If
037  * multiple threads access this object concurrently, and at least one of the
038  * threads modifies it, it must be synchronized externally.
039  *
040  @see <a href="http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance" >
041  *       Wikipedia: Algorithms for calculating variance</a>
042  @see <a href="http://mathworld.wolfram.com/Variance.html">
043  *       Wolfram MathWorld: Variance</a>
044  *
045  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
046  @since 1.0
047  @version 1.0 &mdash; <em>$Date: 2013-09-01 $</em>
048  */
049 public class Variance<N extends Number> extends Mean<N> {
050 
051     private double _m2 = NaN;
052 
053     public Variance() {
054     }
055 
056     /**
057      * Return the variance of the accumulated values.
058      <p><img src="doc-files/variance.gif" alt="Variance" /></p>
059      *
060      @return the variance of the accumulated values, or {@link java.lang.Double#NaN}
061      *          if {@code getSamples() == 0}.
062      */
063     public double getVariance() {
064         double variance = NaN;
065 
066         if (_samples == 1) {
067             variance = _m2;
068         else if (_samples > 1) {
069             variance = _m2/(_samples - 1);
070         }
071 
072         return variance;
073     }
074 
075     /**
076      @throws NullPointerException if the given {@code value} is {@code null}.
077      */
078     @Override
079     public void accumulate(final N value) {
080         if (_samples == 0) {
081             _mean = 0;
082             _m2 = 0;
083         }
084 
085         final double data = value.doubleValue();
086         final double delta = data - _mean;
087 
088         _mean += delta/(++_samples);
089         _m2 += delta*(data - _mean);
090     }
091 
092     @Override
093     public int hashCode() {
094         return hashCodeOf(getClass()).and(super.hashCode()).and(_m2).value();
095     }
096 
097     @Override
098     public boolean equals(final Object obj) {
099         if (obj == this) {
100             return true;
101         }
102         if (obj == null || getClass() != obj.getClass()) {
103             return false;
104         }
105 
106         final Variance<?> variance = (Variance<?>)obj;
107         return eq(_m2, variance._m2&& super.equals(variance);
108     }
109 
110     @Override
111     public String toString() {
112         return format(
113             "%s[samples=%d, mean=%f, stderr=%f, var=%f]",
114             getClass().getSimpleName(),
115             getSamples(),
116             getMean(),
117             getStandardError(),
118             getVariance()
119         );
120     }
121 
122     @Override
123     public Variance<N> clone() {
124         return (Variance<N>)super.clone();
125     }
126 
127 }