# Inference scratch

### Current inference algorithm

* `For` $$m=1 \dots M$$, where $$M$$is the number of parallel MCMC chains (also known as *slots*)
  * Generate initial state
    * Generate an initial set of parameters $$\Theta\_{m,0}$$, and copy this to both the global ($$\Theta^G\_{m,0}$$) and chimeric ($$\Theta^C\_{m,0}$$) parameter chain (sequence ;
    * Generate an initial epidemic trajectory $$Z(\Theta\_{m,0})$$
    * Calculate and record the initial likelihood for each subpopulation, $$\mathcal{L\_i}(D\_i|Z\_i(\Theta\_{m,0}))$ ;
  * `For` $$k= 1 ... K$$ where $$K$$ is the length of the MCMC chain, add to the sequence of parameter values :
    * Generate a proposed set of parameters $$\Theta^\*$$from the current chimeric parameters using the proposal distribution $$g(\Theta^\*|\Theta^C\_{m,k-1})$ ;
    * Generate an epidemic trajectory with these proposed parameters, $$Z(\Theta^\*)$$
    * Calculate the likelihood of the data given the proposed parameters for each subpopulation, $$\mathcal{L}\_i(D\_i|Z\_i(\Theta^\*))$$
    * Calculate the overall likelihood with the proposed parameters, $$\mathcal{L}(D|Z(\Theta^\*))$$
    * Make "global" decision about proposed parameters
      * Generate a uniform random number $$u^G \sim \mathcal{U}\[0,1]$$
      * Calculate the overall likelihood with the current global parameters, $$\mathcal{L}(D|Z(\Theta^G\_{m,k-1}))$$
      * Calculate the acceptance ratio $$\alpha^G=\min \left(1, \frac{\mathcal{L}(D|Z(\Theta^*)) p(\Theta^*) }{\mathcal{L}(D|Z(\Theta^G\_{m,k-1})) p(\Theta^G\_{m,k-1}) } \right)$$​
      * `If` $$\alpha^G > u^G$$: ACCEPT the proposed parameters to the global and chimeric parameter chains
        * Set $$\Theta^G\_{m,k} =$$$$\Theta^\*$$
        * Set $$\Theta\_{m,k}^C=\Theta^\*$$
        * Update the recorded subpopulation-specific likelihood values (chimeric and global) with the likelihoods calculated using the proposed parameter ;
      * `Else`: REJECT the proposed parameters for the global chain and make subpopulation-specific decisions for the chimeric chain
        * Set $$\Theta^G\_{m,k} = \Theta^G\_{m,k-1}$$
        * Make "chimeric" decision:
          * `For` $$i = 1 \dots N$$
            * Generate a uniform random number $$u\_i^C \sim \mathcal{U}\[0,1]$$
            * Calculate the acceptance ratio $$\alpha\_i^C=\frac{\mathcal{L}*i(D\_i|Z\_i(\Theta^*)) p(\Theta^*) }{\mathcal{L}i(D\_i|Z\_i(\Theta^C*{m,k-1})) p(\Theta\_{m,k-1}) }$$
            * `If` $$\alpha\_i^C > u\_i^C$$: ACCEPT the proposed parameters to the chimeric parameter chain for this location
              * Set $$\Theta\_{m,k,i}^C = \Theta^\*\_{i}$$
              * Update the recorded chimeric likelihood value for subpopulation $$i$$ to that calculated with the proposed parameter​
            * `Else`: REJECT the proposed parameters for the chimeric parameter chain for this location
              * Set $$\Theta\_{m,k,i}^C=\Theta\_{m,k-1,i}$$​
            * \`End if ;
          * `End for` $$N$$subpopulations
        * End making chimeric decisions
      * `End if`
    * End making global decision
  * `End for` $$K$$ iterations of each MCMC chain
* `End for` $$M$$ parallel MCMC chains
* Collect the final global parameter values for each parallel chain $$\theta\_m = {\Theta^G\_{m,K}}\_m$$

## Making chimeric decision first
