lm()\[P(A|B) = \frac{P(A \cap B)}{P(B)}\] \[P(A|B)P(B) = P(A \cap B) = P(B|A)P(A)\] \[P(A|B) = \frac{P(B|A)P(A)}{P(B)}\]
\[p(\theta|y) = \frac{p(\theta)*p(y|\theta)}{p(y)}\]
\[p(\theta|y) = \frac{p(\theta)*p(y|\theta)}{p(y)}\]
\[p(hypothesis|data) \propto p(hypotheses)*p(data|hypothesis)\]
\[posterior \propto prior*likelihood\]
\[updated~belief \propto prior~belief*current~evidence\]
A classroom with boys and girls
\(\theta\) approximation
A classroom with boys and girls
\(\theta\) approximation
## [1] 0.4444444
\(p(y|\theta)\)
Back to maths
\[Y \sim ?\]
\(p(y|\theta)\)
Back to maths
\[Y \sim \mathcal B(\theta,n)\]
\(p(y|\theta)\)
Back to maths
\[Y \sim \mathcal B(\theta,n)\] \[p(y|\theta)=\prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}\] \[log(p(y|\theta))=\sum_{n=1}^N{y_n}*log(\theta) + \sum_{n=1}^N(1-y_n)*log(1-\theta)\]
\[p(y|\theta)=\prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}\] \[log(p(y|\theta))=\sum_{n=1}^N{y_n}*log(\theta) + \sum_{n=1}^N(1-y_n)*log(1-\theta)\] \[p(y|\theta=0.1) = ?\]
\[p(y|\theta)=\prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}\] \[log(p(y|\theta))=\sum_{n=1}^N{y_n}*log(\theta) + \sum_{n=1}^N(1-y_n)*log(1-\theta)\] \[p(y|\theta=0.1) = ?\]
log_likelihood <- function(theta, y)
sum(log(theta)*y + log(1-theta)*(1 -y))
log_likelihood_dbinom <- function(theta, y)
sum(dbinom(y, size = 1, prob = theta, log = T))
log_likelihood(0.1, y)## [1] -9.737143
## [1] -6.624756
\(p(\theta)\) ? No information ? Non informative prior !
\[\theta \sim ?\]
\(p(\theta)\) ? No information ? Non informative prior !
\[\theta \sim \mathcal U (0, 1)\] \[\theta \sim \mathcal B (1, 1)\]
\[X \sim \Gamma(\alpha,\beta)~,~f(x)=\frac{\beta^\alpha x^{\alpha-1}e^{-\beta x}}{\Gamma(\alpha)}~,~\Gamma(z)=\int_0^1x^{z-1}e^{-x}dx\]
![]()
\[X \sim B (\alpha,\beta)~,~f(x)=\frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha,\beta)}~,~B(\alpha,\beta)=\frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha+\beta)}\]
![]()
\[p(\theta) \sim \mathcal B(1,1)\]

\(p(\theta|y) \propto ~?\)
\(p(\theta|y) \propto \mathcal L(y|\theta)p(\theta)\)
\(\mathcal L(y|\theta) = \prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}\)
\(p(\theta) = \frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)}~|\alpha=\beta=1\)
\(p(\theta|y) \propto \mathcal \prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}*\frac{\theta^{\alpha-1}(1-\theta)^{\beta-1}}{B(\alpha,\beta)}\)
inference based on the simulation of a high number of random variables
Advantages
Constraints

Now code your own MCMC in R !
\[p(y|\theta)=\prod_{n=1}^N \theta^{y_n}*(1-\theta)^{1-y_n}\]
\[p(\theta|y) \propto p(\theta)*p(y|\theta)\]
\[p(\theta|y) \propto p(\theta)*p(y|\theta)\]
\[p(\theta|y) \propto p(\theta)*p(y|\theta)\]
walk <- function(theta_old, y, sigma_explore = 0.1){
theta_new <- rnorm(1, theta_old, sigma_explore)
if(theta_new < 0)
theta_new <- 10^-6
if(theta_new > 1)
theta_new <- 1-10^-6
ratio <- (dbeta(theta_new, 1, 1)*likelihood(theta_new, y)) / (dbeta(theta_old, 1, 1)*likelihood(theta_old, y))
if(runif(1) < ratio){
return(theta_new)
} else {
return(theta_old)
}
}
walk(0.1, y)## [1] 0.1
## [1] 0.5329307

data.frame(iter = 1:n_iter, theta = theta) %>%
ggplot(aes(theta)) +
geom_density(col = "blue", fill = "blue", alpha = 0.3) +
geom_vline(xintercept = sum(y)/length(y), col = "red")
US spending on science, space, and technology vs Suicides by hanging, strangulation and suffocation
data <- data.frame(year = 1999:2009,
suicide = c(5247, 5688, 6198, 6462, 6635, 7336, 7248,
7491, 8161, 8578, 9000),
science = c(18.079, 18.594, 19.753, 20.734, 20.831,
23.029, 23.597, 23.584, 25.525, 27.731, 29.449))Data sources: U.S. Office of Management and Budget and Centers for Disease Control & Prevention

