I’m reviving an old discussion here, but it might be better than opening a new one. I’ve implemented my own distribution, which is basicall the lognormal distribution from scipy. Sampling the log likelihood is fine, but generating samples for the posterior predictive isn’t working, despite following the suggestions from this thread.
Help is much appreciated.
Attached is my code and the error message:
import pymc as pm
import scipy as sp
import numpy as np
import arviz as az
import matplotlib.pyplot as plt
def logp_lognorm(value, shape, loc, scale):
return -np.log((value-loc)/scale)**2 / (2*shape**2) - np.log(shape*((value-loc)/scale)*np.sqrt(2*np.pi))
def rand_lognorm(point=None, size=None):
# draw a numerical value for the parameters
shape_, loc_, scale_ = draw_values([shape, loc, scale], point=point)
size = 1 if size is None else size
return generate_samples(sp.stats.lognorm.rvs, shape=shape_, loc=loc_, scale=scale_, size=size, broadcast_shape=(size,))
with pm.Model() as model:
shape = pm.Uniform('shape', lower=1, upper=3)
loc = pm.Uniform('loc', lower=4000, upper=5000)
scale = pm.Uniform('scale', lower=8000, upper=9000)
lognorm = pm.DensityDist('likelihood', shape, loc, scale, logp=logp_lognorm, observed=data, random=rand_lognorm)
idata = pm.sample(4000, tune=2000, return_inferencedata=True, chains=1)
idata.extend(pm.sample_posterior_predictive(idata))
TypeError: rand_lognorm() got an unexpected keyword argument ‘rng’
Apply node that caused the error: DensityDist_likelihood_rv{0, (0, 0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7F384431F120>), TensorConstant{[2172 1]}, TensorConstant{11}, shape, loc, scale)
Toposort index: 0
Inputs types: [RandomGeneratorType, TensorType(int64, (2,)), TensorType(int64, ()), TensorType(float64, ()), TensorType(float64, ()), TensorType(float64, ())]
Inputs shapes: [‘No shapes’, (2,), (), (), (), ()]
Inputs strides: [‘No strides’, (8,), (), (), (), ()]
Inputs values: [Generator(PCG64) at 0x7F384431F120, array([2172, 1]), array(11), array(2.37989727), array(4986.3850034), array(8990.54453988)]
Outputs clients: [[‘output’], [‘output’]]