Custom bivariate uniform prior function

Hi,

I am new to PYMC and MCMC sampling in general. I am looking to create a bivariate uniform prior using the CustomDist function - this seems like it should be simple but I’m not exactly sure what I’m doing wrong or how to go about it. I would like the mean of the bivariate normal likelihood to have a uniform prior distribution on the two dimensions. My code is below

#Bounds of Uniform prior 
lower_x = 1018478.02
upper_x = 2226794.57
lower_y = 4750305.11
upper_y = 6205666.13

#defining custom multivariate uniform variable
def logp(mu: np.ndarray | float) -> TensorVariable:
    p = 1/((upper_x - lower_x)*(upper_y - lower_y))
    return pt.as_tensor_variable(np.log(p))


#MCMC sampling with PYMC
basic_model = pm.Model()

with basic_model:
    #Uniform Prior
    mu = pm.CustomDist("prior",logp=logp)
    
    cov = np.identity(2)

    #likelihood
    like = pm.MvNormal("like", mu=mu,cov=cov, observed=data.to_numpy())

    idata = pm.sample()

You should share the error you’re getting.

In any case I would suggest using regular Uniform and passing that as mu:

with pm.Model() as basic_model:
  mu = pm.Uniform("mu", [lower_x, lower_y], [upper_x, upper_y])
  ... # code stays the same
1 Like

Those are also extreme enough values that you will probably have to be careful with initialization, as in sampling from the prior. Also, it’s presumably faster in PyMC to just use univariate normals when there is an identity covariance matrix.

4 posts were split to a new topic: Can PyMC simplify MvNormal with diagonal covariance matrices

ok that works, thank you!