Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "RandomBooleanMatrices"
uuid = "9ae346a0-3d16-5633-ad70-ddb60ab77eac"
license = "MIT"
authors = ["mkborregaard <mkborregaard@snm.ku.dk"]
version = "0.2.1"
version = "0.2.2"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
2 changes: 1 addition & 1 deletion src/RandomBooleanMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/curveball.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Loading