Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

pl_erf.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 #include <plearn/base/general.h> 00037 // #include <iostream> 00038 // #include <cmath> 00039 00040 namespace PLearn { 00041 using namespace std; 00042 00043 #define ITMAX 100 00044 #define EPS 3.0e-7 00045 #define FPMIN 1.0e-30 00046 #define Pi 3.141592653589793 00047 #define Log2Pi 1.837877066409 00048 #define Sqrt2Pi 2.506628274631 00049 00050 static double pl_gammln_cof[7]={ 1.000000000190015 , 00051 76.18009172947146 , 00052 -86.50532032941677 , 00053 24.01409824083091 , 00054 -1.231739572450155 , 00055 0.1208650973866179e-2, 00056 -0.5395239384953e-5 }; 00057 00058 // function gamma returns log(Gamma(z)), where 00059 // Gamma(z) = \int_0^infty t^{z-1}*e^{-t} dt 00060 real pl_gammln(real z) 00061 { 00062 double gz,tmp; 00063 static double gamma = 5.0; 00064 gz = (z+0.5)*log(z+gamma+0.5); 00065 gz -= z+gamma+0.5; 00066 gz += 0.5*log(2*Pi); 00067 tmp = pl_gammln_cof[0]; 00068 for(int i=1;i<7;i++) tmp += pl_gammln_cof[i]/(z+i); 00069 gz += log(tmp/z); 00070 return(gz); 00071 } 00072 00073 // returns d(pl_gammln(z))/dz 00074 real pl_dgammlndz(real z) 00075 { 00076 real tmp0= pl_gammln_cof[0], 00077 tmp1= 0.0; 00078 for(int i= 1; i<7; ++i) 00079 { 00080 tmp0+= pl_gammln_cof[i]/(z+i); 00081 tmp1-= pl_gammln_cof[i]/((z+i)*(z+i)); 00082 } 00083 return (0.5+z)/(5.5+z)-1 + z*(-tmp0/(z*z) + tmp1/z)/tmp0 + log(5.5+z); 00084 } 00085 00086 00087 // returns the series value of 00088 // the incomplete gamma function 00089 real pl_gser(real a, real x) { 00090 real EPSILON = 1e-7; 00091 real g = pl_gammln(a); 00092 real sum,term; 00093 if (x<0 || a<0) 00094 PLERROR("Error in function pl_gser. Bad argument."); 00095 else if (x==0) 00096 return 0; 00097 00098 sum = term = 1/a; 00099 for(int i=1;i<ITMAX;i++) { 00100 term *= x/(a+i); 00101 sum += term; 00102 if (term < sum*EPSILON) break; 00103 } 00104 return exp(-x+a*log(x)-g)*sum; 00105 } 00106 00107 00108 // returns the continued fraction representation of 00109 // the incomplete gamma function 00110 real pl_gcf(real a, real x) 00111 { 00112 int i; 00113 real an,b,c,d,del,h; 00114 00115 real gln=pl_gammln(a); 00116 b=x+1.0-a; 00117 c=1.0/FPMIN; 00118 d=1.0/b; 00119 h=d; 00120 for (i=1;i<=ITMAX;i++) { 00121 an = -i*(i-a); 00122 b += 2.0; 00123 d=an*d+b; 00124 if (fabs(d) < FPMIN) d=FPMIN; 00125 c=b+an/c; 00126 if (fabs(c) < FPMIN) c=FPMIN; 00127 d=1.0/d; 00128 del=d*c; 00129 h *= del; 00130 if (fabs(del-1.0) < EPS) break; 00131 } 00132 if (i > ITMAX) { 00133 PLERROR("a too large, ITMAX too small in pl_gcf"); 00134 } 00135 return exp(-x+a*log(x)-(gln))*h; 00136 } 00137 00138 00139 // returns the incomplete gamma function Q(a,x) = 1 - P(a,x) 00140 // it either uses the series or the continued fraction formula 00141 real pl_gammq(real a, real x) { 00142 if (x<0 || a<0) 00143 PLERROR("Error in function gammax. Bad arguments."); 00144 if (x<a+1) 00145 return 1-pl_gser(a,x); 00146 return pl_gcf(a,x); 00147 } 00148 00149 00150 // returns the error function "erf" 00151 real pl_erf(real x) { 00152 return (x<0?-1:1)*(1-pl_gammq(0.5,x*x)); 00153 } 00154 00155 00156 //returns the gaussian cumulative function 00157 // For X ~ Normal(0,1), cumulative probability function P(X<x) 00158 real gauss_01_cum(real x) { 00159 return 0.5*(1+pl_erf(x/1.414214)); 00160 } 00161 00162 // For X ~ Normal(0,1), inverse of cumulative probability function P(X<x) 00163 // i.e. approximately gauss_01_quantile(gauss_01_cum(x)) ~=~ x 00164 // (the inverse is computed with a binary search, the bisection method) 00165 real gauss_01_quantile(real q) { 00166 // first find a reasonable interval (a,b) s.t. cum(a)<q<cum(b) 00167 real a=-2; 00168 real b=2; 00169 real cum_a=gauss_01_cum(a); 00170 real cum_b=gauss_01_cum(b); 00171 while (cum_a>q) { a*=1.5; cum_a=gauss_01_cum(a); } 00172 while (cum_b<q) { b*=1.5; cum_b=gauss_01_cum(b); } 00173 // then start the bisection loop itself 00174 for (;;) { 00175 real c=0.5*(a+b); 00176 real precision = fabs(b-a); 00177 // PRECISION HERE: 00178 if (precision < 1e-6) 00179 return c; 00180 real cum_c = gauss_01_cum(c); 00181 if (cum_c < q) 00182 a=c; 00183 else 00184 b=c; 00185 } 00186 return 0; // never reached 00187 } 00188 00189 00190 // for X ~ Normal(0,1), return density of X at x 00191 real gauss_01_density(real x) 00192 { 00193 return exp(-0.5*x*x) / Sqrt2Pi; 00194 } 00195 00196 real gauss_01_log_density(real x) 00197 { 00198 return -0.5*x*x - 0.5*Log2Pi; 00199 } 00200 00201 real gauss_log_density_var(real x, real mu, real var) 00202 { 00203 real dx=x-mu; 00204 return -0.5*(dx*dx/var + Log2Pi + log(var)); 00205 00206 } 00207 00208 real gauss_density_var(real x, real mu, real var) { 00209 real dx = x - mu; 00210 return exp(-0.5 * dx * dx / var) / Sqrt2Pi; 00211 } 00212 00213 real gauss_log_density_stddev(real x, real mu, real sigma) 00214 { 00215 real dx = (x-mu) / sigma; 00216 return -0.5*(dx*dx + Log2Pi) - log(sigma); 00217 } 00218 00219 real p_value(real mu, real vn) 00220 { 00221 return 1 - gauss_01_cum(fabs(mu/sqrt(vn))); 00222 } 00223 00224 00225 } // end of namespace PLearn

Generated on Tue Aug 17 16:01:25 2004 for PLearn by doxygen 1.3.7