# Distance Estimation¶

## MinHash Jaccard estimation¶

Given \(k\)-mer sets \(A\) and \(B\), the MinHash algorithm provides an estimation of the Jaccard index:

where \(A_s\) and \(B_s\) are subsets such that \(\lvert A_s \cup B_s \rvert\) is equal to the sketch size, \(s\), allowing for a known error bound as suggested by Broder [1]. This is done by using a merge-sort algorithm to find common values between the two sorted sketches and terminating when the total number of hashes seen reaches the sketch size (or all hashes in both sketches have been seen).

## Mash distance formulation¶

For mutating a sequence with \(t\) total \(k\)-mers and a conserved \(k\)-mer count \(w\), an approximate mutation rate \(d\) can be estimated using a Poisson model of mutations occurring in \(k\)-mers, as suggested by Fan et al. [2]:

In order to use a Jaccard estimate \(j\) between two \(k\)-mer sets of arbitrary sizes, the Jaccard estimate can be framed in terms of the conserved \(k\)-mer count \(w\) and the average set size \(n\):

To substitute \(n\) for the total \(k\)-mer count \(t\) in the mutation estimation, this approximation can be reformulated as:

Substituting \(\frac w n\) for \(\frac w t\) thus yields the Mash distance:

## Assessing significance with p-values¶

Since MinHash distances are probabilistic estimates, it is important to
consider the probability of seeing a given distance by chance. `mash dist`

thus provides p-values with distance estimations. Lower p-values correspond to
more confident distance estimations, and will often be rounded down to 0 due to
floating point limits. If p-values are high (above, say, 0.01), the \(k\)-mer size
is probably too small for the size of the genomes being compared.

When estimating the distance of genome 1 and genome 2 from sketches with the properties:

\(\Sigma\) := alphabet

\(k\) := \(k\)-mer size

\(l_1\) := length of genome 1

\(l_2\) := length of genome 2

\(s\) := sketch size

\(x\) := number of shared \(k\)-mers between sketches of size \(s\) of genome 1 and genome 2

...the chance of a \(k\)-mer appearing in random sequences of lengths \(l_1\) and \(l_2\) are estimated as:

The expected Jaccard index of the sketches of the random sequences is then:

...and the probability of observing at least \(x\) shared \(k\)-mers can be estimated with the tail of a cumulative binomial distribution:

[1] | Broder, A.Z. On the resemblance and containment of documents. Compression and Complexity of Sequences 1997 - Proceedings, 21-29 (1998). |

[2] | Fan, H., Ives, A.R., Surget-Groba, Y. & Cannon, C.H. An assembly and alignment-free method of phylogeny reconstruction from next-generation sequencing data. BMC genomics 16, 522 (2015). |