Repository navigation
Add doctests and fix bugs in DiscreteProbabilitySpace - #42828
Conversation
Add doctests for DiscreteProbabilitySpace.set(), .entropy(), and the check=True and error paths in __init__. Fix two bugs uncovered by the new doctests: - The TypeError messages in __init__ used a %s placeholder but never substituted P, so they printed a literal "%s". - With check=True, __init__ passed check through to DiscreteRandomVariable.__init__, which unconditionally raises NotImplementedError. Since DiscreteProbabilitySpace already checks the probability function itself, pass check=False to the parent. Part of sagemath#9670.
|
Documentation preview for this PR (built with commit b465921; changes) is ready! 🎉 |
|
I'm sorry I am not active in this project these day. |
Okay ! Whom should I reach out ? If u can tell me or even good if u can tell me if u available in future for working together |
| A fair coin has one bit of entropy:: | ||
|
|
||
| sage: X = DiscreteProbabilitySpace([1, 2], {1: 1/2, 2: 1/2}) | ||
| sage: X.entropy().n(digits=10) |
There was a problem hiding this comment.
| A fair coin has one bit of entropy:: | |
| sage: X = DiscreteProbabilitySpace([1, 2], {1: 1/2, 2: 1/2}) | |
| sage: X.entropy().n(digits=10) | |
| A fair coin has an entropy of 1:: | |
| sage: X = DiscreteProbabilitySpace([1, 2], {1: 1/2, 2: 1/2}) | |
| sage: X.entropy() |
Entropy is not a bit and there's no reason to display the result as a numerical approximation. Similar changes below.
| sage: X.entropy().n(digits=10) | ||
| 1.500000000 | ||
|
|
||
| An outcome with zero probability contributes nothing:: |
There was a problem hiding this comment.
This test is not really checking what the docstring is saying (as I am interpreting how it is stated).
| if isinstance(codomain, RationalField): | ||
| if not one == 1: | ||
| raise TypeError("Argument P (= %s) does not define a probability function") | ||
| raise TypeError("Argument P (= %s) does not define a probability function" % P) |
There was a problem hiding this comment.
| raise TypeError("Argument P (= %s) does not define a probability function" % P) | |
| raise TypeError("argument P (= %s) does not define a probability function" % P) |
While-we-are-at-it, let's make the error message use Sage's (which is Python's) convention.
| else: | ||
| if not abs(one - 1) < 2 ** (-codomain.precision() + 1): | ||
| raise TypeError("Argument P (= %s) does not define a probability function") | ||
| raise TypeError("Argument P (= %s) does not define a probability function" % P) |
There was a problem hiding this comment.
| raise TypeError("Argument P (= %s) does not define a probability function" % P) | |
| raise TypeError("argument P (= %s) does not define a probability function" % P) |
| EXAMPLES:: | ||
|
|
||
| A fair coin has one bit of entropy:: |
There was a problem hiding this comment.
Everything after the EXAMPLES:: is over-indented.
| # The probability function has already been checked above, so | ||
| # pass check=False to avoid DiscreteRandomVariable's | ||
| # unimplemented check. |
There was a problem hiding this comment.
| # The probability function has already been checked above, so | |
| # pass check=False to avoid DiscreteRandomVariable's | |
| # unimplemented check. |
I think the check in this code should actually be moved to the discrete random variable as it is more useful there.
There was a problem hiding this comment.
On moving the check into DiscreteRandomVariable: agreed, that's the right home for it. Right now DiscreteProbabilitySpace validates and then passes check=False to the parent, which leaves the parent's check path as a dead NotImplementedError stub.
Do you want that rework here, or in a follow-up? Also — should the random-variable check also verify that keys of f match the space's support, or just validate probabilities?
There was a problem hiding this comment.
Sorry, I thought I had responded to this. I think we should do it here.
There was a problem hiding this comment.
Done — moved the check into DiscreteRandomVariable.__init__.
Approach: the check runs only when check and self is X. When a DiscreteProbabilitySpace is constructed, it calls the inherited DiscreteRandomVariable.__init__ with X = self, so self is X cleanly identifies the "this is a probability function, it must sum to one" case. For a normal DiscreteRandomVariable, check=True is now a no-op instead of raising NotImplementedError.
DiscreteProbabilitySpace.__init__ drops its inline check and just passes check down. All 45 doctests pass and I verified the usual cases (RR + QQ, valid + invalid, plus the RV no-op) interactively.
…ek41-glitch/sage into fix-probability-dps-doctests
|
@tscrim I have address all ur feedback i m not sure about last suggestion please let me know what do u think |
|
@tscrim can u review |
Move the probability-sum check from DiscreteProbabilitySpace.__init__ into DiscreteRandomVariable.__init__, gated on 'check and self is X'. When a DiscreteProbabilitySpace is constructed it calls the inherited DiscreteRandomVariable.__init__ with X = self, so the identity check cleanly detects that the incoming dictionary is a probability function and must sum to one. For a normal DiscreteRandomVariable, check=True now has no effect instead of raising NotImplementedError. This also lets DiscreteProbabilitySpace drop its inline check and pass check through to the parent.
|
@tscrim please review when u have time and let me know what fix needs to be done! |
tscrim
left a comment
There was a problem hiding this comment.
Sorry for being slow about this. These should be the last round of changes.
| # Called from DiscreteProbabilitySpace.__init__: ``f`` is | ||
| # the probability function and must sum to one. |
There was a problem hiding this comment.
| # Called from DiscreteProbabilitySpace.__init__: ``f`` is | |
| # the probability function and must sum to one. |
The first part is way too specific (and could easily change in the future). The rest is easy to see from the code (and a main part of the definition).
|
|
||
| EXAMPLES:: | ||
|
|
||
| A fair coin has an entropy of 1:: |
There was a problem hiding this comment.
All of these examples are overindented.
| r""" | ||
| The entropy of the probability space. | ||
|
|
||
| EXAMPLES:: |
There was a problem hiding this comment.
| EXAMPLES:: | |
| EXAMPLES: |
|
@tscrim i commit fixes please review |
tscrim
left a comment
There was a problem hiding this comment.
Sorry for being slow (it's in the middle of the semester here). Here are the last round of changes.
| - ``check`` -- boolean (default: ``False``); if ``True`` and | ||
| ``self is X`` (that is, we are initializing a | ||
| :class:`DiscreteProbabilitySpace` through its inherited | ||
| :class:`DiscreteRandomVariable` interface), verify that the | ||
| values of ``f`` sum to one. |
There was a problem hiding this comment.
| - ``check`` -- boolean (default: ``False``); if ``True`` and | |
| ``self is X`` (that is, we are initializing a | |
| :class:`DiscreteProbabilitySpace` through its inherited | |
| :class:`DiscreteRandomVariable` interface), verify that the | |
| values of ``f`` sum to one. | |
| - ``check`` -- boolean (default: ``False``); if ``True``, verify | |
| that the values of ``f`` sum to one |
| RR = RealField() | ||
| else: | ||
| RR = codomain | ||
| if check and self is X: |
There was a problem hiding this comment.
| if check and self is X: | |
| if check: |
This should always do the check no matter what X is.
| else: | ||
| if not abs(one - 1) < 2 ** (-RR.precision() + 1): | ||
| raise TypeError("argument P (= %s) does not define a probability function" % f) |
There was a problem hiding this comment.
| else: | |
| if not abs(one - 1) < 2 ** (-RR.precision() + 1): | |
| raise TypeError("argument P (= %s) does not define a probability function" % f) | |
| elif not abs(one - 1) < 2 ** (-RR.precision() + 1): | |
| raise TypeError("argument P (= %s) does not define a probability function" % f) |
| if check and self is X: | ||
| one = sum(f.values()) | ||
| if isinstance(RR, RationalField): | ||
| if not one == 1: |
There was a problem hiding this comment.
| if not one == 1: | |
| if one != 1: |
|
@tscrim please take a look and tell me if any forward edit need |
Part of #9670.
This PR adds doctest coverage for
DiscreteProbabilitySpaceand fixes two bugs uncovered while writing them.Bug fixes
Missing
%substitution in error messages. TheTypeErrormessages inDiscreteProbabilitySpace.__init__contained a%splaceholder but never substitutedP, so users saw a literal%sinstead of the offending probability dictionary.check=Truealways raisedNotImplementedError.DiscreteProbabilitySpace.__init__passedcheckthrough toDiscreteRandomVariable.__init__, which unconditionally raisesNotImplementedErrorwhencheckis truthy. SinceDiscreteProbabilitySpacealready performs its own probability-sum check, passcheck=Falseto the parent.New doctests
set()— a subset of the domain, and the case where the domain is larger than the support.entropy()— fair coin (1 bit), uniform over 4 (2 bits), non-uniform (1.5 bits), and a deterministic distribution (0 bits).__init__—check=Truewith valid and invalid probabilities,codomain=QQ(exact rational check), andcodomain=ZZ(rejected).Note
The
dict_keysreproducer (DiscreteProbabilitySpace(P.keys(), P)) is fixed in #42820, which is independent of this PR. This PR's doctests use plain lists and do not depend on that fix.