Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
gaussian.h
00001 /**************************************************************************
00002  * Copyright (c) 2002-2011 T. M. Murali                                   *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm is distributed in the hope that it will be useful,            *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00014  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00021 #ifndef _GAUSSIAN_H
00022 #define _GAUSSIAN_H
00023 
00024 #include <math.h>
00025 
00026 #include "constants.h"
00027 
00028 // say the functions are INLINE!!!!!! otherwise, they get defined multiple times.
00029 inline double gaussian_prob(double x, double mean, double sigma)
00030 {
00031   if (0 == sigma)
00032     // this is a spike. gaussian's prob. is 1 iff x equals mean.
00033     return(x == mean);
00034   double std = (x - mean)/sigma;
00035   return(exp(-std*std/2)/(sigma*SQRT2PI));
00036 }
00037 
00038 inline double gaussian_log_prob(double x, double mean, double sigma)
00039 {
00040   if (0 == sigma)
00041     // this gaussian is a spike. gaussian's prob. is 1 iff x equals mean.
00042     {
00043       double std = (x == mean);
00044       return(log(std));
00045     }
00046   double std = (x - mean)/sigma;
00047   return(-std*std/2 - log(sigma*SQRT2PI));
00048 }
00049 
00050 // return the prob. that the gaussian takes a value >= x. 
00051 inline double gaussian_pval(double x, double mean, double sigma)
00052 {
00053   return(0.5*erfc((x - mean)/(sigma*SQRT2)));
00054   
00055 }
00056 
00057 
00058 // compute the prob of a gaussian random variable falling inside an
00059 // interval. this value is simply the integral of the gaussian between
00060 // the two limits of the interval or the diff. between the pvalues of
00061 // the two limits.
00062 inline double prob_interval_gaussian(double start, double end, double mean,
00063                               double sigma)
00064   {
00065     return(gaussian_pval(start, mean, sigma) - 
00066            gaussian_pval(end, mean, sigma));
00067   }
00068   
00069 #endif // _GAUSSIAN_H 
 All Classes Functions Variables Typedefs Friends