The discrepancy between the observed pairwise distances D_{ij} and the distances T_{ij} over a phylogenetic tree (i.e. the sum of the branch lengths in the path from leaf i to leaf j) is measured by : S = \sum_{ij} w_{ij} (D_{ij}-T_{ij})^2 where the weights w_{ij} depend on the least squares method used. Least squares distance tree construction aims to find the tree (topology and branch lengths) with minimal S. This is a non-trivial problem. It involves searching the discrete space of unrooted binary tree topologies whose size is exponential in the number of leaves. For n leaves there are 1 • 3 • 5 • ... • (2n-3) different topologies. Enumerating them is not feasible already for a small number of leaves. Heuristic search methods are used to find a reasonably good topology. The evaluation of S for a given topology (which includes the computation of the branch lengths) is a
linear least squares problem. There are several ways to weight the squared errors (D_{ij}-T_{ij})^2, depending on the knowledge and assumptions about the variances of the observed distances. When nothing is known about the errors, or if they are assumed to be independently distributed and equal for all observed distances, then all the weights w_{ij} are set to one. This leads to an ordinary least squares estimate. In the weighted least squares case the errors are assumed to be independent (or their correlations are not known). Given independent errors, a particular weight should ideally be set to the inverse of the variance of the corresponding distance estimate. Sometimes the variances may not be known, but they can be modeled as a function of the distance estimates. In the Fitch and Margoliash method for instance it is assumed that the variances are proportional to the squared distances. ==Generalized least squares==