Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Dirichlet distribution type restrictions #4000

Merged

Conversation

brandonwillard
Copy link
Contributor

@brandonwillard brandonwillard commented Jul 5, 2020

The changes in this PR allow the Dirichlet distribution to take Theano types for the distribution argument.

The current implementation of Dirichlet only effectively allows NumPy types for its distribution arguments, and, since the tests are all performed on NumPy input, this important case was overlooked.

Also, because of the aforementioned testing situation, it was possible to use Dirichlet without having to specify the shape keyword; however, this apparent "feature" was only a side-effect of the incorrect assumption that all Dirichlet parameters should be NumPy arrays, instead shape values should always be required—as they are for most/all other multivariate distributions.

Closes #3999.

@brandonwillard
Copy link
Contributor Author

While setting this up, I noticed that our testing framework—TestMatchesScipy in particular—doesn't appear to perform tests using Theano arguments. This seems like a pretty big oversight, no?

@brandonwillard brandonwillard force-pushed the remove-dirichlet-type-restrictions branch 4 times, most recently from c89d031 to ae6c8e3 Compare July 5, 2020 21:34
@codecov
Copy link

codecov bot commented Jul 5, 2020

Codecov Report

Merging #4000 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4000   +/-   ##
=======================================
  Coverage   86.77%   86.77%           
=======================================
  Files          88       88           
  Lines       14137    14134    -3     
=======================================
- Hits        12267    12265    -2     
+ Misses       1870     1869    -1     
Impacted Files Coverage Δ
pymc3/distributions/multivariate.py 78.17% <100.00%> (+0.04%) ⬆️

Copy link
Contributor

@lucianopaz lucianopaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good except for what @junpenglao noted about shape. With this change, although it will make the Dirichlet be consistent with the rest of the pymc3 distributions, we'll break existing code.

pymc3/distributions/multivariate.py Show resolved Hide resolved
pymc3/distributions/multivariate.py Show resolved Hide resolved
pymc3/distributions/multivariate.py Show resolved Hide resolved
@brandonwillard
Copy link
Contributor Author

brandonwillard commented Jul 6, 2020

Nevertheless, this change will break existing code that doesn't specify a shape (just look at the amount of tests you had to fix). We'll have to add a patch that tries to get the static shape from the parameters and pass it to the distribution kwargs.

