Hey guys,
I’m trying to use VI for inference on a rather complex hierarchical model. I found two problems that I can’t get around, and I’m able to demonstrate them on a simple model.
-
MAP does not work well for me with shape>1. For example
N=11 vec=np.arange(0,10,0.1).round(0) val=50+10*vec+np.random.randn(100) cat=vec.astype(int) with pm.Model() as model1: e_mu = pm.Flat("e_mu") e_sd = pm.HalfFlat("e_sd") e_rel= pm.Normal("e_rel", mu=0,sd=1, shape=N) estim= pm.Deterministic("estim", e_mu + e_rel[cat] * e_sd) rsd = pm.HalfFlat("rsd") res = pm.Normal("res", mu=estim, sd=rsd, observed=val) map1 = pm.find_MAP() with pm.Model() as model2: e_mu = pm.Flat("e_mu") e_sd = pm.HalfFlat("e_sd") rsd = pm.HalfFlat("rsd") e_rel=[None]*N estim=[None]*N res=[None]*N for i in range(N): e_rel[i]= pm.Normal("e_rel"+str(i), mu=0,tau=1) estim[i]= pm.Deterministic("estim"+str(i), e_mu + e_rel[i] * e_sd) res[i] = pm.Normal("res"+str(i), mu=estim[i], sd=rsd, observed=[val[j] for j in range(100) if cat[j]==i]) map2 = pm.find_MAP() print ("\nmodel1 MAP:") for i in range(N): print(map1["estim"][i],"=",map1["e_mu"],"+",map1["e_sd"],"*",map1["e_rel"][i]) e_rel=[None]*N estim=[None]*N res=[None]*N print("\nmodel2 MAP:") for i in range(N): print(map2["estim"+str(i)],"=",map2["e_mu"],"+",map2["e_sd"],"*",map2["e_rel"+str(i)])
The output I get is:
model1 MAP:
50.4339520554 = 66.9733403143941 + 3263.931880542973 * -0.00506732029477
50.4339520554 = 66.9733403143941 + 3263.931880542973 * -0.00214404233975
50.4339520554 = 66.9733403143941 + 3263.931880542973 * 0.00090311929684
50.4339520554 = 66.9733403143941 + 3263.931880542973 * 0.00393404637666
50.4339520554 = 66.9733403143941 + 3263.931880542973 * 0.00716117123596
50.4339520554 = 66.9733403143941 + 3263.931880542973 * 0.00987876353612
59.9753321685 = 66.9733403143941 + 3263.931880542973 * 0.0133180094822
59.9753321685 = 66.9733403143941 + 3263.931880542973 * 0.0161422212201
59.9753321685 = 66.9733403143941 + 3263.931880542973 * 0.0192213932958
59.9753321685 = 66.9733403143941 + 3263.931880542973 * 0.0225604603235
59.9753321685 = 66.9733403143941 + 3263.931880542973 * 0.0256524217578
model2 MAP:
50.43392202694377 = 67.0767613033466 + 1476.1655176669472 * -0.011274372065475788
59.97532270019239 = 67.0767613033466 + 1476.1655176669472 * -0.004810733293904538
69.9209560098227 = 67.0767613033466 + 1476.1655176669472 * 0.0019267451193219207
79.81381426206056 = 67.0767613033466 + 1476.1655176669472 * 0.008628472082754407
90.34696398158516 = 67.0767613033466 + 1476.1655176669472 * 0.015763952212497618
99.21698961999387 = 67.0767613033466 + 1476.1655176669472 * 0.02177278085146191
110.4425660067233 = 67.0767613033466 + 1476.1655176669472 * 0.029377332138143676
119.66051359746791 = 67.0767613033466 + 1476.1655176669472 * 0.03562185382654716
129.71082312983438 = 67.0767613033466 + 1476.1655176669472 * 0.04243024313796448
140.60923419625794 = 67.0767613033466 + 1476.1655176669472 * 0.04981316255722331
150.70128222065802 = 67.0767613033466 + 1476.1655176669472 * 0.056649826809041334
Clearly, it got model1 wrong. For convinience I really prefer to work with something similar to model1, so what can I do to get the correct results?
-
I’m trying to use VI to infer latent variables in the model, but I’m getting nonsensical results:
with model1: sample1 = pm.fit(5000,method=pm.SVGD(jitter=0.1), obj_optimizer=pm.adamax(), start=map1).sample(draws=1000) with model2: sample2 = pm.fit(5000,method=pm.SVGD(jitter=0.1), obj_optimizer=pm.adamax(), start=map2).sample(draws=1000) print ("model1 SVGD mean e_mu:",sample1["e_mu"].mean(),"mean e_sd:",sample1["e_sd"].mean()) print ("model2 SVGD mean e_mu:",sample2["e_mu"].mean(),"mean e_sd:",sample2["e_sd"].mean())
output:
model1 SVGD mean e_mu: 1.45233787185 mean e_sd: 38.4966457737
model2 SVGD mean e_mu: 1.55453451283 mean e_sd: 38.3855233369
Note that when looking at
pm.traceplot(sample1);
I get decent estimates for “estim” (in the same way, in my original model, I can get decent predictions). Also I get good results when I run NUTS on this simple model (but this is very very slow for my original model, so not tractable).
Any help on these issues will be greatly appreciated!
Thanks,
Yoni.