Skip to content

Instantly share code, notes, and snippets.

View mooresm's full-sized avatar

Matt mooresm

View GitHub Profile
@mooresm
mooresm / BoA.Rmd
Last active March 12, 2023 14:10
BayesComp 2023 conference programme booklet
---
title: "BayesComp 2023 abstract booklet"
format: pdf
editor: visual
params:
echo: FALSE
---
```{r, echo=FALSE, eval=TRUE, results='hide'}
library(stringr)
@mooresm
mooresm / EthanolDemo.R
Created March 1, 2023 10:33
A demo using the R packages 'serrsBayes' and 'hyperSpec' to read and analyse a Raman spectrum of ethanol.
library(serrsBayes)
### read the data ----
library(hyperSpec)
etoh <- read.spc("EtOH.spc")
plot(etoh)
wv <- wl(etoh)
i1 <- which.min(wv < 300)
i2 <- which.min(wv < 1800)
@mooresm
mooresm / vaxAus.R
Created April 20, 2022 08:44
Statistical Model of Covid-19 Vaccine Rollout in Australia, Feb to July 2021
# Data downloaded from CovidBaseAU and Our World in Data,
# https://github.com/owid/covid-19-data/
vax_aus <- read.csv("Australia.txt")
vax_aus$Date <- as.Date(vax_aus$date)
ix1 <- which.max(vax_aus$Date > "2021-05-02") - 1
idx <- which.max(vax_aus$Date > "2021-07-02") - 1
ix2 <- which.max(vax_aus$Date > "2021-09-02") - 1
ix3 <- which.max(vax_aus$Date > "2021-11-02") - 1
ix4 <- which.max(vax_aus$Date > "2021-10-02") - 1
vax_aus$Date[idx]
# Piecewise basis functions: splines
# ch. 5 of ESL: Hastie, Tibshirani & Friedman (2009)
# one dimension (p=1)
x <- seq(-0.5,1,by=0.05)[-11]
y <- 2 - 5*exp(-8 * x^2) + rnorm(length(x), sd=0.5)
plot(x,y, ylim=range(-3,2,y))
curve(2 - 5*exp(-8 * x^2), from=-0.5, to=1, n=length(x), col=4, add=T)
# kNN regression: piecewise constant
@mooresm
mooresm / TruncSigmoid.stan
Created June 12, 2017 18:02
Stan model for noisy observations of a sigmoidal function, where the noise is truncated to a finite interval
functions {
vector ft(vector t, real r, real y0, real tC, real L) {
vector[num_elements(t)] mu;
vector[num_elements(t)] exprt;
exprt = exp(r*(t-tC));
mu = y0*L*exprt ./ (L + y0*(exprt - 1));
return mu;
}
vector dfdt(vector t, real r, real tC, real L) {
@mooresm
mooresm / ABC_intro2.R
Created June 12, 2017 07:02
Approximate Bayesian Computation (ABC) for the Ising/Potts model
set.seed(13)
y <- rnorm(n=5, mean=1, sd=2/3)
n <- length(y)
s_sq <- sum((y-1)^2)/n
# weakly informative priors:
nu0 <- 1
s0 <- 1
post_nu <- nu0 + n
post_ssd <- (nu0 * s0^2 + n*s_sq)/2
post_tau <- rgamma(10000, post_nu/2, post_ssd)
@mooresm
mooresm / Huber2016_ch3_part1.R
Last active June 20, 2017 15:17
Implementation in R of algorithms from chapter 3 of Perfect Sampling (M. Huber, 2016, pp. 43-52): coupling from the past; doubling CFTP; and monotonic CFTP for the Ising model.
set.seed(1234)
# Exercise 3.1: simple symmetric random walk on {0,1,2}
upd <- function(x, U) {
x + ifelse(x < 2 && U > 0.5, 1, 0) - ifelse(x > 0 && U <= 0.5, 1, 0)
}
x <- 1
upd(x,0.25)
upd(x,0.75)