StudentDistribution.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: StudentDistribution.cpp 1409 2008-09-24 16:56:22Z architest $"
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 "StudentDistribution.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 StudentDistribution::pdf(double x)
00046    {
00047 
00048          // If ndf == 1, this is a Cauchy distribution
00049       if( ndf == 1 )
00050       {
00051          return ( 1.0 / ( PI * ( 1.0  + x*x ) ) );
00052       }
00053 
00054 
00055          // If ndf == 2, we use a simpler equation
00056       if( ndf == 2 )
00057       {
00058          double temp( 2.0 + x*x );
00059          return ( 1.0 / ( std::sqrt( temp * temp * temp ) ) );
00060       }
00061 
00062       double nu( static_cast<double>(ndf) );
00063 
00064          // Let's compute some terms
00065       double t1( 0.5*nu );
00066       double t2( t1 + 0.5 );
00067       double t3( std::log( std::sqrt(nu*PI) ) );
00068 
00069       return ( std::exp( lngamma(t2) - t2 * std::log(1.0 + x*x/nu)
00070                          - t3 - lngamma(t1) ) );
00071 
00072    }  // End of method 'StudentDistribution::pdf()'
00073 
00074 
00075 
00076       /* Computes the cumulative distribution function
00077        *
00078        * @param x    Value
00079        */
00080    double StudentDistribution::cdf(double x)
00081    {
00082 
00083          // If ndf == 1, this is a Cauchy distribution
00084       if( ndf == 1 )
00085       {
00086          return ( 0.5 + ( std::atan(x) / PI ) );
00087       }
00088 
00089          // If ndf == 2, we use a simpler equation
00090       if( ndf == 2 )
00091       {
00092          return ( 0.5 * ( 1.0 + x / std::sqrt( 2.0 + x*x ) ) );
00093       }
00094 
00095 
00096       double nu( static_cast<double>(ndf) );
00097 
00098          // Let's compute some terms
00099       double t1( 0.5*nu );
00100       double t2( std::sqrt(x*x+nu) );
00101       double t3( 0.5 * ( 1.0 + ( x / t2 ) )  );
00102 
00103       return ( regIncompleteBeta(t3, t1, t1) );
00104 
00105    }  // End of method 'StudentDistribution::cdf()'
00106 
00107 
00108 
00109       /* Set the number of degrees of freedom.
00110        *
00111        * @param n       Degrees of freedom
00112        *
00113        * \warning "n" must be > 0.0, otherwise n = |n|.
00114        */
00115    StudentDistribution& StudentDistribution::setNDF(int n)
00116       throw(InvalidParameter)
00117    {
00118 
00119       if( n == 0 )
00120       {
00121          InvalidParameter e( "Invalid value for NDF." );
00122          GPSTK_THROW(e);
00123       }
00124 
00125       if( n < 0 )
00126       {
00127          ndf = -n;
00128       }
00129       else
00130       {
00131          ndf = n;
00132       }
00133 
00134       return (*this);
00135 
00136    }  // End of method 'StudentDistribution::setNDF()'
00137 
00138 
00139 
00140 }  // End of namespace gpstk

Generated on Thu Feb 9 03:31:01 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1