diff --git a/lib/scikit/extra_estimators.flow b/lib/scikit/extra_estimators.flow index 1b52eab..33a9fbe 100644 --- a/lib/scikit/extra_estimators.flow +++ b/lib/scikit/extra_estimators.flow @@ -478,7 +478,12 @@ export function spectral_biclustering_fit(X: Matrix, n_row_clusters: i32, n_col_ let V: ptr = array_new_f32(p * n_vecs) _bic_right_singular(An, n, p, n_vecs, V) + # U = An V has the singular values as its column norms. Keep them, because + # a matrix of rank r has only r vectors that mean anything: past that the + # block power iteration returns whatever rounding left in the null space, + # and that differs from one BLAS to the next. let U: ptr = array_new_f32(n * n_vecs) + let sigma: ptr = array_new_f32(n_vecs) blas_matmat(An, V, U, n, n_vecs, p, 1.0, 0.0) for c in 0 to n_vecs { let mut norm: f32 = 0.0 @@ -489,6 +494,7 @@ export function spectral_biclustering_fit(X: Matrix, n_row_clusters: i32, n_col_ i2 = i2 + 1 } norm = sqrt((norm) as f64) as f32 + sigma[c] = norm if norm > 0.0000000001 { let inv: f32 = 1.0 / norm let mut i3: i32 = 0 @@ -500,58 +506,100 @@ export function spectral_biclustering_fit(X: Matrix, n_row_clusters: i32, n_col_ } # Rank the vectors after the trivial one by how close to piecewise - # constant they are, and keep the best few. + # constant they are, and keep the best few. Left vectors are ranked + # against the row clusters and right vectors against the column clusters, + # as scikit-learn does. let mut n_best: i32 = 3 if n_best > n_vecs - 1 { n_best = n_vecs - 1 } if n_best < 1 { n_best = 1 } - let scratch: ptr = array_new_f32(n) - let errs: ptr = array_new_f32(n_vecs) - let chosen: ptr = malloc((n_vecs as i64) * 4 + 64) as ptr - for c2 in 0 to n_vecs { chosen[c2] = 0 } + let mut scratch_len: i32 = n + if p > scratch_len { scratch_len = p } + let scratch: ptr = array_new_f32(scratch_len) + let errs_u: ptr = array_new_f32(n_vecs) + let errs_v: ptr = array_new_f32(n_vecs) + # A vector whose singular value is rounding noise next to the leading one + # carries no structure. It goes to the back of the ranking, so it is kept + # only when the matrix has fewer real vectors than n_best. + let sigma_floor: f32 = sigma[0] * 0.0001 for c3 in 1 to n_vecs { - let mut i4: i32 = 0 - while i4 < n { - scratch[i4] = U[i4 * n_vecs + c3] - i4 = i4 + 1 + if sigma[c3] > sigma_floor { + let mut i4: i32 = 0 + while i4 < n { + scratch[i4] = U[i4 * n_vecs + c3] + i4 = i4 + 1 + } + errs_u[c3] = _bic_piecewise_error(scratch, n, n_row_clusters) + let mut j4: i32 = 0 + while j4 < p { + scratch[j4] = V[j4 * n_vecs + c3] + j4 = j4 + 1 + } + errs_v[c3] = _bic_piecewise_error(scratch, p, n_col_clusters) + } else { + errs_u[c3] = 1000000.0 + (c3 as f32) + errs_v[c3] = 1000000.0 + (c3 as f32) } - errs[c3] = _bic_piecewise_error(scratch, n, n_row_clusters) } + + # The best left vectors go into Ub, n by n_best, and the best right + # vectors into Vb, p by n_best. + let Ub: ptr = array_new_f32(n * n_best) + let Vb: ptr = array_new_f32(p * n_best) + let chosen: ptr = malloc((n_vecs as i64) * 4 + 64) as ptr + for c2 in 0 to n_vecs { chosen[c2] = 0 } for b in 0 to n_best { let mut best_c: i32 = -1 let mut best_err: f32 = 999999999999.0 for c4 in 1 to n_vecs { if chosen[c4] == 0 { - if errs[c4] < best_err { - best_err = errs[c4] + if errs_u[c4] < best_err { + best_err = errs_u[c4] best_c = c4 } } } - if best_c >= 0 { chosen[best_c] = 1 } - } - - # Rows and columns are clustered separately, in the space the kept - # vectors span. - let Zr: ptr = array_new_f32(n * n_best) - let Zc: ptr = array_new_f32(p * n_best) - let mut slot: i32 = 0 - for c5 in 1 to n_vecs { - if chosen[c5] == 1 { + if best_c >= 0 { + chosen[best_c] = 1 let mut i5: i32 = 0 while i5 < n { - Zr[i5 * n_best + slot] = U[i5 * n_vecs + c5] + Ub[i5 * n_best + b] = U[i5 * n_vecs + best_c] i5 = i5 + 1 } - let mut j2: i32 = 0 - while j2 < p { - Zc[j2 * n_best + slot] = V[j2 * n_vecs + c5] - j2 = j2 + 1 + } + } + for c6 in 0 to n_vecs { chosen[c6] = 0 } + for b2 in 0 to n_best { + let mut best_c2: i32 = -1 + let mut best_err2: f32 = 999999999999.0 + for c7 in 1 to n_vecs { + if chosen[c7] == 0 { + if errs_v[c7] < best_err2 { + best_err2 = errs_v[c7] + best_c2 = c7 + } + } + } + if best_c2 >= 0 { + chosen[best_c2] = 1 + let mut j5: i32 = 0 + while j5 < p { + Vb[j5 * n_best + b2] = V[j5 * n_vecs + best_c2] + j5 = j5 + 1 } - slot = slot + 1 } } + # Rows and columns are clustered separately, and what is clustered is the + # normalized data projected onto the kept vectors: rows as An Vb, columns + # as An^T Ub. Clustering the unit vectors themselves gave a null space + # vector the same weight as one that carries the checkerboard. Projected, + # it lands near zero and cannot move a point between clusters. + let Zr: ptr = array_new_f32(n * n_best) + let Zc: ptr = array_new_f32(p * n_best) + blas_matmat(An, Vb, Zr, n, n_best, p, 1.0, 0.0) + cblas_sgemm(101, 112, 111, p, n_best, n, 1.0, An, p, Ub, n_best, 0.0, Zc, n_best) + let row_labels: ptr = malloc((n as i64) * 4 + 64) as ptr let col_labels: ptr = malloc((p as i64) * 4 + 64) as ptr _bic_kmeans(Zr, n, n_best, n_row_clusters, row_labels) @@ -560,10 +608,14 @@ export function spectral_biclustering_fit(X: Matrix, n_row_clusters: i32, n_col_ array_free_f32(An) array_free_f32(U) array_free_f32(V) + array_free_f32(Ub) + array_free_f32(Vb) array_free_f32(Zr) array_free_f32(Zc) array_free_f32(scratch) - array_free_f32(errs) + array_free_f32(errs_u) + array_free_f32(errs_v) + array_free_f32(sigma) free(chosen as ptr) return SpectralBiclustering { diff --git a/tests/test_spectral_biclustering.flow b/tests/test_spectral_biclustering.flow index 4788714..471b290 100644 --- a/tests/test_spectral_biclustering.flow +++ b/tests/test_spectral_biclustering.flow @@ -125,11 +125,81 @@ function test_biclustering_recovers_blocks() -> i32 { return failures } +# A two block matrix has rank two, so four of the six singular vectors the fit +# asks for span its null space. What the block power iteration leaves there is +# rounding, and it differs between BLAS builds: on some it came back as zero +# vectors, which are perfectly piecewise constant and so outranked the one +# vector that carries the checkerboard. The fit then put every row in one +# cluster. One shape cannot pin that down, because which shapes fail depends +# on the BLAS, so this sweeps shapes and block contrasts. +function coc_contrast_fixture(n: i32, p: i32, strong: f32, weak: f32) -> Matrix { + let X: Matrix = matrix_new(n, p) + let half_r: i32 = n / 2 + let half_c: i32 = p / 2 + for i in 0 to n { + for j in 0 to p { + let same: bool = (i < half_r && j < half_c) || (i >= half_r && j >= half_c) + if same { matrix_set(X, i, j, strong) } + else { matrix_set(X, i, j, weak) } + } + } + return X +} + +function coc_two_blocks_recovered(labels: ptr, m: i32) -> bool { + if not coc_block_is_uniform(labels, 0, m / 2) { return false } + if not coc_block_is_uniform(labels, m / 2, m) { return false } + return labels[0] != labels[m - 1] +} + +function test_biclustering_rank_deficient_sweep() -> i32 { + println("Test: SpectralBiclustering recovers rank two blocks at every shape") + let mut fits: i32 = 0 + let mut wrong: i32 = 0 + let mut n: i32 = 4 + while n <= 40 { + let mut p: i32 = 4 + while p <= 40 { + for h in 0 to 3 { + let mut strong: f32 = 10.0 + let mut weak: f32 = 1.0 + if h == 1 { strong = 2.0 } + if h == 2 { + strong = 7.5 + weak = 2.5 + } + let X: Matrix = coc_contrast_fixture(n, p, strong, weak) + let model: SpectralBiclustering = spectral_biclustering_fit(X, 2, 2) + let rows_ok: bool = coc_two_blocks_recovered(model.row_labels, n) + let cols_ok: bool = coc_two_blocks_recovered(model.col_labels, p) + fits = fits + 1 + if not (rows_ok && cols_ok) { + wrong = wrong + 1 + if wrong <= 5 { + printf(" FAIL: %d by %d, blocks %.1f and %.1f\n", n, p, strong as f64, weak as f64) + } + } + spectral_biclustering_free(model) + matrix_free(X) + } + p = p + 2 + } + n = n + 2 + } + if wrong == 0 { + printf(" OK: %d fits\n", fits) + return 0 + } + printf(" FAIL: %d of %d fits did not recover the blocks\n", wrong, fits) + return 1 +} + function main() -> i32 { println("=== Spectral biclustering ===") let mut failures: i32 = 0 failures = failures + test_recovers_two_blocks() failures = failures + test_biclustering_recovers_blocks() + failures = failures + test_biclustering_rank_deficient_sweep() if failures > 0 { printf("%d failure(s)\n", failures) return 1