// Short description: Example subroutines in C for perfect simulation of an autologictic model // described in "Spatial-temporal modeling of forest gaps generated by colonization // from below- and above-ground bark beetle species" // by Zhu, Rasmussen, Moeller, Aukema, and Raffa in JASA 103: 162-177 (2008). // File name: perfect_simulation.cpp // Version: June 10, 2008 // Submitted to: JASA Software Archive // Written by Jakob G. Rasmussen // Documented by Jun Zhu // The code here can be freely used and redistributed for non-commercial purposes only. // HEADER void GibbsAuto(int NumSteps, vector & c, vector & d, int NumSites, vector & NumNeigh, short int ** & Neigh, double phi, vector & y); void PSAuto(vector & c, vector & d, int NumSites, vector & NumNeigh, short int ** & Neigh, double phi, vector & y); // FUNCTIONS // Notes: Randoms() generates a sample from uniform(0,1) // Notes: NumNeigh is a vector of # of neighbors at each site // Notes: Neigh contains the site ID of neighbors at each site // Gibbs steps for autologistic model void GibbsAuto(int NumSteps, vector & c, vector & d, int NumSites, vector & NumNeigh, short int ** & Neigh, double phi, vector & y) { int i, j, step; double p; int sum; for (step=0; step & c, vector & d, int NumSites, vector & NumNeigh, short int ** & Neigh, double phi, vector & y) { int step, t, i, j; int coalescence; int maxstep = 100; double seed0 = GetSeed(); vector seed = vector(maxstep); vector U = vector(NumSites); vector L = vector(NumSites); for (step=0; step(int)pow(2.0,step-1); t--){ GibbsAuto(1, c, d, NumSites, NumNeigh, Neigh, phi, U); } // Lower chain, Gibbs steps using new random numbers SetSeed(seed0); for (t=(int)pow(2.0,step); t>(int)pow(2.0,step-1); t--){ GibbsAuto(1, c, d, NumSites, NumNeigh, Neigh, phi, L); } // Saving seed - needed when returning to creating new random numbers seed0 = GetSeed(); // Gibbs steps reusing old random numbers for (j=step-1; j>=0; j--){ // Upper chain, reusing old random numbers SetSeed(seed[j]); for (t=(int)pow(2.0,j); t>(int)pow(2.0,j-1); t--){ GibbsAuto(1, c, d, NumSites, NumNeigh, Neigh, phi, U); } // Lower chain, reusing old random numbers SetSeed(seed[j]); for (t=(int)pow(2.0,j); t>(int)pow(2.0,j-1); t--){ GibbsAuto(1, c, d, NumSites, NumNeigh, Neigh, phi, L); } } // Checking whether chains have reached coalescence coalescence = 1; for (i=0; i