-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Latex representation for SymbolicDistributions #5793
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fae5ea
Template for latex repr for symbolic dists
larryshamalama 929a3b5
Mixture dists look alright
larryshamalama 5839b8d
Removing raise ValueError
larryshamalama c35c676
Fixed printing of mixtures with single component dispatching
larryshamalama 60c3407
working on weight representation for mixtures
larryshamalama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ | |
resize_from_dims, | ||
resize_from_observed, | ||
) | ||
from pymc.printing import str_for_dist | ||
from pymc.printing import str_for_dist, str_for_symbolic_dist | ||
from pymc.util import UNSET | ||
from pymc.vartypes import string_types | ||
|
||
|
@@ -484,15 +484,13 @@ def __new__( | |
initval=initval, | ||
) | ||
|
||
# TODO: Refactor this | ||
# add in pretty-printing support | ||
rv_out.str_repr = lambda *args, **kwargs: name | ||
rv_out._repr_latex_ = f"\\text{name}" | ||
# rv_out.str_repr = types.MethodType(str_for_dist, rv_out) | ||
# rv_out._repr_latex_ = types.MethodType( | ||
# functools.partial(str_for_dist, formatting="latex"), rv_out | ||
# ) | ||
# TODO: if rv_out is a cloned variable, the line below wouldn't work | ||
set_print_name(cls, rv_out) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we clone a variable (e.g., during graph rewrite) this will be lost right? It's not a big issue for now, but of that's the case we should add a comment stating so |
||
|
||
rv_out.str_repr = types.MethodType(str_for_symbolic_dist, rv_out) | ||
rv_out._repr_latex_ = types.MethodType( | ||
functools.partial(str_for_symbolic_dist, formatting="latex"), rv_out | ||
) | ||
return rv_out | ||
|
||
@classmethod | ||
|
@@ -521,7 +519,6 @@ def dist( | |
------- | ||
var : TensorVariable | ||
""" | ||
|
||
if "testval" in kwargs: | ||
kwargs.pop("testval") | ||
warnings.warn( | ||
|
@@ -848,3 +845,7 @@ def default_moment(rv, size, *rv_inputs, rv_name=None, has_fallback=False, ndim_ | |
f"Please provide a moment function when instantiating the {rv_name} " | ||
"random variable." | ||
) | ||
|
||
|
||
def set_print_name(cls, rv): | ||
setattr(rv.owner.op, "_print_name", (f"{cls.__name__}", f"\\operatorname{{{cls.__name__}}}")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative would be to be more clever about mixture print name and on the spot check:
This is where the dispatching idea becomes a bit more powerful. You can have an arbitrarily complex function that specializes on an
Op
and can look at its inputs at evaluation time to figure out a nice name.The same type of logic could be used for Censored (dispatched on at.clip) to call it "CensoredX" like "CensoredNormal", or the future RandomWalk to call them "XRandomWalk" (perhaps with a special case for Normal, where we call it "Gaussian")
The dispatching itself only means we don't need to define the name at creation time (and others can overwrite it more easily). The more fundamental difference is that it uses a per-op function to decide what name to give to the distribution.
The basecase would be the
RandomVariable
Op
which does what was already done eagerly before this PR.This is just an idea. Feel free to investigate something like this or leave it as a separate enhancement/feature request issue!