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^2, chi-square (1 df) with its tail probability, and the decay D_t = D_0 (1 - c)^t of D, D' and r^2 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^2 = 0, chi-square = 0, p = 1 (exact up to rounding, abs 1e-12)passed
D0 ± 1e-126.93889e-18lockedunder 0.01 % of the tolerance usedD_prime0 ± 1e-125.78241e-17lockedunder 0.01 % of the tolerance usedr20 ± 1e-129.55322e-34lockedunder 0.01 % of the tolerance usedchi_square0 ± 1e-099.55322e-31lockedunder 0.01 % of the tolerance usedp_value1 ± 1e-091lockedunder 0.01 % of the tolerance usedp_A0.6 ± 6e-130.6lockedunder 0.01 % of the tolerance usedp_B0.3 ± 3e-130.3lockedunder 0.01 % of the tolerance usedr0002closed form, complete LD: only AB (40) and ab (60) give D' = 1, r^2 = 1, D = pAB - pA pB = 0.4 - 0.16 = 0.24, chi-square = n r^2 = 100 (rel 1e-9)passed
D0.24 ± 2.4e-100.24lockedunder 0.01 % of the tolerance usedD_prime1 ± 1e-091lockedunder 0.01 % of the tolerance usedr21 ± 1e-091lockedunder 0.01 % of the tolerance usedr1 ± 1e-091lockedunder 0.01 % of the tolerance usedchi_square100 ± 1e-07100lockedunder 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^2 = 0.01/0.06 = 1/6, chi-square = 100/6, decay c=0.1 t=10: D_t = 0.1*0.9^10, half-life ln2/-ln0.9 = 6.5788134790 (rel 1e-9)passed
D0.1 ± 1e-100.1lockedunder 0.01 % of the tolerance usedD_prime0.5 ± 5e-100.5lockedunder 0.01 % of the tolerance usedr20.166667 ± 1.7e-100.166667lockedunder 0.01 % of the tolerance usedchi_square16.6667 ± 1.7e-0816.6667lockedunder 0.01 % of the tolerance usedD_after0.0348678 ± 3.5e-110.0348678lockedunder 0.01 % of the tolerance usedD_prime_after0.174339 ± 1.7e-100.174339lockedunder 0.01 % of the tolerance usedr2_after0.0202628 ± 2e-110.0202628lockedunder 0.01 % of the tolerance usedhalf_life_generations6.57881 ± 6.6e-096.57881lockedunder 0.01 % of the tolerance usedgenerations_to_r2_threshold5.71359 ± 5.7e-095.71359lockedunder 0.01 % of the tolerance usedr0004textbook chi-square(1) 95% point 3.841458820694 gives p = 0.05: counts 0.3n,0.2n,0.1n,0.4n with n = 6 x 3.841458820694 so chi2 = n/6 (rel 1e-6, erfc series)passed
chi_square3.84146 ± 3.8e-093.84146lockedunder 0.01 % of the tolerance usedp_value0.05 ± 5e-080.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^2 = 560/4900 (rel 1e-12)passed
count_AB28 ± 2.8e-1128lockedunder 0.01 % of the tolerance usedcount_Ab12 ± 1.2e-1112lockedunder 0.01 % of the tolerance usedcount_aB7 ± 7e-127lockedunder 0.01 % of the tolerance usedcount_ab23 ± 2.3e-1123lockedunder 0.01 % of the tolerance usedn_haplotypes70 ± 7e-1170lockedunder 0.01 % of the tolerance usedD0.114286 ± 1.1e-100.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^2 = 0.36, cis fraction 0.16/0.17 (rel 1e-8, EM tolerance 1e-13)passed
p_AB0.4 ± 4e-090.4lockedunder 0.01 % of the tolerance usedp_Ab0.1 ± 1e-090.1lockedunder 0.01 % of the tolerance usedD0.15 ± 1.5e-090.15lockedunder 0.01 % of the tolerance usedD_prime0.6 ± 6e-090.6lockedunder 0.01 % of the tolerance usedr20.36 ± 3.6e-090.36lockedunder 0.01 % of the tolerance useddouble_het_cis_fraction0.941176 ± 9.4e-090.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^2 level for 'generations to decay to threshold'. |
alpha | 1 | 0.05 | Significance level for the chi-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^2 / (p_A q_A p_B q_B) |
chi_square | 1 | n r^2, one degree of freedom |
p_value | 1 | erfc(sqrt(chi_square / 2)) |
alpha | 1 | |
ld_significant | p_value < alpha | |
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^2 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.