SMC example with arbitrary function as model likelihood

#1

Hi,

In the SMC example Notebook: https://docs.pymc.io/notebooks/SMC2_gaussians.html
I would like to have a model likelihood that uses arbitrary functions. If I change two_gaussians to:

@theano.as_op([tt.dvector], [tt.dscalar])
def two_gaussians(x):
    log_like1 = - 0.5 * n * np.log(2 * np.pi) \
                - 0.5 * np.log(dsigma) \
                - 0.5 * (x - mu1).T.dot(isigma).dot(x - mu1)
    log_like2 = - 0.5 * n * np.log(2 * np.pi) \
                - 0.5 * np.log(dsigma) \
                - 0.5 * (x - mu2).T.dot(isigma).dot(x - mu2)
    return np.log(w1 * np.exp(log_like1) + w2 * np.exp(log_like2))

I get the following error when sampling:

ValueError: expected an ndarray
Apply node that caused the error: MakeVector{dtype='float64'}(llk)
Toposort index: 3
Inputs types: [TensorType(float64, scalar)]
Inputs shapes: [()]
Inputs strides: [()]
Inputs values: [-166.83949357531378]
Outputs clients: [[Sum{acc_dtype=float64}(MakeVector{dtype='float64'}.0)]]

Is there something I am doing wrong?

#2

Hi @davidbrochart you have to replace the last line with

return np.array(np.log(w1 * np.exp(log_like1) + w2 * np.exp(log_like2)))

1 Like