tensorflow / tensorflow/probability

Distributions are not independent for Empirical

Open
#1,776 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

While the documentation states that

The first k dimensions index into a batch of independent distributions

this is not the case for the implementation.
As can be seen from this MWE, the samples for every distribution are always the same:

import numpy as np
import tensorflow_probability as tfp

a = np.random.randint(0, 100, size=1000)
dist = tfp.distributions.Empirical([a, a])
n = 3
dist.sample(n)

# output
# <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
# array([[66, 66],
#        [33, 33],
#        [98, 98]], dtype=int32)>

The root cause seems to be at this location, where indices are sampled only once but re-used for every distribution:
https://github.com/tensorflow/probability/blob/fbc5ebe9b1d343113fb917010096cfd88b32eecf/tensorflow_probability/python/distributions/empirical.py#L236-L238

I now fear that there might be more issues in the implementation of Empirical, rendering it unusable in contexts where I require independency between distributions.

Also the shaping of the output seems strange, which is (n, k), while I would expect it to be (k, n).

Maybe I also completely misunderstood the documentation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tensorflow_probability/python/distributions/empirical.py at the referenced lines 236-238, then run the Python MWE to inspect how samples are produced for batched Empirical distributions. Check the documented independence and output-shape expectations against the observed results; done means the behavior and its tests agree with the documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.