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

Enable MBAR to do bootstrap error estimation #322

Merged
merged 11 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ Changes
Enhancements
- Add a parser to read serialised pandas dataframe (parquet) (issue #316, PR#317).
- workflow.ABFE allow parquet as input (issue #316, PR#317).
- Allow MBAR estimator to use bootstrap to compute error (issue #320, PR#322).

Copy link
Member

Choose a reason for hiding this comment

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

Add a deprecation for using analytical error estimate as the default in ABFE, will change to using 50 bootstraps in 2.3 (see other comments)

Fixes
- Fix the case where visualisation.plot_convergence would fail when the final
error is NaN (issue #318, PR#319).
error is NaN (issue #318, PR#319).

DeprecationWarning
- The default MBAR error estimator in workflow.ABFE.estimate will change from
analytic to bootstrap=50 (issue #320, PR#322).
Copy link
Member

Choose a reason for hiding this comment

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

indentation

Copy link
Member

Choose a reason for hiding this comment

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

say when it will change (target release, current + 2 = 2.3 I think)

Copy link
Member

Choose a reason for hiding this comment

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

Or 2.2, if you want to be aggressive, but state the target release.



06/04/2023 xiki-tempula
Expand Down
17 changes: 16 additions & 1 deletion src/alchemlyb/estimators/mbar_.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class MBAR(BaseEstimator, _EstimatorMixOut):
available via :func:`scipy.optimize.minimize` or
:func:`scipy.optimize.root`.

n_bootstraps : int, optional
Whether to use bootstrap to estimate uncertainty. `0` means use analytic error
estimation. 50~200 is a reasonable range to do bootstrap.

verbose : bool, optional
Set to ``True`` if verbose debug output from :mod:`pymbar` is desired.

Expand Down Expand Up @@ -59,6 +63,8 @@ class MBAR(BaseEstimator, _EstimatorMixOut):
`delta_f_`, `d_delta_f_`, `states_` are view of the original object.
.. versionchanged:: 2.0.0
default value for `method` was changed from "hybr" to "robust"
.. versionchanged:: 2.1.0
`n_bootstraps` option added.
"""

def __init__(
Expand All @@ -67,13 +73,15 @@ def __init__(
relative_tolerance=1.0e-7,
initial_f_k=None,
method="robust",
n_bootstraps=0,
Copy link
Member

Choose a reason for hiding this comment

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

I'd add a reminder comment

 n_bootstraps=0,    # release 2.2: change to 50 (see PR #322)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So my feeling is that for the MBAR estimator, we retain the current behaviour unless people manually turn it on as bootstrap does come with a computational cost.

It is only for the estimate method in workflow.ABFE where I know that the input is decorrelated and I'm prepared to pay the computational cost, I will set the default to 50.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's ok, we just can't make the default behave differently without a minimal warning period.

verbose=False,
):
self.maximum_iterations = maximum_iterations
self.relative_tolerance = relative_tolerance
self.initial_f_k = initial_f_k
self.method = method
self.verbose = verbose
self.n_bootstraps = n_bootstraps

# handle for pymbar.MBAR object
self._mbar = None
Expand Down Expand Up @@ -108,8 +116,15 @@ def fit(self, u_nk):
verbose=self.verbose,
initial_f_k=self.initial_f_k,
solver_protocol=self.method,
n_bootstraps=self.n_bootstraps,
)
if self.n_bootstraps == 0:
uncertainty_method = None
else:
uncertainty_method = "bootstrap"
out = self._mbar.compute_free_energy_differences(
return_theta=True, uncertainty_method=uncertainty_method
)
out = self._mbar.compute_free_energy_differences(return_theta=True)
self._delta_f_ = pd.DataFrame(
out["Delta_f"], columns=self._states_, index=self._states_
)
Expand Down
16 changes: 16 additions & 0 deletions src/alchemlyb/tests/test_fep_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ def test_states_(self, estimator):
_estimator = estimator()
with pytest.raises(AttributeError):
_estimator.states_ = 1


def test_bootstrap(gmx_benzene_Coulomb_u_nk):
u_nk = alchemlyb.concat(gmx_benzene_Coulomb_u_nk)
mbar = MBAR(n_bootstraps=2)
mbar.fit(u_nk)
mbar_bootstrap_mean = mbar.delta_f_.loc[0.00, 1.00]
mbar_bootstrap_err = mbar.d_delta_f_.loc[0.00, 1.00]

mbar = MBAR()
mbar.fit(u_nk)
mbar_mean = mbar.delta_f_.loc[0.00, 1.00]
mbar_err = mbar.d_delta_f_.loc[0.00, 1.00]

assert mbar_bootstrap_mean == mbar_mean
assert mbar_bootstrap_err != mbar_err
11 changes: 11 additions & 0 deletions src/alchemlyb/tests/test_workflow_ABFE.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ def test_single_estimator_mbar(self, workflow, monkeypatch):
summary = workflow.generate_result()
assert np.isclose(summary["MBAR"]["Stages"]["TOTAL"], 21.645742066696315, 0.1)

def test_mbar_n_bootstraps(self, workflow, monkeypatch):
monkeypatch.setattr(workflow, "estimator", dict())
workflow.estimate(estimators="MBAR", n_bootstraps=2)
summary = workflow.generate_result()
bootstrap_error = summary["MBAR_Error"]["Stages"]["TOTAL"]
monkeypatch.setattr(workflow, "estimator", dict())
workflow.estimate(estimators="MBAR", n_bootstraps=0)
summary = workflow.generate_result()
non_bootstrap_error = summary["MBAR_Error"]["Stages"]["TOTAL"]
assert bootstrap_error != non_bootstrap_error

def test_single_estimator_ti(self, workflow, monkeypatch):
monkeypatch.setattr(workflow, "estimator", dict())
monkeypatch.setattr(workflow, "summary", None)
Expand Down
7 changes: 7 additions & 0 deletions src/alchemlyb/workflows/abfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ def estimate(self, estimators=("MBAR", "BAR", "TI"), **kwargs):
'MBAR']. Note that the estimators are in their original form where
no unit conversion has been attempted.

.. versionchanged:: 2.1.0
DeprecationWarning for using analytic error for MBAR estimator.
Copy link
Member

Choose a reason for hiding this comment

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

indent


"""
# Make estimators into a tuple
if isinstance(estimators, str):
Expand Down Expand Up @@ -436,6 +439,10 @@ def estimate(self, estimators=("MBAR", "BAR", "TI"), **kwargs):
for estimator in estimators:
if estimator == "MBAR":
logger.info("Run MBAR estimator.")
warnings.warn(
"From 2.2.0, n_bootstraps=50 will be the default for estimating MBAR error.",
DeprecationWarning,
)
self.estimator[estimator] = MBAR(**kwargs).fit(u_nk)
elif estimator == "BAR":
logger.info("Run BAR estimator.")
Expand Down