Changeset 196
- Timestamp:
- 05/24/08 00:05:18 (8 months ago)
- Files:
-
- projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/model.c (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/model.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/params.py (modified) (7 diffs)
- projects/AsynCluster/trunk/svpmc/project.py (modified) (4 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_params.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf
r175 r196 25 25 26 26 27 Jumps 27 Variable Value Description 28 28 ------------------------------------------------------------------------------- 29 0.25, 0.05, 0.01, 0.002 30 29 D 6 Number of jump deviations in the D-kernel sim 30 Df 0.2 Fractional value of each dev from prev (or 1.0) 31 wiggle 0.5 Range +/- of uniform random walks on z for h sim 32 N1 30 Proposals on z for each importance-sampling of h 33 N2 20 Hybrid-Gibbs iterations for h sim 31 34 32 35 projects/AsynCluster/trunk/svpmc/model.c
r192 r196 323 323 324 324 // The number of time series making up a single multivariate sample 325 const int Nt = N z[0];325 const int Nt = Nx[0]; 326 326 // The number of multivariate samples 327 const int Ns = N z[1];327 const int Ns = Nx[1]; 328 328 329 329 int ki, k0, k1, k2, k3; … … 371 371 372 372 // The "previous" log-volatility of the first time-series sample is set to 373 // the log-volatility offset. 374 // 375 // TODO: Treat it as another latent parameter and simulate its value 373 // the log-volatility offset plus a simulated, latent-parameter value. 374 // TODO 376 375 for(k0=0; k0<np; k0++) { 377 376 for(k1=0; k1<Nt; k1++) projects/AsynCluster/trunk/svpmc/model.py
r195 r196 64 64 ########################################################################### 65 65 """ 66 def __init__(self, projectManager, y): 67 self.modelObj = Model(paramNames=projectManager.paramNames, y=y) 66 def __init__(self, projectManager, y, **kw): 67 kw['y'] = y 68 kw['paramNames'] = projectManager.paramNames 69 self.modelObj = Model(**kw) 68 70 self.queue = projectManager.queue 69 71 … … 307 309 sc[:,1] = s.log(s.sqrt(2*s.pi) * sc[:,0]) 308 310 311 print self.wiggle, self.N1, self.N2, self.Ne 309 312 # Run the hybrid Gibbs rounds, returning the likelihood from the last 310 313 # one along with the state of the IID variate vector at that point. projects/AsynCluster/trunk/svpmc/params.py
r195 r196 268 268 the only pertinent parameter is the constant, unvarying I{loc}. 269 269 270 @ivar V: A 1-D array of all the jump deviations that are used in271 computing probability densities of jumps272 273 270 """ 274 271 keyAttrs = {'V':None} … … 322 319 323 320 def _newDraw(self): 321 D = len(self.V) 324 322 rJumps = self.rdsObj.rvs(self.N_draw) 325 V_inv = 1.0 / self.V 326 pJumps = self.rdsObj.pdf( 327 s.kron(rJumps, V_inv).reshape(self.N_draw, len(V_inv))) 328 pJumps *= V_inv 323 # Right side 324 V = self.V[::-1] 325 pJumpsL = self.rdsObj.pdf( 326 s.kron(rJumps, V).reshape(self.N_draw, D)) 327 # Left side 328 V = 1.0/V[:0:-1] 329 pJumpsR = self.rdsObj.pdf( 330 s.kron(rJumps, V).reshape(self.N_draw, D-1)) 331 pJumps = s.column_stack([pJumpsL, pJumpsR]) 329 332 return {'k':-1, 'N':self.N_draw, 'R':rJumps, 'P':pJumps} 330 333 331 334 def _rJumpDraw(self): 332 335 if 'draw' not in self.cache: … … 376 379 each possible jump deviation in the I{p} array of the result. 377 380 """ 381 D = len(self.V) 378 382 for k in xrange(self.N_attempts): 379 jumpValue, pV= self._rJumpDraw()383 jumpValue, P = self._rJumpDraw() 380 384 jumpValue *= self.V[vIndex] 385 pV = P[D-vIndex-1:2*D-vIndex-1] / self.V 381 386 x = startValue + jumpValue 382 387 px = self.pdf(x) … … 384 389 break 385 390 else: 391 print "Oops" 386 392 # Could't come up with a jump from this point, fall back to an 387 393 # independent draw from the prior distribution 388 394 x = self.rvs() 389 395 px = self.pdf(x) 390 pV = px * s.ones _like(self.V)396 pV = px * s.ones(D) 391 397 return self.Jump(x, px, pV) 392 398 … … 402 408 403 409 @ivar paramNames: A list of the parameter names in sequence. 404 405 @ivar V: A 1-D array of all the jump deviations that are used in406 computing probability densities of jumps made via my prior objects.407 410 408 411 """ 409 412 typeChecking = True 410 413 411 def __init__(self, p, n , V):414 def __init__(self, p, n): 412 415 self.p = p 413 416 self.n = n 414 self.V = V415 417 416 418 def priorArray(self, paramName, *shape): … … 455 457 I{vWeights} array. 456 458 """ 457 if vIndex < 0 or vIndex >= len(self.V):458 raise ValueError("Invalid jump deviation index %d" % vIndex)459 if getattr(vWeights, 'shape', 0) != self.V.shape:460 raise ValueError(461 "Must supply a mixture weight array with one element per "+\462 "jump deviation")463 459 if self.typeChecking and \ 464 460 not isinstance(paramContainer, ParameterContainer): … … 479 475 jumpsFA = priorsFA.call('rJump', startValues, vIndex) 480 476 Lp += s.log(jumpsFA.px).sum() 481 for k in xrange(len( self.V)):477 for k in xrange(len(vWeights)): 482 478 pJumps[k] *= jumpsFA.pV.call('item', k).prod() 483 479 setattr(newVersion, name, jumpsFA.x) projects/AsynCluster/trunk/svpmc/project.py
r193 r196 67 67 sequences used in the project. 68 68 69 @ivar V: A 1-D array of thejump deviations used by the D-kernel PMC69 @ivar D: The number of jump deviations used by the D-kernel PMC 70 70 inference engine that will be running the project. 71 72 @ivar Df: The value of a given jump deviation as a fraction of the previous 73 one (the first value is 1.0) 71 74 72 75 @ivar priors: An instance of L{params.PriorContainer} constructed in … … 87 90 self.iteration = 0 88 91 #--- Use the NullQueue for debugging, thread queue normally ----------- 89 self.queue = NullQueue()90 #self.queue = asynqueue.ThreadQueue(1)92 #self.queue = NullQueue() 93 self.queue = asynqueue.ThreadQueue(1) 91 94 #---------------------------------------------------------------------- 92 95 specDir = os.path.dirname(specFile) 93 96 self.tables = self._parseSpec(specFile) 94 # Jump Deviations 95 self.V = s.array([ 96 float(x) for x in self.tables['jumps'][0][0].split(',')]) 97 # Set some simulation parameters 98 for name, value, null in self.tables['variable']: 99 if "." in value: 100 value = float(value) 101 else: 102 value = int(value) 103 setattr(self, name, value) 104 V = [1.0] 105 for k in xrange(self.D-1): 106 V.append(self.Df * V[-1]) 107 self.V = s.array(V) 97 108 # Observations 98 109 tsData, seriesTitles = self._setupTimeSeries(specDir) … … 100 111 # Parameters 101 112 paramTitles, dimensions = self._setupParams() 102 self.mgr = model.ModelManager(self, tsData) 113 self.mgr = model.ModelManager( 114 self, tsData, wiggle=self.wiggle, N1=self.N1, N2=self.N2) 103 115 self._setupCDF( 104 116 os.path.join(specDir, ncFileName), … … 206 218 titles, dimensions = [], [] 207 219 xcorrs = self.xcorrs = int(0.5*(self.p**2 + self.p)) 208 self.priors = params.PriorContainer(self.p, self.n , self.V)220 self.priors = params.PriorContainer(self.p, self.n) 209 221 for name, dims, title in self.tables['parameter']: 210 222 shape = parseDims(dims) projects/AsynCluster/trunk/svpmc/test/test_params.py
r194 r196 221 221 222 222 class Test_Prior(util.TestCase): 223 def setUp(self): 224 self.V = s.array([1.0, 0.1, 0.01]) 225 self.p = params.Prior(dname='norm', loc=1, scale=1, D=3, Df=0.1) 226 self.p.N_draw = 10 227 self.pdf = self.p.rdsObj.pdf 228 223 229 def test_uniformPrior(self): 224 230 p = params.Prior(dname='uniform', loc=1, scale=2) … … 229 235 self.failUnless(s.equal(s.array([p.pdf(x) for x in X]), 0.5).all()) 230 236 237 def _checkDraw(self, rv, pV): 238 pdf = self.p.rdsObj.pdf 239 for k, v in enumerate(self.V): 240 self.failUnlessAlmostEqual(pdf(rv/v)/v, pV[k], 5) 241 242 def test_newDraw(self): 243 draw = self.p._newDraw() 244 k = draw['k'] 245 while k < draw['N']: 246 self._checkDraw(draw['R'][k], draw['P'][k]) 247 k += 1 248 249 def test_rJumpDraw(self): 250 for k in xrange(103): 251 rv, pV = self.p._rJumpDraw() 252 self._checkDraw(rv, pV) 253 254 def test_rJump(self): 255 for startValue in s.linspace(-1, +3, 10): 256 for vIndex, v in enumerate(self.V): 257 jumpObj = self.p.rJump(startValue, vIndex) 258 print "\n", vIndex, v 259 jumpValue = jumpObj.x - startValue 260 rv = jumpValue / v 261 print jumpValue, rv, jumpObj.pV[vIndex] 262 self.failUnlessAlmostEqual(self.pdf(rv)/v, jumpObj.pV[vIndex]) 263 print "OK" 264 231 265 232 266 class Test_PriorContainer(util.TestCase):
