1
- struct Proposal{T<: ProposalStyle , P}
2
- type :: T
3
- proposal :: P
1
+ abstract type Proposal{P} end
2
+
3
+ struct StaticProposal{P} <: Proposal{P}
4
+ proposal:: P
4
5
end
5
6
6
- # Random draws
7
- Base. rand (p:: Proposal{<:ProposalStyle, <:Distribution} ) = rand (p. proposal)
8
- function Base. rand (p:: Proposal{<:ProposalStyle, <:AbstractArray} )
9
- return map (rand, p. proposal)
7
+ struct RandomWalkProposal{P} <: Proposal{P}
8
+ proposal:: P
10
9
end
11
10
11
+ # Random draws
12
+ Base. rand (p:: Proposal{<:Distribution} ) = rand (p. proposal)
13
+ Base. rand (p:: Proposal{<:AbstractArray} ) = map (rand, p. proposal)
14
+
12
15
# Densities
13
- function Distributions. logpdf (p:: Proposal{<:ProposalStyle, <:UnivariateDistribution} , v)
14
- return sum (logpdf (p. proposal, v))
15
- end
16
- function Distributions. logpdf (p:: Proposal{<:ProposalStyle, <:MultivariateDistribution} , v)
17
- return sum (logpdf (p. proposal, v))
18
- end
19
- function Distributions. logpdf (p:: Proposal{<:ProposalStyle, <:MatrixDistribution} , v)
20
- return sum (logpdf (p. proposal, v))
21
- end
22
- function Distributions. logpdf (p:: Proposal{<:ProposalStyle, <:AbstractArray} , v)
23
- return sum (map (x -> logpdf (x[1 ], x[2 ]), zip (p. proposal, v)))
24
- end
25
- function Distributions. logpdf (p:: Proposal{<:ProposalStyle, <:Function} , v)
26
- return logpdf (p. proposal (v), v)
16
+ Distributions. logpdf (p:: Proposal{<:Distribution} , v) = logpdf (p. proposal, v)
17
+ function Distributions. logpdf (p:: Proposal{<:AbstractArray} , v)
18
+ # `mapreduce` with multiple iterators requires Julia 1.2 or later
19
+ return mapreduce (((pi , vi),) -> logpdf (pi , vi), + , zip (p. proposal, v))
27
20
end
28
21
29
22
# ##############
30
23
# Random Walk #
31
24
# ##############
32
25
33
- function propose (
34
- proposal:: Proposal{RandomWalk, <:Distribution} ,
35
- model:: DensityModel ,
36
- t
37
- )
38
- return t + rand (proposal)
26
+ function propose (p:: RandomWalkProposal , m:: DensityModel )
27
+ return propose (StaticProposal (p. proposal), m)
39
28
end
40
29
41
30
function propose (
42
- proposal:: Proposal{RandomWalk, <: AbstractArray} ,
31
+ proposal:: RandomWalkProposal{<:Union{Distribution, AbstractArray} } ,
43
32
model:: DensityModel ,
44
33
t
45
34
)
46
35
return t + rand (proposal)
47
36
end
48
37
49
38
function q (
50
- proposal:: Proposal{RandomWalk, <: Distribution} ,
39
+ proposal:: RandomWalkProposal{<:Union{ Distribution,AbstractArray} } ,
51
40
t,
52
41
t_cond
53
42
)
54
- return sum (logpdf (proposal, t - t_cond))
55
- end
56
-
57
- function q (
58
- proposal:: Proposal{RandomWalk, <:AbstractArray} ,
59
- t,
60
- t_cond
61
- )
62
- return sum (logpdf (proposal, t - t_cond))
43
+ return logpdf (proposal, t - t_cond)
63
44
end
64
45
65
46
# #########
66
47
# Static #
67
48
# #########
68
49
69
- propose (p:: Proposal{RandomWalk} , m:: DensityModel ) = propose (Proposal (Static (), p. proposal), m)
70
50
function propose (
71
- proposal:: Proposal{Static, <: Distribution} ,
72
- model:: DensityModel ,
51
+ proposal:: StaticProposal{<:Union{ Distribution,AbstractArray}} ,
52
+ model:: DensityModel ,
73
53
t= nothing
74
54
)
75
55
return rand (proposal)
76
56
end
77
57
78
- function propose (
79
- p:: Proposal{Static, <:AbstractArray} ,
80
- model:: DensityModel ,
81
- t= nothing
82
- )
83
- props = map (x -> rand (x), p. proposal)
84
- return props
85
- end
86
-
87
58
function q (
88
- proposal:: Proposal{Static, <: Distribution} ,
59
+ proposal:: StaticProposal{<:Union{ Distribution,AbstractArray}} ,
89
60
t,
90
61
t_cond
91
62
)
92
- return sum (logpdf (proposal, t))
93
- end
94
-
95
- function q (
96
- proposal:: Proposal{Static, <:AbstractArray} ,
97
- t,
98
- t_cond
99
- )
100
- return sum (logpdf (proposal, t))
63
+ return logpdf (proposal, t)
101
64
end
102
65
103
66
# ###########
104
67
# Function #
105
68
# ###########
106
69
70
+ # function definition with abstract types requires Julia 1.3 or later
71
+ for T in (StaticProposal, RandomWalkProposal)
72
+ @eval begin
73
+ (p:: $T{<:Function} )() = $ T (p. proposal ())
74
+ (p:: $T{<:Function} )(t) = $ T (p. proposal (t))
75
+ end
76
+ end
77
+
107
78
function propose (
108
- proposal:: Proposal{<:ProposalStyle, <: Function} ,
79
+ proposal:: Proposal{<:Function} ,
109
80
model:: DensityModel
110
81
)
111
- p = proposal. proposal
112
- return rand (proposal. proposal ())
82
+ return propose (proposal (), model)
113
83
end
114
84
115
85
function propose (
116
- proposal:: Proposal{<:ProposalStyle, <: Function} ,
86
+ proposal:: Proposal{<:Function} ,
117
87
model:: DensityModel ,
118
88
t
119
89
)
120
- p = proposal. proposal
121
- return rand (proposal. proposal (t))
90
+ return propose (proposal (t), model)
122
91
end
123
92
124
93
function q (
125
- proposal:: Proposal{<:ProposalStyle, <: Function} ,
94
+ proposal:: Proposal{<:Function} ,
126
95
t,
127
96
t_cond
128
97
)
129
- p = proposal. proposal
130
- return sum (logpdf .(p (t_cond), t))
98
+ return q (proposal (t_cond), t, t_cond)
131
99
end
0 commit comments