Chi2Distribution.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Chi2Distribution.cpp 1389 2008-09-04 17:06:43Z ckiesch $"
00002 
00009 //============================================================================
00010 //
00011 //  This file is part of GPSTk, the GPS Toolkit.
00012 //
00013 //  The GPSTk is free software; you can redistribute it and/or modify
00014 //  it under the terms of the GNU Lesser General Public License as published
00015 //  by the Free Software Foundation; either version 2.1 of the License, or
00016 //  any later version.
00017 //
00018 //  The GPSTk is distributed in the hope that it will be useful,
00019 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 //  GNU Lesser General Public License for more details.
00022 //
00023 //  You should have received a copy of the GNU Lesser General Public
00024 //  License along with GPSTk; if not, write to the Free Software Foundation,
00025 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 //
00027 //  Dagoberto Salazar - gAGE ( http://www.gage.es ). 2008
00028 //
00029 //============================================================================
00030 
00031 
00032 #include "Chi2Distribution.hpp"
00033 
00034 
00035 using namespace std;
00036 
00037 namespace gpstk
00038 {
00039 
00040 
00041       /* Computes the probability density function
00042        *
00043        * @param x    Value
00044        */
00045    double Chi2Distribution::pdf(double x)
00046    {
00047 
00048       if( x <= 0.0 )
00049       {
00050          return (0.0);
00051       }
00052 
00053          // Compute ndf/2
00054       double khalf( static_cast<double>(ndf) / 2.0 );
00055 
00056          // Compute the natural logarithm of terms
00057       double t1( -0.69314718055994529 * khalf );
00058       double t2( -lngamma( khalf ) );
00059       double t3( ( khalf - 1.0 ) * log(x) );
00060       double t4( - x / 2.0 );
00061 
00062       return ( exp( t1 + t2 + t3 + t4 ) );
00063 
00064    }  // End of method 'Chi2Distribution::pdf()'
00065 
00066 
00067 
00068       /* Computes the cumulative distribution function
00069        *
00070        * @param x    Value
00071        */
00072    double Chi2Distribution::cdf(double x)
00073    {
00074 
00075 
00076       if( x <= 0.0 )
00077       {
00078          return (0.0);
00079       }
00080 
00081          // Compute ndf/2
00082       double khalf( static_cast<double>(ndf) / 2.0 );
00083 
00084       return ( gammaP( khalf, (x/2.0) ) );
00085 
00086    }  // End of method 'Chi2Distribution::cdf()'
00087 
00088 
00089 
00090       /* Set the number of degrees of freedom.
00091        *
00092        * @param n       Degrees of freedom
00093        *
00094        * \warning "n" must be > 0.0, otherwise n = |n|.
00095        */
00096    Chi2Distribution& Chi2Distribution::setNDF(int n)
00097       throw(InvalidParameter)
00098    {
00099 
00100       if( n == 0 )
00101       {
00102          InvalidParameter e( "Invalid value for NDF." );
00103          GPSTK_THROW(e);
00104       }
00105 
00106       if( n < 0 )
00107       {
00108          ndf = -n;
00109       }
00110       else
00111       {
00112          ndf = n;
00113       }
00114 
00115       return (*this);
00116 
00117    }  // End of method 'Chi2Distribution::setNDF()'
00118 
00119 
00120 
00121 }  // End of namespace gpstk

Generated on Wed Feb 8 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1