Skip to content

Commit bd8d492

Browse files
added Bayes to README
1 parent 55fa727 commit bd8d492

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,49 @@ We can also add psuedo-random realisations of this process (conditional on the d
138138

139139
Rather than Maximum Likelihood, we could specify priors on the (hyper-)parameters of the ACV, and peform Bayesian inference. In general, to do this we need an MCMC tool to sample from the posterior. Here I use [tonic](https://github.com/svdataman/tonic).
140140

141+
```R
142+
# define the 'priors' for the parameter values
143+
logPrior <- function(theta) {
144+
mu.d <- dnorm(theta[1], sd = 5, log = TRUE)
145+
nu.d <- dlnorm(theta[2], meanlog = 0, sdlog = 1, log = TRUE)
146+
A.d <- dlnorm(theta[3], meanlog = 0, sdlog = 1, log = TRUE)
147+
l.d <- dlnorm(theta[4], meanlog = 0, sdlog = 1, log = TRUE)
148+
return(mu.d + nu.d + A.d + l.d)
149+
}
150+
```
151+
152+
Now we use the GW sampler to sample from the posterior
153+
154+
```R
155+
# Use gw.mcmc to generate parameter samples
156+
chain <- tonic::gw_sampler(gp_logPosterior, theta.0 = theta,
157+
acv.model = acv, logPrior = logPrior,
158+
dat = dat, burn.in = 1e4,
159+
nsteps = 20e4,
160+
chatter = 1, thin = 10)
161+
```
162+
163+
This takes a minute or two to cook. It should produce 2,000 samples after `thinning' by a factor 10 (it generates 20,000 samples but keeps only 1 in 10).
164+
First, we inspect the traces and autocorrelation of the chains.
165+
166+
```R
167+
# plot MCMC diagnostics
168+
tonic::chain_diagnosis(chain)
169+
```
170+
171+
Now we can visualise the posterior:
172+
173+
```R
174+
# name the parameters
175+
colnames(chain$theta) <- c("mu", "nu", "A", "l")
176+
177+
# plot scatter diagrams
178+
tonic::contour_matrix(chain$theta, prob.levels = c(1,2,3), sigma = TRUE,
179+
prob1d = 0)
180+
```
181+
182+
![bayesian](figs/fig5.png)
183+
141184
## References
142185

143186
For more on GPs, the best reference is:

figs/fig5.png

12.9 KB
Loading

tests/gp_test1.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ logPrior <- function(theta) {
100100
}
101101

102102
# Use gw.mcmc to generate parameter samples
103-
chain <- tonic::gw_sampler(gp_logPosterior, theta.0 = theta, acv.model = acv,
104-
dat = dat, burn.in = 1e4, nwalkers = 50, nsteps = 1e4, chatter = 1)
103+
chain <- tonic::mh_sampler(gp_logPosterior, theta.0 = theta,
104+
acv.model = acv, logPrior = logPrior,
105+
dat = dat, burn.in = 1e4,
106+
nchains = 5, nsteps = 5e4,
107+
adapt = TRUE, chatter = 1)
105108

106109
# name the parameters
107110
colnames(chain$theta) <- c("mu", "nu", "A", "l")

0 commit comments

Comments
 (0)