Hello,
I am new to PyMC3 and Bayesian statistics in general. I am running into an issue trying to create my own likelihood function (following this example), but am running into some trouble.
Basically, my likelihood functions (I have one version that uses numpy and one theano.tensor) compare data to a forward model synthetic generated with parameters w
and h
, with uncertainty sigma
. Here’s a code snippet:
import matplotlib.pyplot as plt
import pandas as pd
import pymc3 as pm
import theano
import theano.tensor as T
def loglike1_np(n,dat,syn,sigma):
likelihood = np.zeros(n)
likelihood = np.abs(np.divide(syn-dat,sigma)) - np.log(2.0*sigma)
logp = np.sum(-1*likelihood)
logp = pm.theanof.floatX(logp)
return logp
def loglike1_tt(n, data,syn,sigma):
likelihood = T.abs_(T.true_div(T.sub(syn-data),sigma)) - T.log(2*sigma)
logp = -1*T.sum(likelihood)
logp = pm.theanof.floatX(logp)
return logp
def forward_model(w,h,...
return synthetic
...
model = pm.Model()
with model:
numiter = int(nburnin + nsample)
data = pm.Data('data', amplitudes)
H = pm.Uniform('h', lower=h1, upper=h2)
W = pm.Uniform('w', lower=0.1, upper=100.0)
sigma = pm.HalfCauchy('sigma',beta=2)
synth = forward_model(W, H, ...)
likelihood = pm.DensityDist('likelihood', loglike1_np, observed={'n':n, 'data':data, 'syn':synth, 'sigma':sigma})
# likelihood = pm.DensityDist('likelihood', loglike1_tt, observed={'n':n, 'data':data, 'syn':synth, 'sigma':sigma})
step = pm.Metropolis()
trace = pm.sample(numiter, step=step)
burned_trace = trace[int(nburnin):]
pm.traceplot(burned_trace)
For both loglike1_ functions, I get this error within the DensityDist():
TypeError: float() argument must be a string or a number, not ‘TensorVariable’
I think this results from my usage of the data
or synth
variables within the loglike1_() functions, as they are both vectors containing elements of type >theano.tensor.var.TensorVariable
I am at a complete loss on how to resolve this, however. I’ve been beating my head against the table (read: searching for the solution here and on StackOverflow, Theano docs) for about 4 hours now. Any advice is greatly appreciated.