Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
binomial.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 _BINOMIAL_H
00022 #define _BINOMIAL_H
00023 
00024 #include <iostream>
00025 
00026 #include <math.h>
00027 
00028 #include "constants.h"
00029 
00030 inline double log_fac(int n)
00031 {
00032   // take care of special cases first.
00033   if (0 == n)
00034     return(0);
00035   if (1 == n)
00036     return(0);
00037   return(0.5*log(2 * PI * n) + n*log(n/E));
00038 }
00039 
00040 inline double fac(int n)
00041 {
00042   return(exp(log_fac(n)));
00043 }
00044 
00045 inline double log_choose(int n, int k)
00046 {
00047   return(log_fac(n) - log_fac(k) - log_fac(n - k));
00048 }
00049 
00050 inline double choose(int n, int k)
00051 {
00052   return(exp(log_choose(n, k)));
00053 }
00054 
00055 inline double log_prob_binomial(int n, int k, double prob)
00056 {
00057     if (n < k)
00058       {
00059 #ifdef DEBUG
00060         cerr << "log_prob_binomial(): n = " << n << " < k = " << k << ", returning 0.\n";
00061 #endif
00062         return(0);
00063       }
00064     if (0 > n )
00065       {
00066 #ifdef DEBUG
00067         cerr << "log_prob_binomial(): n = " << n << " < 0, returning 0.\n";
00068 #endif
00069         return(0);
00070       }
00071     if (1 < prob)
00072       {
00073 #ifdef DEBUG
00074         cerr << "log_prob_binomial(): p = " << prob << " >= 1, returning 0.\n";
00075 #endif
00076         return(0);
00077       }
00078     if (1 == prob)
00079       {
00080         if (n != k)
00081           {
00082 #ifdef DEBUG
00083 //            cerr << "log_prob_binomial(): p = " << prob << " == 1 and n = "
00084 //            << n << " != k = " << k << ", returning 0.\n";
00085 #endif
00086             return(0);
00087           }
00088         else
00089           {
00090 #ifdef DEBUG
00091 //            cerr << "log_prob_binomial(): p = " << prob << " == 1 and n = "
00092 //            << n << " == k = " << k << ", returning 1.\n";
00093 #endif
00094             return(1);
00095           }
00096       }
00097     if (0 > prob)
00098          {
00099 #ifdef DEBUG
00100            cerr << "log_prob_binomial(): p = " << prob << " <= 0, returning 0.\n";
00101 #endif
00102            return(0);
00103          }
00104     if (0 == prob)
00105       {
00106         if (0 != k)
00107           {
00108 #ifdef DEBUG
00109 //             cerr << "log_prob_binomial(): p = " << prob << " == 1 and k = " << k
00110 //                  << " != 0, returning 0.\n";
00111 #endif
00112             return(0);
00113           }
00114         else
00115           {
00116 #ifdef DEBUG
00117 //             cerr << "log_prob_binomial(): p = " << prob << " == 1 and k = " << k
00118 //                  << " == 0, returning 1.\n";
00119 #endif
00120             return(1);
00121           }
00122       }
00123     return(log_choose(n, k) + k*log(prob) + (n - k)*log(1 - prob));
00124   }
00125 
00126 inline double sum_binomial_probs(int n, int start, int end, double prob)
00127   {
00128     double sum = 0;
00129     for (int i = start; i <= end; i++)
00130       {
00131         sum += exp(log_prob_binomial(n, i, prob));
00132       }
00133     return(sum);
00134   }
00135 
00136 // there are total items. of these, specialSize items are special.
00137 // pick a sample of size sampleSize. what is the probability that at
00138 // specialSizeInSample in the sample are special?
00139 inline double sum_binomial_product(int total, int specialSize, int sampleSize,
00140                             int specialSizeInSample)
00141 {
00142   int i;
00143   double sum = 0;
00144   for (i = specialSizeInSample; i < sampleSize; i++)
00145     // pick i special and sampleSize - i non-special
00146     sum += choose(specialSize, i)*choose(total - specialSize, sampleSize - i);
00147   // there are choose(total, sampleSize) possible choices.
00148   sum = sum/choose(total, sampleSize);
00149   return(sum);
00150 }
00151 
00152 #endif // _BINOMIAL_H 
 All Classes Functions Variables Typedefs Friends