mecfs_bio.build_system.task.gwaslab.ldsc_diagnostic
The arithmetic behind the LD-score-regression diagnostic plot.
LD score regression models the per-variant chi-square as a straight line in the LD score: E[chi^2_j] = intercept + (N * h2 / M) * ld_j, where N is the sample size, M the number of reference variants, and h2 the observed-scale heritability. The diagnostic plot bins variants by LD score and shows each bin's mean chi-square against that line, so a bin that sits well off the line, or an intercept that drifts away from one, is visible by eye.
This module holds only the pure math: binning variants into equal-count LD-score bins, the fitted line, and the rescaling that turns the chi-square axis into heritability-slope units. It takes plain numpy arrays and returns plain values, so it can be tested without reading any asset or drawing anything.
Classes:
-
LdscDiagnosticBins–Per-bin summaries of variants grouped into equal-count LD-score bins, ordered by
Functions:
-
bin_by_ld_score–Group variants into n_bins equal-count bins by ascending LD score and summarize each.
-
chi2_to_heritability_units–Rescale chi-square to chi^2 * M / N, the units in which the fitted line's slope equals
-
fit_line_chi2–The LDSC fitted line in chi-square units: intercept + (N * h2 / M) * ld.
LdscDiagnosticBins
Per-bin summaries of variants grouped into equal-count LD-score bins, ordered by ascending LD score. Every array is 1-D of length n_bins.
Attributes:
bin_by_ld_score
Group variants into n_bins equal-count bins by ascending LD score and summarize each.
Equal-count (rather than equal-width) bins put a comparable number of variants in every point, so each mean carries similar noise -- matching the binned plots in the original LDSC papers. Bin sizes differ by at most one when n_bins does not divide the variant count.
Source code in mecfs_bio/build_system/task/gwaslab/ldsc_diagnostic.py
chi2_to_heritability_units
Rescale chi-square to chi^2 * M / N, the units in which the fitted line's slope equals the observed-scale heritability. This is the secondary y-axis, and because M and N are divided out it is comparable across traits of different sample size.
Source code in mecfs_bio/build_system/task/gwaslab/ldsc_diagnostic.py
fit_line_chi2
The LDSC fitted line in chi-square units: intercept + (N * h2 / M) * ld.