com.hfg.math.SimpleSampleStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.math;
import java.util.Collection;
import java.util.Map;
//------------------------------------------------------------------------------
/**
Lightweight sample statistics. The individual values are not retained.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class SimpleSampleStats
{
//**************************************************************************
// PRIVATE FIELDS
//**************************************************************************
private long mSampleSize;
private double mSumX;
private double mSumX2;
private Double mMax;
private Double mMin;
//**************************************************************************
// PUBLIC FUNCTIONS
//**************************************************************************
//--------------------------------------------------------------------------
public void add(SimpleSampleStats inStats)
{
mSampleSize += inStats.mSampleSize;
mSumX += inStats.mSumX;
mSumX2 += inStats.mSumX2;
if (inStats.mMax > mMax) mMax = inStats.mMax;
if (inStats.mMin < mMin) mMin = inStats.mMin;
}
//--------------------------------------------------------------------------
public void addAll(Collection extends Number> inValues)
{
for (Number value : inValues)
{
add(value.doubleValue());
}
}
//--------------------------------------------------------------------------
public void addAll(int[] inValues)
{
for (double value : inValues)
{
add(value);
}
}
//--------------------------------------------------------------------------
public void addAll(double[] inValues)
{
for (double value : inValues)
{
add(value);
}
}
//--------------------------------------------------------------------------
public void addAll(Map inValueCountMap)
{
for (Number value : inValueCountMap.keySet())
{
Integer count = inValueCountMap.get(value);
if (count != null
&& count != 0)
{
double doubleValue = value.doubleValue();
if (null == mMin
|| doubleValue < mMin)
{
mMin = doubleValue;
}
if (null == mMax
|| doubleValue > mMax)
{
mMax = doubleValue;
}
mSampleSize += count;
mSumX += doubleValue * count;
mSumX2 += (doubleValue * doubleValue) * count;
}
}
}
//--------------------------------------------------------------------------
public void add(Number inValue)
{
add(inValue.doubleValue());
}
//--------------------------------------------------------------------------
public void add(int inValue)
{
add((double) inValue);
}
//--------------------------------------------------------------------------
public void add(double inValue)
{
if (null == mMin
|| inValue < mMin)
{
mMin = inValue;
}
if (null == mMax
|| inValue > mMax)
{
mMax = inValue;
}
mSampleSize++;
mSumX += inValue;
mSumX2 += (inValue * inValue);
}
//--------------------------------------------------------------------------
public void clear()
{
mSampleSize = 0;
mSumX = 0;
mSumX2 = 0;
mMax = null;
mMin = null;
}
//--------------------------------------------------------------------------
public double getMin()
{
return mMin;
}
//--------------------------------------------------------------------------
public double getMax()
{
return mMax;
}
//--------------------------------------------------------------------------
public double getMean()
{
return mSumX / mSampleSize;
}
//--------------------------------------------------------------------------
public long getSampleSize()
{
return mSampleSize;
}
//--------------------------------------------------------------------------
public double getSampleStandardDeviation()
{
// The preferred equation is StdDev = Sqrt(Sum(X[i] - Xmean)^2 / (N-1) )
// but since we don't keep all the values in this version and can't calculate the
// mean until the end, we need to use this somewhat less precise version:
// StdDev = Sqrt( (N SumX2 - SumX^2) / (N (N-1)) )
return Math.sqrt((mSampleSize * mSumX2 - (mSumX * mSumX))/ (mSampleSize * (mSampleSize - 1)));
}
//--------------------------------------------------------------------------
public StandardNormalDistribution getStandardNormalDistribution()
{
return new StandardNormalDistribution().setMean(getMean()).setSampleStandardDeviation(getSampleStandardDeviation());
}
}