-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #644 - treat missing and Vector{Missing} inputs as parameters #651
Conversation
Thanks, @mohamed82008 an example to illustrate what syntax is changed in this PR will be very helpful for understanding the code. |
Here are examples of what's now possible: julia> using Turing
julia> @model gdemo(x) = begin
s ~ InverseGamma(2,3)
m ~ Normal(0, sqrt(s))
x[1] ~ Normal(m, sqrt(s))
x[2] ~ Normal(m, sqrt(s))
return s, m
end
gdemo (generic function with 2 methods)
julia> model = gdemo(fill(missing, 2));
julia> model.pvars
(:s, :m, :x) and julia> @model gdemo(x = Vector{Real}(undef, 2)) = begin
s ~ InverseGamma(2,3)
m ~ Normal(0, sqrt(s))
x[1] ~ Normal(m, sqrt(s))
x[2] ~ Normal(m, sqrt(s))
return s, m
end
gdemo (generic function with 2 methods)
julia> model = gdemo(missing);
julia> model.pvars
(:s, :m, :x) and julia> @model testmodel01(x) = begin
x ~ Bernoulli(0.5)
return x
end
testmodel01 (generic function with 2 methods)
julia> model = testmodel01(missing);
julia> model.pvars
(:x,) Note that if an input Another thing now possible is sampling missing data automatically. Notice that julia> @model gdemo(x) = begin
s ~ InverseGamma(2,3)
m ~ Normal(0, sqrt(s))
x[1] ~ Normal(m, sqrt(s))
x[2] ~ Normal(m, sqrt(s))
return s, m
end
gdemo (generic function with 2 methods)
julia> x = Union{Real, Missing}[1.0, missing];
julia> model = gdemo(x);
julia> model.pvars
(:s, :m)
julia> sample(model, HMC(1000, 0.1, 5));
[HMC] Sampling...100% Time: 0:00:05
[HMC] Finished with
Running time = 5.188032614999971;
Accept rate = 0.997;
#lf / sample = 4.995;
#evals / sample = 6.995;
pre-cond. metric = [1.0].
julia> x
2-element Array{Union{Missing, Real},1}:
1.0
1.3189310726427184 |
Notice that all the cases above used to not work before, so this is technically a non-breaking change. |
Thanks, @mohamed82008. Since it involves some syntax changes, it's better to merge this during the hackathon this week. |
Any comments on this one? |
137d79e
to
cafef71
Compare
Here is a summary of the discussion during yesterday's hackathon. For However, for scenarios with mixed @model gdemo(x, y) = begin
s ~ InverseGamma(2,3)
m ~ Normal(0, sqrt(s))
x[1] ~ Normal(m, sqrt(s))
x[2] ~ Normal(m, sqrt(s)) # L1
y ~ Normal(x[2], sqrt(s)) # L2
end
julia> x = Union{Real, Missing}[1.0, missing];
julia> model = gdemo(x);
There are two potential issues here. Firstly, when Secondly, when @model demo(data1, data2 ; hypers) = begin
end |
The syntax using keyword arguments for hyper-parameters looks good to me. It seems to me that as long as nothing specified as |
Would hyperparameters work something like this, in that proposal? @model gdemo(x; s=missing, m=missing) = begin
s ~ InverseGamma(2,3)
m ~ Normal(0, sqrt(s))
x ~ Normal(m, sqrt(s))
end Then, when I called this, I could either sample from the priors on |
In the model your provide, the hyper-parameters should look like this @model gdemo(x; a=2, b=3) = begin
s ~ InverseGamma(a, b)
m ~ Normal(0, sqrt(s))
x ~ Normal(m, sqrt(s))
end In your example, whether or not to providing Maybe it's also nice to have what you proposed. Let's see how other people think. |
@xukai92 Do you suggest to leave this at the courtesy of the user or to check for it in runtime? I don't think the latter is possible in the general case. The only other alternative is to completely switch off this feature. @yebai If the last feature is controversial, I can remove it from this PR; it is literally just a branch in an |
I think what Kai means is to perform this checking at runtime. This is probably a feature not many people need and could cause side effects if not used carefully. So I suggest:
|
I will remove it for now. |
cafef71
to
6f96076
Compare
This PR fixes #644. This PR also allows mixing
Real
andmissing
in data which is a feature requested by @trappmartin in #544.