GaussianDistribution.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: GaussianDistribution.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 "GaussianDistribution.hpp"
00033 
00034 
00035 using namespace std;
00036 
00037 namespace gpstk
00038 {
00039 
00040 
00041       /* Default constructor. Sets a standard normal distribution:
00042        *  mean = 0.0, and standard deviation = 1.0.
00043        */
00044    GaussianDistribution::GaussianDistribution()
00045       : mean(0.0), sigma(1.0)
00046    {
00047 
00048       recompute();
00049 
00050    }  // End of constructor 'GaussianDistribution::GaussianDistribution()'
00051 
00052 
00053 
00054       /* Explicit constructor.
00055        *
00056        * @param mu      Mean
00057        * @param sig     Standard deviation
00058        *
00059        * \warning If (sig <= 0.0), it will be set to 1.0.
00060        */
00061    GaussianDistribution::GaussianDistribution( double mu,
00062                                                double sig )
00063       : mean(mu), sigma(sig)
00064    {
00065 
00066       recompute();
00067 
00068    }  // End of constructor 'GaussianDistribution::GaussianDistribution()'
00069 
00070 
00071 
00072       /* Computes the probability density function
00073        *
00074        * @param x    Value
00075        */
00076    double GaussianDistribution::pdf(double x)
00077    {
00078 
00079       return ( a * exp( b * (x - mean) * (x - mean) ) );
00080 
00081    }  // End of method 'GaussianDistribution::pdf()'
00082 
00083 
00084 
00085       /* Computes the cumulative distribution function
00086        *
00087        * @param x    Value
00088        */
00089    double GaussianDistribution::cdf(double x)
00090    {
00091 
00092       return ( 0.5 *
00093              ( 1.0
00094              + gpstk::erf( 0.70710678118654746 * (x - mean)/sigma ) ) );
00095 
00096    }  // End of method 'GaussianDistribution::cdf()'
00097 
00098 
00099 
00100       /* Computes the quantile function ( cdf^-1() )
00101        *
00102        * @param p    Probability value
00103        *
00104        * \ warning Value "p" must be in the range (0, 1)
00105        */
00106    double GaussianDistribution::invcdf(double p)
00107       throw(InvalidParameter)
00108    {
00109 
00110       double inf( 9.0e+99 );
00111 
00112          // Check limits
00113       if( ( p < 0.0 ) ||
00114           ( p > 1.0 ) )
00115       {
00116          InvalidParameter e( "Invalid input value for 'p'." );
00117          GPSTK_THROW(e);
00118       }
00119 
00120       if( p == 0.0 ) return -inf;
00121       if( p == 1.0 ) return inf;
00122 
00123          // Compute invcdf
00124       return ( mean
00125              + 1.4142135623730951 * sigma * gpstk::inverf( 2.0 * p - 1.0 ) );
00126 
00127    }  // End of method 'GaussianDistribution::invcdf()'
00128 
00129 
00130 
00131       /* Sets the standard deviation
00132        *
00133        * @param sig     Standard deviation
00134        *
00135        * \warning If (sig <= 0.0), it will be set to 1.0.
00136        */
00137    GaussianDistribution& GaussianDistribution::setSigma(double sig)
00138    {
00139 
00140       if( sig <= 0.0 )
00141       {
00142          sig = 1.0;
00143       }
00144 
00145       sigma = sig;
00146 
00147       recompute();
00148 
00149       return (*this);
00150 
00151    }  // End of method 'GaussianDistribution::setSigma()'
00152 
00153 
00154 
00155       /* Sets all parameters in one pass.
00156        *
00157        * @param mu      Mean
00158        * @param sig     Standard deviation
00159        *
00160        * \warning If (sig <= 0.0), it will be set to 1.0.
00161        */
00162    GaussianDistribution& GaussianDistribution::setParameters( double mu,
00163                                                               double sig )
00164    {
00165 
00166       mean = mu;
00167 
00168          // Set sigma, check limits and recompute
00169       setSigma(sig);
00170 
00171       return (*this);
00172 
00173    }  // End of method 'GaussianDistribution::setParameters()'
00174 
00175 
00176 
00177       // Compute internal parameters
00178    void GaussianDistribution::recompute(void)
00179    {
00180 
00181          // If sigma is less or equal than zero, let's set it to 1.0
00182       if( sigma <= 0.0)
00183       {
00184          sigma = 1.0;
00185       }
00186 
00187       a = 0.3989422804014327 / sigma;
00188       b = -0.5 / (sigma*sigma);
00189 
00190       return;
00191 
00192    }  // End of method 'GaussianDistribution::recompute()'
00193 
00194 
00195 
00196 }  // End of namespace gpstk

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