ld_stats
Linkage disequilibrium between two biallelic loci (A/a, B/b) from phased haplotype counts, or from unphased 3x3 genotype counts by EM gene counting: haplotype frequencies, D, Lewontin's D', r, r², χ-square (1 df) with its tail probability, and the decay D_t = D_0 (1 - c)^t of D, D' and r² under random mating with recombination fraction c.
- Field
- Genomics
- Version
- 0.1.0
- Runs as
- Compiled (Rust)
- Typical run
- 0.05 s
Compiled from source: run sh packs/install.sh once before admitting it.
Known answers it must pass
r0001closed form, independence: counts equal n pA pB (pA=0.6, pB=0.3, n=1000) give D = 0, D' = 0, r² = 0, χ-square = 0, p = 1 (exact up to rounding, abs 10⁻¹²)passed
D0 ± 10⁻¹²6.93889 × 10⁻¹⁸lockedunder 0.01 % of the tolerance usedD_prime0 ± 10⁻¹²5.78241 × 10⁻¹⁷lockedunder 0.01 % of the tolerance usedr20 ± 10⁻¹²9.55322 × 10⁻³⁴lockedunder 0.01 % of the tolerance usedchi_square0 ± 10⁻⁹9.55322 × 10⁻³¹lockedunder 0.01 % of the tolerance usedp_value1 ± 10⁻⁹1lockedunder 0.01 % of the tolerance usedp_A0.6 ± 6 × 10⁻¹³0.6lockedunder 0.01 % of the tolerance usedp_B0.3 ± 3 × 10⁻¹³0.3lockedunder 0.01 % of the tolerance usedr0002closed form, complete LD: only AB (40) and ab (60) give D' = 1, r² = 1, D = pAB - pA pB = 0.4 - 0.16 = 0.24, χ-square = n r² = 100 (rel 10⁻⁹)passed
D0.24 ± 2.4 × 10⁻¹⁰0.24lockedunder 0.01 % of the tolerance usedD_prime1 ± 10⁻⁹1lockedunder 0.01 % of the tolerance usedr21 ± 10⁻⁹1lockedunder 0.01 % of the tolerance usedr1 ± 10⁻⁹1lockedunder 0.01 % of the tolerance usedchi_square100 ± 10⁻⁷100lockedunder 0.01 % of the tolerance usedr0003closed form (30,20,10,40): D = 0.3 - 0.5·0.4 = 0.1, D' = 0.1/min(0.3,0.2) = 0.5, r² = 0.01/0.06 = 1/6, χ-square = 100/6, decay c=0.1 t=10: D_t = 0.1·0.9¹⁰, half-life ln2/-ln0.9 = 6.5788134790 (rel 10⁻⁹)passed
D0.1 ± 10⁻¹⁰0.1lockedunder 0.01 % of the tolerance usedD_prime0.5 ± 5 × 10⁻¹⁰0.5lockedunder 0.01 % of the tolerance usedr20.166667 ± 1.7 × 10⁻¹⁰0.166667lockedunder 0.01 % of the tolerance usedchi_square16.6667 ± 1.7 × 10⁻⁸16.6667lockedunder 0.01 % of the tolerance usedD_after0.0348678 ± 3.5 × 10⁻¹¹0.0348678lockedunder 0.01 % of the tolerance usedD_prime_after0.174339 ± 1.7 × 10⁻¹⁰0.174339lockedunder 0.01 % of the tolerance usedr2_after0.0202628 ± 2 × 10⁻¹¹0.0202628lockedunder 0.01 % of the tolerance usedhalf_life_generations6.57881 ± 6.6 × 10⁻⁹6.57881lockedunder 0.01 % of the tolerance usedgenerations_to_r2_threshold5.71359 ± 5.7 × 10⁻⁹5.71359lockedunder 0.01 % of the tolerance usedr0004textbook χ-square(1) 95% point 3.841458820694 gives p = 0.05: counts 0.3n,0.2n,0.1n,0.4n with n = 6 × 3.841458820694 so χ₂ = n/6 (rel 10⁻⁶, erfc series)passed
chi_square3.84146 ± 3.8 × 10⁻⁹3.84146lockedunder 0.01 % of the tolerance usedp_value0.05 ± 5 × 10⁻⁸0.05lockedunder 0.01 % of the tolerance usedr0005exact gene counting, EM on phase-unambiguous genotypes (no Aa Bb): returns the true haplotype counts AB 28, Ab 12, aB 7, ab 23 of 70 chromosomes, D = (28·23 - 12·7)/70² = 560/4900 (rel 10⁻¹²)passed
count_AB28 ± 2.8 × 10⁻¹¹28lockedunder 0.01 % of the tolerance usedcount_Ab12 ± 1.2 × 10⁻¹¹12lockedunder 0.01 % of the tolerance usedcount_aB7 ± 7 × 10⁻¹²7lockedunder 0.01 % of the tolerance usedcount_ab23 ± 2.3 × 10⁻¹¹23lockedunder 0.01 % of the tolerance usedn_haplotypes70 ± 7 × 10⁻¹¹70lockedunder 0.01 % of the tolerance usedD0.114286 ± 1.1 × 10⁻¹⁰0.114286lockedunder 0.01 % of the tolerance usedr0006MLE at exact expectation: HWE genotype counts from haplotypes (0.4,0.1,0.1,0.4), N=10000, are a fixed point of EM, so EM returns them: D = 0.15, D' = 0.6, r² = 0.36, cis fraction 0.16/0.17 (rel 10⁻⁸, EM tolerance 10⁻¹³)passed
p_AB0.4 ± 4 × 10⁻⁹0.4lockedunder 0.01 % of the tolerance usedp_Ab0.1 ± 10⁻⁹0.1lockedunder 0.01 % of the tolerance usedD0.15 ± 1.5 × 10⁻⁹0.15lockedunder 0.01 % of the tolerance usedD_prime0.6 ± 6 × 10⁻⁹0.6lockedunder 0.01 % of the tolerance usedr20.36 ± 3.6 × 10⁻⁹0.36lockedunder 0.01 % of the tolerance useddouble_het_cis_fraction0.941176 ± 9.4 × 10⁻⁹0.941176lockedunder 0.01 % of the tolerance usedem_converged= truetruelockedexact match
Each line is one reading the kernel checked. The bright band is the tolerance; the needle is the value the instrument actually returned when it was admitted. A needle near the centre line used little of its tolerance.
Inputs
| Name | Unit | Default | Meaning |
|---|---|---|---|
n_AB | 1 | Phased data: chromosomes carrying haplotype AB. Give all four haplotype counts, or genotype_counts instead. | |
n_Ab | 1 | Phased data: chromosomes carrying haplotype Ab. | |
n_aB | 1 | Phased data: chromosomes carrying haplotype aB. | |
n_ab | 1 | Phased data: chromosomes carrying haplotype ab. | |
genotype_counts | 1 | Unphased data: 3x3 counts of individuals, rows AA, Aa, aa and columns BB, Bb, bb. Haplotype frequencies are the maximum-likelihood ones under random union of gametes (EM over the phase of the double heterozygotes Aa Bb). | |
recomb_rate | 1 | 0.01 | Recombination fraction c between the loci, per generation (0.5 = unlinked; about 0.01 per cM for short distances). |
generations | 1 | 100 | Generations of random mating for the decay of D (whole number). |
r2_threshold | 1 | 0.2 | r² level for 'generations to decay to threshold'. |
alpha | 1 | 0.05 | Significance level for the χ-square test of D = 0. |
em_tol | 1 | 1e-13 | EM stops when a step changes the haplotype frequencies by less than this. |
em_max_iter | 1 | 100000 | EM iteration cap per start. |
Readings
| Name | Unit | Meaning |
|---|---|---|
mode | haplotypes (counts given) or genotypes_em (estimated from genotype counts) | |
n_haplotypes | 1 | number of chromosomes (2 x individuals in genotype mode) |
count_AB | 1 | |
count_Ab | 1 | |
count_aB | 1 | |
count_ab | 1 | |
p_AB | 1 | |
p_Ab | 1 | |
p_aB | 1 | |
p_ab | 1 | |
p_A | 1 | |
p_B | 1 | |
D | 1 | p_AB p_ab - p_Ab p_aB = p_AB - p_A p_B |
D_max | 1 | |
D_prime | 1 | Lewontin's D / D_max, signed; null when a locus is monomorphic |
D_prime_abs | 1 | |
r | 1 | |
r2 | 1 | D² / (p_A q_A p_B q_B) |
chi_square | 1 | n r², one degree of freedom |
p_value | 1 | erfc(√(chi_square / 2)) |
alpha | 1 | |
ld_significant | p_value < α | |
min_expected_count | 1 | |
recomb_rate | 1 | |
generations | 1 | |
decay_factor | 1 | (1 - c)^t |
D_after | 1 | |
D_prime_after | 1 | |
r2_after | 1 | |
half_life_generations | 1 | generations for D to halve: ln 2 / -ln(1 - c) |
r2_threshold | 1 | |
generations_to_r2_threshold | 1 | generations until r² falls to r2_threshold (fractional) |
haplotypes | ||
decay_table | ||
n_individuals | 1 | |
n_double_het | 1 | |
double_het_cis_fraction | 1 | |
em_iterations | 1 | |
em_converged | EM met em_tol | |
em_optima | 1 | |
log_likelihood | 1 | |
warnings | list of text |
Try the call
Change a value and the command updates. Nothing runs here; paste it into your terminal or give it to your agent.