Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 2x 2x 200x 200x 200x 200x 200x 200x 200x 2x 153x 153x 153x 153x 153x 153x 153x 2x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 36x 36x 36x 36x 36x 36x 36x 115x 115x 115x 115x 115x 115x 115x 115x 36x 36x 2x 30x 30x 30x 30x 30x 29x 29x 29x 29x 29x 30x 29x 29x 29x 29x 29x 30x 21x 21x 21x 21x 2x 8x 8x 8x 8x 7x 7x 8x 21x 21x 21x 7x 7x 7x 8x 21x 21x 21x 21x 21x 7x 7x 8x 7x 7x 7x 2x 14x 14x 12x 12x 12x 12x 12x 2x 2x 2x 28x 28x 28x 28x 2x 2x 10x 10x 10x 10x 10x 11x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 85x 10x 10x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x | /**
* One quantity compared between groups: the summaries and the tests a *Signal
* by group* run reports (§18.8).
*
* **Rank tests**, because the quantities this is asked about are neither normal
* nor on one scale: a ChIP track's per-bin means are a long right tail, an
* eigenvector is signed, a GC fraction is bounded. Mann–Whitney for a pair of
* groups and Kruskal–Wallis over all of them, each corrected for ties — a zoom
* level of a bigWig hands many bins the same value — and read through the
* normal and chi-square approximations, which at the hundreds of bins a genome
* gives are exact enough for any P a reader acts on. They are the asymptotic
* forms `scipy.stats.mannwhitneyu` and `scipy.stats.kruskal` use, and the tests
* hold them to SciPy's own numbers.
*
* Pure: arrays in, numbers out.
*/
import { standardNormalCdf } from './chromosight'
/** What a group's values look like, in the columns a table prints. */
export interface GroupSummary {
n: number
mean: number
/** Sample standard deviation (n − 1); `NaN` below two values. */
sd: number
median: number
q1: number
q3: number
}
/** The finite values, ascending. */
export function sortedFinite(values: ArrayLike<number>): Float64Array {
let n = 0
for (let i = 0; i < values.length; i++) if (Number.isFinite(values[i])) n++
const out = new Float64Array(n)
let at = 0
for (let i = 0; i < values.length; i++) if (Number.isFinite(values[i])) out[at++] = values[i]
return out.sort()
}
/** The `p` quantile of ascending values, interpolated linearly (R's type 7, NumPy's default). */
export function quantileOf(ascending: ArrayLike<number>, p: number): number {
const n = ascending.length
if (n === 0) return Number.NaN
const at = Math.min(n - 1, Math.max(0, p * (n - 1)))
const lo = Math.floor(at)
const hi = Math.min(n - 1, lo + 1)
return ascending[lo] + (ascending[hi] - ascending[lo]) * (at - lo)
}
export function summarise(values: ArrayLike<number>): GroupSummary {
const sorted = sortedFinite(values)
const n = sorted.length
if (n === 0) {
return { n: 0, mean: Number.NaN, sd: Number.NaN, median: Number.NaN, q1: Number.NaN, q3: Number.NaN }
}
let sum = 0
for (let i = 0; i < n; i++) sum += sorted[i]
const mean = sum / n
let squares = 0
for (let i = 0; i < n; i++) squares += (sorted[i] - mean) ** 2
return {
n,
mean,
sd: n > 1 ? Math.sqrt(squares / (n - 1)) : Number.NaN,
median: quantileOf(sorted, 0.5),
q1: quantileOf(sorted, 0.25),
q3: quantileOf(sorted, 0.75),
}
}
/**
* The ranks of the pooled values, ties sharing their mean rank, and the tie
* term `Σ (t³ − t)` over every run of `t` equal values.
*/
function midranks(pooled: Float64Array): { ranks: Float64Array; ties: number } {
const n = pooled.length
const order = Array.from({ length: n }, (_, i) => i).sort((a, b) => pooled[a] - pooled[b])
const ranks = new Float64Array(n)
let ties = 0
let i = 0
while (i < n) {
let j = i
while (j + 1 < n && pooled[order[j + 1]] === pooled[order[i]]) j++
const rank = (i + j) / 2 + 1
for (let k = i; k <= j; k++) ranks[order[k]] = rank
const t = j - i + 1
if (t > 1) ties += t * t * t - t
i = j + 1
}
return { ranks, ties }
}
export interface MannWhitney {
/** Pairs in which the first group's value is the larger, ties counting half. */
u: number
z: number
/** Two-sided. */
p: number
/**
* The rank-biserial correlation `2U / (n₁n₂) − 1`: +1 where every value of the
* first group is above every value of the second, −1 the other way round.
*/
effect: number
}
/**
* Mann–Whitney U of `a` against `b`, two-sided, by the normal approximation
* with the tie and continuity corrections.
*/
export function mannWhitney(a: ArrayLike<number>, b: ArrayLike<number>): MannWhitney {
const first = sortedFinite(a)
const second = sortedFinite(b)
const n1 = first.length
const n2 = second.length
if (n1 === 0 || n2 === 0) return { u: Number.NaN, z: Number.NaN, p: Number.NaN, effect: Number.NaN }
const pooled = new Float64Array(n1 + n2)
pooled.set(first, 0)
pooled.set(second, n1)
const { ranks, ties } = midranks(pooled)
let r1 = 0
for (let i = 0; i < n1; i++) r1 += ranks[i]
const u = r1 - (n1 * (n1 + 1)) / 2
const total = n1 + n2
const mean = (n1 * n2) / 2
const variance = ((n1 * n2) / 12) * (total + 1 - ties / (total * (total - 1)))
const effect = (2 * u) / (n1 * n2) - 1
// Every value tied: the two samples cannot be told apart at all, which SciPy
// reports as P = 1 rather than as no answer.
if (!(variance > 0)) return { u, z: 0, p: 1, effect }
const away = u - mean
const z = (away - 0.5 * Math.sign(away)) / Math.sqrt(variance)
return { u, z, p: Math.min(1, 2 * standardNormalCdf(-Math.abs(z))), effect }
}
export interface KruskalWallis {
h: number
df: number
p: number
}
/** Kruskal–Wallis H over every group with a value, tie-corrected, against χ² on k − 1. */
export function kruskalWallis(groups: ReadonlyArray<ArrayLike<number>>): KruskalWallis {
const kept = groups.map(sortedFinite).filter((group) => group.length > 0)
const k = kept.length
const total = kept.reduce((sum, group) => sum + group.length, 0)
if (k < 2 || total < 2) return { h: Number.NaN, df: Math.max(0, k - 1), p: Number.NaN }
const pooled = new Float64Array(total)
let at = 0
for (const group of kept) {
pooled.set(group, at)
at += group.length
}
const { ranks, ties } = midranks(pooled)
let statistic = 0
at = 0
for (const group of kept) {
let sum = 0
for (let i = 0; i < group.length; i++) sum += ranks[at + i]
statistic += (sum * sum) / group.length
at += group.length
}
statistic = (12 / (total * (total + 1))) * statistic - 3 * (total + 1)
const correction = 1 - ties / (total * total * total - total)
if (!(correction > 0)) return { h: Number.NaN, df: k - 1, p: Number.NaN }
const h = statistic / correction
return { h, df: k - 1, p: chiSquareSurvival(h, k - 1) }
}
/** `P(χ²ₖ > x)`: the regularised upper incomplete gamma `Q(k/2, x/2)`. */
export function chiSquareSurvival(x: number, df: number): number {
if (!(df > 0) || Number.isNaN(x)) return Number.NaN
if (x <= 0) return 1
return upperGamma(df / 2, x / 2)
}
/**
* `Q(s, x)`, by the series for its complement below `x = s + 1` and by the
* continued fraction above it — the split at which each converges in a few
* dozen terms (Numerical Recipes §6.2).
*/
function upperGamma(s: number, x: number): number {
const scale = Math.exp(-x + s * Math.log(x) - lnGamma(s))
if (x < s + 1) {
let term = 1 / s
let sum = term
for (let n = 1; n < 1000; n++) {
term *= x / (s + n)
sum += term
if (Math.abs(term) < Math.abs(sum) * 1e-16) break
}
return Math.max(0, 1 - sum * scale)
}
const tiny = 1e-300
let b = x + 1 - s
let c = 1 / tiny
let d = 1 / b
let h = d
for (let i = 1; i < 1000; i++) {
const an = -i * (i - s)
b += 2
d = an * d + b
if (Math.abs(d) < tiny) d = tiny
c = b + an / c
if (Math.abs(c) < tiny) c = tiny
d = 1 / d
const delta = d * c
h *= delta
if (Math.abs(delta - 1) < 1e-16) break
}
return Math.min(1, scale * h)
}
/** `ln Γ(x)`, by Lanczos (g = 7, nine terms). */
function lnGamma(x: number): number {
if (x < 0.5) return Math.log(Math.PI / Math.sin(Math.PI * x)) - lnGamma(1 - x)
const c = [
0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313,
-176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6,
1.5056327351493116e-7,
]
const shifted = x - 1
let a = c[0]
const t = shifted + 7.5
for (let i = 1; i < 9; i++) a += c[i] / (shifted + i)
return 0.5 * Math.log(2 * Math.PI) + (shifted + 0.5) * Math.log(t) - t + Math.log(a)
}
|