Skip to content

Add doctests and fix bugs in DiscreteProbabilitySpace - #42828

Merged
vbraun merged 9 commits into
sagemath:developfrom
vivek41-glitch:fix-probability-dps-doctests
Nov 8, 2026
Merged

vbraun merged 9 commits into
sagemath:developfrom
vivek41-glitch:fix-probability-dps-doctests

Conversation

@vivek41-glitch

Copy link
Copy Markdown
Contributor

Part of #9670.

This PR adds doctest coverage for DiscreteProbabilitySpace and fixes two bugs uncovered while writing them.

Bug fixes

  1. Missing % substitution in error messages. The TypeError messages in DiscreteProbabilitySpace.__init__ contained a %s placeholder but never substituted P, so users saw a literal %s instead of the offending probability dictionary.

  2. check=True always raised NotImplementedError. DiscreteProbabilitySpace.__init__ passed check through to DiscreteRandomVariable.__init__, which unconditionally raises NotImplementedError when check is truthy. Since DiscreteProbabilitySpace already performs its own probability-sum check, pass check=False to 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=True with valid and invalid probabilities, codomain=QQ (exact rational check), and codomain=ZZ (rejected).

Note

The dict_keys reproducer (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.

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.
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR (built with commit b465921; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@tscrim @pelegm please review when u have time

@pelegm

pelegm commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

I'm sorry I am not active in this project these day.

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +416 to +419
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.

Comment thread src/sage/probability/random_variable.py Outdated
sage: X.entropy().n(digits=10)
1.500000000

An outcome with zero probability contributes nothing::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test is not really checking what the docstring is saying (as I am interpreting how it is stated).

Comment thread src/sage/probability/random_variable.py Outdated
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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.

Comment thread src/sage/probability/random_variable.py Outdated
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
raise TypeError("Argument P (= %s) does not define a probability function" % P)
raise TypeError("argument P (= %s) does not define a probability function" % P)

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +414 to +416
EXAMPLES::

A fair coin has one bit of entropy::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Everything after the EXAMPLES:: is over-indented.

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +372 to +374
# The probability function has already been checked above, so
# pass check=False to avoid DiscreteRandomVariable's
# unimplemented check.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry, I thought I had responded to this. I think we should do it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@tscrim I have address all ur feedback i m not sure about last suggestion please let me know what do u think

@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@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.
@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@tscrim please review when u have time and let me know what fix needs to be done!

@tscrim tscrim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry for being slow about this. These should be the last round of changes.

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +97 to +98
# Called from DiscreteProbabilitySpace.__init__: ``f`` is
# the probability function and must sum to one.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# 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).

Comment thread src/sage/probability/random_variable.py Outdated

EXAMPLES::

A fair coin has an entropy of 1::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All of these examples are overindented.

Comment thread src/sage/probability/random_variable.py Outdated
r"""
The entropy of the probability space.

EXAMPLES::

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
EXAMPLES::
EXAMPLES:

@vivek41-glitch
vivek41-glitch requested a review from tscrim October 19, 2026 10:20
@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@tscrim i commit fixes please review

@tscrim tscrim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry for being slow (it's in the middle of the semester here). Here are the last round of changes.

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +69 to +73
- ``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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- ``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

Comment thread src/sage/probability/random_variable.py Outdated
RR = RealField()
else:
RR = codomain
if check and self is X:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if check and self is X:
if check:

This should always do the check no matter what X is.

Comment thread src/sage/probability/random_variable.py Outdated
Comment on lines +101 to +103
else:
if not abs(one - 1) < 2 ** (-RR.precision() + 1):
raise TypeError("argument P (= %s) does not define a probability function" % f)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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)

Comment thread src/sage/probability/random_variable.py Outdated
if check and self is X:
one = sum(f.values())
if isinstance(RR, RationalField):
if not one == 1:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if not one == 1:
if one != 1:

@vivek41-glitch
vivek41-glitch requested a review from tscrim October 28, 2026 16:34
@vivek41-glitch

Copy link
Copy Markdown
Contributor Author

@tscrim please take a look and tell me if any forward edit need

@tscrim tscrim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you. LGTM.

@vbraun
vbraun merged commit 8bae313 into sagemath:develop Nov 8, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants