001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.util;
031
032/**
033 * This class is used for providing a running mean and variance of streams of
034 * data. Use the {@link #push(double)} method to add data from the stream.
035 * 
036 * @author John Cook (http://www.johndcook.com/)
037 * @author Converted to Java by David Dupplaw (dpd@ecs.soton.ac.uk)
038 * @created 25 Jan 2013
039 * @version $Author$, $Revision$, $Date$
040 * @see "http://www.johndcook.com/standard_deviation.html"
041 */
042public class RunningStat
043{
044        private int n = 0;
045
046        private double m_oldM, m_newM, m_oldS, m_newS;
047
048        /**
049         * Default constructor
050         */
051        public RunningStat()
052        {
053                this.n = 0;
054        }
055        
056        /**
057         *      Constructor that takes the first value
058         *      @param firstValue the first value in the stream
059         */
060        public RunningStat( final double firstValue )
061        {
062                this.n = 0;
063                this.push( firstValue );
064        }
065
066        /**
067         * Reset the running stats
068         */
069        public void clear()
070        {
071                this.n = 0;
072        }
073
074        /**
075         *      Push a data value from the stream into the calculation.
076         *      @param x The data value to push
077         */
078        public void push( final double x )
079        {
080                this.n++;
081
082                // See Knuth TAOCP vol 2, 3rd edition, page 232
083                if( this.n == 1 )
084                {
085                        this.m_oldM = this.m_newM = x;
086                        this.m_oldS = 0.0;
087                }
088                else
089                {
090                        this.m_newM = this.m_oldM + (x - this.m_oldM) / this.n;
091                        this.m_newS = this.m_oldS + (x - this.m_oldM) * (x - this.m_newM);
092
093                        // set up for next iteration
094                        this.m_oldM = this.m_newM;
095                        this.m_oldS = this.m_newS;
096                }
097        }
098
099        /**
100         * Returns the number of data values that have been processed.
101         * 
102         * @return the number of data values that have been processed.
103         */
104        public int numDataValues()
105        {
106                return this.n;
107        }
108
109        /**
110         * Returns the running mean.
111         * 
112         * @return The running mean
113         */
114        public double mean()
115        {
116                return (this.n > 0) ? this.m_newM : 0.0;
117        }
118
119        /**
120         * Returns the running variance
121         * 
122         * @return the running variance
123         */
124        public double variance()
125        {
126                return ((this.n > 1) ? this.m_newS / (this.n - 1) : 0.0);
127        }
128
129        /**
130         * Returns the running standard deviation
131         * 
132         * @return The running standard deviation
133         */
134        public double standardDeviation()
135        {
136                return Math.sqrt( this.variance() );
137        }
138};