Sleipnir
src/statisticsi.h
00001 /*****************************************************************************
00002 * This file is provided under the Creative Commons Attribution 3.0 license.
00003 *
00004 * You are free to share, copy, distribute, transmit, or adapt this work
00005 * PROVIDED THAT you attribute the work to the authors listed below.
00006 * For more information, please see the following web page:
00007 * http://creativecommons.org/licenses/by/3.0/
00008 *
00009 * This file is a component of the Sleipnir library for functional genomics,
00010 * authored by:
00011 * Curtis Huttenhower (chuttenh@princeton.edu)
00012 * Mark Schroeder
00013 * Maria D. Chikina
00014 * Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
00015 *
00016 * If you use this library, the included executable tools, or any related
00017 * code in your work, please cite the following publication:
00018 * Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
00019 * Olga G. Troyanskaya.
00020 * "The Sleipnir library for computational functional genomics"
00021 *****************************************************************************/
00022 #ifndef STATISTICSI_H
00023 #define STATISTICSI_H
00024 
00025 #include "fullmatrix.h"
00026 #include "mathb.h"
00027 #include "meta.h"
00028 
00029 namespace Sleipnir {
00030 
00031 class CStatisticsImpl : protected CMath {
00032 protected:
00033     static double Chi2CDF( double, double, double, double );
00034     static double IncompleteGamma( double, double );
00035     static double Normal01CDF( double );
00036     static double GammaLog( double );
00037     static double IncompleteBeta( double, double, double );
00038     static double IncompleteBetaCF( double, double, double );
00039     static double ModifiedBesselI( size_t, double );
00040     static bool MatrixLUSubstitute( CDataMatrix&, const std::vector<size_t>&, std::vector<float>& );
00041 
00042     static double EpsilonDouble( ) {
00043         double  dRet;
00044 
00045         for( dRet = 1; 1 < ( 1 + dRet ); dRet /= 2 );
00046 
00047         return ( 2 * dRet ); }
00048 
00049     static bool MatrixMultiply( const std::vector<float>& vecdLeft, const CDataMatrix& MatRight,
00050         std::vector<float>& vecdOut ) {
00051         size_t  i, j;
00052 
00053         if( vecdLeft.size( ) != MatRight.GetRows( ) )
00054             return false;
00055 
00056         vecdOut.resize( MatRight.GetColumns( ) );
00057         for( i = 0; i < vecdOut.size( ); ++i ) {
00058             vecdOut[ i ] = 0;
00059             for( j = 0; j < vecdLeft.size( ); ++j )
00060                 vecdOut[ i ] += vecdLeft[ j ] * MatRight.Get( j, i ); }
00061 
00062         return true; }
00063 
00064     static double MatrixMultiply( const std::vector<float>& vecdLeft, const std::vector<float>& vecdRight ) {
00065         size_t  i;
00066         double  dRet;
00067 
00068         if( vecdLeft.size( ) != vecdRight.size( ) )
00069             return CMeta::GetNaN( );
00070 
00071         for( dRet = 0,i = 0; i < vecdLeft.size( ); ++i )
00072             dRet += vecdLeft[ i ] * vecdRight[ i ];
00073 
00074         return dRet; }
00075 
00076     template<class tType>
00077     static bool SumsSkip( tType Value ) {
00078 
00079         return false; }
00080 
00081     static bool SumsSkip( float dValue ) {
00082 
00083         return CMeta::IsNaN( dValue ); }
00084 
00085     static bool SumsSkip( double dValue ) {
00086 
00087         return CMeta::IsNaN( dValue ); }
00088 
00089     template<class tType>
00090     static void Sums( tType Begin, tType End, double* pdSum, double* pdSumSq, size_t* piN ) {
00091         tType   Cur;
00092 
00093         if( pdSum )
00094             *pdSum = 0;
00095         if( pdSumSq )
00096             *pdSumSq = 0;
00097         if( piN )
00098             *piN = 0;
00099         for( Cur = Begin; Cur != End; ++Cur ) {
00100             if( SumsSkip( *Cur ) )
00101                 continue;
00102             if( piN )
00103                 *piN += 1;
00104             if( pdSum )
00105                 *pdSum += *Cur;
00106             if( pdSumSq )
00107                 *pdSumSq += *Cur * *Cur; } }
00108 };
00109 
00110 }
00111 
00112 #endif // STATISTICSI_H