Skip to content

Commit b910d57

Browse files
committed
Work on docstrings and restructure the file tree
1 parent 9118f22 commit b910d57

18 files changed

+397
-321
lines changed

src/PartialRejectionSampling.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,26 @@ export generate_sample,
5252
# Code inclusions
5353

5454
include("common.jl")
55-
5655
include("utils.jl")
57-
include("window.jl")
5856

59-
# sampling
60-
include("dominated_cftp.jl")
57+
include(joinpath("spatial", "window.jl"))
58+
include(joinpath("graph", "window.jl"))
59+
6160
include("grid_prs.jl")
6261

63-
# Spatial point processes
64-
include("poisson.jl")
65-
include("strauss.jl")
66-
include("hard_core_spatial.jl")
62+
## Spatial point processes
63+
include(joinpath("spatial", "dominated_cftp.jl"))
64+
include(joinpath("spatial", "poisson.jl"))
65+
include(joinpath("spatial", "hard_core.jl"))
66+
include(joinpath("spatial", "strauss.jl"))
6767

68-
# Graph point processes
69-
include("ising.jl")
70-
include("hard_core_graph.jl")
71-
include("rooted_spanning_forest.jl")
72-
include("sink_free_graph.jl")
68+
## Graph point processes
69+
include(joinpath("graph", "ising.jl"))
70+
include(joinpath("graph", "hard_core.jl"))
71+
include(joinpath("graph", "rooted_spanning_forest.jl"))
72+
include(joinpath("graph", "sink_free_graph.jl"))
7373

7474
# Misc
75-
include("pattern_free_string.jl")
75+
include(joinpath("misc", "pattern_free_string.jl"))
7676

7777
end

src/common.jl

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ The type `T` corresponds to the elements' type of the point process (vectors, gr
1111
abstract type AbstractPointProcess{T} end
1212
Base.eltype(pp::AbstractPointProcess{T}) where {T} = T
1313

