# Weighted Histogram Analysis Method

The Weighted Histogram Analysis Method (WHAM) is one of the earliest methods that take into account information from all Intermediate States. By analyzing all the information at once, we can reduce the number of cycles and loops me must run through, improving efficiency. The precursor to WHAM and first version of multiple histogram relighting techniques came from Ferrenberg and Swendsen;[1] WHAM was developed later for alchemical simulations[2].

WHAM works on the principal that if you have a discrete number of states, you can create a histogram with discrete bins that provide you a relative probability of observing the states of interest, assuming you create the bins along whatever reaction path you have selected. In our cases, the reaction path is the alchemical variable. From these probabilities, you can calculate free energies and other observables.

# Derivation

WHAM's derivation[2] is best done by starting from visualizing the problem. If you are given $K$ number of states, a number of run simulations, $S$, $N_i$ samples from each simulation, and then $K$ free energies to calculate: $A_1,\,A_2,\,\cdots A_K$; we now wish to assign the reweighed potentials into bins, $B$. Terms common to this site's definitions page are preserved and while new terms are explained in context. Consider the core free energy difference equation between two states $i$ and $j$ of

$\displaystyle \Delta A_{ij} = -k_B T \ln \frac{Q_j}{Q_i}$

Instead of solving this directly, we will look at the the probability density, which is the ratio between the two partition functions (i.e. ignore the $-k_B T \ln$ part). Recall that the partition function of any state is often written as

$\displaystyle Q_i = \int_{V_i} \Omega_i(U) \exp\left(-\beta U \right) dU$,
$\displaystyle Q_j = \int_{V_j} \Omega_j(U) \exp\left(-\beta U \right) dU$

Where we have defined an unknown density of states for each of the thermodynamic states we are simulating, $\Omega_i$.

However, we could generalize to arbitrary integration coordinates, not just energy, which gives us:

$\displaystyle Q_i = \int_{V_i} \Omega_i(\vec{q}) \exp\left(-\beta U(\vec{q}) \right) d\vec{q}$,
$\displaystyle Q_j = \int_{V_j} \Omega_j(\vec{q}) \exp\left(-\beta U(\vec{q}) \right) d\vec{q}$

$\vec{q}$ could be coordinates, in which case $\Omega_i(\vec{q}) = 1$, but more usually it has only one or two dimensions.

If we assume discrete states, with counts in each bin $B$, we can write the density of states out for a given simulation, $i$ as:

$\displaystyle \Omega_i(\vec{q},\lambda_i) = B_i(\vec{q},\lambda_i)\exp\left[\left(\sum_{j=0}^K \beta_i \lambda_{j,i} U(\vec{q}_j) \right)-\beta_i A_i\right]$

where the set of states available during simulation $i$ is $\left\{\lambda\right\}_i = \left\{\lambda_1, \lambda_2, \dots \lambda_K \right\}_i = \left\{\lambda_{1,i}, \lambda_{2,i}, \dots \lambda_{K,i} \right\}$ and $B_i$ is the value of the histogram bin $i$ evaluated at $\vec{q}$ and $\lambda_i$. The best estimate for the density of states is then

$\displaystyle \sum_{i=1}^S \omega_i(\vec{q})\Omega_i(\vec{q},\lambda_i)$
$\displaystyle \sum_{i=1}^S \omega_i(\vec{q})=1$

The best estimators for $\omega_i$ are the ones that minimize the statistical noise. It turns out then, that the best estimator is the one that minimize the error of $B_i$ for all $i$. The error of any individual $B_i$ is then

$\delta^2 B_i = g_i\left\langle B_i \right\rangle$

where $g_i = 1+2\tau_i$ and $\tau_i$ is the correlation time of a given simulation. The best estimator for the expectation of the bin value is then

$\displaystyle \hat{\left\langle B_i \right\rangle} = N_i\Omega\exp\left(\beta_i A_i - \beta\sum_{j=0}^K \lambda_{j,i} U(\vec{q}_j) \right)$.

Please note that $\Omega$ does not have a subscript in the previous equation, as it is now the density of states determined from all of the simulations.

From here you can just substitute back in to estimate $/Q_i$ to get the final WHAM equation of

$\displaystyle \hat{Q_i} = \sum_{j=1}^K \frac{\sum\limits_{x=1}^S g_x^{-1} B_x(\vec{q},\lambda_j) \exp\left[-\beta\lambda_0\vec{q}_0 - \beta\sum\limits_{l=1}^K \lambda_l U_l(\vec{q}) \right]}{\sum\limits_{y=1}^S g_y^{-1} N_y \exp\left[\beta A_y - \beta\lambda_0\vec{q}_0 - \beta\sum\limits_{m=1}^K \lambda_{m,y} U_m(\vec{q}) \right]}$

where the nought subscript indicates the conditions at the unmodified state. It is then trivial to get a free energy of this state. This is only a relative free energy though as the system must be self-consistently solved. It is common to set one of the states to a free energy of zero so you can calculate differences in free energy between states.

## Zero Width Bins

It is possible to take the bin width to zero in the WHAM formula. Although this is a limiting case, it does clean up the equation a bit and looks like

$\displaystyle \hat{A_i} = - \beta^{-1}\ln \sum_{k=1}^K \sum_{n=1}^{N_k} \frac{ \exp[-\beta U_i(\vec{q}_{kn})]}{\sum\limits_{k'=1}^K N_{k'} \, \exp[\beta A_{k'} - \beta U_{k'}(\vec{q}_{kn})]}$

which turns out to be the exact equation for MBAR.[3]

# Usage of WHAM

Given a particular implementation of WHAM (see below), one may be tempted just to let the analysis give you a black-box result. You should always understand what is happening and so the results should not be taken on blind faith alone.

Since WHAM collects data from all states, you will need to calculate $\Delta U_{k,j}(\vec{q})$ for all $j=1,2,\cdots,K$ states. Even though $\Delta U_{k,k}(\vec{q})$ does not need to be calculated since it should be zero, it is highly recommended that you do. This lets you check to see if the re-evaluation function is working as intended, and help you identify possible errors. Although this does not tell you what the error is, it will at least tell you there is one. One possible source of error is the output of your coordinate files should be higher accuracy than a standard PDB files, $10^5$Å may be sufficient but binary format coordinates are preferred.

Because you are applying a discrete set and finite number of bins, there is a bias introduced since all variables must fall into the bins. This is the predominate problem with WHAM and everything that comes with it, so exercise caution. Error estimates are not directly available for WHAM, and so methods such as bootstrap sampling are required.