Bayesian Correlation using PyMC -
i'm pretty new pymc, , i'm trying implement simple bayesian correlation model, defined in chapter 5 of "bayesian cognitive modeling: practical course", defined below:
i've put code in ipython notebook here, code snippet follows:
mu1 = normal('mu1', 0, 0.001) mu2 = normal('mu2', 0, 0.001) lambda1 = gamma('lambda1', 0.001, 0.001) lambda2 = gamma('lambda2', 0.001, 0.001) rho = uniform('r', -1, 1) @pymc.deterministic def mean(mu1=mu1, mu2=mu2): return np.array([mu1, mu2]) @pymc.deterministic def precision(lambda1=lambda1, lambda2=lambda2, rho=rho): sigma1 = 1 / sqrt(lambda1) sigma2 = 1 / sqrt(lambda2) ss1 = sigma1 * sigma2 ss2 = sigma2 * sigma2 rss = rho * sigma1 * sigma2 return np.power(np.mat([[ss1, rss], [rss, ss2]]), -1) xy = mvnormal('xy', mu=mean, tau=precision, value=data, observed=true) m = pymc.mcmc(locals()) m.sample(10000, 5000)
the error "error: failed in converting 3rd argument `tau' of flib.prec_mvnorm c/fortran array"
i found 1 other reference error (in this question) couldn't see how apply answer there code.
this uninformative error due way have organized data vector. 2 rows n
columns, , pymc
expects n
rows 2 columns. following modification makes code (almost) work me:
xy = mvnormal('xy', mu=mean, tau=precision, value=data.t, observed=true)
i because changed precision matrix not have matrix power part. think mvnormal
in figure has variance-covariance matrix second parameter, while mvnormal
in pymc
expects precision matrix (equal inverse of c
).
here notebook has no more errors, has warning requires additional investigation.
Comments
Post a Comment