In bugs it's: B.raw[j,1:K] ~ dmnorm(mu.raw[], Tau.B.raw[,])
but I can't seem to find an equivalent construct in pymc.
Here is the bugs file:
model {
for (i in 1:n){
y[i] ~ dnorm(y.hat[i], tau.y)
y.hat[i] <- inprod(B[county[i],],X[i,])
e.y[i] <- y[i] - y.hat[i]
}
tau.y <- pow(sigma.y, -2)
sigma.y ~ dunif(0, 100)
for (j in 1:J) {
for(k in 1:K) {
B[j,k] <- xi[k] * B.raw[j,k]
}
B.raw[j,1:K] ~ dmnorm(mu.raw[], Tau.B.raw[,])
}
for(k in 1:K) {
mu[k] <- xi[k] * mu.raw[k]
mu.raw[k] ~ dnorm(0,.0001)
xi[k] ~ dunif(0,100)
}
Tau.B.raw[1:K,1:K] ~ dwish(W[,],df)
df <- K + 1
Sigma.B.raw[1:K,1:K] <- inverse(Tau.B.raw[,])
for(k in 1:K) {
for(k.prime in 1:K) {
rho.B[k,k.prime] <- Sigma.B.raw[k,k.prime] /
sqrt(Sigma.B.raw[k,k] * Sigma.B.raw[k.prime,k.prime])
}
sigma.B[k] <- abs(xi[k]) * sqrt(Sigma.B.raw[k,k])
}
}
and what I have so far:
# Priors
sigma_y = pymc.Uniform('sigma_y', lower=0, upper=100)
tau_y = pymc.Lambda('tau_y', lambda s=sigma_y: s**-2)
xi = pymc.Uniform('xi', lower=0, upper=100, value=np.zeros(k))
mu_raw = pymc.Normal('mu_raw', mu=0., tau=0.0001, value=np.zeros(k))
Tau_B_raw = pymc.Wishart('Tau_B_raw', df, Tau=np.diag(np.ones(k)))
## FIXME:
B_raw = pymc.Normal('B_raw',mu_raw,Tau_B_raw)
@pymc.deterministic(plot=False)
def Sigma_B_raw(Tau_B_raw=Tau_B_raw):
inverse(Tau_B_raw)
@pymc.deterministic(plot=False)
def rho_B(Sigma_B_raw=Sigma_B_raw):
Sigma_B_raw / sqrt(diag(Sigma_B_raw) * Sigma_B_raw)
@pymc.deterministic(plot=False)
def Sigma_B(xi=xi,Sigma_B_raw=Sigma_B_raw):
abs(xi) * sqrt(diag(Sigma_B_raw))
@pymc.deterministic(plot=False)
def B(xi=xi, B_raw=B_raw):
dot(xi, B_raw)
@pymc.deterministic(plot=False)
def mu(xi=xi, mu_raw=mu_raw):
xi * mu_raw
Thanks,
Whit
I'm struggling with the definition of B_raw:
B_raw = []
B_raw_m = np.zeros( (J,K) )
for i in range(J):
B_raw.append(pymc.MvNormal('B_raw_%i' % i, mu_raw, Tau_B_raw,
value=B_raw_m[i]))
I need to use the coefficients as a contiguous array. Hence, I
initialize B_raw_m as a numpy array and then pass each row of the
array as the value argument to the MvNormal function.
Does MvNormal make a deep copy of the numpy array? Or can it share
memory with the B_raw_m object? I need the values of B_raw_m to be
updated whenever the B_raw_i's are updated.
Does this issue have anything to do with cache_depth?
Thanks,
Whit
Does MvNormal make a deep copy of the numpy array? Or can it share
memory with the B_raw_m object? I need the values of B_raw_m to be
updated whenever the B_raw_i's are updated.
Does this issue have anything to do with cache_depth?
Thanks, Anand.
My first version of the model used deterministic to do the rbind at
each iteration. but the model seems to run extremely slowly (i.e.
much slower than then bugs model).