Changeset 158
- Timestamp:
- 04/25/08 00:24:02 (7 months ago)
- Files:
-
- projects/AsynCluster/trunk/doc/gevolver (deleted)
- projects/AsynCluster/trunk/doc/svpmc (added)
- projects/AsynCluster/trunk/doc/svpmc/L_for_log-volatility_sim.pdf (added)
- projects/AsynCluster/trunk/svpmc/model.c (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/model.py (modified) (8 diffs)
- projects/AsynCluster/trunk/svpmc/params.py (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/test/test_model.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/svpmc/model.c
r157 r158 95 95 double norm, normp, L, Lp, dL; 96 96 97 // Seed the PRNG on the first run only 98 static int seeded = 0; 99 if (!seeded) { 100 int i; 101 int j = 0; 102 unsigned short seed[3]; 103 for(i=0;i<3;i++) { 104 do { 105 j++; 106 } while (j < (i+1)*10000); 107 seed[i] = clock()*clock() % 65537; 108 } 109 seed48(seed); 110 seeded = 1; 111 } 97 112 98 113 // for each multivariate sample... … … 113 128 for(k2=0; k2<M; k2++) { 114 129 // Generate a uniform, symmetric, random walk proposal 115 normp = norm + (double) wiggle * (drand48() - 0.5);130 normp = norm + (double) wiggle * (drand48() - 0.5); 116 131 // Do the ratio in log space 117 132 Lp = -0.5 * pow(normp, 2); projects/AsynCluster/trunk/svpmc/model.py
r157 r158 128 128 paramContainer.pLogLikelihood = -s.inf 129 129 up = pack.Unpacker(result) 130 paramContainer. pLogLikelihood= up()131 paramContainer. latent= up()130 paramContainer.L = up() 131 paramContainer.z = up() 132 132 133 133 def gotLikelihood(result): 134 paramContainer. pLogLikelihood= result[0]135 paramContainer. latent= result[1]134 paramContainer.L = result[0] 135 paramContainer.z = result[1] 136 136 137 137 if (remote or self.remoteMode) and not local: … … 150 150 Construct me with a 2-D array of time series values, one time series per 151 151 row. 152 152 153 153 Then you can call me with a C{p x 1} drift vector I{a} and a C{p x p} 154 154 VAR(1) cross-correlation matrix I{b}, to get a C{p x n} vector of residuals … … 191 191 _logU_index = -1 192 192 193 keyAttrs = {'tsList':None, 'Mv': 100, 'Mz':100}193 keyAttrs = {'tsList':None, 'Mv':50, 'Mz':50} 194 194 paramNames = params.PARAM_NAMES 195 195 … … 250 250 return cv 251 251 252 def decaySamples(self, x, tol): 253 """ 254 Returns the number of samples needed for the effect of an impulse to 255 the VAR(1) process having the supplied lag-1 correlation matrix I{x} to 256 decay below I{tol}. 257 """ 258 return s.ceil(s.log(tol) / s.log(x.diagonal())).max() 252 def decaySamples(self, tol): 253 """ 254 Returns as an int the number of samples needed for the effect of an 255 impulse to the log-volatility VAR(1) process to decay below I{tol}. 256 """ 257 if 'ds' not in self.cache: 258 p = self.e.shape[0] 259 # The impulse 260 x = s.column_stack([s.ones(p), s.zeros((p, self.n))]) 261 # Simulate the impulse response with a VAR object 262 self.cache['ds'] = VAR(x).forward(x, s.zeros(p), self.e).max(0) 263 I = s.less(self.cache['ds'], tol).nonzero()[0] 264 if len(I): 265 return I.min() 266 return self.n 259 267 260 268 def logUniform(self): … … 321 329 return L0, L, h[:,1:] 322 330 323 def hybridGibbs(self, z, x, sigma, N=100):331 def hybridGibbs(self, z, x, sigma, tol=1E-4): 324 332 """ 325 333 Does one iteration of a hybrid Gibbs sampler for the log-volatilities, … … 328 336 Returns the likelihood of the innovations in I{x} given the simulated 329 337 log-volatilities. Updates the IID normal variates in place. 338 339 The method will account for the effect of log-volatility proposals on 340 downstream samples. You can specify the maximum fractional effect to 341 account for with I{tol}. Smaller tolerances require more post-sample 342 log-volatility recomputations and hence more CPU time. 330 343 """ 331 344 L_total = 0 332 345 acceptances = 0 333 346 rv = self.rv 347 N = self.decaySamples(tol) 334 348 # Initialize volatility shocks from cross-correlation of the IID 335 349 # variates... … … 369 383 Call this method with a parameter object that defines: 370 384 371 1. a set of values for my parameters, and 372 373 2. a latent parameter consisting of an array of IID normal 374 variates to be used as a starting point for another simulation 375 draw of log volatilities, and 376 377 3. a 1-D array of log-likelihoods for each observation time given 378 the IID variates after cross-correlation and VAR(1). 385 1. A set of values for my parameters. 386 387 2. A latent parameter I{z} consisting of an array of IID normal 388 variates to be used as a starting point for another simulation 389 draw of log volatilities. 390 391 3. The log-likelihood I{L} of my observations given the parameters 392 and after this method has done a simulation draw of 393 log-volatilities conditional on those parameter values and 394 starting with the IID variates in I{z}. 379 395 380 396 A new 2-D array of log-volatilities will be drawn from a hybrid Gibbs … … 400 416 self.setParamSequence(paramContainer.paramSequence()) 401 417 # ...including the latent parameter of IID normal variates 402 z = paramContainer. latent.copy()418 z = paramContainer.z.copy() 403 419 # Innovations as residuals of the reversed VAR(1) 404 420 x = self.var.reverse(self.a, self.b) projects/AsynCluster/trunk/svpmc/params.py
r151 r158 239 239 """ 240 240 """ 241 keyAttrs = {' latent':[], 'pLogLikelihood':None, 'pLogProposal':None}241 keyAttrs = {'z':[], 'L':None} 242 242 paramNames = PARAM_NAMES 243 243 244 244 def __add__(self, other): 245 newVersion = self.__class__( latent=self.latent)245 newVersion = self.__class__(z=self.z) 246 246 newVersion.setParamSequence([ 247 247 getattr(self, name) + getattr(other, name) projects/AsynCluster/trunk/svpmc/test/test_model.py
r157 r158 288 288 if VERBOSE: 289 289 self.fig.add_subplot(111).plot(z1, z2) 290 291 def test_decaySamples(self): 292 x = s.zeros((5, 1000)) 293 x[2,0] = 1.0 # The impulse 294 var = model.VAR(x) 295 a = s.zeros(5) 296 modelObj = model.Model(tsList=util.TS_LIST) 297 for j in xrange(20): 298 # Keep all coefficients small enough to ensure stability 299 b = modelObj.e = 0.3*s.rand(5,5) 300 y = var.forward(x, a, b) 301 for tol in s.logspace(-5, -1, 10): 302 N = modelObj.decaySamples(tol) 303 print tol, N 304 failed = abs(y[:,N].max()) > tol 305 if (N > 100 or failed) and VERBOSE: 306 N_plot = min([3*N, 30]) 307 sp = self.fig.add_subplot(111) 308 sp.semilogy(y[:,:N_plot].transpose()) 309 sp.axvline(N) 310 sp.axhline(tol) 311 self.failIf( 312 failed, 313 "Estimate of %d samples " % N +\ 314 "to decay within %f tolerance was insufficient" % tol) 290 315 291 316 … … 507 532 508 533 509 class Test_Model_hybridGibbs(Model_BC_Mixin, Multivariate_BaseCase):510 N = 10000511 corr = 0.0512 params = {513 'a': [0],514 'b': [[0]],515 'c': [],516 'd': [0.01],517 'e': [[0.0]],518 'f': [],519 'g': [corr],520 }521 522 523 534 class Test_Model_likelihood(Model_BC_Mixin, Multivariate_BaseCase): 524 N = 500535 N = 3000 525 536 corr = 0.0 526 537 params = {