\[Y \sim ?\]
\[Y \sim \mathcal N ( \beta_0 + \beta X,\sigma)\]
\[min(\sum(\hat Y- Y))\]
\[max(P(\hat{Y}=Y))\] \[P(Y|(\beta_0,\beta, \sigma)) \sim \mathcal N(\hat{Y},\sigma)\] \[P(Y|(\beta_0,\beta, \sigma)) \sim \mathcal N(\beta_0 + \beta X,\sigma)\]
\[P(Y|(\beta_0,\beta, \sigma)) \sim \mathcal N(\hat{Y},\sigma)\] \[P(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac12(\frac{x-\mu}{\sigma})^2}\] \[P(Y|(\beta_0,\beta, \sigma)) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac12(\frac{Y-\hat Y}{\sigma})^2}\] \[\mathcal L(Y|(\beta_0,\beta, \sigma)) = \prod P(Y|(\beta_0,\beta))\] \[log \mathcal L(Y|(\beta_0,\beta, \sigma)) = \sum \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac12(\frac{Y-\hat Y}{\sigma})^2}\] \[log \mathcal L(Y|(\beta_0,\beta, \sigma)) = \sum \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac12(\frac{Y-\beta_0+\beta*X}{\sigma})^2}\]
\[log(AGR_i+1) \sim \mathcal N (G_{max} * e^{-\frac12(\frac{log(\frac{DBH}{D_{opt}})}{K_s})^2}, \sigma)\]

\(G_{max} \sim \mathcal N ( \mu_G , \sigma_G)\)
\(p(G_{max}|K_s, D_{opt}, \sigma) = ~?\)
\[p(G_{max}|K_s, D_{opt}, \sigma) = \prod_1^n \frac1{\sigma\sqrt {2\pi}} e^{-\frac12(\frac{Y-\hat Y}\sigma)^2}*\frac1{\sigma_G\sqrt{2\pi}}e^{-\frac12(\frac{G_{max}-\mu_G}\sigma)^2}\] \[f(DBH) = exp(-\frac12(\frac{log(\frac{DBH}{D_{opt}})}{K_s})^2\] \[p(G_{max}|K_s, D_{opt}, \sigma) = \prod_1^n \frac1{\sigma\sqrt {2\pi}} e^{-\frac12(\frac{Y-G_{max}*f(DBH)}\sigma)^2}*\frac1{\sigma_G\sqrt{2\pi}}e^{-\frac12(\frac{G_{max}-\mu_G}\sigma)^2}\]
\[p(G_{max}|K_s, D_{opt}, \sigma) = \prod_1^n \frac1{\sigma\sqrt {2\pi}} e^{-\frac12(\frac{Y-G_{max}*f(DBH)}\sigma)^2}*\frac1{\sigma_G\sqrt{2\pi}}e^{-\frac12(\frac{G_{max}-\mu_G}\sigma)^2}\] \[p(G_{max}|K_s, D_{opt}, \sigma) \sim \mathcal N( \frac{\sigma^2\sum_1^n f(DBH)Y+\frac{\mu_G}{\sigma_G^2}}{\sigma^2\sum_1^n f(DBH)^2+\frac{1}{\sigma_G^2}}, \frac{1}{\frac1{\sigma^2}\sum_1^n f(DBH)^2+\frac{1}{\sigma_G^2}})\]
Bayesian inference Using Gibbs Sampling
\[Y \sim \mathcal N ( \beta_0 + \beta X,\sigma)\]

US spending on science, space, and technology vs Suicides by hanging, strangulation and suffocation
\(\tau = 2.88*10^-5,~\beta_0 = -154.7, ~\beta = 317.8\)

## Loading required package: coda
## Loading required package: boot
model <- "./data/model.txt"
data <- list(Y = c(5247, 5688, 6198, 6462, 6635, 7336, 7248, 7491, 8161, 8578, 9000), X = c(18.079, 18.594, 19.753, 20.734, 20.831, 23.029, 23.597, 23.584, 25.525, 27.731, 29.449))
inits <- list(list(tau = 1, beta_0=0, beta=100))
Niter <- 6e3
Nburning <- ceiling(Niter/2)
Nthin <- 5
parameters <- c('beta_0','beta','tau')
resu.bugs <- bugs(data, inits, parameters, model,
n.chains = 1, n.iter = Niter, n.burnin = Nburning,
bugs.directory = "./documents/Initiation Bayes et WinBugs/Winbugs/WinBUGS14/",
working.directory = getwd())
codaobj <- read.bugs('coda1.txt')## Abstracting beta ... 1000 valid values
## Abstracting beta_0 ... 1000 valid values
## Abstracting deviance ... 1000 valid values
## Abstracting tau ... 1000 valid values
## Inference for Bugs model at "./data/model.txt", fit using WinBUGS,
## 1 chains, each with 6000 iterations (first 3000 discarded), n.thin = 3
## n.sims = 1000 iterations saved
## mean sd 2.5% 25% 50% 75% 97.5%
## beta_0 -152.5 370.1 -874.4 -386.3 -159.1 87.1 593.6
## beta 317.6 16.1 286.2 307.2 318.1 328.1 348.6
## tau 0.0 0.0 0.0 0.0 0.0 0.0 0.0
## deviance 147.1 2.4 144.2 145.3 146.4 148.2 153.4
##
## DIC info (using the rule, pD = Dbar-Dhat)
## pD = 3.0 and DIC = 150.0
## DIC is an estimate of expected predictive error (lower deviance is better).


\[p(\theta|y) = \frac{p(\theta)*p(y|\theta)}{p(y)}\]
\[p(hypothesis|data) \propto p(hypotheses)*p(data|hypothesis)\]
\[posterior \propto prior*likelihood\]
stan
greta
stan websitegreta website