Changeset 175
- Timestamp:
- 05/08/08 23:30:51 (7 months ago)
- Files:
-
- projects/AsynCluster/trunk/asyncluster/master/control.py (modified) (1 diff)
- projects/AsynCluster/trunk/asyncluster/ndm/client.py (modified) (1 diff)
- projects/AsynCluster/trunk/asyncluster/ndm/node.py (modified) (1 diff)
- projects/AsynCluster/trunk/console (modified) (1 diff)
- projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf (modified) (1 diff)
- projects/AsynCluster/trunk/doc/svpmc/example/sv_pmc.py (modified) (5 diffs)
- projects/AsynCluster/trunk/svpmc/model.py (modified) (10 diffs)
- projects/AsynCluster/trunk/svpmc/pmc.py (modified) (5 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_model.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_pmc.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/test/test_weave.py (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/weave.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/asyncluster/master/control.py
r128 r175 343 343 return d 344 344 345 def remote_wall(self, message): 346 """ 347 """ 348 return self.ctl.allRemote('message', message) 349 345 350 def remote_registerClasses(self, *args): 346 351 """ projects/AsynCluster/trunk/asyncluster/ndm/client.py
r127 r175 114 114 def remote_message(self, message): 115 115 """ 116 Displays a pop-up message for the current user.117 """ 118 pass116 Displays a pop-up message on the session window, if one is present. 117 """ 118 self.main.message(message) 119 119 120 120 @checkTrust projects/AsynCluster/trunk/asyncluster/ndm/node.py
r127 r175 123 123 return defer.succeed(None) 124 124 125 def message(self, msg): 126 """ 127 If there is a session underway, displays the message on it. 128 """ 129 if hasattr(self, 'sessionWindow'): 130 self.sessionWindow.status(msg) 131 125 132 126 133 def run(): projects/AsynCluster/trunk/console
r127 r175 108 108 if cmd == 'user': 109 109 cmd = 'userAction' 110 elif cmd == 'wall': 111 args = (" ".join(args),) 110 112 elif cmd == 'resetup': 111 113 srcDir = args[0] projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf
r174 r175 27 27 Jumps 28 28 ------------------------------------------------------------------------------- 29 0. 5, 0.1, 0.02, 0.00429 0.25, 0.05, 0.01, 0.002 30 30 31 31 projects/AsynCluster/trunk/doc/svpmc/example/sv_pmc.py
r171 r175 39 39 p = OptionParser(usage=__doc__) 40 40 p.add_option( 41 "-s", "--sigma",42 dest="sigma", action="store", type="float",43 help="Jump deviation for the log-volatility simulations")44 p.add_option(45 41 "-i", "--N_iter", 46 42 dest="N_iter", action="store", type="int", … … 62 58 dest="socket", action="store", 63 59 help="Path of UNIX-domain socket for AsynCluster master server") 64 p.set_defaults( 65 sigma=0.5, N_iter=100, N_members=10000, 60 p.add_option( 61 "-p", "--profile", 62 dest="profile", action="store_true", 63 help="Run with profiling") 64 65 p.set_defaults(N_iter=100, N_members=10000, 66 66 ncFileName="svpmc.nc", socket="/tmp/.ndm") 67 67 return p.parse_args() … … 80 80 81 81 82 def run(pmcObj, N_iter, V=None):82 def run(pmcObj, N_iter, profile=False): 83 83 """ 84 84 Have this called when the reactor starts. … … 88 88 print "\n%s\n" % ("-"*79,) 89 89 reactor.stop() 90 91 d = pmcObj.run(N_iter, V) 92 d.addCallbacks(lambda _: reactor.stop(), oops) 93 return d 90 91 def reallyRun(): 92 d = pmcObj.run(N_iter) 93 d.addCallbacks(lambda _: reactor.stop(), oops) 94 95 if profile: 96 print "Running with profiler..." 97 import pstats, cProfile as profile 98 prof = profile.Profile() 99 try: 100 prof.runcall(reallyRun) 101 prof.dump_stats("profile.data") 102 except SystemExit: 103 pass 104 stats = pstats.Stats("profile.data") 105 stats.sort_stats("cumulative").print_stats() 106 else: 107 reallyRun() 94 108 95 109 … … 106 120 specFile, opts.ncFileName, opts.N_members) 107 121 pmcObj = pmc.PMC(projectManager, socket) 108 reactor.callWhenRunning(run, pmcObj, opts.N_iter )122 reactor.callWhenRunning(run, pmcObj, opts.N_iter, profile=opts.profile) 109 123 reactor.run() 110 124 projects/AsynCluster/trunk/svpmc/model.py
r173 r175 123 123 # An error from the remote likelihood method call is treated as 124 124 # infinitely low likelihood 125 result = [-s.inf, [], []]125 result = (-s.inf, [], [], 0) 126 126 elif isinstance(result, str): 127 127 # Unpack string result from remote call 128 128 result = list(pack.Unpacker(result)) 129 for k, name in enumerate(('Lx', 'h', 'z' )):129 for k, name in enumerate(('Lx', 'h', 'z', 'acceptanceRate')): 130 130 setattr(paramContainer, name, result[k]) 131 131 return paramContainer … … 184 184 _logU_index = -1 185 185 186 keyAttrs = {'y':None, 'Mv': 20, 'Mz':30}186 keyAttrs = {'y':None, 'Mv':40, 'Mz':30} 187 187 188 188 #--- Properties ----------------------------------------------------------- … … 305 305 return L0, L, h[:,1:] 306 306 307 def hybridGibbs(self, z, x, rv, sigma, tol=1E- 4):307 def hybridGibbs(self, z, x, rv, sigma, tol=1E-3): 308 308 """ 309 309 Does one iteration of a hybrid Gibbs sampler for the log-volatilities, … … 312 312 Updates the IID normal variates in place. Returns the likelihood of the 313 313 innovations in I{x}, given the simulated log-volatilities, along with 314 the last sample's multivariate log-volatility value. 314 the last sample's multivariate log-volatility value and the fractional 315 acceptance rate of the MCMC steps. 315 316 316 317 The method will account for the effect of log-volatility proposals on … … 320 321 """ 321 322 L_total = 0 323 acceptances = 0 322 324 N = self.decaySamples(tol) 323 325 # Initialize volatility shocks from cross-correlation of the IID … … 345 347 z[:,k] = zp[:,k] 346 348 v[:,k] = vp[:,k] 349 acceptances += 1 347 350 # Update stuff for the next sample 348 351 h0 = LV[2][:,0] … … 350 353 # If the likelihood at this point is so bad that the parameter is 351 354 # guaranteed not to be resampled, just bail out now and save time 352 if L_total < -1E 20:353 return -s.inf, s.zeros_like(h0) 354 return L_total, h0 355 if L_total < -1E6: 356 return -s.inf, s.zeros_like(h0), 0.0 357 return L_total, h0, float(acceptances)/self.n 355 358 356 359 … … 383 386 If a L{linalg.LinAlgError} is raised due to an invalid correlation 384 387 matrix parameter, the method returns with no result. Otherwise, it 385 returns a listcontaining the following values reached at the end of388 returns a tuple containing the following values reached at the end of 386 389 the log-volatility simulation: 387 390 … … 391 394 392 395 3. The IID normal variates underlying the log-volatilities. 396 397 4. The mean acceptance rate of the Metropolis-Hastings proposals. 393 398 394 399 The returned likelihood does not consider the prior probability of the … … 414 419 # one along with the state of the IID variate vector at that point, 415 420 # bailing out with no result if invalid parameter raises exception 421 acceptRates = [] 416 422 try: 417 423 for k in xrange(self.Mv-1): 418 self.hybridGibbs(z, x, rv, sigma)424 acceptRates.append(self.hybridGibbs(z, x, rv, sigma)[2]) 419 425 except linalg.LinAlgError: 420 426 return 421 return list(self.hybridGibbs(z, x, rv, sigma)) + [z] 422 427 L, h0, thisRate = self.hybridGibbs(z, x, rv, sigma) 428 return L, h0, z, sum(acceptRates + [thisRate])/self.Mv 429 projects/AsynCluster/trunk/svpmc/pmc.py
r174 r175 140 140 141 141 """ 142 chunkSize = 100143 initialSigma = 0. 05142 chunkSize = 500 143 initialSigma = 0.5 144 144 145 145 def __init__(self, projectManager, socket=None): … … 179 179 paramContainer, vIndex, self.allocator.W) 180 180 return XP 181 181 182 182 @defer.deferredGenerator 183 183 def weightedProposals(self, X, vIndex): … … 200 200 if s.isfinite(paramContainer.Lx): 201 201 L = paramContainer.Lx + paramContainer.Lp - paramContainer.Lj 202 print "-+"[int(L>-1000)], 202 info = " ".join([ 203 "%+12.2f" % (getattr(paramContainer, x),) 204 for x in ("Lx", "Lp", "Lj")]) 205 info += "\t\t%02d%%" % (100*paramContainer.acceptanceRate,) 203 206 else: 204 print "|",207 info = "" 205 208 L = -s.inf 206 sys.stdout.flush()209 print "%6.4f\t%s" % (self.pm.V[vIndex], info) 207 210 return L 208 211 … … 253 256 % (N_iter, N_members) 254 257 for i in xrange(N_iter): 255 print "\n%03d:" % (i+1,), 256 self.allocator.setAllocations(self.R) 258 print "\n%03d\n%s" % (i+1, "-"*79) 257 259 dList, resultList = [], [] 258 260 for vIndex, I, d in self.allocator.assembler(resultList): … … 273 275 I1 = self.resampler(W[I0], N_members) 274 276 X = XP[I0][I1] 275 self.allocator.updateAllocations(I )277 self.allocator.updateAllocations(I0[I1]) 276 278 else: 277 279 # Nothing in the proposed population had any plausibility projects/AsynCluster/trunk/svpmc/test/test_model.py
r168 r175 47 47 h = s.zeros(self.p) 48 48 z = s.zeros(self.n) 49 return [likelihoodFunc(paramContainer, self.drift), h, z]49 return likelihoodFunc(paramContainer, self.drift), h, z, 0.44 50 50 51 51 … … 82 82 self.JobClient = model.jobs.JobClient 83 83 model.jobs.JobClient = Mock_Client 84 # TODO: Need mock project manager 84 85 self.mgr = model.ModelManager( 85 86 util.PARAM_NAMES, s.zeros((self.model.p, self.model.n))) projects/AsynCluster/trunk/svpmc/test/test_pmc.py
r174 r175 37 37 self.N = 100 38 38 self.V = [0.1, 0.01, 0.001] 39 self.allocator = pmc.Allocator(self.N, len(self.V)) 39 self.D = len(self.V) 40 self.allocator = pmc.Allocator(self.N, self.D) 40 41 41 42 def test_init(self): … … 72 73 dList.append(d) 73 74 return defer.DeferredList(dList).addCallback(check) 75 76 @defer.deferredGenerator 77 def test_updateAllocations_lopsided(self): 78 for favoredBin in xrange(self.D): 79 dList, resultList = [], [] 80 for k, I, d in self.allocator.assembler(resultList): 81 if k == favoredBin: 82 magic_I = I[0] 83 d.addCallback(lambda _: (s.zeros_like(I),)) 84 dList.append(d) 85 wfd = defer.waitForDeferred(defer.DeferredList(dList)) 86 yield wfd 87 wfd.getResult() 88 # Check that the favored bin is favored in the new allocations 89 self.allocator.updateAllocations(magic_I * s.ones(self.D)) 90 self.failUnlessEqual(self.allocator.R[favoredBin], 96) 74 91 75 92 projects/AsynCluster/trunk/svpmc/test/test_weave.py
r164 r175 56 56 57 57 def __call__(self, x): 58 return self.inline('x')[0] 58 self.inline('x') 59 return x[0] 59 60 60 61 def foo(self, x): 61 return self.inline('x', 'y', x=2*s.array([x]))[0] 62 x = 2*s.array([x]) 63 self.inline('x', 'y') 64 return x[0] 62 65 63 66 projects/AsynCluster/trunk/svpmc/weave.py
r171 r175 90 90 the variable I{return_val}, is returned. 91 91 """ 92 local_dict = kw 92 93 frame = inspect.currentframe() 93 94 methodName = frame.f_back.f_code.co_name … … 96 97 for varName in args: 97 98 if varName in kw: 98 value = kw[varName]99 elif varName in callerVarNames:100 value= frame.f_back.f_locals[varName]99 continue 100 if varName in callerVarNames: 101 local_dict[varName] = frame.f_back.f_locals[varName] 101 102 elif hasattr(self, varName): 102 value= getattr(self, varName)103 local_dict[varName] = getattr(self, varName) 103 104 else: 104 105 raise AttributeError( 105 106 "Variable '%s' not defined " % varName +\ 106 107 "in keyword, caller, or instance namespace") 107 exec "%s = value" % varName108 108 finally: 109 109 # Avoid cycle problems, per Python Library Reference 3.11.4 110 110 del frame 111 111 return inline( 112 self.code[methodName], 113 args,extra_compile_args=['-w'], support_code=self.code['support'])112 self.code[methodName], args, local_dict, 113 extra_compile_args=['-w'], support_code=self.code['support']) 114 114 115 115
