mecfs_bio.build_system.task.ppp_ldsc.batched_ldsc_h2
Batched univariate LD-score-regression heritability for the UKB-PPP database.
Because every protein's chi-square is defined on the SAME shared SNP set (see ppp_ldsc_context), we process K proteins at once: stack chi-square into an (S, K) matrix and run the weighted LD-score regression plus block jackknife for all K in vectorized numpy. Per-protein exclusions (missing variants, the chi-square filter, and cis variants) are encoded as ZERO regression weights rather than by dropping rows, so all proteins keep the same row set and the same shared contiguous jackknife blocks.
This is validated (experiments/claude/ppp_ldsc/batched_vs_exact_h2_probe.py) to agree with the repo's exact per-protein GenomicSEM port (genomic_sem_ldsc.estimate_h2): the h2 point estimate is machine-identical and the jackknife SE agrees to <=~1.5% (the only difference is that the exact method cuts the KEPT SNPs into blocks whereas the shared-block method cuts the FULL set, shifting block membership by a few SNPs).
Weighting note (the one easy bug): the effective WLS weight is omega = het * oc (the PRODUCT), because estimate_h2 multiplies BOTH the design and the response by initial_w = sqrt(het * oc). The sum-to-1 normalization is a common scalar that cancels in the 2x2 slope solve, so it is omitted here.
Classes:
-
BatchedH2Result–Per-protein heritability outputs; all arrays are length K (protein order preserved).
-
ExactH2Result–
Functions:
-
batched_h2–Estimate LDSC heritability for K proteins sharing the SNP set.
-
exact_h2_single–Reference single-protein heritability via the repo's exact GenomicSEM port. Drops
Attributes:
BatchedH2Result
Per-protein heritability outputs; all arrays are length K (protein order preserved).
Attributes:
ExactH2Result
batched_h2
batched_h2(
chi2: ndarray,
ld: ndarray,
n: ndarray,
m: float,
*,
n_blocks: int = DEFAULT_N_BLOCKS,
exclude: ndarray | None = None,
) -> BatchedH2Result
Estimate LDSC heritability for K proteins sharing the SNP set.
chi2: (S, K) chi-square (= (BETA/SE)^2), NaN where a protein lacks the variant. ld: (S,) LD score (== weight LD score), genome-sorted for contiguous blocks. n: (K,) per-protein sample size (constant across a protein's SNPs). m: total reference-SNP count. exclude: optional (S, K) boolean; True drops that variant for that protein (e.g. cis).
NOTE: - To understand the weighting, see the docstring for _het_oc_initial_weight in genomic_sem_ldsc.
Source code in mecfs_bio/build_system/task/ppp_ldsc/batched_ldsc_h2.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
exact_h2_single
exact_h2_single(
chi2: ndarray,
ld: ndarray,
n: float,
m: float,
*,
n_blocks: int = DEFAULT_N_BLOCKS,
exclude: ndarray | None = None,
) -> ExactH2Result
Reference single-protein heritability via the repo's exact GenomicSEM port. Drops (rather than zero-weights) filtered/cis SNPs, then blocks the KEPT SNPs -- this is what the batched kernel is validated against. chi2/ld are genome-sorted (S,) arrays.