convergencetest.h

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner and Hoifung Poon.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00009  * Poon. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00032  * Poon in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00064  * 
00065  */
00066 #ifndef CONVERGENCETEST_H_SEP_30_2005
00067 #define CONVERGENCETEST_H_SEP_30_2005
00068 
00069 #include <cmath>
00070 #include "meanvariance.h"
00071 
00072 // Grabbed from http://home.online.no/~pjacklam/notes/invnorm/impl/misra/
00073 // normsinv.html:
00074 //
00075 // Lower tail quantile for standard normal distribution function.
00076 //
00077 // This function returns an approximation of the inverse cumulative
00078 // standard normal distribution function.  I.e., given P, it returns
00079 // an approximation to the X satisfying P = Pr{Z <= X} where Z is a
00080 // random variable from the standard normal distribution.
00081 //
00082 // The algorithm uses a minimax approximation by rational functions
00083 // and the result has a relative error whose absolute value is less
00084 // than 1.15e-9.
00085 //
00086 // Author:    Peter J. Acklam
00087 // (Javascript version by Alankar Misra @ Digital Sutras 
00088 // (alankar@digitalsutras.com))
00089 // Time-stamp:  2003-05-05 05:15:14
00090 // E-mail:    pjacklam@online.no
00091 // WWW URL:    http://home.online.no/~pjacklam
00092 
00093 // An algorithm with a relative error less than 1.15�10-9 in the 
00094 // entire region.
00095 
00096 inline double NORMSINV(double p)
00097 {
00098   double a[] = {-3.969683028665376e+01,  2.209460984245205e+02,
00099                 -2.759285104469687e+02,  1.383577518672690e+02,
00100                 -3.066479806614716e+01,  2.506628277459239e+00};
00101   double b[] = {-5.447609879822406e+01,  1.615858368580409e+02,
00102                 -1.556989798598866e+02,  6.680131188771972e+01,
00103                 -1.328068155288572e+01 };
00104 
00105   double c[] = {-7.784894002430293e-03, -3.223964580411365e-01,
00106                 -2.400758277161838e+00, -2.549732539343734e+00,
00107                  4.374664141464968e+00,  2.938163982698783e+00};
00108 
00109   double d[] = {7.784695709041462e-03, 3.224671290700398e-01,
00110                 2.445134137142996e+00, 3.754408661907416e+00};
00111 
00112     // Define break-points.
00113   double plow  = 0.02425;
00114   double phigh = 1 - plow;
00115 
00116     // Rational approximation for lower region:
00117   if (p < plow) 
00118   {
00119     double q = sqrt(-2*log(p));
00120     return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
00121             ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
00122   }
00123 
00124       // Rational approximation for upper region:
00125     if (phigh < p) 
00126     {
00127       double q  = sqrt(-2*log(1-p));
00128       return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
00129               ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
00130     }
00131       // Rational approximation for central region:
00132     double q = p - 0.5;
00133     double r = q*q;
00134     return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
00135            (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
00136 }
00137 
00139 
00140 
00141 class ConvergenceTest
00142 {
00143  public:
00144     // epsilonFrac_ is allowable error, as a fraction of the mean of the samples
00145     // gamma is the probability that you want to be within that error
00146   ConvergenceTest(const int& numChains, const double& gamma, 
00147                   const double& epsilonFrac) 
00148     : numChains_(numChains), chainTotals_(new double[numChains]), 
00149       totalOverAllChains_(0), numSamplesPerChain_(0), 
00150       epsilonFrac_(epsilonFrac), gamma_(gamma)
00151   {
00152     memset(chainTotals_, 0, numChains_*sizeof(double));
00153   }
00154 
00155 
00156   ~ConvergenceTest() { delete [] chainTotals_; }
00157 
00158 
00159     // values is array of size numChains_
00160   void appendNewValues(const double* const & values) 
00161   {
00162     for (int i = 0; i < numChains_; i++) 
00163     {
00164       chainTotals_[i] += values[i];
00165       totalOverAllChains_ += values[i];
00166     }    
00167     numSamplesPerChain_++;
00168   }
00169 
00170 
00171   void appendNewValues(const bool* const & values) 
00172   {
00173     for (int i = 0; i < numChains_; i++) 
00174     {
00175       chainTotals_[i] += ((double)values[i]);
00176       totalOverAllChains_ += ((double)values[i]);
00177     }    
00178     numSamplesPerChain_++;
00179   }
00180 
00181 
00182     // An estimate of how many samples are neccessary, per chain
00183   double getConvergenceScore()
00184   {
00185     //double m0 = numSamplesPerChain_;
00186     //double S = getS();
00187     //double sigma = sqrt(m0) * S;
00188     //double v = getV(sigma);
00189     //return v / numChains_;
00190     return getV(sqrt((double)numSamplesPerChain_)*getS()) / numChains_;
00191   }
00192 
00193 
00194   int getNumSamplesAdded() const { return numSamplesPerChain_; }
00195 
00196 
00197   static bool checkConvergenceOfAtLeast(ConvergenceTest* tests[], 
00198                                         const int& numTests,
00199                                         const double& threshold, 
00200                                         const double& fracConverged,
00201                                         const bool& print=false) 
00202   {
00203     if (threshold <= 0) return false;
00204 
00205     int numConverged    = 0;
00206     int numNotConverged = 0;
00207     int numBad          = 0;
00208     for (int f = 0; f < numTests; f++) 
00209     {
00210       double score = tests[f]->getConvergenceScore();
00211       if (!finite(score))
00212         numBad++;
00213       else 
00214       {
00215         if (score > threshold)
00216           numNotConverged++;
00217         else
00218           numConverged++;
00219       }
00220     }
00221   
00222     if (numConverged + numNotConverged == 0) 
00223     {
00224       if (print) cout << "All scores were inf or nan!" << endl;
00225       return false;
00226     }
00227     else 
00228     {
00229       double frac = ((double)numConverged) / (numConverged+numNotConverged);
00230       if (print) cout << " fraction converged: " << frac << endl;
00231       if (frac >= fracConverged) return true;
00232       else                       return false;
00233     }
00234   }
00235 
00236 
00237  private:
00238   double getS()
00239   {
00240     meanVar_.reset();
00241     for (int i = 0; i < numChains_; i++) 
00242       meanVar_.appendValue(chainTotals_[i] / numSamplesPerChain_);
00243     return sqrt(meanVar_.getVariance());
00244   }
00245 
00246 
00247   double getV(const double& sigma)
00248   {
00249     double invNorm = NORMSINV( (gamma_+1)/2 );
00250     double val = invNorm * sigma / epsilonFrac_;
00251     return val*val;
00252   }
00253 
00254 
00255  private:
00256   int numChains_;
00257   double* chainTotals_;
00258   double totalOverAllChains_;
00259   int numSamplesPerChain_;
00260 
00261   double epsilonFrac_;
00262   double gamma_;
00263 
00264   MeanVariance meanVar_;
00265 };
00266 
00267   
00268 
00269 
00270 
00271 
00272 #endif

Generated on Tue Jan 16 05:30:02 2007 for Alchemy by  doxygen 1.5.1