14+
@doc raw"""
15+
AbstractGraphPointProcess{T<:Vector{Float64}} <: AbstractPointProcess{T}
16+
17+
Abstract type encoding point processes defined on graphs
18+
"""
19+
abstract type AbstractGraphPointProcess{T} <: AbstractPointProcess{T} end
20+
1421
@doc raw"""
1522
AbstractSpatialPointProcess{T<:Vector{Float64}} <: AbstractPointProcess{T}
1623
@@ -21,28 +28,48 @@ Concrete instances must have a `window` field of type [`PRS.AbstractSpatialWindo
2128
abstract type AbstractSpatialPointProcess{T<:Vector{Float64}} <: AbstractPointProcess{T} end
2229

2330
"""
24-
window(pp::AbstractSpatialPointProcess) = pp.window
31+
window(pp::AbstractSpatialPointProcess)::AbstractSpatialWindow = pp.window
2532
"""
26-
window(pp::AbstractSpatialPointProcess) = pp.window
33+
function window(pp::AbstractSpatialPointProcess)::AbstractSpatialWindow
34+
return pp.window
35+
end
2736

2837
"""
2938
dimension(pp::AbstractSpatialPointProcess) = dimension(window(pp))
3039
31-
**See also**
32-
- [`window`](@ref)
40+
**See also** [`window`](@ref)
3341
"""
3442
dimension(pp::AbstractSpatialPointProcess) = dimension(window(pp))
3543

36-
abstract type AbstractGraphPointProcess{T} <: AbstractPointProcess{T} end
44+
"""
45+
Abstract type representing a window
3746
38-
# Default methods
47+
**See also**
3948
49+
- [`PRS.AbstractDiscreteWindow`](@ref)
50+
- [`PRS.AbstractSpatialWindow`](@ref)
4051
"""
41-
Default exact sampling method
52+
abstract type AbstractWindow end
53+
54+
@doc raw"""
55+
AbstractSpatialWindow{T<:Float64} <: AbstractWindow{T}
56+
57+
Abstract type representing a spatial window ``\subseteq \mathbb{R}^d``
58+
59+
**See also**
60+
61+
- [`PRS.AbstractRectangleWindow`](@ref)
62+
- [`PRS.RectangleWindow`](@ref)
63+
- [`PRS.SquareWindow`](@ref)
64+
- [`PRS.BallWindow`](@ref)
4265
"""
43-
function generate_sample end
66+
abstract type AbstractSpatialWindow{T<:Float64} <: AbstractWindow end
4467

4568
"""
46-
Default exact sampling method using Partial Rejection Sampling (PRS, [GuJeLi19](@cite))
69+
Abstract type representing a window on a discrete state space
70+
71+
**See also**
72+
73+
- [`PRS.GraphNode`](@ref)
4774
"""
48-
function generate_sample_prs end
75+
abstract type AbstractDiscreteWindow{T} <: AbstractWindow end

src/hard_core_graph.jl renamed to src/graph/hard_core.jl

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
@doc raw"""
22
HardCoreGraph{T<:Integer} <: AbstractGraphPointProcess{T}
33
4-
Point process defined on the vertices of a `graph` ``=(V, E)``, and parametrized by β ``\geq 0``, with joint density
4+
Concrete type representing a point process on the vertices of a `graph` ``=(V, E)`` parametrized by `\beta ≥ 0` which characterizes the distribution on the [independent sets](https://en.wikipedia.org/wiki/Independent_set_(graph_theory)) of `graph`, where each vertex is present with marginal probability ``\frac{\beta}{1+\beta}``.
5+
6+
In other words, it can also be viewed as the product distribution ``\operatorname{Bernoulli}(\frac{\beta}{1+\beta})^{\otimes |V|}`` on the vertices of `graph` conditioned on forming an independent set,
57
68
```math
79
\mathbb{P}\!\left[ \mathcal{X} = X \right]
810
\propto
911
\prod_{x\in X}
1012
\frac{\beta}{1+\beta}
11-
1_{X \text{forms an independent set}}
13+
1_{X \text{forms an independent set}}.
1214
```
1315
14-
It can be viewed as a distribution on the [independent sets](https://en.wikipedia.org/wiki/Independent_set_(graph_theory)) of `graph`, where each vertex is present with marginal probability ``\frac{\beta}{1+\beta}``.
15-
In other words, it can also be viewed as the product distribution ``\operatorname{Bernoulli}(\frac{\beta}{1+\beta})^{\otimes |V|}`` on the vertices of `graph` conditioned on forming an independent set.
16-
1716
**See also**
1817
1918
- Section 7.2 of [GuJeLi19](@cite)
2019
- Example 4.1 of [MoKr20](@ref)
21-
- [`PRS.HardCoreSpatial`](@ref)
20+
- [`PRS.HardCorePointProcess`](@ref), the spatial counterpart of [`PRS.HardCoreGraph`](@ref)
2221
"""
2322
struct HardCoreGraph{T<:Integer} <: AbstractGraphPointProcess{T}
2423
"Graph"
@@ -32,7 +31,7 @@ end
3231
β::Real
3332
) where {T<:Integer}
3433
35-
Construct a [`HardCoreGraph`](@ref)
34+
Construct a [`HardCoreGraph`](@ref).
3635
"""
3736
function HardCoreGraph(
3837
graph::LG.SimpleGraph{T},
@@ -44,11 +43,28 @@ end
4443

4544
# Sampling
4645

46+
"""
47+
generate_sample(
48+
pp::HardCoreGraph{T};
49+
rng=-1
50+
)::Vector{T} where {T}
51+
52+
Generate an exact sample from the [`PRS.SinkFreeGraph`](@ref).
53+
54+
Default sampler is [`PRS.generate_sample_prs`](@ref).
55+
"""
56+
function generate_sample(
57+
pp::HardCoreGraph{T};
58+
rng=-1
59+
)::Vector{T} where {T}
60+
return generate_sample_prs(pp; rng=rng)
61+
end
62+
4763
## Partial Rejeciton Sampling (PRS)
4864

4965
"""
5066
generate_sample_prs(
51-
hcg::HardCoreGraph{T};
67+
pp::HardCoreGraph{T};
5268
rng=-1
5369
)::Vector{T} where {T}
5470
@@ -59,15 +75,15 @@ Sample from [`PRS.HardCoreGraph`](@ref) using Partial Rejection Sampling (PRS),
5975
- Example 4.1 of [MoKr20](@ref)
6076
"""
6177
function generate_sample_prs(
62-
hcg::HardCoreGraph{T};
78+
pp::HardCoreGraph{T};
6379
rng=-1
6480
)::Vector{T} where {T}
6581

66-
proba = hcg.β / (one(hcg.β) + hcg.β)
82+
proba = pp.β / (one(pp.β) + pp.β)
6783

6884
rng = getRNG(rng)
69-
adj = LG.adjacency_matrix(hcg.graph)
70-
occupied = randsubseq(rng, LG.vertices(hcg.graph), proba)
85+
adj = LG.adjacency_matrix(pp.graph)
86+
occupied = randsubseq(rng, LG.vertices(pp.graph), proba)
7187
while true
7288
# Check if occupied vertices form an independent set
7389
sub_graph = LG.SimpleGraph(adj[occupied, occupied])
@@ -80,7 +96,7 @@ function generate_sample_prs(
8096
else # Construct the resampling set of vertices
8197
union!(resample, occupied[cc])
8298
for v in cc
83-
union!(resample, LG.neighbors(hcg.graph, occupied[v]))
99+
union!(resample, LG.neighbors(pp.graph, occupied[v]))
84100
end
85101
end
86102
end

src/ising.jl renamed to src/graph/ising.jl

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
@doc raw"""
22
Ising{T<:Int} <: AbstractGraphPointProcess{T}
33
4-
[Ising](https://en.wikipedia.org/wiki/Ising_model) is a point process defined on the vertices of `graph` ``=(V, E)`` with joint density proportional to
4+
The [Ising model](https://en.wikipedia.org/wiki/Ising_model) characterizes a point process defined on the vertices of graph ``(V, E)`` with joint density proportional to
55
66
```math
77
\mathbb{P}\!\left[ \mathcal{X}=X \right]
8+
\propto
89
\prod_{i \in V}
910
\exp(h_i x_i)
10-
\prod_{\{i, j\} \subseteq E}
11+
\prod_{\{i, j\} \in E}
1112
\exp(J x_i x_j)
1213
```
1314
14-
where ``(h_i)_{V}`` are called magnetization paremeters and ``J`` the interaction coefficient (``J \pm 0`` characterizes ferro/antiferro magnetic interactions).
15+
where ``(h_i)_{V}`` are called magnetization paremeters and ``J`` the interaction coefficient (``J \gtreqless 0`` characterizes ferro/antiferro magnetic interactions).
1516
"""
1617
struct Ising{T<:Int} <: AbstractGraphPointProcess{T}
1718
graph::LG.SimpleGraph{T}
@@ -96,7 +97,51 @@ function Ising(
9697
return Ising{T}(graph, float.(h), J)
9798
end
9899

100+
@doc raw"""
101+
generate_sample_prs(pp::Ising; rng=-1)
102+
103+
Generate an exact sample form `pp` using Partial Rejection Sampling (PRS), see Section 4.2 of [GuJeLi19](@cite)
104+
105+
Default sampler is [`PRS.generate_sample_grid_prs`](@ref)
106+
107+
**See also**
108+
109+
- [FeViYi19](@cite)
110+
- [FeGuYi19](@cite) [`PRS.generate_sample_gibbs_perfect`](@ref)
111+
"""
112+
generate_sample_prs(pp::Ising; rng=-1) = generate_sample_grid_prs(pp; rng=rng)
113+
114+
@doc raw"""
115+
generate_sample(
116+
pp::Ising{T},
117+
idx::T;
118+
rng=-1
119+
)::T where {T<:Int}
120+
121+
Generate an exact sample from the marginal distribution of state ``i=`` `idx` of `pp`.
122+
123+
More specifically,
124+
125+
```math
126+
x_i
127+
\sim
128+
\operatorname{Bernoulli}_{-1, 1}
129+
(\sigma(h_i)),
130+
```
131+
132+
where ``\sigma`` denotes the [`PRS.sigmoid`](@ref) function.
133+
"""
134+
function generate_sample(
135+
pp::Ising{T},
136+
idx::T;
137+
rng=-1
138+
)::T where {T<:Int}
139+
rng = getRNG(rng)
140+
hᵢ = pp.h isa Real ? pp.h : pp.h[idx]
141+
return rand(rng) < sigmoid(hᵢ) ? one(T) : -one(T)
142+
end
143+
99144
## Sampling
100145

101-
include("ising/ising_sampling_gibbs_perfect.jl")
102-
include("ising/ising_sampling_grid_prs.jl")
146+
include("ising_sampling_gibbs_perfect.jl")
147+
include("ising_sampling_grid_prs.jl")

src/ising/ising_sampling_gibbs_perfect.jl renamed to src/graph/ising_sampling_gibbs_perfect.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function generate_sample_gibbs_perfect(
2020

2121
while !isempty(R)
2222
i = rand(rng, R)
23-
if bayes_filter(ising, state, i, R; rng=-1)
23+
if bayes_filter(ising, state, i, R; rng=rng)
2424
generate_sample_conditional!(state, i, ising; rng=rng)
2525
delete!(R, i)
2626
else
@@ -39,15 +39,7 @@ end
3939
rng=-1
4040
) where {T<:Int}
4141
42-
Generate an exact sample from the marginal distribution of each states of the [`Ising`](@ref) model `ising` at the prescribed `indices`
43-
44-
```math
45-
x_i
46-
\sim
47-
\operatorname{Bernoulli}_{-1, 1}
48-
(\sigma(h_i)),
49-
```
50-
where ``\sigma`` denotes the [`sigmoid`](@ref) function.
42+
Generate an exact sample from the marginal distribution of each states of the [`Ising`](@ref) model `ising` at the prescribed `indices`.
5143
"""
5244
function generate_sample!(
5345
state::AbstractVector{T},

0 commit comments

Comments
 (0)