Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit 0c2f8ce

Browse files
committed
Refactor rand to include multiple algorithms
1 parent 2c2244c commit 0c2f8ce

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

src/processes.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,35 @@ Tells whether or not the spatial point process `p` is homogeneous.
1717
ishomogeneous(p::PointProcess) = false
1818

1919
"""
20-
rand(p, r, n=1)
20+
rand(p, r, n=1; [algo])
2121
2222
Generate `n` realizations of spatial point process `p`
23-
inside spatial region `r`.
23+
inside spatial region `r`. Optionally specify sampling
24+
algorithm `algo`.
2425
"""
25-
Base.rand(p::PointProcess, r::AbstractRegion, n::Int) =
26-
[rand_single(p, r) for i in 1:n]
26+
Base.rand(p::PointProcess, r::AbstractRegion, n::Int;
27+
algo=default_sampling_algorithm(p)) =
28+
[rand_single(p, r, algo) for i in 1:n]
2729

28-
Base.rand(p::PointProcess, r::AbstractRegion) = rand_single(p, r)
30+
Base.rand(p::PointProcess, r::AbstractRegion;
31+
algo=default_sampling_algorithm(p)) =
32+
rand_single(p, r, algo)
2933

3034
"""
31-
rand_single(p, r)
35+
rand_single(p, r, algo)
3236
3337
Generate a single realization of spatial point process
34-
`p` inside spatial region `r`.
38+
`p` inside spatial region `r` with sampling `algo`.
3539
"""
36-
rand_single(p::PointProcess, r::AbstractRegion) = @error "not implemented"
40+
rand_single(p::PointProcess, r::AbstractRegion, algo) =
41+
@error "not implemented"
42+
43+
"""
44+
default_sampling_algorithm(p)
45+
46+
Default sampling algorithm for spatial point process `p`.
47+
"""
48+
default_sampling_algorithm(p::PointProcess) = @error "not implemented"
3749

3850
"""
3951
p₁ ∪ p₂

src/processes/binomial.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ end
1313

1414
ishomogeneous(p::BinomialProcess) = true
1515

16-
function rand_single(p::BinomialProcess, r::RectangleRegion{T,N}) where {N,T}
16+
struct BinomialSampling end
17+
18+
default_sampling_algorithm(::BinomialProcess) = BinomialSampling()
19+
20+
function rand_single(p::BinomialProcess, r::RectangleRegion{T,N},
21+
algo::BinomialSampling) where {N,T}
1722
# region configuration
1823
lo = lowerleft(r)
1924
up = upperright(r)

src/processes/poisson.jl

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@ struct PoissonProcess{L<:Union{Real,Function}} <: PointProcess
1212
end
1313

1414
ishomogeneous(p::PoissonProcess{<:Real}) = true
15+
ishomogeneous(p::PoissonProcess{<:Function}) = false
1516

17+
struct ProductSampling end
18+
struct ThinnedSampling end
19+
struct DiscretizedSampling end
20+
21+
default_sampling_algorithm(::PoissonProcess{<:Real}) = ProductSampling()
22+
default_sampling_algorithm(::PoissonProcess{<:Function}) = DiscretizedSampling()
23+
24+
# -----------------
1625
# homogeneous case
17-
function rand_single(p::PoissonProcess{<:Real}, r::RectangleRegion{T,N}) where {N,T}
26+
# -----------------
27+
function rand_single(p::PoissonProcess{<:Real}, r::RectangleRegion{T,N},
28+
algo::ProductSampling) where {N,T}
1829
# region configuration
1930
lo = lowerleft(r)
2031
up = upperright(r)
@@ -30,9 +41,35 @@ function rand_single(p::PoissonProcess{<:Real}, r::RectangleRegion{T,N}) where {
3041
PointPattern(rand(U, n))
3142
end
3243

44+
# -------------------
3345
# inhomogeneous case
34-
function rand_single(p::PoissonProcess{<:Function}, r::RectangleRegion{T,N}) where {N,T}
46+
# -------------------
47+
function rand_single(p::PoissonProcess{<:Function}, r::RectangleRegion{T,N},
48+
algo::DiscretizedSampling) where {N,T}
49+
# discretize region
50+
# TODO
51+
52+
# discretize retention
53+
# TODO
54+
55+
# sample each element
3556
# TODO
3657
end
3758

38-
Base.union(p₁::PoissonProcess, p₂::PoissonProcess) = PoissonProcess(p₁.λ + p₂.λ)
59+
function rand_single(p::PoissonProcess{<:Function}, r::RectangleRegion{T,N},
60+
algo::ThinnedSampling) where {N,T}
61+
# Lewis-Shedler algorithm
62+
# TODO
63+
end
64+
65+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Real}) =
66+
PoissonProcess(p₁.λ + p₂.λ)
67+
68+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Function}) =
69+
PoissonProcess(x -> p₁.λ(x) + p₂.λ(x))
70+
71+
Base.union(p₁::PoissonProcess{<:Real}, p₂::PoissonProcess{<:Function}) =
72+
PoissonProcess(x -> p₁.λ + p₂.λ(x))
73+
74+
Base.union(p₁::PoissonProcess{<:Function}, p₂::PoissonProcess{<:Real}) =
75+
PoissonProcess(x -> p₁.λ(x) + p₂.λ)

src/processes/union.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ end
1414

1515
ishomogeneous(p::UnionProcess) = ishomogeneous(p.p₁) && ishomogeneous(p.p₂)
1616

17-
function rand_single(p::UnionProcess, r::AbstractRegion)
17+
struct UnionSampling end
18+
19+
default_sampling_algorithm(::UnionProcess) = UnionSampling()
20+
21+
function rand_single(p::UnionProcess, r::AbstractRegion, algo::UnionSampling)
1822
pp₁ = rand(p.p₁, r)
1923
pp₂ = rand(p.p₂, r)
2024

src/thinning/random.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ thin(p::PoissonProcess{<:Real}, t::RandomThinning{<:Real}) =
1818
PoissonProcess(t.p * p.λ)
1919

2020
thin(p::PoissonProcess{<:Function}, t::RandomThinning{<:Function}) =
21-
PoissonProcess(u -> t.p(u) * p.λ(u))
21+
PoissonProcess(x -> t.p(x) * p.λ(x))
2222

2323
thin(p::PoissonProcess{<:Real}, t::RandomThinning{<:Function}) =
24-
PoissonProcess(u -> t.p(u) * p.λ)
24+
PoissonProcess(x -> t.p(x) * p.λ)
2525

2626
thin(p::PoissonProcess{<:Function}, t::RandomThinning{<:Real}) =
27-
PoissonProcess(u -> t.p * p.λ(u))
27+
PoissonProcess(x -> t.p * p.λ(x))
2828

2929
# -----------------------
3030
# thinning point pattern

0 commit comments

Comments
 (0)