This paper reviews the mathematical basis of maximum likelihood. The likelihood function for macromolecular structures is extended to include prior phase information and experimental standard uncertainties. The assumption that different parts of a structure might have different errors is considered. A method for estimating sigma(A) using 'free' reflections is described and its effects analysed. The derived equations have been implemented in the program REFMAC. This has been tested on several proteins at different stages of refinement (bacterial alpha-amylase, cytochrome c', cross-linked insulin and oligopeptide binding protein). The results derived using the maximum-likelihood residual are consistently better than those obtained from least-squares refinement.