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

Require all step methods to return stats #6313

Merged
merged 2 commits into from
Nov 19, 2022

Conversation

michaelosthege
Copy link
Member

@michaelosthege michaelosthege commented Nov 18, 2022

The reason for these changes are the resulting simplification of code, including simpler branching and less type ambiguity.

This change may break custom step methods, but whoever had enough skill to implement a custom step method should have no trouble to append a , [] to the return of their (a)step method.

Closes #6270

Checklist

Major / Breaking Changes

  • Reuires all step methods to return stats from their step/asteo method.
  • The BlockedStep.generates_stats attribute was removed.

Bugfixes / New features

  • Removed a branch in Compound.step that supported namedtuple stats which are in violation of the BlockedStep.step signature.

Docs / Maintenance

  • Added & fixed some type hints on step and astep methods. (arraystep.py now passing mypy!)

@michaelosthege michaelosthege self-assigned this Nov 18, 2022
@michaelosthege michaelosthege added maintenance major Include in major changes release notes section labels Nov 18, 2022
@michaelosthege michaelosthege added this to the v4.4.0 milestone Nov 18, 2022
@codecov
Copy link

codecov bot commented Nov 18, 2022

Codecov Report

Merging #6313 (da7e14a) into main (4acd98e) will increase coverage by 0.05%.
The diff coverage is 88.98%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6313      +/-   ##
==========================================
+ Coverage   94.17%   94.22%   +0.05%     
==========================================
  Files         111      111              
  Lines       23908    23865      -43     
==========================================
- Hits        22515    22487      -28     
+ Misses       1393     1378      -15     
Impacted Files Coverage Δ
pymc/step_methods/hmc/hmc.py 92.85% <ø> (-0.13%) ⬇️
pymc/step_methods/hmc/nuts.py 97.29% <ø> (-0.02%) ⬇️
pymc/step_methods/metropolis.py 83.76% <76.78%> (+0.39%) ⬆️
pymc/aesaraf.py 94.86% <100.00%> (ø)
pymc/blocking.py 95.55% <100.00%> (+0.20%) ⬆️
pymc/sampling/mcmc.py 92.21% <100.00%> (-0.06%) ⬇️
pymc/sampling/parallel.py 87.76% <100.00%> (+0.43%) ⬆️
pymc/sampling/population.py 72.22% <100.00%> (+0.79%) ⬆️
pymc/step_methods/arraystep.py 94.73% <100.00%> (+0.41%) ⬆️
pymc/step_methods/compound.py 100.00% <100.00%> (+15.38%) ⬆️
... and 4 more

@michaelosthege michaelosthege marked this pull request as ready for review November 18, 2022 23:49
The reason for this change is the resulting simplification of code,
including simpler branching and less type ambiguity.
At the same time it allowed for fixing of a lot of type hints
and method signatures on step methods.

Closes pymc-devs#6270
def astep(
self, apoint: RaveledVars, point: PointType, *args
) -> Union[RaveledVars, Tuple[RaveledVars, StatsType]]:
def astep(self, apoint: RaveledVars, *args) -> Tuple[RaveledVars, StatsType]:
Copy link
Member Author

Choose a reason for hiding this comment

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

The exact composition of the args is specified via the constructor parameters 🤐

def astep(self, q0: RaveledVars, logp) -> Tuple[RaveledVars, List[Dict[str, Any]]]:

def astep(self, q0: RaveledVars, *args) -> Tuple[RaveledVars, StatsType]:
logp = args[0]
Copy link
Member Author

Choose a reason for hiding this comment

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

BinaryMetropolis is not even covered by our tests!

Comment on lines 504 to 493
def astep(self, q0: RaveledVars, *args) -> Tuple[RaveledVars, StatsType]:
logp: Callable[[RaveledVars], np.ndarray] = args[0]
Copy link
Member Author

Choose a reason for hiding this comment

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

BinaryGibbsMetropolis is also not covered by tests!

if rng is None:
rng = nr
return rng.normal(scale=self.s)
return (rng or nr).normal(scale=self.s)
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes resolve two mypy errors:

  1. rng is no longer reassigned, and
  2. the (rng or nr) has type Generator | Module("numpy.random") which resolves Item "None" of "Optional[Generator]" has no attribute "normal"

Copy link
Member

Choose a reason for hiding this comment

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

Looks much less readable though

@ricardoV94
Copy link
Member

We shouldn't remove the trust_input. It allows evaluating the Aesara function much faster because it doesn't check the input types are valid/need to be converted every time it's called

@michaelosthege
Copy link
Member Author

michaelosthege commented Nov 19, 2022

We shouldn't remove the trust_input. It allows evaluating the Aesara function much faster because it doesn't check the input types are valid/need to be converted every time it's called

But we're not using the trust_input anywhere in the codebase! Or is it a feature of the Aesara function that is not correctly advertised by the API?
Because mypy complained about it

@michaelosthege
Copy link
Member Author

Ah I see, it's some feature of the function... I'll investigate and drop that commit.

Anything else you want to get changed, @ricardoV94 ?

@michaelosthege michaelosthege merged commit 2c3f544 into pymc-devs:main Nov 19, 2022
@michaelosthege michaelosthege deleted the issue-6270 branch November 19, 2022 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance major Include in major changes release notes section
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require step methods to emit sampler stats
2 participants