Testing with pymc traces


do you have advice for testing functions/units that return traces or posterior inferences?
How to deal with the inherent stochasticity in assertions?
For example, if I know my posterior is a Gaussian with given mean and variance, how would I assert my output samples by means of a pytest?

You can set a random seed when sampling so that the randomness is reproducible for unit tests - e.g. pm.sample(random_seed=0). You could also use math.isclose to test something is near enough to the expected value

I think the random seed does not really solve the problem here because there is no ground truth for the approximation; you would rather have analytic expressions as reference. Therefore, the isclose() approach sounds interesting. Though I guess its not clear how to define what’s close enough.

Hi Tim,
I think looking at PyMC3’s tests will help you here: https://github.com/pymc-devs/pymc3/tree/master/pymc3/tests
I think we do exactly what you’re talking about. Hope this helps :vulcan_salute:


thanks. I’ll check it out

Just checking through these - looks like what you want is in the model tests.
Seems numpy has a bunch of test tools with several variations on isclose, among others. I didn’t know about these, thanks @AlexAndorra