00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef GELMANCONVERGENCETEST_H_SEP_30_2005
00067 #define GELMANCONVERGENCETEST_H_SEP_30_2005
00068
00069 #include "meanvariance.h"
00070
00074 class GelmanConvergenceTest
00075 {
00076 public:
00077 GelmanConvergenceTest(const int& numChains) :
00078 numChains_(numChains),
00079 withinChainMeanVars_(new MeanVariance[numChains]),
00080 numSamples_(0) {}
00081
00082 ~GelmanConvergenceTest() { delete [] withinChainMeanVars_; }
00083
00084
00085
00086 void appendNewValues(const double* const & values)
00087 {
00088 for (int i = 0; i < numChains_; i++)
00089 withinChainMeanVars_[i].appendValue(values[i]);
00090 numSamples_++;
00091 }
00092
00093
00094 void appendNewValues(const bool* const & values)
00095 {
00096 for (int i = 0; i < numChains_; i++)
00097 withinChainMeanVars_[i].appendValue((double)values[i]);
00098 numSamples_++;
00099 }
00100
00101
00102 double getConvergenceScore()
00103 {
00104 betweenChainsMeanVar_.reset();
00105 double totalW = 0;
00106 for (int i = 0; i < numChains_; i++)
00107 {
00108 betweenChainsMeanVar_.appendValue( withinChainMeanVars_[i].getMean() );
00109 totalW += withinChainMeanVars_[i].getVariance();
00110 }
00111 int numValues = withinChainMeanVars_[0].getNumValues();
00112
00113 double B = betweenChainsMeanVar_.getVariance() * numValues;
00114 double W = totalW / numChains_;
00115
00116
00117 double score = B/W;
00118 return score;
00119 }
00120
00121
00122 int getNumSamplesAdded() { return numSamples_; }
00123
00124
00125 static bool checkConvergenceOfAll(GelmanConvergenceTest* tests[],
00126 const int& numTests,
00127 const bool& print=false)
00128 {
00129
00130 double threshold = 1 + 0.44 * tests[0]->getNumSamplesAdded();
00131 int maxItem = -1;
00132 double maxScore = -1;
00133 int numbad = 0;
00134
00135 for (int f = 0; f < numTests; f++)
00136 {
00137 double score = tests[f]->getConvergenceScore();
00138
00139 if (!finite(score)) { numbad++; continue; }
00140
00141 if (score > threshold)
00142 {
00143 if (print)
00144 cout << " Item " << f << "'s score of " << score << " > threshold of "
00145 << threshold << endl;
00146 return false;
00147 }
00148
00149 if (score > maxScore)
00150 {
00151 maxScore = score;
00152 maxItem = f;
00153 }
00154 }
00155
00156 if (numbad == numTests)
00157 {
00158 if (print) cout << " All scores were inf or Nan!" << endl;
00159 return false;
00160 }
00161
00162
00163
00164 if (print)
00165 cout << " max item is " << maxItem << " with score " << maxScore
00166 << " < threshold of " << threshold << endl;
00167
00168 return true;
00169 }
00170
00171
00172 private:
00173 int numChains_;
00174 MeanVariance* withinChainMeanVars_;
00175 int numSamples_;
00176 MeanVariance betweenChainsMeanVar_;
00177
00178 };
00179
00180
00181 #endif