Parameter estimation (cont.)
- Dr. Jarad Niemi
STAT 544 - Iowa State University
January 24, 2019
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 1 / 32
Parameter estimation (cont.) Dr. Jarad Niemi STAT 544 - Iowa State - - PowerPoint PPT Presentation
Parameter estimation (cont.) Dr. Jarad Niemi STAT 544 - Iowa State University January 24, 2019 Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 1 / 32 Outline Normal model, unknown mean Jeffreys prior Natural
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 1 / 32
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 2 / 32
Normal model, unknown mean Jeffreys prior for µ
−Ey
∂µ2 log p(y|µ)
∂µ2
1 2s2
n
i=1 (yi − µ)2
= −Ey
∂µ2
1 2s2
n
i=1 y2 i − 2µny + nµ2
= −Ey
∂µ
1 2s2 (−2ny + 2nµ)
1 2s2 (2n)
p(µ) ∝
∝ 1
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 3 / 32
Normal model, unknown mean Jeffreys prior for µ
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 4 / 32
Normal model, unknown mean Natural conjugate prior
iid
1 2s2/n
2
s2 µ2 − 2µ n s2 y
2
s2 µ2 − 2µ n s2 y
2
C µ2 − 2µ 1 C m
2
C + n s2
C m + n s2 y
1 2( 1
C + n s2 ) −1
1
C + n s2 )
C m + n s2 y
Parameter estimation (cont.) January 24, 2019 5 / 32
Normal model, unknown mean Natural conjugate prior
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 6 / 32
Normal model, unknown mean Natural conjugate prior
# Prior m = 0 C = 1; P = 1/C # Data mu = 1 s2 = 1; Q = 1/s2 n = 3 set.seed(6); (y = rnorm(n,mu,sqrt(1/Q))) [1] 1.2696060 0.3700146 1.8686598 # Posterior nQ = n*Q Pp = P+nQ mp = (P*m+nQ*mean(y))/Pp Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 7 / 32
Normal model, unknown mean Natural conjugate prior
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 8 / 32
Normal model, unknown variance
−Ey
∂(σ2)2 log p(y|σ2)
∂(σ2)2 − n log(2πσ2)/2 − 1 2σ2
n
i=1 (yi − m)2
∂(σ2) − n 2σ2 + 1 2(σ2)2
n
i=1 (yi − m)2
2(σ2)2 − 1 (σ2)3
n
i=1 (yi − m)2
n 2(σ2)2 + n (σ2)3 σ2
= n
2 (σ2)−2
p(σ2) ∝
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 9 / 32
Normal model, unknown variance
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 10 / 32
Normal model, unknown variance
iid
1 2σ2
i=1[yi − m]2
1 2σ2
i=1[yi − m]2
σ2
i=1[yi − m]2/2
i=1[yi − m]2/2.
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 11 / 32
Normal model, unknown variance
# Prior a = b = 1 # Data m = 1 n = length(y) y [1] 1.2696060 0.3700146 1.8686598 # Posterior ap = a+n/2 (bp = b+sum((y-m)^2)/2) [1] 1.612069 Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 12 / 32
Normal model, unknown variance
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 13 / 32
Normal model, unknown variance Summary
i=1(yi − µ)2/2
Parameter estimation (cont.) January 24, 2019 14 / 32
JAGS
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 15 / 32
JAGS Binomial model
model = " model { y ~ dbin(theta,n) # notice p then n theta ~ dbeta(a,b) } " dat = list(n=10, y=3, a=1, b=1) m = jags.model(textConnection(model), dat) Compiling model graph Resolving undeclared variables Allocating nodes Graph information: Observed stochastic nodes: 1 Unobserved stochastic nodes: 1 Total graph size: 5 Initializing model r = coda.samples(m, "theta", n.iter=1000) Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 16 / 32
JAGS Binomial model
summary(r) Iterations = 1001:2000 Thinning interval = 1 Number of chains = 1 Sample size per chain = 1000
plus standard error of the mean: Mean SD Naive SE Time-series SE 0.324050 0.122776 0.003883 0.004139
2.5% 25% 50% 75% 97.5% 0.1110 0.2337 0.3103 0.4091 0.5760 Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 17 / 32
JAGS Binomial model
plot(r)
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 18 / 32
JAGS Normal model, unknown mean
model = " model { for (i in 1:n) { # iterate over observations y[i] ~ dnorm(mu,1/s2) # precision instead of variance } mu ~ dnorm(m,1/C) # cannot use improper prior in JAGS } " dat = list(m=0,C=1,s2=1,y=y) dat$n = length(dat$y) m = jags.model(textConnection(model), dat) Compiling model graph Resolving undeclared variables Allocating nodes Graph information: Observed stochastic nodes: 3 Unobserved stochastic nodes: 1 Total graph size: 10 Initializing model r = coda.samples(m, "mu", n.iter=1000) Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 19 / 32
JAGS Normal model, unknown mean
summary(r) Iterations = 1:1000 Thinning interval = 1 Number of chains = 1 Sample size per chain = 1000
plus standard error of the mean: Mean SD Naive SE Time-series SE 0.87238 0.48908 0.01547 0.01547
2.5% 25% 50% 75% 97.5%
0.5529 0.8768 1.2132 1.8182 Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 20 / 32
JAGS Normal model, unknown mean
plot(r)
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 21 / 32
JAGS Normal model, unknown variance
model = " model { for (i in 1:n) { y[i] ~ dnorm(m,tau) # precision instead of variance } tau ~ dgamma(a,b) # Inverse gamma is not a built in distribution sigma2 <- 1/tau # Functions of parameters } " dat = list(m=1,a=1,b=1,y=y) dat$n = length(dat$y) m = jags.model(textConnection(model), dat) Compiling model graph Resolving undeclared variables Allocating nodes Graph information: Observed stochastic nodes: 3 Unobserved stochastic nodes: 1 Total graph size: 10 Initializing model r = coda.samples(m, "sigma2", n.iter=1000) Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 22 / 32
JAGS Normal model, unknown variance
summary(r) Iterations = 1:1000 Thinning interval = 1 Number of chains = 1 Sample size per chain = 1000
plus standard error of the mean: Mean SD Naive SE Time-series SE 1.02769 0.97260 0.03076 0.03076
2.5% 25% 50% 75% 97.5% 0.2535 0.5048 0.7412 1.2081 3.7243 Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 23 / 32
JAGS Normal model, unknown variance
plot(r)
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 24 / 32
Stan
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 25 / 32
Stan Binomial model
model = " data { int<lower=0> n; // define range and type int<lower=0> a; // and notice semicolons int<lower=0> b; int<lower=0,upper=n> y; } parameters { real<lower=0,upper=1> theta; } model { y ~ binomial(n,theta); theta ~ beta(a,b); } " dat = list(n=10, y=3, a=1, b=1) m = stan_model(model_code = model) # Only needs to be done once Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 26 / 32
Stan Binomial model
r = sampling(m, data=dat, iter = 11000, warmup = 1000, refresh=5000) r Inference for Stan model: 0fbec116a458ec7c035f79496aeedb4a. 4 chains, each with iter=11000; warmup=1000; thin=1; post-warmup draws per chain=10000, total post-warmup draws=40000. mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat theta 0.33 0.00 0.13 0.11 0.24 0.32 0.42 0.61 14038 1 lp__
0.01 0.75 -10.28 -8.33 -7.87 -7.69 -7.64 17281 1 Samples were drawn using NUTS(diag_e) at Thu Jan 24 10:51:51 2019. For each parameter, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat=1). Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 27 / 32
Stan Binomial model
plot(r)
theta
0.2 0.4 0.6
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 28 / 32
Stan Normal model, unknown mean
model = " data { int<lower=0> n; real y[n]; // vector real<lower=0> s2; real m; real<lower=0> C; } transformed data { // run once real<lower=0> s; real<lower=0> sqrtC; s = sqrt(s2); sqrtC = sqrt(C); } parameters { real mu; // if used alone, implies a uniform prior } model { y ~ normal(mu,s); // vectorized, i.e. assumed independent mu ~ normal(m,sqrtC); // standard deviation } " dat = list(m=0,C=1,s2=1,y=y) dat$n = length(dat$y) m = stan_model(model_code = model) r = sampling(m, data = dat, iter = 11000, warmup = 1000, refresh=5000) Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 29 / 32
Stan Normal model, unknown mean
plot(r)
mu
0.0 0.5 1.0 1.5 2.0
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 30 / 32
Stan Normal model, unknown variance
model = " data { int<lower=0> n; real y[n]; real m; real<lower=0> a; real<lower=0> b; } parameters { real<lower=0> sigma2; // if used alone, implies a uniform prior on (0,Inf) } transformed parameters { // deterministic function of parameters real<lower=0> sigma; sigma = sqrt(sigma2); } model { y ~ normal(m,sigma); sigma2 ~ inv_gamma(a,b); // built in inverse gamma distribution } " dat = list(a=1,b=1,m=1,y=y) dat$n = length(dat$y) m = stan_model(model_code = model) r = sampling(m, data = dat, iter = 11000, warmup = 1000, refresh=5000) r Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 31 / 32
Stan Normal model, unknown variance
plot(r)
sigma2 sigma
1 2 3 4
Jarad Niemi (STAT544@ISU) Parameter estimation (cont.) January 24, 2019 32 / 32