While true, the "greater" truth is that it is arguably more broken in its current state. The existing code that will break due to these changes is also operating in err, and the expectations it sets are themselves confusing and error-prone (e.g. we don't have to specify shape here, but most everywhere else?). I don't see a path down which we can continue to allow this user-level use of Dirichlet and it leads to anything good—without also solving the shape problems, that is.

@AlexAndorra
Copy link
Contributor

Where do we stand on this? It seems like there is no way to make Dirichlet consistent with the rest of PyMC3 distributions without making a breaking change -- do you agree @lucianopaz ?

@lucianopaz
Copy link
Contributor

I think that it is possible. There are two ways to do it:

  1. A quick and dirty patch in the dirichlet distribution itself to make it infer the shape when the concentration isn't a theano related instance. From the prior discussion, Brandon is against this.
  2. Write something more general at the Distribution base class level that can infer a shape from the parameters when they aren't theano things. This would require some major work to include a bunch of meta information in each distribution so that it knows what are the distribution's parameters and how to infer a shape from them. Again, from what we discussed so far, @brandonwillard prefers this, but it goes way beyond this particular PR.

I like option 2 the most, but it will take a lot of work. I think that we should write a dirty patch, as in option 1, so we can merge this PR, and then address option 2 with a different dedicated PR.

@AlexAndorra
Copy link
Contributor

I like that too (and option 1 is kind of already what we were doing before this PR, IIUC), although the difficulty could be to find volunteers to implement option 2 once this PR is merged 😬

@brandonwillard brandonwillard force-pushed the remove-dirichlet-type-restrictions branch 2 times, most recently from 23442b8 to b9cfe04 Compare July 17, 2020 19:43
@brandonwillard
Copy link
Contributor Author

brandonwillard commented Jul 17, 2020

I've added a test value shape inference fallback accompanied by a deprecation warning.

Again, this sort of functionality is irredeemably wrong, since the shapes inferred from test values can very easily be incorrect when shared variables are involved, or when the relevant Theano graphs are compiled and called with inputs that differ from the test values.

@brandonwillard
Copy link
Contributor Author

brandonwillard commented Jul 17, 2020

Looks like we have some errors due to Matplotlib version differences.

See #4022.

@brandonwillard brandonwillard force-pushed the remove-dirichlet-type-restrictions branch from b9cfe04 to e9deb8f Compare July 20, 2020 22:03
Copy link
Contributor

@lucianopaz lucianopaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything seems fine now. Thanks @brandonwillard!

@lucianopaz lucianopaz merged commit f07c273 into pymc-devs:master Jul 21, 2020
@brandonwillard brandonwillard deleted the remove-dirichlet-type-restrictions branch July 21, 2020 21:49
gmingas added a commit to alan-turing-institute/pymc3 that referenced this pull request Jul 22, 2020
* Update GP NBs to use standard notebook style (pymc-devs#3978)

* update gp-latent nb to use arviz

* rerun, run black

* rerun after fixes from comments

* rerun black

* rewrite radon notebook using ArviZ and xarray (pymc-devs#3963)

* rewrite radon notebook using ArviZ and xarray

Roughly half notebook has been updated

* add comments on xarray usage

* rewrite 2n half of notebook

* minor fix

* rerun notebook and minor changes

* rerun notebook on pymc3.9.2 and ArviZ 0.9.0

* remove unused import

* add change to release notes

* SMC: refactor, speed-up and run multiple chains in parallel for diagnostics (pymc-devs#3981)

* first attempt to vectorize smc kernel

* add ess, remove multiprocessing

* run multiple chains

* remove unused imports

* add more info to report

* minor fix

* test log

* fix type_num error

* remove unused imports update BF notebook

* update notebook with diagnostics

* update notebooks

* update notebook

* update notebook

* Honor discard_tuned_samples during KeyboardInterrupt (pymc-devs#3785)

* Honor discard_tuned_samples during KeyboardInterrupt

* Do not compute convergence checks without samples

* Add time values as sampler stats for NUTS (pymc-devs#3986)

* Add time values as sampler stats for NUTS

* Use float time counters for nuts stats

* Add timing sampler stats to release notes

* Improve doc of time related sampler stats

Co-authored-by: Alexandre ANDORRA <[email protected]>

Co-authored-by: Alexandre ANDORRA <[email protected]>

* Drop support for py3.6 (pymc-devs#3992)

* Drop support for py3.6

* Update RELEASE-NOTES.md

Co-authored-by: Colin <[email protected]>

Co-authored-by: Colin <[email protected]>

* Fix Mixture distribution mode computation and logp dimensions

Closes pymc-devs#3994.

* Add more info to divergence warnings (pymc-devs#3990)

* Add more info to divergence warnings

* Add dataclasses as requirement for py3.6

* Fix tests for extra divergence info

* Remove py3.6 requirements

* follow-up of py36 drop (pymc-devs#3998)

* Revert "Drop support for py3.6 (pymc-devs#3992)"

This reverts commit 1bf867e.

* Update README.rst

* Update setup.py

* Update requirements.txt

* Update requirements.txt

Co-authored-by: Adrian Seyboldt <[email protected]>

* Show pickling issues in notebook on windows (pymc-devs#3991)

* Merge close remote connection

* Manually pickle step method in multiprocess sampling

* Fix tests for extra divergence info

* Add test for remote process crash

* Better formatting in test_parallel_sampling

Co-authored-by: Junpeng Lao <[email protected]>

* Use mp_ctx forkserver on MacOS

* Add test for pickle with dill

Co-authored-by: Junpeng Lao <[email protected]>

* Fix keep_size for arviz structures. (pymc-devs#4006)

* Fix posterior pred. sampling keep_size w/ arviz input.

Previously posterior predictive sampling functions did not properly
handle the `keep_size` keyword argument when getting an xarray Dataset
as parameter.

Also extended these functions to accept InferenceData object as input.

* Reformatting.

* Check type errors.

Make errors consistent across sample_posterior_predictive and fast_sample_posterior_predictive, and add 2 tests.

* Add changelog entry.

Co-authored-by: Robert P. Goldman <[email protected]>

* SMC-ABC add distance, refactor and update notebook (pymc-devs#3996)

* update notebook

* move dist functions out of simulator class

* fix docstring

* add warning and test for automatic selection of sort sum_stat when using wassertein and energy distances

* update release notes

* fix typo

* add sim_data test

* update and add tests

* update and add tests

* add docs for interpretation of length scales in periodic kernel (pymc-devs#3989)

* fix the expression of periodic kernel

* revert change and add doc

* FIXUP: add suggested doc string

* FIXUP: revertchanges in .gitignore

* Fix Matplotlib type error for tests (pymc-devs#4023)

* Fix for issue 4022.

Check for support for `warn` argument in `matplotlib.use()` call. Drop it if it causes an error.

* Alternative fix.

* Switch from pm.DensityDist to pm.Potential to describe the likelihood in MLDA notebooks and script examples. This is done because of the bug described in arviz-devs/arviz#1279. The commit also changes a few parameters in the MLDA .py example to match the ones in the equivalent notebook.

* Remove Dirichlet distribution type restrictions (pymc-devs#4000)

* Remove Dirichlet distribution type restrictions

Closes pymc-devs#3999.

* Add missing Dirichlet shape parameters to tests

* Remove Dirichlet positive concentration parameter constructor tests

This test can't be performed in the constructor if we're allowing Theano-type
distribution parameters.

* Add a hack to statically infer Dirichlet argument shapes

Co-authored-by: Brandon T. Willard <[email protected]>

Co-authored-by: Bill Engels <[email protected]>
Co-authored-by: Oriol Abril-Pla <[email protected]>
Co-authored-by: Osvaldo Martin <[email protected]>
Co-authored-by: Adrian Seyboldt <[email protected]>
Co-authored-by: Alexandre ANDORRA <[email protected]>
Co-authored-by: Colin <[email protected]>
Co-authored-by: Brandon T. Willard <[email protected]>
Co-authored-by: Junpeng Lao <[email protected]>
Co-authored-by: rpgoldman <[email protected]>
Co-authored-by: Robert P. Goldman <[email protected]>
Co-authored-by: Tirth Patel <[email protected]>
Co-authored-by: Brandon T. Willard <[email protected]>
@kyleabeauchamp kyleabeauchamp added this to the 3.9.3 milestone Jul 28, 2020
@Sayam753 Sayam753 mentioned this pull request Feb 2, 2021
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dirichlet doesn't allow basic Theano types
5 participants