tensorflow / probability

Probabilistic reasoning and statistical analysis in TensorFlow
https://www.tensorflow.org/probability/
Apache License 2.0
4.27k stars 1.11k forks source link

How to calculate quantile and variance for the tensorflow_probability output? #482

Open mnozary opened 5 years ago

mnozary commented 5 years ago

I test the following code:

TFP Probabilistic Layers: Regression

In "Custom PSD Kernel" section, I got yhat as the prediction for the test data.

My purpose is to have a confidence interval for this prediction. If I want to calculate the variance or quantile for this prediction with the variance and quantile methods, respectively, I will get the following error:

NotImplementedError: variance is not implemented: TransformedDistribution NotImplementedError: quantile is not implemented: Independent Therefore my question is:

How can I obtain the confidence interval for this prediction?

I need something like the following picture [Confidence Interval for the prediction (yhat.mean())]: image

Thanks for your help!

trainorpj commented 5 years ago

I recognize this example from Bayesian Methods for Hackers. If you're reading it, I'll point you to this code cell, which produces that plot.

I'm not sure what your problem is, exactly (providing code would help 🙂), but it does look like you're confusing credible intervals with confidence intervals. The difference is important, and in Bayesian statistics one will report a credible interval (also called "credibility interval" and related to the "region of highest posterior density (HPD)"). This answer on stack overflow provides an explanation on the difference between credible and confidence intervals.

In the code in that notebook they:

Hopefully that cleared some things up. I recommend going through that notebook. Also, apologies if I went over something you already knew. I didn't answer your question directly, since it looks like there were some fundamental (and very common!) misunderstandings.

Note: your question is likely better-suited for stack overflow, but I couldn't find anything on credible intervals, so I decided to answer it here

rybekci commented 3 years ago

The question is relevant and still in effect. If you use tfpl.IndependentNormal() as the output layer, you will get a tf distribution object, and it is expected to have a .quantile() method. But it doesn't. It gives

NotImplementedError: quantile is not implemented: Independent
a-mazzetto commented 9 months ago

Hello, in case it helps, I worked around this fact by extending the tfp.distributions.Independent class for cases where I know that the underlying distribution has a quantile() method implemented.

class IndependentWithQuantile(tfp.distributions.Independent):

    def _quantile(self, value, **kwargs):
        return self.distribution.quantile(value, **kwargs)

Out of curiosity, could anyone explain why doesn't tfp.distributions.Independent automatically check whether the underlying distribution has a quantile() method implemented and inherit where possible?