-
Notifications
You must be signed in to change notification settings - Fork 15
/
variable.jl
212 lines (150 loc) · 10.1 KB
/
variable.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
export AbstractVariable, degree
export is_clamped, is_marginalisation, is_moment_matching
export FoldLeftProdStrategy, FoldRightProdStrategy, CustomProdStrategy
export getprediction, getpredictions, getmarginal, getmarginals, setmarginal!, setmarginals!, name, as_variable
export setmessage!, setmessages!
using Rocket
abstract type AbstractVariable end
## Base interface extensions
Base.broadcastable(v::AbstractVariable) = Ref(v)
## Variable collection type
abstract type AbstractVariableCollectionType end
struct VariableIndividual <: AbstractVariableCollectionType end
struct VariableVector <: AbstractVariableCollectionType
index::Int
end
struct VariableArray{S, I} <: AbstractVariableCollectionType
size :: S
index :: I
end
linear_index(::VariableIndividual) = nothing
linear_index(v::VariableVector) = v.index
linear_index(v::VariableArray) = LinearIndices(v.size)[v.index]
string_index(::VariableIndividual) = ""
string_index(v::VariableVector) = string("[", v.index, "]")
string_index(v::VariableArray) = string("[", join(v.index.I, ", "), "]")
indexed_name(::VariableIndividual, name::Symbol) = string(name)
indexed_name(seq::VariableVector, name::Symbol) = string(name, "_", seq.index)
indexed_name(array::VariableArray, name::Symbol) = string(name, "_", join(array.index.I, "_"))
indexed_name(::Nothing) = nothing
indexed_name(collection::Tuple) = map(indexed_name, collection)
indexed_name(randomvar::AbstractVariable) = indexed_name(collection_type(randomvar), name(randomvar))
## Messages to Marginal product strategies
struct FoldLeftProdStrategy end
struct FoldRightProdStrategy end
struct CustomProdStrategy{F}
prod_callback_generator::F
end
"""
messages_prod_fn(strategy, prod_constraint, form_constraint, form_check_strategy)
Returns a suitable prod computation function for a given strategy and constraints
See also: [`FoldLeftProdStrategy`](@ref), [`FoldRightProdStrategy`](@ref), [`CustomProdStrategy`](@ref)
"""
function messages_prod_fn end
messages_prod_fn(::FoldLeftProdStrategy, prod_constraint, form_constraint, form_check_strategy) = prod_foldl_reduce(prod_constraint, form_constraint, form_check_strategy)
messages_prod_fn(::FoldRightProdStrategy, prod_constraint, form_constraint, form_check_strategy) = prod_foldr_reduce(prod_constraint, form_constraint, form_check_strategy)
messages_prod_fn(strategy::CustomProdStrategy, prod_constraint, form_constraint, form_check_strategy) = strategy.prod_callback_generator(prod_constraint, form_constraint, form_check_strategy)
function marginal_prod_fn(strategy, prod_constraint, form_constraint, form_check_strategy)
return let prod_fn = messages_prod_fn(strategy, prod_constraint, form_constraint, form_check_strategy)
return (messages) -> as_marginal(prod_fn(messages))
end
end
## Common functions
function degree end
add_pipeline_stage!(variable::AbstractVariable, stage) = error("Its not possible to add a new pipeline stage for $(variable)")
# Helper functions
# Getters
getprediction(variable::AbstractVariable) = _getprediction(variable)
getpredictions(variables::AbstractArray{<:AbstractVariable}) = collectLatest(map(v -> getprediction(v), variables))
getmarginal(variable::AbstractVariable) = getmarginal(variable, SkipInitial())
getmarginal(variable::AbstractVariable, skip_strategy::MarginalSkipStrategy) = apply_skip_filter(_getmarginal(variable), skip_strategy)
getmarginals(variables::AbstractArray{<:AbstractVariable}) = getmarginals(variables, SkipInitial())
getmarginals(variables::AbstractArray{<:AbstractVariable}, skip_strategy::MarginalSkipStrategy) = collectLatest(map(v -> getmarginal(v, skip_strategy), variables))
## Setters
### Marginals
setmarginal!(variable::AbstractVariable, marginal) = setmarginal!(getmarginal(variable, IncludeAll()), marginal)
setmarginals!(variables::AbstractArray{<:AbstractVariable}, marginal::PointMass) = _setmarginals!(Base.HasLength(), variables, Iterators.repeated(marginal, length(variables)))
setmarginals!(variables::AbstractArray{<:AbstractVariable}, marginal::Distribution) = _setmarginals!(Base.HasLength(), variables, Iterators.repeated(marginal, length(variables)))
setmarginals!(variables::AbstractArray{<:AbstractVariable}, marginals) = _setmarginals!(Base.IteratorSize(marginals), variables, marginals)
function _setmarginals!(::Base.IteratorSize, variables::AbstractArray{<:AbstractVariable}, marginals)
foreach(zip(variables, marginals)) do (variable, marginal)
setmarginal!(variable, marginal)
end
end
function _setmarginals!(::Any, variables::AbstractArray{<:AbstractVariable}, marginals)
error("setmarginals!() failed. Default value is neither an iterable object nor a distribution.")
end
### Messages
setmessage!(variable::AbstractVariable, index::Int, message) = setmessage!(messageout(variable, index), message)
setmessage!(variable::AbstractVariable, message) = foreach(i -> setmessage!(variable, i, message), 1:degree(variable))
setmessages!(variables::AbstractArray{<:AbstractVariable}, message::PointMass) = _setmessages!(Base.HasLength(), variables, Iterators.repeated(message, length(variables)))
setmessages!(variables::AbstractArray{<:AbstractVariable}, message::Distribution) = _setmessages!(Base.HasLength(), variables, Iterators.repeated(message, length(variables)))
setmessages!(variables::AbstractArray{<:AbstractVariable}, messages) = _setmessages!(Base.IteratorSize(messages), variables, messages)
function _setmessages!(::Base.IteratorSize, variables::AbstractArray{<:AbstractVariable}, messages)
foreach(zip(variables, messages)) do (variable, message)
setmessage!(variable, message)
end
end
function _setmessages!(::Any, variables::AbstractArray{<:AbstractVariable}, marginals)
error("setmessages!() failed. Default value is neither an iterable object nor a distribution.")
end
## Utility functions
name(variable::AbstractVariable) = variable.name
isanonymous(variable::AbstractVariable) = false
isanonymous(variables::AbstractArray) = all(isanonymous, variables)
##
struct VariableReferenceProxyUnchecked end
struct VariableReferenceProxyChecked end
"""
resolve_variable_proxy
This function resolves variable that should be used for factorisation constraints resolution or in other places. The idea here is that random variables can be automatically created by the model specification
language and user might be unaware of them. Such variables also have some randomly generated names and cannot be used explicitly in constraints specification language. However, ReactiveMP.jl keeps
track of `proxy_variables`. During the first call of `get_factorisation_reference` we check if there are some proxy variables at all and:
1. if not we simply return name and linear index of the current variable
2. if yes we pass it futher to the `unchecked` version of the function
2.1 `unchecked` version return immediatelly if there is only one proxy var (see bullet 1)
2.2 in case of multiple proxy vars we filter only `RandomVariable` and call `checked` version of the function
3. `checked` version of the function return immediatelly if there is only one proxy random variable left, if there are multiple proxy random vars we throw an error as this case is ambigous for factorisation constraints specification
This function is a part of private API and should not be used explicitly.
"""
function resolve_variable_proxy end
function resolve_variable_proxy(var::AbstractVariable)
if !isanonymous(var)
return resolve_variable_proxy(var, VariableReferenceProxyChecked(), nothing)
else
return resolve_variable_proxy(var, VariableReferenceProxyUnchecked(), proxy_variables(var))
end
end
function resolve_variable_proxy(var::AbstractVariable, ::Union{VariableReferenceProxyChecked, VariableReferenceProxyUnchecked}, ::Nothing)
return (name(var), linear_index(collection_type(var)), var)
end
resolve_variable_proxy(var::AbstractVariable, ::VariableReferenceProxyUnchecked, proxy::Tuple{T}) where {T <: AbstractVariable} = resolve_variable_proxy(first(proxy))
resolve_variable_proxy(var::AbstractVariable, ::VariableReferenceProxyUnchecked, proxy::Tuple) = resolve_variable_proxy(var, VariableReferenceProxyChecked(), filter(v -> v isa RandomVariable, proxy))
resolve_variable_proxy(::AbstractVariable, ::VariableReferenceProxyChecked, proxy::Tuple{T}) where {T <: AbstractVariable} = resolve_variable_proxy(first(proxy))
resolve_variable_proxy(var::AbstractVariable, ::VariableReferenceProxyChecked, proxy::Tuple) = error(
"Multiple proxy vars $(map(name, proxy)) for variable $(name(var)) are dissalowed in reference resolution function. This may happened because of the deterministic relation in the model that has more than one random variable as an input. This setting does not play nicely with constraints or meta specification languages. As a workaround create and give a specific name for the output variable of this deterministic relation."
)
## Helper functions
"""
as_variable(x)
Converts an object (or array of objects) `x` to an instance of `AbstractVariable` (or to an array). Does nothing if `x` is already an instance of `AbstractVariable`.
See also: [`ReactiveMP.undo_as_variable`](@ref)
"""
function as_variable end
as_variable(x) = constvar(:anonymous_constvar, x)
as_variable(t::Tuple) = map(as_variable, t)
as_variable(v::AbstractVariable) = v
as_variable(v::AbstractArray{<:AbstractVariable}) = v
## undo as_variable
"""
undo_as_variable(x)
Undoes the operation of `as_variable` if possible. Otherwise does nothing.
See also: [`ReactiveMP.as_variable`](@ref)
"""
function undo_as_variable end
undo_as_variable(x) = error("Cannot undo `as_variable` operation for variable `x`. `x = $(x)` should be an instance of `AbstractVariable`")
undo_as_variable(v::AbstractVariable) = v
israndom(v::AbstractArray{<:AbstractVariable}) = all(israndom, v)
isdata(v::AbstractArray{<:AbstractVariable}) = all(isdata, v)
isconst(v::AbstractArray{<:AbstractVariable}) = all(isconst, v)
isproxy(v::AbstractArray{<:AbstractVariable}) = any(isproxy, v)