How to define likelihood by myself in Pymc3

I’m using pymc3 to process some experimental data.I need to define a likelihood by myself to remove background signal.But I don’t know how.For example ,I define a normal likelihood,but it dose’t work well .Compared to pm.Normal, it creates larger standard deviation and less accuracy.When it works, a userwarning come out : The effect of Potentials on other parameters is ignored during prior predictive sampling. This is likely to lead to invalid or biased predictive samples.
I don’t know how to write a likelihood which works the same an pm.normal?
there are my codes:

def likelihood(observed,sigma,mu):
     return pm.math.exp(-0.5 * ((observed - mu) / sigma) ** 2) / (pm.math.sqrt(2 * np.pi) * sigma) 

with pm.Model() as ballabio_model:
    amp1 = pm.Normal("amp1", mu=np.sum(data/Ascale), sigma=0.2*np.sum(data/Ascale))
    Tion1 = pm.Uniform('Tion1',lower=0.5,upper=10.0)
    const1 = pm.Uniform('constant1', 0, 10)
    sigma=noise_total1
    output = pm.Deterministic('output',amp1*ballabio(E_mid,Tion1) + const1)
    likelihood = pm.Potential('likelihood',likelihood(data,sigma,output))
    prior = pm.sample_prior_predictive()
    posterior2 = pm.sample(draws = Samples, target_accept = 0.9,chains=4,cores=1)
    posterior_without_background = pm.sample_posterior_predictive(posterior1)
    az.plot_trace(posterior2, var_names = ['amp1','Tion1','constant1'])
    result_no_background2=az.summary(posterior2, var_names = ['amp1','Tion1','constant1'])

and results:

You need to provide a log likelihood, not a likelihood to PyMC.

The warning you see happens when you call sample_prior_predictive or posterior_predictive in a model with Potentials because PyMC won’t know how to take random draws from those terms.

If you implement a CustomDist you can provide a random method that PyMC can use in those cases: pymc.CustomDist — PyMC dev documentation

Thanks, so I should use pymc.CustomDist to define my likelihood? Besides, may I export the result of simple when I’m using PyMC4? I know I can accomplish this by
backgroud_parameters=posterior3[‘amp’],posterior3[‘rate’],posterior3[‘E_start’]
If I’m using PyMC4,what should I do?

It’s up to you, CustomDist with behave more like a traditional model variable, specially if you can provide a random method.

Thigs should work fine in V5 (no point in v4), just by default your get an InferenceData when you sample, but you can find the values easily in result.posterior["varname”].

If you have trouble someone here will be able to help you out.

I want to get the sample result as an array. There are three parameters in my model, ‘amp’ ,‘rate’ and ‘E_start’. In v3, I use
backgroud_parameters=posterior3[‘amp’],posterior3[‘rate’],posterior3[‘E_start’]
If I want to accomplish the same result as v3 in v5, which function I should use ?