Fitting a histogram

One thing you can try is to add a scaling factor to the mixture_density, as the histogram (even with density=True) might not gives a prior pdf that could fit with the mixture distribution you specified.

def mixture_density(w, mu, sigma, scaling, x):
    logp =  pm.NormalMixture.dist(w, mu, sigma).logp(x)
    return tt.exp(logp) * scaling

 
with m:
    w = pm.Dirichlet('w', np.ones_like(centers)*.5)
    mu = pm.Normal('mu', 0., 5., shape=centers.size)
    sigma = pm.HalfCauchy('sigma', 1., shape=centers.size)
    scaling = pm.TruncatedNormal('scaling', 1., 0.1, lower=0)
    y= mixture_density(w, mu, sigma, scaling, x)
    y_obs=pm.Normal('y_obs',mu=y,observed=df['y'][0])
1 Like