Changeset 203
- Timestamp:
- 05/30/08 08:27:51 (6 months ago)
- Files:
-
- projects/AsynCluster/branches/GM_WWT_Normal-svpmc_model.c (copied) (copied from projects/AsynCluster/trunk/svpmc/model.c) (1 diff)
- projects/AsynCluster/branches/svpmc_model_Lx-Lz (added)
- projects/AsynCluster/branches/svpmc_model_Lx-Lz/model.c (copied) (copied from projects/AsynCluster/trunk/svpmc/model.c) (5 diffs)
- projects/AsynCluster/branches/svpmc_model_Lx-Lz/model.py (copied) (copied from projects/AsynCluster/trunk/svpmc/model.py) (1 diff)
- projects/AsynCluster/branches/svpmc_model_Lx-Lz/test_model.py (copied) (copied from projects/AsynCluster/trunk/svpmc/test/test_model.py) (2 diffs)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate (copied) (copied from projects/AsynCluster/trunk)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/doc/svpmc/example/svpmc.conf (copied) (copied from projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/model.c (copied) (copied from projects/AsynCluster/trunk/svpmc/model.c)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/model.py (copied) (copied from projects/AsynCluster/trunk/svpmc/model.py)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/params.py (modified) (1 diff)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/pmc.py (copied) (copied from projects/AsynCluster/trunk/svpmc/pmc.py)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/project.py (copied) (copied from projects/AsynCluster/trunk/svpmc/project.py) (4 diffs)
- projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/test/test_model.py (copied) (copied from projects/AsynCluster/trunk/svpmc/test/test_model.py) (2 diffs)
- projects/AsynCluster/trunk/svpmc/params.py (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/project.py (modified) (4 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_model.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/branches/GM_WWT_Normal-svpmc_model.c
r201 r203 172 172 static double logC = -log(2 * M_PI); 173 173 return -0.5*x*x + logC; 174 } 175 176 177 // Normal random variate generation 178 // 179 // From http://people.scs.fsu.edu/~burkardt/cpp_src/ziggurat/ziggurat.C and 180 // G. Marsaglia, et al, "The Ziggurat Method for Generating Random Variables," 181 // Journal of Statistical Software, Volume 5, Number 8, October 2000. 182 #define SHR3 (jz=jsr, jsr^=(jsr<<13), jsr^=(jsr>>17), jsr^=(jsr<<5), jz+jsr) 183 #define UNI (0.5 + (signed) SHR3 * 0.2328306e-09) 184 float normal(void) 185 { 186 const float r = 3.442620; 187 static float x, y; 188 static long int hz; 189 static unsigned long int iz, jz; 190 static unsigned long int jsr = 123456789; 191 static unsigned int ke[256], kn[128]; 192 static float fe[256], fn[128], we[256], wn[128]; 193 194 // Set things up on the first run only 195 static int ready = 0; 196 if(!ready) { 197 const double m1 = 2147483648.0; 198 int k; 199 double q; 200 double dn = 3.442619855899; 201 double tn = 3.442619855899; 202 double vn = 9.91256303526217e-03; 203 q = vn / exp(-0.5 * dn*dn); 204 kn[0] = (dn / q) * m1; 205 kn[1] = 0; 206 207 wn[0] = q / m1; 208 wn[127] = dn / m1; 209 210 fn[0] = 1.0; 211 fn[127] = exp(-0.5 * dn*dn); 212 213 for (k=126; 1 <= k; k--) { 214 dn = sqrt( -2.0 * log(vn/dn + exp(-0.5 * dn*dn))); 215 printf("%f %f %f %f\n", dn, tn, m1, (dn/tn)*m1); 216 kn[k+1] = (dn / tn) * m1; 217 tn = dn; 218 fn[k] = exp(-0.5 * dn*dn); 219 wn[k] = dn / m1; 220 } 221 ready = 1; 222 } 223 224 // Return the quick result if possible... 225 hz = SHR3; 226 iz = hz & 127; 227 x = hz * wn[iz]; 228 printf("%f %f\n", fabs(hz), kn[iz]); 229 if(fabs(hz) < kn[iz]) { 230 printf("A: %f\n",x); 231 return x; 232 } 233 234 // ...and compute things otherwise if not 235 for (;;) { 236 if(iz == 0) { 237 do { 238 x = -log(UNI) * 0.2904764; 239 y = -log(UNI); 240 } while (y+y < x*x); 241 printf("B: %f\n",(0<hz) ? r+x : -r-x); 242 return (0<hz) ? r+x : -r-x; 243 } 244 if(fn[iz] + UNI*(fn[iz-1] - fn[iz]) < exp(-0.5 * x*x)) { 245 printf("C: %f\n",x); 246 return x; 247 } 248 hz = SHR3; 249 iz = hz & 127; 250 if(fabs(hz) < kn[iz]) { 251 printf("D: %f\n",x); 252 return hz * wn[iz]; 253 } 254 } 174 255 } 175 256 projects/AsynCluster/branches/svpmc_model_Lx-Lz/model.c
r202 r203 343 343 // The number of multivariate samples 344 344 const int Ns = Nx[1]; 345 // The last iteration index 346 const int ki_n = ni-1; 345 347 346 348 int ki, ke; … … 355 357 // first time-series sample of the evaluation interval 356 358 double he[2][Nt]; 359 // Accumulator for log-probability of the simulated log-volatilities, relative 360 // to that of the baseline zero-valued log-volatilities. 361 double Lz; 357 362 // Log-likelihood of the innovation for the first time-series sample in current 358 // and proposal evalutions, working values and accumulated for each iteration 359 double Lxk[2], Lx[ni]; 363 // and proposal evalutions, working values and accumulated for the last iteration 364 double Lxk[2]; 365 double Lx = 0; 360 366 361 367 // A set of evaluation sample structs … … 378 384 } 379 385 386 // Start with a baseline, all-zero set of log-volatilities... 387 for(k0=0; k0<Ns; k0++) { 388 for(k1=0; k1<Nt; k1++) 389 Z2(k1, k0) = 0; 390 } 391 // ...and the negative of their log-densitities, for the reference value of 392 // Log(P(z|w)) 393 Lz = -Ns*Nt*normLogDensity(0); 394 380 395 // For each iteration... 381 396 for(ki=0; ki<ni; ki++) { 382 383 // Initialize this iteration's accumulator384 Lx[ki] = 0;385 397 386 398 // The "previous" log-volatility of the first time-series sample is set to … … 486 498 // 487 499 // P(x,z|w) = P(x|z,w) * P(z|w) 488 // 489 // P(z|w) doesn't need to be accounted for, however, because the z's are 490 // simulated from it. 491 492 // Accumulate Log(P(xk,zk|w) = Lxk to eventually get Log(P(x,z|w) 493 Lx[ki] += Lxk[ke]; 500 501 if(ki==ki_n) { 502 // Last iteration, accumulate positive Log(P(zk|w)) for log-density... 503 for(k2=0; k2<Nt; k2++) 504 Lz += normLogDensity(Z2(k2, k0)); 505 // ...and Log(P(xk|zk,w) = Lxk for Log(P(x|z,w) 506 Lx += Lxk[ke]; 507 } 494 508 495 509 // Done with log-volatility draw for this time-series sample … … 513 527 } 514 528 515 // Compute the result, the integrated P(x|w) over the last 3/4 of the 516 // simulations on z 517 k0 = (int)(100*ni/400); 518 519 // Compute the maximum log density and save it to subtract from everybody so 520 // that the maximum log density is normalized to zero 521 maxVal = -HUGE_VAL; 522 for(ki=k0; ki<ni; ki++) { 523 if(Lx[ki] > maxVal) 524 maxVal = Lx[ki]; 525 } 526 // Accumulate the linearized normalized densities 527 val = 0; 528 for(ki=k0; ki<ni; ki++) 529 val += exp(Lx[ki] - maxVal); 530 // ...and return the denormalized, log mean of the linear densities... 531 return_val = log(val) + maxVal - log(ni-k0); 529 // Return the log-likelihood of the innovations given the log-volatilities at 530 // the end of the simulation, Log(P(x|z,w))... 531 py::tuple result(2); 532 result[0] = Lx; 533 // ...along with the log-probability of the simulated log-volatilities, 534 // Log(P(z|w)) 535 result[1] = Lz; 536 return_val = result; 537 projects/AsynCluster/branches/svpmc_model_Lx-Lz/model.py
r202 r203 298 298 299 299 # Run the hybrid Gibbs rounds 300 Lx = self.inline(300 Lx, Lz = self.inline( 301 301 'z', 'h', 'x', 'w', 'ni', 302 302 'd', 'e', 'g', 'rz', 'ri', 'sc', 303 303 w=self.wiggle, ni=self.Ni) 304 return Lx, z, h304 return Lx, Lz, z, h projects/AsynCluster/branches/svpmc_model_Lx-Lz/test_model.py
r202 r203 637 637 if j==0 or not sameZ: 638 638 inlineVars['z'] = z = s.zeros((1, Ns)) 639 Lx = modelObj.inlineDirect(639 Lx, Lz = modelObj.inlineDirect( 640 640 modelObj.code['likelihood'], **inlineVars) 641 641 hSim = self._z_to_h(z[0,:], eValue, 1.0) 642 yield hReal, hSim, Lx 642 yield hReal, hSim, Lx, Lz 643 643 644 644 def test_basic(self): … … 672 672 sp.set_title("%s=%d" % (name, value)) 673 673 674 def test_Lx (self):674 def test_Lx_vs_Lz(self): 675 675 # Wiggle seems fairly critical, best at 0.5-1.0. 676 676 # Tried independent proposals +/- 4.0, didn't work well. 677 677 wiggle = 1.0 678 678 eValue = 0.97 679 Ns, Ni, Nr = 200, 100, 100679 Ns, Ni, Nr = 200, 20, 100 680 680 simResults = {} 681 L = s.empty(Nr) 681 Lx = s.empty(Nr) 682 Lz = s.empty(Nr) 682 683 k = 0 683 684 inlineVars = self._inlineVars(wiggle, eValue, Ni) 684 for hReal, hSim, Lx in self._likelihood(685 for hReal, hSim, Lxk, Lzk in self._likelihood( 685 686 Ns, Nr, inlineVars, sameX=True, sameZ=False): 686 L[k] = Lx 687 simResults[k] = (hReal, hSim, "Lx=%f" % (Lx,)) 687 Lx[k] = Lxk 688 Lz[k] = Lzk 689 simResults[k] = (hReal, hSim, "Lx=%f, Lz=%f" % (Lxk, Lzk)) 688 690 if VERBOSE: 689 print "%3d: %4.2f " % (k, Lx)691 print "%3d: %4.2f\t%4.2f" % (k, Lxk, Lzk) 690 692 k += 1 691 693 print "Lx: mean=%4.2f, sdev=%4.2f, range=%4.2f" % ( 692 L.mean(), L.std(), L.max()-L.min()) 694 Lx.mean(), Lx.std(), Lx.max()-Lx.min()) 695 print "Lz: mean=%4.2f, sdev=%4.2f, range=%4.2f" % ( 696 Lz.mean(), Lz.std(), Lz.max()-Lz.min()) 693 697 if VERBOSE: 694 self._plot(L) 698 self._plot(Lx, Lz) 699 self.fig.add_subplot(111).plot(Lx, Lz, '.') 695 700 # Worst 696 plotArgs = [simResults[x] for x in L.argsort()[:4]] 701 I = Lx.argsort() 702 plotArgs = [simResults[x] for x in [I[-1], I[0]]] 697 703 fig = self._plot(*plotArgs) 698 fig.text(0.05, 0.95, "Worst", size='x-large') 699 # Best 700 plotArgs = [simResults[x] for x in L.argsort()[::-1][:4]] 701 fig = self._plot(*plotArgs) 702 fig.text(0.05, 0.95, "Best", size='x-large') 704 fig.text(0.05, 0.95, "Best & Worst", size='x-large') 703 705 704 706 def test_optimize_Ni(self): projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/params.py
r196 r203 347 347 Returns probability density of the supplied parameter value I{x}. 348 348 """ 349 if x.shape: 350 return self.distObj.pdf(x) 349 351 return float(self.distObj.pdf(x)) 350 352 projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/project.py
r201 r203 92 92 self.m = m 93 93 self.iteration = 0 94 #--- Use the NullQueue for debugging, thread queue normally -----------95 #self.queue = NullQueue()96 self.queue = asynqueue.ThreadQueue(1)97 #----------------------------------------------------------------------98 94 specDir = os.path.dirname(specFile) 99 95 self.tables = self._parseSpec(specFile) … … 114 110 # Parameters 115 111 paramTitles, dimensions = self._setupParams() 112 # Open the NetCDF file 113 self.cdf = NetCDF.NetCDFFile( 114 os.path.join(specDir, ncFile), ('w', 'r')[int(readOnly)]) 115 # If in read-only mode, we're done now 116 if readOnly: 117 return 118 # Write mode, write some initial stuff to the NetCDF file... 119 self._setupCDF(tsData, seriesTitles, paramTitles, dimensions) 120 for k, title in enumerate(seriesTitles): 121 obs = self.cdf.variables['observations'] 122 obs[k,:] = tsData[k,:].astype('f') 123 setattr(obs, "series_%02d" % (k,), title) 124 # ...and construct a queue and model manager for a simulation 125 self.queue = asynqueue.ThreadQueue(1) 116 126 self.mgr = model.ModelManager( 117 127 self, tsData, wiggle=self.wiggle, Ni=self.Ni) 118 # If not in read-only mode, setup the NetCDF file for writing119 if not readOnly:120 self._setupCDF(121 os.path.join(specDir, ncFile),122 tsData, seriesTitles, paramTitles, dimensions)123 # Write the observation data124 for k, title in enumerate(seriesTitles):125 obs = self.cdf.variables['observations']126 obs[k,:] = tsData[k,:].astype('f')127 setattr(obs, "series_%02d" % (k,), title)128 128 129 129 def _parseSpec(self, filePath): … … 236 236 return titles, dimensions 237 237 238 def _setupCDF(self, filePath, tsData, sTitles, pTitles, pDims): 239 """ 240 """ 241 cdf = NetCDF.NetCDFFile(filePath, 'w') 242 self.cdf = cdf 243 #self.cdf = CDF_Wrapper(cdf) 238 def _setupCDF(self, tsData, sTitles, pTitles, pDims): 239 """ 240 """ 241 cdf = self.cdf 244 242 cdf.title = self.tables['project'][0][0] 245 243 # Dimensions … … 298 296 Call this when the project is done. 299 297 """ 300 d = self.mgr.shutdown() 301 d.addCallback(lambda _: self.queue.shutdown()) 302 d.addCallback(lambda _: self.cdf.close()) 303 return d 304 298 if hasattr(self, 'mgr'): 299 d = self.mgr.shutdown() 300 d.addCallback(lambda _: self.queue.shutdown()) 301 d.addCallback(lambda _: self.cdf.close()) 302 return d 303 self.cdf.close() 304 projects/AsynCluster/tags/20080530-Integ_L_Estimate/svpmc/test/test_model.py
r202 r203 679 679 Ns, Ni, Nr = 200, 100, 100 680 680 simResults = {} 681 L = s.empty(Nr)681 Lx = s.empty(Nr) 682 682 k = 0 683 683 inlineVars = self._inlineVars(wiggle, eValue, Ni) 684 for hReal, hSim, Lx in self._likelihood(684 for hReal, hSim, Lxk in self._likelihood( 685 685 Ns, Nr, inlineVars, sameX=True, sameZ=False): 686 L [k] = Lx687 simResults[k] = (hReal, hSim, "Lx=%f" % (Lx ,))686 Lx[k] = Lxk 687 simResults[k] = (hReal, hSim, "Lx=%f" % (Lxk,)) 688 688 if VERBOSE: 689 print "%3d: %4.2f" % (k, Lx )689 print "%3d: %4.2f" % (k, Lxk) 690 690 k += 1 691 691 print "Lx: mean=%4.2f, sdev=%4.2f, range=%4.2f" % ( 692 L .mean(), L.std(), L.max()-L.min())692 Lx.mean(), Lx.std(), Lx.max()-Lx.min()) 693 693 if VERBOSE: 694 self._plot(L) 695 # Worst 696 plotArgs = [simResults[x] for x in L.argsort()[:4]] 694 self._plot(Lx) 695 # Best & Worst 696 I = Lx.argsort() 697 plotArgs = [simResults[x] for x in [I[-1], I[0]]] 697 698 fig = self._plot(*plotArgs) 698 fig.text(0.05, 0.95, "Worst", size='x-large') 699 # Best 700 plotArgs = [simResults[x] for x in L.argsort()[::-1][:4]] 701 fig = self._plot(*plotArgs) 702 fig.text(0.05, 0.95, "Best", size='x-large') 699 fig.text(0.05, 0.95, "Best & Worst", size='x-large') 703 700 704 701 def test_optimize_Ni(self): … … 710 707 Nr = 1000 711 708 wiggle = 1.0 712 eValue = 0.9 5709 eValue = 0.97 713 710 Ns = 1000 714 711 inlineVars = self._inlineVars(wiggle, eValue, 1) projects/AsynCluster/trunk/svpmc/params.py
r196 r203 347 347 Returns probability density of the supplied parameter value I{x}. 348 348 """ 349 if x.shape: 350 return self.distObj.pdf(x) 349 351 return float(self.distObj.pdf(x)) 350 352 projects/AsynCluster/trunk/svpmc/project.py
r201 r203 92 92 self.m = m 93 93 self.iteration = 0 94 #--- Use the NullQueue for debugging, thread queue normally -----------95 #self.queue = NullQueue()96 self.queue = asynqueue.ThreadQueue(1)97 #----------------------------------------------------------------------98 94 specDir = os.path.dirname(specFile) 99 95 self.tables = self._parseSpec(specFile) … … 114 110 # Parameters 115 111 paramTitles, dimensions = self._setupParams() 112 # Open the NetCDF file 113 self.cdf = NetCDF.NetCDFFile( 114 os.path.join(specDir, ncFile), ('w', 'r')[int(readOnly)]) 115 # If in read-only mode, we're done now 116 if readOnly: 117 return 118 # Write mode, write some initial stuff to the NetCDF file... 119 self._setupCDF(tsData, seriesTitles, paramTitles, dimensions) 120 for k, title in enumerate(seriesTitles): 121 obs = self.cdf.variables['observations'] 122 obs[k,:] = tsData[k,:].astype('f') 123 setattr(obs, "series_%02d" % (k,), title) 124 # ...and construct a queue and model manager for a simulation 125 self.queue = asynqueue.ThreadQueue(1) 116 126 self.mgr = model.ModelManager( 117 127 self, tsData, wiggle=self.wiggle, Ni=self.Ni) 118 # If not in read-only mode, setup the NetCDF file for writing119 if not readOnly:120 self._setupCDF(121 os.path.join(specDir, ncFile),122 tsData, seriesTitles, paramTitles, dimensions)123 # Write the observation data124 for k, title in enumerate(seriesTitles):125 obs = self.cdf.variables['observations']126 obs[k,:] = tsData[k,:].astype('f')127 setattr(obs, "series_%02d" % (k,), title)128 128 129 129 def _parseSpec(self, filePath): … … 236 236 return titles, dimensions 237 237 238 def _setupCDF(self, filePath, tsData, sTitles, pTitles, pDims): 239 """ 240 """ 241 cdf = NetCDF.NetCDFFile(filePath, 'w') 242 self.cdf = cdf 243 #self.cdf = CDF_Wrapper(cdf) 238 def _setupCDF(self, tsData, sTitles, pTitles, pDims): 239 """ 240 """ 241 cdf = self.cdf 244 242 cdf.title = self.tables['project'][0][0] 245 243 # Dimensions … … 298 296 Call this when the project is done. 299 297 """ 300 d = self.mgr.shutdown() 301 d.addCallback(lambda _: self.queue.shutdown()) 302 d.addCallback(lambda _: self.cdf.close()) 303 return d 304 298 if hasattr(self, 'mgr'): 299 d = self.mgr.shutdown() 300 d.addCallback(lambda _: self.queue.shutdown()) 301 d.addCallback(lambda _: self.cdf.close()) 302 return d 303 self.cdf.close() 304 projects/AsynCluster/trunk/svpmc/test/test_model.py
r202 r203 679 679 Ns, Ni, Nr = 200, 100, 100 680 680 simResults = {} 681 L = s.empty(Nr)681 Lx = s.empty(Nr) 682 682 k = 0 683 683 inlineVars = self._inlineVars(wiggle, eValue, Ni) 684 for hReal, hSim, Lx in self._likelihood(684 for hReal, hSim, Lxk in self._likelihood( 685 685 Ns, Nr, inlineVars, sameX=True, sameZ=False): 686 L [k] = Lx687 simResults[k] = (hReal, hSim, "Lx=%f" % (Lx ,))686 Lx[k] = Lxk 687 simResults[k] = (hReal, hSim, "Lx=%f" % (Lxk,)) 688 688 if VERBOSE: 689 print "%3d: %4.2f" % (k, Lx )689 print "%3d: %4.2f" % (k, Lxk) 690 690 k += 1 691 691 print "Lx: mean=%4.2f, sdev=%4.2f, range=%4.2f" % ( 692 L .mean(), L.std(), L.max()-L.min())692 Lx.mean(), Lx.std(), Lx.max()-Lx.min()) 693 693 if VERBOSE: 694 self._plot(L) 695 # Worst 696 plotArgs = [simResults[x] for x in L.argsort()[:4]] 694 self._plot(Lx) 695 # Best & Worst 696 I = Lx.argsort() 697 plotArgs = [simResults[x] for x in [I[-1], I[0]]] 697 698 fig = self._plot(*plotArgs) 698 fig.text(0.05, 0.95, "Worst", size='x-large') 699 # Best 700 plotArgs = [simResults[x] for x in L.argsort()[::-1][:4]] 701 fig = self._plot(*plotArgs) 702 fig.text(0.05, 0.95, "Best", size='x-large') 699 fig.text(0.05, 0.95, "Best & Worst", size='x-large') 703 700 704 701 def test_optimize_Ni(self): … … 710 707 Nr = 1000 711 708 wiggle = 1.0 712 eValue = 0.9 5709 eValue = 0.97 713 710 Ns = 1000 714 711 inlineVars = self._inlineVars(wiggle, eValue, 1)
