CamDavidsonPilon / Probabilistic-Programming-and-Bayesian-Methods-for-Hackers

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)
http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/
MIT License
26.71k stars 7.88k forks source link

Chapter 3 (TFP) joint log probability calculation issue #472

Open mvuksano opened 5 years ago

mvuksano commented 5 years ago

I'm a bit confused about joint_log_prob function used in Chapter 3 (Tensorflow Probability).

I can see that rv_prob is a Uniform random variable from in the range [0, 1]. We don't really do anything with that variable but we use it to calculate how likely sample_prob_1 and sample_prob_2 are to be from that distribution.

I assume that: 1) we can remove the two lines of code that use rv_prob in the calculation of joint log probability OR 2) sample rv_prob (as p) and use that as probability of sampling from first distribution and use 1 - p as probability of sampling from the second distribution. Something like:

p = rv_prob.sample()
rv_assignments=tfd.Categorical(probs=tf.stack([p, 1-p]))
rv_observations = tfd.MixtureSameFamily(
  mixture_distribution=rv_assignments,
  component_distribution=tdf.Normal(loc=sample_centers, scale=sample_sds))

And then we could include rv_assignments.log_prob(sample_prob_1) + rv_assignments.log_prob(sample_prob_2) in joint_log_probability calculation.