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

Correct the order of rvs sent to compile_dlogp in find_MAP #5928

Merged
merged 1 commit into from
Jun 26, 2022

Conversation

quantheory
Copy link
Contributor

@quantheory quantheory commented Jun 25, 2022

This PR is a quick fix for the bad behavior of find_MAP in v4 (closes #5923).

When using a gradient-based method in scipy.minimize, we need the order of derivatives in the Jacobian to match the order of the variables in x. Since x0.point_map_info is used to define this order in the rest of the function, I just use it to set the order of variables in the rvs argument sent to compile_dlogp, which fixes the issue.

I've also added test_find_MAP_issue_5923, which fails for the old code and passes in this commit. (In fact, the original test_find_MAP test would also fail if we used a starting point slightly farther from the MAP.)

@@ -120,7 +120,8 @@ def find_MAP(
compiled_logp_func = DictToArrayBijection.mapf(model.compile_logp(jacobian=False), start)
logp_func = lambda x: compiled_logp_func(RaveledVars(x, x0.point_map_info))

rvs = [model.values_to_rvs[value] for value in vars]
vars_dict = {var.name: var for var in vars}
Copy link
Member

Choose a reason for hiding this comment

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

we could replace the var_names set above with this dict so that we don't create two objects with similar purposes.

@ricardoV94
Copy link
Member

Looks perfect! I left a small refactoring suggestion above, let me know what you think.

@quantheory quantheory force-pushed the fix-find_MAP-vars-order branch from 5ff87c0 to 4970a76 Compare June 25, 2022 19:52
@quantheory
Copy link
Contributor Author

Makes sense. I've made the change you suggested.

@codecov
Copy link

codecov bot commented Jun 25, 2022

Codecov Report

Merging #5928 (4970a76) into main (729f79c) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #5928   +/-   ##
=======================================
  Coverage   89.52%   89.52%           
=======================================
  Files          73       73           
  Lines       13267    13267           
=======================================
  Hits        11877    11877           
  Misses       1390     1390           
Impacted Files Coverage Δ
pymc/tuning/starting.py 92.56% <100.00%> (ø)

@ricardoV94 ricardoV94 merged commit be048a4 into pymc-devs:main Jun 26, 2022
@ricardoV94
Copy link
Member

Thanks @quantheory!

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.

Variables in logp and dlogp functions used in find_MAP are not always aligned
3 participants