00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanNormalProbability.h" 00028 00029 #include "CeylanOperators.h" 00030 00031 00032 using std::string ; 00033 00034 using namespace Ceylan ; 00035 using namespace Ceylan::Maths ; 00036 using namespace Ceylan::Maths::Random ; 00037 00038 00039 00040 00041 NormalProbabilityFunction::NormalProbabilityFunction( Sample mean, 00042 Deviation sigma ) : 00043 ProbabilityFunction(), 00044 _mean( mean ), 00045 _sigma( sigma ) 00046 { 00047 00048 if ( Maths::IsNull( _sigma ) ) 00049 throw MathsException( "NormalProbabilityFunction constructor: " 00050 "standard deviation (sigma) must not be null or almost." ) ; 00051 00052 } 00053 00054 00055 00056 NormalProbabilityFunction::~NormalProbabilityFunction() throw() 00057 { 00058 00059 } 00060 00061 00062 00063 Probability NormalProbabilityFunction::operator() ( Sample aSample ) const 00064 { 00065 00066 /* 00067 * Gaussian law: 00068 * P(x) = exp( -1/2*((x-mean)/sigma)²) / (sigma * sqrt(2.Pi)) 00069 * 00070 */ 00071 00072 return static_cast<Probability>( 00073 Exp( -0.5 * Pow( ( aSample - _mean ) / _sigma, 2 ) 00074 / ( _sigma * Sqrt( 2 * Pi ) ) ) ) ; 00075 00076 } 00077 00078 00079 00080 const string NormalProbabilityFunction::toString( 00081 VerbosityLevels level ) const 00082 { 00083 00084 return "Normal probability function whose mean is " 00085 + Ceylan::toString( _mean ) 00086 + " and whose standard deviation is " 00087 + Ceylan::toString( _sigma ) ; 00088 00089 } 00090