Changeset 182
- Timestamp:
- 05/15/08 13:35:24 (7 months ago)
- Files:
-
- projects/AsynCluster/trunk/svpmc/model.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/svpmc/model.c
r181 r182 111 111 // Log-likelihood of the current innovation, given h 112 112 double Lx; 113 // Linear probability density of the proposal, given h 114 double pProb; 113 115 // Multivariate normal proposal 114 116 double *z; … … 240 242 241 243 242 // Returns with zero only if the p roposals are close enough to finish243 // likelihood computation244 int notCloseEnough(struct sample *sp[ ], int n)244 // Returns with zero only if the post-first proposals are close enough to 245 // finish likelihood computation 246 int notCloseEnough(struct sample *sp[2][], int n) 245 247 { 246 248 int k; … … 249 251 double Lmax = -1E20; // Anything will be more positive 250 252 for(k=0; k<n; k++) { 251 L = sp[ k]->Lx;253 L = sp[1][k]->Lx; 252 254 if(L < Lmin) 253 255 Lmin = L; … … 270 272 // x Innovation values, [pn] array 271 273 // w Wiggle value for +/- proposals (float) 272 // n Number of proposals to try per sample (int)274 // n Number of proposals to try per time-series sample (int) 273 275 274 276 //--- Model parameters, direct --- … … 289 291 const int Ns = Nz[1]; 290 292 291 int k0, k1, k2, k3; 292 double zp, Lx, Lh; 293 294 struct sample sp[n]; 293 int notFirst; 294 int k0, k1, k2, k3, kp; 295 double zp; 296 double Lp = 0; 297 298 double *xk; 299 struct sample sp[2][n]; 295 300 struct parameters P; 296 301 py::tuple result(2); … … 302 307 303 308 // Initialize various arrays 304 x 0= (double *) calloc(sizeof(double), Nt);309 xk = (double *) calloc(sizeof(double), Nt); 305 310 for(k0=0; k0<n; k0++) { 306 sp[k0].h = (double *) calloc(sizeof(double), Nt); 307 sp[k0].x0 = (double *) calloc(sizeof(double), Nt); 308 sp[k0].x1 = (double *) calloc(sizeof(double), Nt); 311 for(k1=0; k1<n; k1++) { 312 sp[k0][k1].h = (double *) calloc(sizeof(double), Nt); 313 sp[k0][k1].x0 = (double *) calloc(sizeof(double), Nt); 314 sp[k0][k1].x1 = (double *) calloc(sizeof(double), Nt); 315 } 309 316 } 310 317 … … 314 321 // Simulate n sets of h samples from separate proposals on z until a 315 322 // substantially common-valued sample is reached 323 316 324 k1 = k0; 317 325 do { 318 // Generate the next set of sample structs for the proposals... 319 for(k2=0; k2<n, k2++) { 326 327 // Prepare current-innovation vector xk and a set of sample structs for 328 // proposals, one set for the first time-series sample and another set for 329 // any remaining time-series samples to be computed 330 for(k2=0; k2<Nt; k2++) 331 PA1(xk, k2) = X2(k2, k1); 332 if(k1 == k0) { 333 // First time-series sample 334 notFirst = 0; 335 } else if(k1 == k0+1) { 336 // Second time-series sample, copy h arrays from proposal structs for 337 // first time-series sample 338 notFirst = 1; 339 for(k2=0; k2<n; k2++) { 340 for(k3=0; k3<Nt; k3++) 341 SPA1(sp[1][k2], h, k3) = SPA1(sp[0][k2], h, k3); 342 } 343 } else { 344 // Third or later time-series sample 345 notFirst = 1; 346 } 347 348 // Make proposals on z in the set of sample structs... 349 for(k2=0; k2<n; k2++) { 320 350 for(k3=0; k3<Nt; k3++) { 321 351 zp = Z2(k3, k1) + uniform(-w, +w); 322 SPA1(sp[ k2], z, k3) = zp;323 sp[ k2].Lz = normLogDensity(zp);352 SPA1(sp[notFirst][k2], z, k3) = zp; 353 sp[notFirst][k2].Lz = normLogDensity(zp); 324 354 } 325 355 } 326 356 // ...compute their log-volatilities 327 for(k2=0; k2<n, k2++)328 logVol(&sp[k2], &P);329 // ...and the log-likelihood of the innovation for this sample, for each330 // proposal on z and its log-volatility331 for(k2=0; k2<Nt; k2++)332 PA1(x0, k2) = X2(k2, k1);333 357 for(k2=0; k2<n; k2++) 334 logLikelihood(&sp[k2], &P, x0); 335 336 // TODO 358 logVol(&sp[notFirst][k2], &P); 359 // ...and the log-likelihood of the innovation for this time-series sample, 360 // for each proposal on z and its log-volatility 361 for(k2=0; k2<n; k2++) 362 logLikelihood(&sp[notFirst][k2], &P, xk); 337 363 338 364 // Ready for the next time series sample, if we go that far 339 365 k1++; 340 366 341 } while (notCloseEnough(sp, n)); 342 343 // TODO: Importance sample proposal using total Lz+Lh 344 345 } 367 } while (k1==k0+1 || (k1<Ns && notCloseEnough(sp, n))); 368 369 // Importance sample proposal using pProb=exp(Lz+Lx) for each 370 kp = sample(sp, n); 371 Lp += log(sp[1][kp]->pProb); 372 373 } 374 375 // Save the multivariate log-volatility simulated for the last time-series 376 // sample 377 for(k0=0; k0<Nt; k0++) 378 H1(k0) = SPA1(sp[1] 346 379 347 380 // Free array memory 348 381 free(x0); 349 for(k0=0; k0<n; k0++) { 350 free(sp[k0].h); 351 free(sp[k0].x0); 352 free(sp[k0].x1); 382 for(k0=0; k0<2; k0++) { 383 for(k1=0; k1<n; k1++) { 384 free(sp[k0][k1].h); 385 free(sp[k0][k1].x0); 386 free(sp[k0][k1].x1); 387 } 353 388 } 354 389 355 390 // All done 356 result[0] = Lx; 357 result[1] = Lh; 391 result[0] = 0; 392 for(k0=0; k0<Nt; k0++) { 393 result[0] += sp[0][k0]->Lx; 394 result[1] = Lp; 358 395 return_val = result;
