I was surprised that in the model I've been working on, whenever I optimized it, the hyper variances ran out to the upper bound very quickly. To understand what was going on I set up a toy in R, which indeed shows that while point estimates of centered hyper variances are in general too low, those of noncentered variances appear unbounded in their desire to be large...
Is there some reason I should have expected this? Have I misunderstood something crucial about the noncentered approach, or about optimizing in Stan? Below is the toy script, thanks!
hn=50
hmu<-2
hsd<-.2
ar=.6
mu<-rnorm(hn,hmu,hsd)
n=10
diffusion=.4
x<-matrix(rnorm(hn,1/(1-ar)*mu,sqrt(diffusion^2/(1-ar^2))),ncol=hn)
for(i in 2:n){
x <- rbind(x,x[i-1,]*ar+mu+rnorm(hn,0,diffusion))
}
require(rstan)
noncentered<-"
data{
int hn;
int n;
vector[n] x[hn];
}
parameters{
real<lower=0.0001,upper=1> ar;
real<lower=.001,upper=10> diffusion;
real<lower=.001,upper=10> hsd;
real hmu;
vector[hn] mudev;
}
model{
vector[hn] mu;
mudev~normal(0,1);
mu<-hmu+mudev*hsd;
for(subjecti in 1:hn){
x[subjecti,1]~normal(1/(1-ar)*mu[subjecti],((diffusion^2)/(1-ar^2)));
for(rowi in 2:n) x[subjecti,rowi]~normal(mu[subjecti]+ar*x[subjecti,rowi-1],diffusion^2);
}
}"
centered<-"
data{
int hn;
int n;
vector[n] x[hn];
}
parameters{
real<lower=0.0001,upper=1> ar;
real<lower=.001,upper=10> diffusion;
real<lower=.001,upper=10> hsd;
real hmu;
vector[hn] mu;
}
model{
mu~normal(hmu,hsd);
for(subjecti in 1:hn){
x[subjecti,1]~normal(1/(1-ar)*mu[subjecti],((diffusion^2)/(1-ar^2)));
for(rowi in 2:n) x[subjecti,rowi]~normal(mu[subjecti]+ar*x[subjecti,rowi-1],diffusion^2);
}
}"
standata<-list(x=t(x),hn=hn,n=n)
centerednuts<-stan(model_code = c(centered),
data = standata, iter = 1000, chains = 3,
cores=3)
centerednuts
noncenterednuts<-stan(model_code = c(noncentered),
data = standata, iter = 1000, chains = 3,
cores=3)
noncenterednuts
centeredoptim<- optimizing(object = centerednuts@stanmodel,
algorithm='BFGS',
as_vector=F,
tol_obj=1e-10, tol_grad=1e-10,tol_param=1e-10,tol_rel_grad=0, tol_rel_obj=0,
data = standata)
centeredoptim
noncenteredoptim<- optimizing(object = noncenterednuts@stanmodel,
algorithm='BFGS',
as_vector=F,
tol_obj=1e-10, tol_grad=1e-10,tol_param=1e-10,tol_rel_grad=0, tol_rel_obj=0,
data = standata)
noncenteredoptim