SlideShare a Scribd company logo
1 of 30
Adat klónozás
híd a Bayes-i és frekventista
statisztikai paradigmák között
Sólymos Péter
http://psolymos.github.com
BURN Meetup | 2014.07.16 | Budapest 1
Motiváció
• Ökológia: a kölcsönhatások tudománya
– Környezet  Élőlény
– Élőlény  Környezet
– Élőlény  Élőlény
• Adataink nem mindíg ideálisak:
– Függetlenség nem teljesül (térbeli, időbeli,
leszármazási függőség)
– Megfigyelési hiba (függő és független
változók esetén egyaránt)
– Hiányzó adatok
2
Hierarchikus modellek
• Inferencia:
– Megfigyelések
– Látens folyamat
– Paraméterek
– Likelihood
• Komputáció:
– Sokdimenziós integrál – nehéz kiszámítani
– Zajos likelihood fuggvény – kihívás a numerikus módszereknek
– Második deriváltak – számítási nehézségek
3
MCMC arzenál
• Inferencia a poszterior eloszlás alapján:
• A normalizáló konstanst nehéz kiszámolni, de
az MCMC algoritmusoknak hála erre nincs is
szükség.
• Sok általános MCMC program elérhető jól
használható R interfésszel:
– WinBUGS, OpenBUGS, JAGS
– Újabban Stan, NIMBLE, stb.
4
5
Poisson – Log-Normal model
set.seed(1234)
n <- 50
beta <- c(1.8, -0.9)
sigma <- 0.2
x <- runif(n, min = 0, max = 1)
X <- model.matrix(~ x)
alpha <- rnorm(n, mean = 0, sd = sigma)
lambda <- exp(alpha + drop(X %*% beta))
Y <- rpois(n, lambda)
𝑌𝑖 𝜆𝑖 ~Poisson 𝜆𝑖
log 𝜆𝑖 = 𝛽0𝑖 + 𝛽1 𝑥𝑖
𝛽0𝑖~Normal(𝛽0, 𝜎2
)
𝜃 = (𝛽0, 𝛽1, 𝜎2)
6
Szekvenciális JAGS munkamenet
library(dclone)
Library(rjags)
model <- function() {
for (i in 1:n) {
Y[i] ~ dpois(lambda[i])
lambda[i] <- exp(alpha[i] +
inprod(X[i,], beta[1,]))
alpha[i] ~ dnorm(0, tau)
}
for (j in 1:np) {
beta[1,j] ~ dnorm(0, 0.001)
}
log.sigma ~ dnorm(0, 0.001)
sigma <- exp(log.sigma)
tau <- 1 / pow(sigma, 2)
}
d <- list(Y = Y, X = X, n = n,
np = ncol(X))
7
load.module("glm")
fn <- write.jags.model(model)
system.time(jm <- jags.model(fn, d,
n.chains=3))
system.time(update(jm,
n.iter=10000))
system.time(m <- coda.samples(jm,
c("beta", "sigma"),
n.iter=5000, thin=1))
clean.jags.model(fn)
unload.module("glm")
Parallel JAGS munkamenet
cl <- makePSOCKcluster(3)
parLoadModule(cl, "glm")
system.time(parJagsModel(cl, name="res", file=model, data=d,
n.chains = 3, n.adapt=1000))
system.time(parUpdate(cl, "res", n.iter=10000))
system.time(m2 <- parCodaSamples(cl, "res", c("beta", "sigma"),
n.iter=5000))
parUnloadModule(cl, "glm")
stopCluster(cl)
8
jags.fit wrapper
load.module("glm")
system.time(m3 <- jags.fit(d,
c("beta", "sigma"), model,
n.update = 10000))
unload.module("glm")
## bugs.fit & stan.fit
## wrapper is van!
9
cl <- makePSOCKcluster(3)
parLoadModule(cl, "glm")
system.time(m4 <- jags.parfit(cl, d,
c("beta", "sigma"), model,
n.update = 10000))
parUnloadModule(cl, "glm")
stopCluster(cl)
## bugs.parfit & stan.parfit
## wrapper is van!
A burnin idő nem
csökken 0-ra:
1/n.iter + overhead
RNG – nagyon fontos!
Másodperc
Láncok száma
10
http://lego.gizmodo.com/5020703/35310-lego-star-wars-clone-trooper-army-invades-earth
Data cloning
Lele et al. Ecol. Lett. 10:551–563, 2007
Lele et al. JASA 105:1617–1625, 2010
Sólymos R Journal, 2:29-37, 2010
Adat klónozás
• Elméleti eredmények:
• Konzekvenciák:
– Bayesian MCMC algoritmusik használhatók frekventista célokra
– A poszterior átlag az MLE
– K * poszterior variancia = MLE variancia
– Nem kell többdimenziós integrált és deriváltakat számolni
– Nem baj ha zajos a likelihood függvény, a hatványozás kiemeli a
globális maximumot
– Az eredmény nem függ a prior eloszlástól.
11
12
Adat klónozás vizuálisan
Az adatok előkészítése és dc.fit
13
Másodperc
Klónok száma
d2 <- dclone(d, n.clones = 2,
multiply = "n", unchanged = "np")
nclones(d2)
str(d)
str(d2)
k <- c(1, 2, 4, 8)
system.time(m5 <- dc.fit(d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np"))
plot(dctable(m5))
Parallelizáció: dc.parfit
cl <- makePSOCKcluster(3)
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="parchains"))
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="balancing"))
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="both"))
stopCluster(cl)
14
Parallel chains:
Tanulás (jobb mixing,
kevesebb burnin)
Size balancing:
A legnagyobb probláma
számít
Mindkettő:
Ez a legkompaktabb
opció
Parallelizáció: dc.parfit
cl <- makePSOCKcluster(3)
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="parchains"))
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="balancing"))
system.time(m6 <- dc.parfit(cl, d,
c("beta", "sigma"),
model, n.clones = k, n.iter = 1000,
multiply = "n", unchanged = "np",
partype="both"))
stopCluster(cl)
15
Parallel chains:
Tanulás (jobb mixing,
kevesebb burnin)
Size balancing:
A legnagyobb probláma
számít
Mindkettő:
Ez a legkompaktabb
opció
16
Cluster opciók összefoglalása
Emellett multicore
jellegű forking:
cl <- 3
Parallel RNG
## 'base::BaseRNG' factory
str(parallel.inits(NULL, 2))
## 'lecuyer::RngStream' factory
load.module("lecuyer", quiet = TRUE)
str(parallel.inits(NULL, 2))
unload.module("lecuyer", quiet = TRUE)
• WinBUGS, OpenBUGS: a seed módszer nem
garantálja a nagyon hosszú láncok
függetlenségét
• Stan: L’Ecuyer RNG az alap
• JAGS: 4 kül. RNG az alap + lecuyer modul:
17
Mennyi klón kell?
18
> dcdiag(m5)
n.clones lambda.max ms.error r.squared r.hat
1 1 0.29978344 0.123200602 0.009283392 1.036925
2 2 0.13688967 0.050523626 0.007621271 1.037907
3 4 0.08255999 0.006328008 0.001040713 1.043169
4 8 0.02986512 0.009152263 0.001472445 1.034379
A poszterior degenerált
MVN eloszlássá változik
K emelkedésével
• A maximum sajátérték
a degeneráció fokát
mutatja
• QQ plot jellegű
mérőszámok mutatják
a MVN elvárás
teljesülését
Paraméter azonosíthatóság
19
Ha a likelihood függvény platót alkot, akkor
az eloszlás nem degenerálódik
plató
MLE nem
emelkedik ki
N-mixture
20
Abundancia (látens):
𝑁𝑖 𝜆𝑖 ~Poisson 𝜆𝑖
log 𝜆𝑖 = 𝛽0 + 𝛽1 𝑥𝑖
Megfigyelés:
𝑌𝑖 𝑁𝑖, 𝑝𝑖 ~Binomial 𝑁𝑖, 𝑝𝑖
logit 𝑝𝑖 = 𝛼0 + 𝛼1 𝑧𝑖
Nem azonosítható ha (𝜆𝑖 𝑝𝑖) állandó, de jól mszétválik ha
(𝜆𝑖 𝑝𝑖) kovariánsokkal magyarázható.
Összegzés
1. Az adat klónozás egy globális optimalizációs
algoritmus, ami kihasználja a Bayesi MCMC
módszereket, hogy frekventista becslést végezzen
(MLE és Fisher információs mátrix).
2. A modell paraméterek azonosíthatóságáról
azonnal tájékoztat.
3. A tanulási folyamat (erős prior) javítja a mixing-et
és csökkenti a burin-t.
4. Ehhez az adatokat klónozni kell, ami jelentősen
növelheti a DAG (directed acyclic graph) méretét.
5. A klónok számát a számolásigény limitálhatja,
ezért HPC eszközök használata javasolt.
21
Példa: SAR
22
Lineáris kevert modell, meta-elemzés:
- Heteroszkadasztikus
- A random hatások konfidencia határaitra
voltunk kíváncsiak (shrinkage)
Patiño et al. 2014. Differences in species–area
relationships among the major lineages of land
plants: a macroecological perspective. Global
Ecology and Biogeography, in press.
sharx
23
library(sharx)
## ez a függvény amit a felhasználó használ
hsarx
## ez értelmezi a formulát
sharx:::parse_hsarx
## ez készíti el az elemzési objektumot
sharx:::make_hsarx
dcmle csomag
24
paurelia <- c(17,29,39,63,185,258,267,392,510,570,650,560,575,650,550,
480,520,500)
paramecium <- new("dcFit")
paramecium@data <- list(ncl=1, n=length(paurelia),
Y=dcdim(data.matrix(paurelia)))
paramecium@model <- function() {
for (k in 1:ncl) {
for(i in 2:(n+1)){
Y[(i-1), k] ~ dpois(exp(X[i, k]))
X[i, k] ~ dnorm(mu[i, k], 1 / sigma^2)
mu[i, k] <- X[(i-1), k] + log(lambda) - log(1 + beta * exp(X[(i-1), k]))
}
X[1, k] ~ dnorm(mu0, 1 / sigma^2)
}
beta ~ dlnorm(-1, 1)
sigma ~ dlnorm(0, 1)
tmp ~ dlnorm(0, 1)
lambda <- tmp + 1
mu0 <- log(2) + log(lambda) - log(1 + beta * 2)
}
paramecium@multiply <- "ncl"
paramecium@unchanged <- "n"
paramecium@params <- c("lambda","beta","sigma")
dcmle csomag
25
(mm1 <- dcmle(paramecium, n.clones=1, n.iter=1000))
(mm2 <- dcmle(paramecium, n.clones=2, n.iter=1000))
(mm3 <- dcmle(paramecium, n.clones=1:3, n.iter=1000))
cl <- makePSOCKcluster(3)
(mm4 <- dcmle(paramecium, n.clones=2, n.iter=1000, cl=cl))
(mm5 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl))
(mm6 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl,
partype="parchains"))
(mm7 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl,
partype="both"))
stopCluster(cl)
Classic BUGS példák
26
> listDcExamples()$topic
[1] blocker bones dyes epil equiv leuk
[7] litters lsat mice oxford pump rats
[13] salm seeds air alli asia beetles
[19] biops birats cervix dugongs eyes hearts
[25] ice jaw orange pigs schools paramecium
sourceDcExample("seeds")
seeds@model
custommodel(seeds@model)
seeds
mm <- dcmle(seeds, n.clones=10)
summary(mm)
PVAClone csomag
27
library(PVAClone)
#example(pva, run.dontrun=TRUE)
data(redstart)
mod <- pva(redstart, gompertz("poisson"), c(5,10))
summary(mod)
coef(mod)
vcov(mod)
confint(mod)
## DC alapú likelihood ratio teszt: model.select
## DC alapú profile likelihood
## ld.: - Ponciano et al. 2009. Ecology 90:356-–362.
## - Nadeem & Lele 2012. Oikos 121:1656--1664.
PVAClone csomag
28
> summary(mod)
PVA object:
Gompertz growth model with Poisson observation error
Time series with 30 observations (missing: 0)
Call:
pva(x = redstart, model = gompertz("poisson"), n.clones = c(5,
10))
Settings:
start end thin n.iter n.chains n.clones
2001 7000 1 5000 3 10
Coefficients:
Estimate Std. Error z value Pr(>|z|)
a 0.4472 0.3727 1.200 0.2302
b -0.2241 0.1930 -1.161 0.2456
sigma 0.2964 0.1185 2.502 0.0124 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Convergence:
n.clones lambda.max ms.error r.squared r.hat
5 0.11825 NA NA NA
10 0.05847 NA NA NA
Szerszámosláda
29
• Ami MCMC-vel megoldható, arra van frekventista
módszerünk!
• Már elérhető:
– Aszimptotikus inferencia (MLE, Fisher információs
mátrix)
– Profile likelihood, LR teszt (hosszadalmas)
– Bootstrap (hosszadalmas)
• Folyamatban:
– Diagnosztikus próbák (R-hat problémáitól mentes)
– Megközelítő bootstrap és profile likelihood (gyors akár
a Bayes)
– Honlap, könyv...
Szerszámosláda
30
• Ami MCMC-vel megoldható, arra van frekventista
módszerünk!
• Már elérhető:
– Aszimptotikus inferencia (MLE, Fisher információs
mátrix)
– Profile likelihood, LR teszt (hosszadalmas)
– Bootstrap (hosszadalmas)
• Folyamatban:
– Diagnosztikus próbák (R-hat problémáitól mentes)
– Megközelítő bootstrap és profile likelihood (gyors akár
a Bayes)
– Honlap, könyv...

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

