| 1 |
.. _strategies_for_solution: |
| 2 |
|
| 3 |
Strategies for solution |
| 4 |
####################### |
| 5 |
|
| 6 |
---------------------------------------- |
| 7 |
Approaches to solving the MBAR equations |
| 8 |
---------------------------------------- |
| 9 |
|
| 10 |
The multistate reweighting approach to calculate free energies can be |
| 11 |
formulated in several ways. The multistate reweighting equations are |
| 12 |
a set of coupled, implicit equations for the free energies of *K* |
| 13 |
states, given samples from these *K* states. If one can calculate the |
| 14 |
energies of each of the *K* states, for each sample, then one can |
| 15 |
solve for the *K* free energies satisfying the equations. The |
| 16 |
solutions are unique only up to an overall constant, which ``pymbar`` |
| 17 |
removes by setting the first free energy to zero to 0, leaving *K-1*. |
| 18 |
free energies. |
| 19 |
|
| 20 |
By rearrangement, this set of self-consistent equations can be written |
| 21 |
as simultaneous roots to *K* equations. This set of roots also turns |
| 22 |
out to be the Jacobian of single maximum likelihood function of all |
| 23 |
the free energies. We then can find the MBAR solutions by either |
| 24 |
maximization/minimization techiques, or by root finding. |
| 25 |
|
| 26 |
Because the second derivative of the likelihood is always negative, |
| 27 |
there is only one possivle solution. However, if there is poor |
| 28 |
overlap, it is not uncommon that some of the optimal :math:`f_k` could be |
| 29 |
in extremely flat region of solution space, and therefore have |
| 30 |
significant round-off errors resulting in slow or no convergence to the |
| 31 |
solution, and low overlap can also lead to underflow and overflow |
| 32 |
leading to crashed solutions. |
| 33 |
|
| 34 |
----------------- |
| 35 |
``scipy`` solvers |
| 36 |
----------------- |
| 37 |
|
| 38 |
``pymbar`` is set up to use the ``scipy.optimize.minimize()`` and |
| 39 |
``scipy.optimize.roots()`` functionality to perform this |
| 40 |
minimization. We use only the gradient-based methods, as the |
| 41 |
analytical gradient-based optimization is obtainable from the MBAR |
| 42 |
equations. Available ``scipy.optimize.minimize()`` methods include |
| 43 |
``L-BFGS-B``, ``dogleg``, ``CG``, ``BFGS``, ``Newton-CG``, ``TNC``, ``trust-ncg``, |
| 44 |
``trust-krylov``, ``trust-exact``, and ``SLSQP``. and |
| 45 |
``scipy.optimize.roots`` options are ``hybr`` and ``lm``. Methods that |
| 46 |
take a Hessian (``dogleg``, ``Newton-CG``, ``trust-ncg``, ``trust-krylov``, |
| 47 |
``trust-exact``) are passed the analytical Hessian. Options can be |
| 48 |
passed to each of these methods through the ``MBAR`` object |
| 49 |
initialization interface. |
| 50 |
|
| 51 |
------------------ |
| 52 |
Built-in solutions |
| 53 |
------------------ |
| 54 |
|
| 55 |
In addition to the ``scipy`` solcers, ``pymbar`` also includes an |
| 56 |
adaptive solver designed directly for MBAR. At every step, the |
| 57 |
adaptive solver calculates both the next iteration of the |
| 58 |
self-consistent iterative formula presented in Shirts et |
| 59 |
al. :cite:`shirts-chodera:jcp:2008:mbar`, and takes a Newton-Raphson |
| 60 |
step. In both cases, it calculates the gradients of the points |
| 61 |
resulting after the two steps, and selects the move that makes the |
| 62 |
magnitude of the gradient (i.e. the dot product of the gradient with |
| 63 |
itself) smallest. Far from the solution, the self-consistent iteration |
| 64 |
tends have the smaller gradient, while closer to the solution, the |
| 65 |
Newton-Raphson step tends to have the smallest gradient. It always |
| 66 |
chooses the self-consistent iteration for the first `min_sc_iter` |
| 67 |
iterations, as if overlap is poor then Newton=Raphson iteration can |
| 68 |
fail. ``min_sc_iter``'s default is 2, but if one is starting from a |
| 69 |
good guess for the free energies, one could start with |
| 70 |
``min_sc_iter=0`` |
| 71 |
|
| 72 |
----------------------------- |
| 73 |
Constructing solver protocols |
| 74 |
----------------------------- |
| 75 |
|
| 76 |
We have found that in general, different solvers have different |
| 77 |
convergence properties and difference convergence behavior. Even at |
| 78 |
the same input tolerance level, different algorithms may not yield the |
| 79 |
same result. Additionally, accurate free energy estimates and other. |
| 80 |
Although data with states that have significant overlap in |
| 81 |
configuration states usually converge successfully with all simulation |
| 82 |
algorithms, different algorithms succeed and fail on different "hard" |
| 83 |
data sets. We have therefore constructed a very general interface to |
| 84 |
allow the user to try different algorithms. |
| 85 |
|
| 86 |
``pymbar`` uses the concept of a "solver protocol", where one applies |
| 87 |
a series of methods one or multiple times. In most cases, one will |
| 88 |
never need to interact with this interface, because several different |
| 89 |
protocols have already been implemented, and are accessible with |
| 90 |
string keywords. These are currently ``solver_protocl=default`` (also |
| 91 |
the default) and ``solver_protocol=robust``. |
| 92 |
|
| 93 |
However, a user has the option of creating their own solver |
| 94 |
protocol. Solver protocols are created as tuples of dictionaries, |
| 95 |
where each dictionary is an optimization operation. The user has the |
| 96 |
ability to continue with the resulting free energies each time, or |
| 97 |
restarting back from the initialized free energies. |
| 98 |
|
| 99 |
Take for example the default solver protocol, designed to give a high |
| 100 |
accuracy answer in most circumstances: |
| 101 |
|
| 102 |
.. code-block:: python |
| 103 |
|
| 104 |
solver_protocol = ( |
| 105 |
dict(method="hybr", continuation=True), |
| 106 |
dict(method="adaptive", options=dict(min_sc_iter=0)), |
| 107 |
) |
| 108 |
|
| 109 |
Here, the first pass through optimization uses the ``scipy.optimize.roots`` function ``hybr``, |
| 110 |
and then if it is successful, continues on by running the built-in ``adaptive`` method, but |
| 111 |
with no self-consistent iteration choices forced, as described above. If `hybr` fails, |
| 112 |
it will still attempted to continue on with the resulting free energies, from whatever point it |
| 113 |
ended up, issue a warning in case that `adaptive` is still unable to solve the result. |
| 114 |
|
| 115 |
The ``options`` dictionary is passed onto the method, so whatever |
| 116 |
options the scipy method uses it its documentation, it can be passed |
| 117 |
on through this approach. ``tol`` is a direct option to |
| 118 |
`scipy.optimize` methods, and not passed on through the dictionary, |
| 119 |
and thus is passed on directly in the solver protocol. |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
solver_protocol = ( |
| 124 |
dict(method="hybr"), |
| 125 |
dict(method="L-BFGS-B", tol = 1.0e-5, continuation = True, options=dict(maxiter=1000)), |
| 126 |
dict(method="adaptive", tol = 1.0e-12, options=dict(maxiter=1000,min_sc_iter=5)) |
| 127 |
) |
| 128 |
|
| 129 |
In this case, it would first use "hybr" with the default options and |
| 130 |
tolerance and if successful, exit. If not successful with "hybr", it |
| 131 |
would continue on to "L-BFGS-B", with 1000 maximum iterations, and a |
| 132 |
tolerance of :math:`10^{-5}` but would not use the results from the "hybr" |
| 133 |
call. If "L-BFGS-B" was either successful or unsuccessful, it would |
| 134 |
pass the results to "adaptive", where it would choose the |
| 135 |
self-consistent iteration the first five times, the numberof maximum |
| 136 |
iteractions would be 1000, and the tolerance is :math:`10^{-12}`. |
| 137 |
|
| 138 |
-------------- |
| 139 |
Initialization |
| 140 |
-------------- |
| 141 |
|
| 142 |
One can initialize the soution process in a number of ways. The |
| 143 |
simplest is to start from all zeros, which is the default (and also |
| 144 |
has keyword ``initialize=zeros``). If the keyword ``f_k_initial`` is |
| 145 |
used, then the length *K*. |
| 146 |
|
| 147 |
Two other options for ``initialize`` are ``BAR`` and |
| 148 |
``average-enthalpies``. ``average-enthalpies`` which approximates the |
| 149 |
free energy of each state using the average enthalpy of each states, |
| 150 |
which will be valid in the limit of no entropy differences beween |
| 151 |
states. ``initialize=BAR`` can be used whenever states are given in a |
| 152 |
natural sequence of overlap, such that state 0 has the most |
| 153 |
configurational overlap with state 1, state 1 has significant |
| 154 |
configurational overlap with both states 0 and state 2, and so forth. |
| 155 |
In the limit there is only overlap between neighboring states, MBAR |
| 156 |
converges to give the same answer for :math:`\Delta f_k = f_{k+1} - f_k` |
| 157 |
that BAR gives. Although BAR also requires an iterative solution, it |
| 158 |
is a single variable problem, and thus the *K-1* BAR iterations that |
| 159 |
need to be done are much faster than a single *K-1* dimensional |
| 160 |
problem. The initial input for state *k* in the solution process is |
| 161 |
then :math:`f_{k,initial} = \sum_j \Delta f_{j,BAR}` |
| 162 |
|
| 163 |
Note that if both ``initialize`` and ``f_k_initial`` are selected, the |
| 164 |
logic is somewhat different. Specifing ``f_k_initial`` overwrites |
| 165 |
``initialize=zeros``, but ``initialize=BAR`` starts each application |
| 166 |
of BAR with the (reduced) free energy difference between states *k* |
| 167 |
and *k+1* in from ``f_k_initial``. |
| 168 |
|
| 169 |
----------------------------- |
| 170 |
Calculating uncertainties |
| 171 |
----------------------------- |
| 172 |
|
| 173 |
The MBAR equations contain analytical estimates of uncertainties. |
| 174 |
These are essentially, however, the functional form is bit more |
| 175 |
complicated, since they include modifications for error propagation |
| 176 |
with implicit equations. |
| 177 |
|
| 178 |
For free energies and expectations, one includes the analytical |
| 179 |
uncertainties by adding the keyword ``compute_uncertainties=True``. |
| 180 |
|
| 181 |
In some cases, to peform additional error analysis, one might need |
| 182 |
access to the covariance matrix of $\ln f_k$. This is accessed in |
| 183 |
``results['Theta']``, and included by setting ``compute_theta=True``, or |
| 184 |
if ``compute_uncertainties=True`` and uncertainty_method is not |
| 185 |
``bootstrap``. |
| 186 |
|
| 187 |
If ``uncertainty_method=bootstrap``, then the analytical error |
| 188 |
analysis is not performed, and instead bootstrap samples are pulled |
| 189 |
from the original distribution. Bootstrapping is done on each set of |
| 190 |
$N_k$ samples from each *K* states individually, rather than on the |
| 191 |
set as a whole, as the number of samples drawn from each state should |
| 192 |
not change in the bootstrapping process, or it would be a different |
| 193 |
process. |
| 194 |
|
| 195 |
For bootstrapping to be used in calculating error estimates, the |
| 196 |
``MBAR`` object must be initialized with the keyword `n_bootstraps`, |
| 197 |
which must be an integer greater than zero. In general, 50--200 |
| 198 |
bootstraps should be used to estimate uncertainties with a good degree |
| 199 |
of accuracy. |
| 200 |
|
| 201 |
|
| 202 |
Note that users have complete control over the solver sequence for |
| 203 |
bootstrapped solutions, using the same API as for solvers of the |
| 204 |
original solution, with keyword ``bootstrap_solver_protocol``. As an |
| 205 |
example, the default bootstrap protocol is: |
| 206 |
|
| 207 |
.. code-block:: python |
| 208 |
|
| 209 |
bootstrap_solver_protocol = (dict(method="adaptive", tol = 1e-6, options=dict(min_sc_iter=0,maximum_iterations=100))) |
| 210 |
|
| 211 |
The solutions for bootstrapped data should be relatively close to the |
| 212 |
solutions for the original data set; additionally, they do not need to |
| 213 |
be quite as accurate, since they are used to compute the variances. |
| 214 |
|
| 215 |
Bootstrapped uncertainties (using ``uncertainty_method=bootstrap`` is |
| 216 |
also available for all functions calculating expectations, but again |
| 217 |
requires initialization with "n_bootstraps" when initalizing the MBAR |
| 218 |
object. |