# Linkage disequilibrium between two loci from phased haplotype counts

`popgen_linkage_disequilibrium` · Runs: On the server · Level: Checked against a reference: exact arithmetic: Python fractions on the definitions, written before the engine, with hand-worked cases, and a second implementation in Python by a separate agent of the same model family

Works out D, Dmax, signed D′ and r² between two biallelic loci from the four phased haplotype counts.

Research and education software; not medical advice, and not for diagnosis or treatment decisions. It computes from the inputs it receives. It does not check the person's data, design or sample.

[Catalogue](https://www.drybench.com/mcp/catalogue) / [Genetics and ecology](https://www.drybench.com/mcp/catalogue/genetics-and-ecology)

[Ask your assistant](https://www.drybench.com/mcp/connect)

```cite
Linkage disequilibrium between two loci from phased haplotype counts (popgen_linkage_disequilibrium), Drybench MCP server 1.0.0; popgen-linkage-disequilibrium@1.0.2 (Checked against a reference); https://www.drybench.com/mcp/tools/popgen-linkage-disequilibrium
```

## What it is

Returns D, Dmax, signed D′ and r² between two biallelic loci from the four phased haplotype counts. Use when haplotypes AB, Ab, aB and ab have been counted in a sample, for a candidate pair or a teaching table. Not for linkage in a cross or family from recombinants: use \`genetics\_lod\_score\`; not for a test of association: use \`stats\_contingency\_table\`. Counts in unit 1; D′ and r² withheld at a monomorphic locus; unphased genotypes are not accepted.

Field: [Genetics, evolution and ecology](https://www.drybench.com/mcp/catalogue/genetics-and-ecology).

## What it does

**You give**

- `countAB` (required): Phased haplotypes AB counted, 0 or more. Must be at least 0 and at most 10000000.
- `countAb` (required): Phased haplotypes Ab counted, 0 or more. Must be at least 0 and at most 10000000.
- `countaB` (required): Phased haplotypes aB counted, 0 or more. Must be at least 0 and at most 10000000.
- `countab` (required): Phased haplotypes ab counted, 0 or more. Must be at least 0 and at most 10000000.

No request is recorded for this tool.

**Limits**

- Research and teaching arithmetic on the caller's data.
- Not for diagnosis, genetic counselling, clinical, breeding, conservation-management or animal-welfare decisions.
- The tool checks arithmetic, not the data.
- D, D′ and r² from phased haplotype counts at two biallelic loci; no test of association and no inference about selection or demography.
- Every computed number a reply returns, other than an integer or an exact decimal, is rounded once to 12 significant figures: to the nearest, a change of at most 5 × 10⁻¹² of its value, except that a lower bound is rounded down and an upper bound up, a change of less than 1 × 10⁻¹¹; the method computes, and decides any pass or fail, on the unrounded value.

**It will not answer when**

- all four haplotype counts are zero.

### Every input, with its units

4 inputs

- `countAB` (required): Phased haplotypes AB counted, 0 or more. Must be at least 0 and at most 10000000.
- `countAb` (required): Phased haplotypes Ab counted, 0 or more. Must be at least 0 and at most 10000000.
- `countaB` (required): Phased haplotypes aB counted, 0 or more. Must be at least 0 and at most 10000000.
- `countab` (required): Phased haplotypes ab counted, 0 or more. Must be at least 0 and at most 10000000.

### Every refusal, with its code

1 refusal

| Code | When |
| --- | --- |
| `NO_HAPLOTYPES` | all four haplotype counts are zero. |

## The calculation

The method's equations, constants and worked example.

### [Linkage disequilibrium between two biallelic loci from phased haplotype counts](https://www.drybench.com/methods/m/popgen-linkage-disequilibrium/1.0.2)

`popgen-linkage-disequilibrium@1.0.2`

**Equation 1**

```text
n = nAB + nAb + naB + nab
pAB = nAB/n
pA = (nAB + nAb)/n
pB = (nAB + naB)/n
```

| Symbol | Meaning |
| --- | --- |
| `n` | haplotypes counted |
| `nAB, nAb, naB, nab` | counts of the four phased haplotypes |
| `pAB` | frequency of haplotype AB |
| `pA` | frequency of allele A at the first locus |
| `pB` | frequency of allele B at the second locus |

**Equation 2**

```text
D = pAB − pA·pB = (nAB·nab − nAb·naB)/n²
```

| Symbol | Meaning |
| --- | --- |
| `D` | coefficient of linkage disequilibrium |
| `pAB, pA, pB` | haplotype and allele frequencies |

Source 1.

**Equation 3**

```text
Dmax = min(pA(1 − pB), (1 − pA)pB) when D > 0
min(pA·pB, (1 − pA)(1 − pB)) when D ≤ 0
```

| Symbol | Meaning |
| --- | --- |
| `Dmax` | maximum attainable magnitude of D given the allele frequencies |
| `pA, pB` | allele frequencies |

Source 1.

**Equation 4**

```text
D′ = D/Dmax, signed as D
D′ = 0 when D = 0
```

| Symbol | Meaning |
| --- | --- |
| `D′` | D over its maximum attainable magnitude given the allele frequencies, signed as D |
| `D, Dmax` | D and its maximum magnitude |

Source 1.

**Equation 5**

```text
r² = D²/(pA(1 − pA)·pB(1 − pB))
```

| Symbol | Meaning |
| --- | --- |
| `r²` | squared correlation of allele indicators on haplotypes |
| `D, pA, pB` | D and the allele frequencies |

Source 1.

A definition, not a model: the equation states what the quantity means. A different convention gives a different number.

**Worked example**

Worked example: one case of popgen-linkage-disequilibrium\@1.0.2, from its inputs through each step to the answer. Values are written as a person writes them; the working carries more digits, so your own figure may differ in the last digit shown.

**Inputs**

- countAB = 40
- countAb = 10
- countaB = 10
- countab = 40

**Steps**

1. pA = 0.5, from countAB, countAb, countaB, countab; by equation 1; gives alleleFrequencyA.
2. pB = 0.5, from countAB, countAb, countaB, countab; by equation 1; gives alleleFrequencyB.
3. d = 0.15, from countAB, countAb, countaB, countab, pA, pB; by equation 2; gives d.
4. dMax = 0.25, from pA, pB, d; by equation 3; gives dMax.
5. dPrime = 0.6, from d, dMax; by equation 4; gives dPrime.
6. r2 = 0.36, from d, pA, pB; by equation 5; gives rSquared.

The working, as the case writes it:

```text
n 100
pAB 0.40, pA 0.50, pB 0.50
D = 0.40 - 0.25 = 0.15
D > 0 so Dmax = min(0.5 x 0.5, 0.5 x 0.5) = 0.25
D' = 0.15/0.25 = 0.6
r2 = 0.0225/(0.25 x 0.25) = 0.36.
```

**Answer**

- alleleFrequencyA = 0.5
- alleleFrequencyB = 0.5
- d = 0.15
- dMax = 0.25
- dPrime = 0.6
- rSquared = 0.36

Where these values came from: our own exact-arithmetic script and a hand-worked case; stored reference values: exact rational arithmetic on the definitions.

At build the tool returned this answer within the tolerance the file states: D, Dmax, D', r2 and allele frequencies absolute 1e-14; statuses, codes, units and withheld rules exact.

Who worked this case: not recorded.

## Sources

1. VanLiere JM, Rosenberg NA, Mathematical properties of the r² measure of linkage disequilibrium, Theoretical Population Biology 74:130–137 (2008) Used for: Linkage disequilibrium between two biallelic loci from phased haplotype counts. At: p. 135, section 2.5, the text after Eq. (13): D = p\_ab − p\_a p\_b; p. 135, section 2.5, the text after Eq. (13), after Lewontin 1964, as stated in VanLiere and Rosenberg 2008: Dmax = min(p\_a p\_b, (1 − p\_a)(1 − p\_b)) if D \< 0 and min(p\_a(1 − p\_b), (1 − p\_a)p\_b) if D > 0 (Lewontin, 1964); D = 0 taken in the D ≤ 0 branch, the record's, where D′ = 0 either way; equation 13, p. 135, section 2.5: D′ = D/Dmax; equation 1, p. 131, section 2, after Hill and Robertson 1968, as stated in VanLiere and Rosenberg 2008: r² = (p\_ab − p\_a p\_b)²/(p\_a(1 − p\_a)p\_b(1 − p\_b)). **Read in full, 24 November 2026**

Read by AI-assisted sessions. No person has checked these readings.

## How it is checked

**Level:** Checked against a reference.

**Checked against:** Our own exact-arithmetic script and a hand-worked case.

**Cases:** No case set is linked to this version.

**Not checked:** Lewontin 1964 (PMC1210557: the scan is behind a bot check for every session) and Hill and Robertson 1968 (not in PMC) were not read; Dmax is taken as VanLiere and Rosenberg 2008 attribute it to Lewontin; no published worked table is compared. Each count is from zero to ten million and not all zero (the tool's schema; NO\_HAPLOTYPES when all are zero); with n at most forty million, n² is below 2^53, so D's cross-product, n² and the Dmax products are exact integers, and the sign of D is decided on the integer cross-product.

No independent scientist has yet reviewed this.

### What was not checked, in full

from the method records

- Lewontin 1964 (PMC1210557: the scan is behind a bot check for every session) and Hill and Robertson 1968 (not in PMC) were not read; Dmax is taken as VanLiere and Rosenberg 2008 attribute it to Lewontin; no published worked table is compared.
- Each count is from zero to ten million and not all zero (the tool's schema; NO\_HAPLOTYPES when all are zero); with n at most forty million, n² is below 2^53, so D's cross-product, n² and the Dmax products are exact integers, and the sign of D is decided on the integer cross-product.
- Genotype (unphased) data and their EM estimate, multi-allelic loci and any test of association are outside the method.
- Whether the haplotypes were phased correctly is the caller's.

### Changes to its methods

0 changes

No change is logged for these method versions.
