Skip to content

Implemented CategoricalGibbsMetropolis, optimized BinaryGibbsMetropolis. #1439

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

Merged
merged 3 commits into from
Oct 13, 2016
Merged

Implemented CategoricalGibbsMetropolis, optimized BinaryGibbsMetropolis. #1439

merged 3 commits into from
Oct 13, 2016

Conversation

ozankabak
Copy link
Contributor

I took up on the discussion we had in Issue #1431, and implemented the CategoricalGibbsMetropolis sampler @twiecki and I contemplated there. I also optimized the implementation of BinaryGibbsMetropolis, and deprecated ElemwiseCategorical.

@springcoil
Copy link
Contributor

I think this is fine based on what @twiecki said.

On 11 Oct 2016 4:54 PM, "Mehmet Ozan Kabak" [email protected]
wrote:

I took up on the discussion we had in Issue #1431
#1431, and implemented the
CategoricalGibbsMetropolis sampler @twiecki https://github.com/twiecki
and I contemplated there. I also optimized the implementation of

BinaryGibbsMetropolis, and deprecated ElemwiseCategorical.

You can view, comment on, or merge this pull request online at:

#1439
Commit Summary

  • Implemented CategoricalGibbsMetropolis, optimized
    BinaryGibbsMetropolis.

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1439, or mute the thread
https://github.com/notifications/unsubscribe-auth/AA8DiB8SsFpdADalEZVhI8VQ9ZOVzqyBks5qy7EtgaJpZM4KTzD0
.

@fonnesbeck
Copy link
Member

There should also be one or more unit tests for the step method.

candidate += 1
return candidate

def softmax(x):
Copy link
Member

Choose a reason for hiding this comment

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

Why not use theano.tensor.nnet.softmax?

Copy link
Member

Choose a reason for hiding this comment

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

Can you use this version for more numerical stability: http://www.johndcook.com/blog/2010/01/20/how-to-compute-the-soft-maximum/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am already making use of that trick. See the - np.max(x) term there? This multivariate form is a generalization of John Cook's bivariate expression.

@@ -283,6 +295,122 @@ def competence(var):
return Competence.IDEAL
return Competence.INCOMPATIBLE

class CategoricalGibbsMetropolis(ArrayStep):
"""Metropolis-Hastings optimized for categorical variables. This step
Copy link
Member

Choose a reason for hiding this comment

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

Also add info that this is doing "Gibbs" -- i.e. updating each dimension one at a time, rather than blocked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. See new commit.

@twiecki
Copy link
Member

twiecki commented Oct 11, 2016

Agreed on tests.

@wanderer2 Why is the binary step method more efficient? Or rather, why can't the Categorical be also as efficient?

@ozankabak
Copy link
Contributor Author

OK, I will send one more commit with:

  1. A unit test for the CategoricalGibbsMetropolis class.
  2. Add a note about the "Gibbs" aspect to both BinaryGibbsMetropolis and CategoricalGibbsMetropolis classes.

@twiecki: Theano's nnet.softmax is basically equivalent to the two-liner I used. See here. In this case, I don't think there is a significant advantage to using Theano's softmax, but we can use it instead if you guys do.

About the binary step method: It is more efficient because it proposes new samples by simple binary negation, whereas the categorical step method would incur the overhead of going through random sampling only to propose the same value.

Also Added extra comments requested by PyMC3 devs, and fixed a minor bug
in the proportional proposal utilized by CategoricalGibbsMetropolis.
@ozankabak
Copy link
Contributor Author

Hi guys. This latest commit has the additions you guys requested. Cheers!

@twiecki
Copy link
Member

twiecki commented Oct 12, 2016

This looks good. Can you check the example notebooks whether they need updating?

@ColCarroll
Copy link
Member

I have been working on updating the example notebooks -- a bunch have drifted out of date. I'll push what I have later tonight.

@ozankabak
Copy link
Contributor Author

ozankabak commented Oct 13, 2016

Looking at the notebooks after @ColCarroll's updates, the only notebook that uses ElemwiseCategorical is gaussian_mixture_model. We can change it to use CategoricalGibbsMetropolis right away, or make the change later on when/if we decide to remove ElemwiseCategorical completely. (My commits only deprecate ElemwiseCategorical; it is not removed.)

I say we keep that notebook as is (for now) and update it later on. It will provide an example use for ElemwiseCategorical while it is still a part of the library.

As a bonus, I have been working on a new notebook that demonstrates how CategoricalGibbsMetropolis is used. It is still a WIP, and it might take a while before I create a PR for it.

All in all, this PR is ready to get merged, in my opinion. Any objections?

@twiecki twiecki merged commit 6f0013f into pymc-devs:master Oct 13, 2016
@twiecki
Copy link
Member

twiecki commented Oct 13, 2016

Thanks for the contribution!

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.

5 participants