BURN-Solymos-Adat-klonozas-2014-07-16

  • 1. Adat klónozás híd a Bayes-i és frekventista statisztikai paradigmák között Sólymos Péter http://psolymos.github.com BURN Meetup | 2014.07.16 | Budapest 1
  • 2. Motiváció • Ökológia: a kölcsönhatások tudománya – Környezet  Élőlény – Élőlény  Környezet – Élőlény  Élőlény • Adataink nem mindíg ideálisak: – Függetlenség nem teljesül (térbeli, időbeli, leszármazási függőség) – Megfigyelési hiba (függő és független változók esetén egyaránt) – Hiányzó adatok 2
  • 3. Hierarchikus modellek • Inferencia: – Megfigyelések – Látens folyamat – Paraméterek – Likelihood • Komputáció: – Sokdimenziós integrál – nehéz kiszámítani – Zajos likelihood fuggvény – kihívás a numerikus módszereknek – Második deriváltak – számítási nehézségek 3
  • 4. MCMC arzenál • Inferencia a poszterior eloszlás alapján: • A normalizáló konstanst nehéz kiszámolni, de az MCMC algoritmusoknak hála erre nincs is szükség. • Sok általános MCMC program elérhető jól használható R interfésszel: – WinBUGS, OpenBUGS, JAGS – Újabban Stan, NIMBLE, stb. 4
  • 5. 5
  • 6. Poisson – Log-Normal model set.seed(1234) n <- 50 beta <- c(1.8, -0.9) sigma <- 0.2 x <- runif(n, min = 0, max = 1) X <- model.matrix(~ x) alpha <- rnorm(n, mean = 0, sd = sigma) lambda <- exp(alpha + drop(X %*% beta)) Y <- rpois(n, lambda) 𝑌𝑖 𝜆𝑖 ~Poisson 𝜆𝑖 log 𝜆𝑖 = 𝛽0𝑖 + 𝛽1 𝑥𝑖 𝛽0𝑖~Normal(𝛽0, 𝜎2 ) 𝜃 = (𝛽0, 𝛽1, 𝜎2) 6
  • 7. Szekvenciális JAGS munkamenet library(dclone) Library(rjags) model <- function() { for (i in 1:n) { Y[i] ~ dpois(lambda[i]) lambda[i] <- exp(alpha[i] + inprod(X[i,], beta[1,])) alpha[i] ~ dnorm(0, tau) } for (j in 1:np) { beta[1,j] ~ dnorm(0, 0.001) } log.sigma ~ dnorm(0, 0.001) sigma <- exp(log.sigma) tau <- 1 / pow(sigma, 2) } d <- list(Y = Y, X = X, n = n, np = ncol(X)) 7 load.module("glm") fn <- write.jags.model(model) system.time(jm <- jags.model(fn, d, n.chains=3)) system.time(update(jm, n.iter=10000)) system.time(m <- coda.samples(jm, c("beta", "sigma"), n.iter=5000, thin=1)) clean.jags.model(fn) unload.module("glm")
  • 8. Parallel JAGS munkamenet cl <- makePSOCKcluster(3) parLoadModule(cl, "glm") system.time(parJagsModel(cl, name="res", file=model, data=d, n.chains = 3, n.adapt=1000)) system.time(parUpdate(cl, "res", n.iter=10000)) system.time(m2 <- parCodaSamples(cl, "res", c("beta", "sigma"), n.iter=5000)) parUnloadModule(cl, "glm") stopCluster(cl) 8
  • 9. jags.fit wrapper load.module("glm") system.time(m3 <- jags.fit(d, c("beta", "sigma"), model, n.update = 10000)) unload.module("glm") ## bugs.fit & stan.fit ## wrapper is van! 9 cl <- makePSOCKcluster(3) parLoadModule(cl, "glm") system.time(m4 <- jags.parfit(cl, d, c("beta", "sigma"), model, n.update = 10000)) parUnloadModule(cl, "glm") stopCluster(cl) ## bugs.parfit & stan.parfit ## wrapper is van! A burnin idő nem csökken 0-ra: 1/n.iter + overhead RNG – nagyon fontos! Másodperc Láncok száma
  • 10. 10 http://lego.gizmodo.com/5020703/35310-lego-star-wars-clone-trooper-army-invades-earth Data cloning Lele et al. Ecol. Lett. 10:551–563, 2007 Lele et al. JASA 105:1617–1625, 2010 Sólymos R Journal, 2:29-37, 2010
  • 11. Adat klónozás • Elméleti eredmények: • Konzekvenciák: – Bayesian MCMC algoritmusik használhatók frekventista célokra – A poszterior átlag az MLE – K * poszterior variancia = MLE variancia – Nem kell többdimenziós integrált és deriváltakat számolni – Nem baj ha zajos a likelihood függvény, a hatványozás kiemeli a globális maximumot – Az eredmény nem függ a prior eloszlástól. 11
  • 13. Az adatok előkészítése és dc.fit 13 Másodperc Klónok száma d2 <- dclone(d, n.clones = 2, multiply = "n", unchanged = "np") nclones(d2) str(d) str(d2) k <- c(1, 2, 4, 8) system.time(m5 <- dc.fit(d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np")) plot(dctable(m5))
  • 14. Parallelizáció: dc.parfit cl <- makePSOCKcluster(3) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="parchains")) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="balancing")) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="both")) stopCluster(cl) 14 Parallel chains: Tanulás (jobb mixing, kevesebb burnin) Size balancing: A legnagyobb probláma számít Mindkettő: Ez a legkompaktabb opció
  • 15. Parallelizáció: dc.parfit cl <- makePSOCKcluster(3) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="parchains")) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="balancing")) system.time(m6 <- dc.parfit(cl, d, c("beta", "sigma"), model, n.clones = k, n.iter = 1000, multiply = "n", unchanged = "np", partype="both")) stopCluster(cl) 15 Parallel chains: Tanulás (jobb mixing, kevesebb burnin) Size balancing: A legnagyobb probláma számít Mindkettő: Ez a legkompaktabb opció
  • 16. 16 Cluster opciók összefoglalása Emellett multicore jellegű forking: cl <- 3
  • 17. Parallel RNG ## 'base::BaseRNG' factory str(parallel.inits(NULL, 2)) ## 'lecuyer::RngStream' factory load.module("lecuyer", quiet = TRUE) str(parallel.inits(NULL, 2)) unload.module("lecuyer", quiet = TRUE) • WinBUGS, OpenBUGS: a seed módszer nem garantálja a nagyon hosszú láncok függetlenségét • Stan: L’Ecuyer RNG az alap • JAGS: 4 kül. RNG az alap + lecuyer modul: 17
  • 18. Mennyi klón kell? 18 > dcdiag(m5) n.clones lambda.max ms.error r.squared r.hat 1 1 0.29978344 0.123200602 0.009283392 1.036925 2 2 0.13688967 0.050523626 0.007621271 1.037907 3 4 0.08255999 0.006328008 0.001040713 1.043169 4 8 0.02986512 0.009152263 0.001472445 1.034379 A poszterior degenerált MVN eloszlássá változik K emelkedésével • A maximum sajátérték a degeneráció fokát mutatja • QQ plot jellegű mérőszámok mutatják a MVN elvárás teljesülését
  • 19. Paraméter azonosíthatóság 19 Ha a likelihood függvény platót alkot, akkor az eloszlás nem degenerálódik plató MLE nem emelkedik ki
  • 20. N-mixture 20 Abundancia (látens): 𝑁𝑖 𝜆𝑖 ~Poisson 𝜆𝑖 log 𝜆𝑖 = 𝛽0 + 𝛽1 𝑥𝑖 Megfigyelés: 𝑌𝑖 𝑁𝑖, 𝑝𝑖 ~Binomial 𝑁𝑖, 𝑝𝑖 logit 𝑝𝑖 = 𝛼0 + 𝛼1 𝑧𝑖 Nem azonosítható ha (𝜆𝑖 𝑝𝑖) állandó, de jól mszétválik ha (𝜆𝑖 𝑝𝑖) kovariánsokkal magyarázható.
  • 21. Összegzés 1. Az adat klónozás egy globális optimalizációs algoritmus, ami kihasználja a Bayesi MCMC módszereket, hogy frekventista becslést végezzen (MLE és Fisher információs mátrix). 2. A modell paraméterek azonosíthatóságáról azonnal tájékoztat. 3. A tanulási folyamat (erős prior) javítja a mixing-et és csökkenti a burin-t. 4. Ehhez az adatokat klónozni kell, ami jelentősen növelheti a DAG (directed acyclic graph) méretét. 5. A klónok számát a számolásigény limitálhatja, ezért HPC eszközök használata javasolt. 21
  • 22. Példa: SAR 22 Lineáris kevert modell, meta-elemzés: - Heteroszkadasztikus - A random hatások konfidencia határaitra voltunk kíváncsiak (shrinkage) Patiño et al. 2014. Differences in species–area relationships among the major lineages of land plants: a macroecological perspective. Global Ecology and Biogeography, in press.
  • 23. sharx 23 library(sharx) ## ez a függvény amit a felhasználó használ hsarx ## ez értelmezi a formulát sharx:::parse_hsarx ## ez készíti el az elemzési objektumot sharx:::make_hsarx
  • 24. dcmle csomag 24 paurelia <- c(17,29,39,63,185,258,267,392,510,570,650,560,575,650,550, 480,520,500) paramecium <- new("dcFit") paramecium@data <- list(ncl=1, n=length(paurelia), Y=dcdim(data.matrix(paurelia))) paramecium@model <- function() { for (k in 1:ncl) { for(i in 2:(n+1)){ Y[(i-1), k] ~ dpois(exp(X[i, k])) X[i, k] ~ dnorm(mu[i, k], 1 / sigma^2) mu[i, k] <- X[(i-1), k] + log(lambda) - log(1 + beta * exp(X[(i-1), k])) } X[1, k] ~ dnorm(mu0, 1 / sigma^2) } beta ~ dlnorm(-1, 1) sigma ~ dlnorm(0, 1) tmp ~ dlnorm(0, 1) lambda <- tmp + 1 mu0 <- log(2) + log(lambda) - log(1 + beta * 2) } paramecium@multiply <- "ncl" paramecium@unchanged <- "n" paramecium@params <- c("lambda","beta","sigma")
  • 25. dcmle csomag 25 (mm1 <- dcmle(paramecium, n.clones=1, n.iter=1000)) (mm2 <- dcmle(paramecium, n.clones=2, n.iter=1000)) (mm3 <- dcmle(paramecium, n.clones=1:3, n.iter=1000)) cl <- makePSOCKcluster(3) (mm4 <- dcmle(paramecium, n.clones=2, n.iter=1000, cl=cl)) (mm5 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl)) (mm6 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl, partype="parchains")) (mm7 <- dcmle(paramecium, n.clones=1:3, n.iter=1000, cl=cl, partype="both")) stopCluster(cl)
  • 26. Classic BUGS példák 26 > listDcExamples()$topic [1] blocker bones dyes epil equiv leuk [7] litters lsat mice oxford pump rats [13] salm seeds air alli asia beetles [19] biops birats cervix dugongs eyes hearts [25] ice jaw orange pigs schools paramecium sourceDcExample("seeds") seeds@model custommodel(seeds@model) seeds mm <- dcmle(seeds, n.clones=10) summary(mm)
  • 27. PVAClone csomag 27 library(PVAClone) #example(pva, run.dontrun=TRUE) data(redstart) mod <- pva(redstart, gompertz("poisson"), c(5,10)) summary(mod) coef(mod) vcov(mod) confint(mod) ## DC alapú likelihood ratio teszt: model.select ## DC alapú profile likelihood ## ld.: - Ponciano et al. 2009. Ecology 90:356-–362. ## - Nadeem & Lele 2012. Oikos 121:1656--1664.
  • 28. PVAClone csomag 28 > summary(mod) PVA object: Gompertz growth model with Poisson observation error Time series with 30 observations (missing: 0) Call: pva(x = redstart, model = gompertz("poisson"), n.clones = c(5, 10)) Settings: start end thin n.iter n.chains n.clones 2001 7000 1 5000 3 10 Coefficients: Estimate Std. Error z value Pr(>|z|) a 0.4472 0.3727 1.200 0.2302 b -0.2241 0.1930 -1.161 0.2456 sigma 0.2964 0.1185 2.502 0.0124 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Convergence: n.clones lambda.max ms.error r.squared r.hat 5 0.11825 NA NA NA 10 0.05847 NA NA NA
  • 29. Szerszámosláda 29 • Ami MCMC-vel megoldható, arra van frekventista módszerünk! • Már elérhető: – Aszimptotikus inferencia (MLE, Fisher információs mátrix) – Profile likelihood, LR teszt (hosszadalmas) – Bootstrap (hosszadalmas) • Folyamatban: – Diagnosztikus próbák (R-hat problémáitól mentes) – Megközelítő bootstrap és profile likelihood (gyors akár a Bayes) – Honlap, könyv...
  • 30. Szerszámosláda 30 • Ami MCMC-vel megoldható, arra van frekventista módszerünk! • Már elérhető: – Aszimptotikus inferencia (MLE, Fisher információs mátrix) – Profile likelihood, LR teszt (hosszadalmas) – Bootstrap (hosszadalmas) • Folyamatban: – Diagnosztikus próbák (R-hat problémáitól mentes) – Megközelítő bootstrap és profile likelihood (gyors akár a Bayes) – Honlap, könyv...