diff options
author | Björn Milcke <bm@openoffice.org> | 2003-12-12 11:37:02 +0000 |
---|---|---|
committer | Björn Milcke <bm@openoffice.org> | 2003-12-12 11:37:02 +0000 |
commit | 9a9c2d21ceeea887b0081a2066321c11db427fe0 (patch) | |
tree | 0f3f7e193929acb68821d3317ca72c2f44d42ee7 /chart2/source | |
parent | 78ddbd462f71bf53f03c526ab44b887aa65bc9e9 (diff) |
some statistics functions
Diffstat (limited to 'chart2/source')
-rw-r--r-- | chart2/source/inc/StatisticsHelper.hxx | 98 | ||||
-rw-r--r-- | chart2/source/tools/StatisticsHelper.cxx | 154 |
2 files changed, 252 insertions, 0 deletions
diff --git a/chart2/source/inc/StatisticsHelper.hxx b/chart2/source/inc/StatisticsHelper.hxx new file mode 100644 index 000000000000..c377df772e61 --- /dev/null +++ b/chart2/source/inc/StatisticsHelper.hxx @@ -0,0 +1,98 @@ +/************************************************************************* + * + * $RCSfile: StatisticsHelper.hxx,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: bm $ $Date: 2003-12-12 12:36:26 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * 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 + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2003 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ +#ifndef CHART2_STATISTICSHELPER_HXX +#define CHART2_STATISTICSHELPER_HXX + +#ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_ +#include <com/sun/star/uno/Sequence.hxx> +#endif + +namespace chart +{ + +class StatisticsHelper +{ +public: + /** Calculates 1/n * sum (x_i - x_mean)^2. + + @see http://mathworld.wolfram.com/Variance.html + + @param bUnbiasedEstimator + If true, 1/(n-1) * sum (x_i - x_mean)^2 is returned. + */ + static double getVariance( const ::com::sun::star::uno::Sequence< double > & rData, + bool bUnbiasedEstimator = false ); + + // square root of the variance + static double getStandardDeviation( const ::com::sun::star::uno::Sequence< double > & rData ); + + // also called "Standard deviation of the mean (SDOM)" + static double getStandardError( const ::com::sun::star::uno::Sequence< double > & rData ); + +private: + // not implemented + StatisticsHelper(); +}; + +} // namespace chart + +// CHART2_STATISTICSHELPER_HXX +#endif diff --git a/chart2/source/tools/StatisticsHelper.cxx b/chart2/source/tools/StatisticsHelper.cxx new file mode 100644 index 000000000000..e0d1a2ec4a52 --- /dev/null +++ b/chart2/source/tools/StatisticsHelper.cxx @@ -0,0 +1,154 @@ +/************************************************************************* + * + * $RCSfile: StatisticsHelper.cxx,v $ + * + * $Revision: 1.1 $ + * + * last change: $Author: bm $ $Date: 2003-12-12 12:37:02 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * 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 + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2003 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ +#include "StatisticsHelper.hxx" + +#ifndef INCLUDED_RTL_MATH_HXX +#include <rtl/math.hxx> +#endif + +using ::com::sun::star::uno::Sequence; + +namespace +{ + +double lcl_getVariance( const Sequence< double > & rData, sal_Int32 & rOutValidCount, + bool bUnbiasedEstimator ) +{ + const sal_Int32 nCount = rData.getLength(); + rOutValidCount = nCount; + + double fSum = 0.0; + double fQuadSum = 0.0; + + for( sal_Int32 i = 0; i < nCount; ++i ) + { + const double fData = rData[i]; + if( ::rtl::math::isNan( fData )) + --rOutValidCount; + else + { + fSum += fData; + fQuadSum += fData * fData; + } + } + + double fResult; + if( rOutValidCount == 0 ) + ::rtl::math::setNan( & fResult ); + else + { + const double fN = static_cast< double >( rOutValidCount ); + if( bUnbiasedEstimator ) + fResult = (fQuadSum - fSum*fSum/fN) / (fN - 1); + else + fResult = (fQuadSum - fSum*fSum/fN) / fN; + } + + return fResult; +} + +} // anonymous namespace + +namespace chart +{ + +// static +double StatisticsHelper::getVariance( + const Sequence< double > & rData, + bool bUnbiasedEstimator /* = false */ ) +{ + sal_Int32 nValCount; + return lcl_getVariance( rData, nValCount, bUnbiasedEstimator ); +} + +// static +double StatisticsHelper::getStandardDeviation( const Sequence< double > & rData ) +{ + double fResult = getVariance( rData ); + if( ! ::rtl::math::isNan( fResult )) + fResult = sqrt( fResult ); + + return fResult; +} + +// static +double StatisticsHelper::getStandardError( const Sequence< double > & rData ) +{ + sal_Int32 nValCount; + double fVar = lcl_getVariance( rData, nValCount, false ); + double fResult; + + if( nValCount == 0 || + ::rtl::math::isNan( fVar )) + { + ::rtl::math::setNan( & fResult ); + } + else + { + // standard-deviation / sqrt(n) + fResult = sqrt( fVar ) / sqrt( nValCount ); + } + + return fResult; +} + + +} // namespace chart |