From 85fabe3e4380c8de9d2f17c1a10213d4237963ed Mon Sep 17 00:00:00 2001 From: mkborregaard Date: Mon, 2 Nov 2026 16:32:08 +0900 Subject: [PATCH 1/2] Curveball: trade distinct column pairs; cache trading columns in the generator Draw the two columns of each trade without replacement, as in the paper's R and Python scripts. Pairing a column with itself is a no-op, so drawing with replacement wasted 1/C of the trades (a third on a 3-column matrix). The generator now stores the columns that can trade, which never change along the chain, so a draw no longer scans every column; the buffer size is taken from those columns too. Co-Authored-By: Claude Opus 5.5 --- src/RandomBooleanMatrices.jl | 2 +- src/curveball.jl | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/RandomBooleanMatrices.jl b/src/RandomBooleanMatrices.jl index 459aef5..c812aa9 100644 --- a/src/RandomBooleanMatrices.jl +++ b/src/RandomBooleanMatrices.jl @@ -64,7 +64,7 @@ function _sampler!(method::matrixrandomizations, sm::SparseMatrixCSC{Bool, Int}, method == exact && return _exact_counts(_margins(sm)...) if method == curveball _sis!(sm, rng) - return CurveballSampler(something(trades, _defaulttrades(sm))) + return CurveballSampler(something(trades, _defaulttrades(sm)), _tradingcolumns(sm)) end error("undefined method") end diff --git a/src/curveball.jl b/src/curveball.jl index 8fe88e0..85c84df 100644 --- a/src/curveball.jl +++ b/src/curveball.jl @@ -62,16 +62,17 @@ _tradingcolumns(m::SparseMatrixCSC) = findall(>(0), diff(SparseArrays.getcolptr( # The default number of trades per call: five per column that can trade. _defaulttrades(m::SparseMatrixCSC) = 5 * length(_tradingcolumns(m)) -function _curveball!(m::SparseMatrixCSC{Bool, Int}, rng = Random.GLOBAL_RNG, trades::Int = _defaulttrades(m)) - cols = _tradingcolumns(m) +function _curveball!(m::SparseMatrixCSC{Bool, Int}, rng = Random.GLOBAL_RNG, trades::Int = _defaulttrades(m), cols = _tradingcolumns(m)) C = length(cols) C < 2 && return m - mcs = min(2maximum(diff(m.colptr)), size(m, 1)) + mcs = min(2maximum(j -> m.colptr[j+1] - m.colptr[j], cols), size(m, 1)) not_shared, shared = Vector{Int}(undef, mcs), Vector{Int}(undef, mcs) newa, newb = Vector{Int}(undef, mcs), Vector{Int}(undef, mcs) for rep ∈ 1:trades - A, B = cols[rand(rng, 1:C)], cols[rand(rng, 1:C)] + # a random pair of distinct columns + i, j = rand(rng, 1:C), rand(rng, 1:C-1) + A, B = cols[i], cols[j + (j >= i)] # use views directly into the sparse matrix to avoid copying a, b = view(m.rowval, m.colptr[A]:m.colptr[A+1]-1), view(m.rowval, m.colptr[B]:m.colptr[B+1]-1) @@ -95,16 +96,18 @@ function _curveball!(m::SparseMatrixCSC{Bool, Int}, rng = Random.GLOBAL_RNG, tra end """ - CurveballSampler(trades) + CurveballSampler(trades, cols) Generator state for the curveball method: each draw advances the stored matrix by -`trades` curveball trades. The matrix is itself a running Markov chain, so the +`trades` curveball trades among `cols`, the columns that can trade (fixed along the +chain, so found once). The matrix is itself a running Markov chain, so the generator warm-starts it from an independent SIS draw (see `matrixrandomizer`) to decorrelate the first sample from the input, and `trades` controls how far the chain moves between successive draws (thinning). """ struct CurveballSampler trades::Int + cols::Vector{Int} end -_draw!(m::SparseMatrixCSC{Bool, Int}, rng, s::CurveballSampler) = _curveball!(m, rng, s.trades) +_draw!(m::SparseMatrixCSC{Bool, Int}, rng, s::CurveballSampler) = _curveball!(m, rng, s.trades, s.cols) From 97a3606c361096196e277aa1f5da0d0fdbd4baf8 Mon Sep 17 00:00:00 2001 From: mkborregaard Date: Mon, 2 Nov 2026 17:10:51 +0900 Subject: [PATCH 2/2] Bump to 0.2.2 Co-Authored-By: Claude Opus 5.5 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c022c78..8179292 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "RandomBooleanMatrices" uuid = "9ae346a0-3d16-5633-ad70-ddb60ab77eac" license = "MIT" authors = ["mkborregaard