Skip to content

Conversation

@torfjelde
Copy link
Member

@torfjelde torfjelde commented May 20, 2020

With this method you can now do:

julia> using Turing

julia> @model function linear_reg(x, y, σ = 0.1)
           β ~ Normal(0, 1)

           for i  eachindex(y)
               y[i] ~ Normal* x[i], σ)
           end
       end
DynamicPPL.ModelGen{var"###generator#384",(:x, :y, :σ),(:σ,),Tuple{Float64}}(##generator#384, (σ = 0.1,))

julia> f(x) = 2 * x + 0.1 * randn()
f (generic function with 1 method)

julia> Δ = 0.1
0.1

julia> xs_train = 0:Δ:10; ys_train = f.(xs_train);

julia> xs_test = [10 + Δ, 10 + 2 * Δ]; ys_test = f.(xs_test);

julia> # Infer
       m_lin_reg = linear_reg(xs_train, ys_train);

julia> chain_lin_reg = sample(m_lin_reg, NUTS(100, 0.65), 200);
┌ Info: Found initial step size
└   ϵ = 0.003125

julia> m_lin_reg_test = linear_reg(xs_test, Vector{Union{Missing, Float64}}(undef, length(ys_test)));

julia> predictions = Turing.Inference.predict(m_lin_reg_test, chain_lin_reg)
Object of type Chains, with data of type 100×2×1 Array{Float64,3}

Iterations        = 1:100
Thinning interval = 1
Chains            = 1
Samples per chain = 100
parameters        = y[1], y[2]

2-element Array{ChainDataFrame,1}

Summary Statistics
  parameters     mean     std  naive_se     mcse       ess   r_hat
  ──────────  ───────  ──────  ────────  ───────  ────────  ──────
        y[1]  20.1978  0.0921    0.0092  missing  224.2130  0.9904
        y[2]  20.3739  0.1056    0.0106  missing  273.7488  0.9919

Quantiles
  parameters     2.5%    25.0%    50.0%    75.0%    97.5%
  ──────────  ───────  ───────  ───────  ───────  ───────
        y[1]  20.0378  20.1254  20.1863  20.2668  20.3704
        y[2]  20.1650  20.3117  20.3800  20.4430  20.5696


julia> ys_pred = collect(vec(mean(predictions[:y].value; dims = 1)))
2-element Array{Float64,1}:
 20.19778013967426
 20.373873670853182

julia> sum(abs2, ys_test - ys_pred)
0.0016295979352728104

One question: should this method be exported? I'm currently overloading StatsBase.predict because Turing will often used together with StatsBase and so I figured overloading rather than defining our own is preferable to avoid warnings. This is also up for discussion; I'm fine either way.

@torfjelde torfjelde requested a review from cpfiffer May 20, 2020 14:27
@devmotion
Copy link
Member

I'm currently overloading StatsBase.predict because Turing will often used together with StatsBase and so I figured overloading rather than defining our own is preferable to avoid warnings.

IMO we should definitely overload StatsBase.predict and not define our own method, but that's orthogonal to the question if we should (re-)export it. Maybe it's more convenient for users if we reexport it, but I don't have a strong opinion on that question.

@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #1286 into master will increase coverage by 0.48%.
The diff coverage is 95.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1286      +/-   ##
==========================================
+ Coverage   67.81%   68.29%   +0.48%     
==========================================
  Files          25       25              
  Lines        1367     1391      +24     
==========================================
+ Hits          927      950      +23     
- Misses        440      441       +1     
Impacted Files Coverage Δ
src/inference/Inference.jl 91.26% <95.83%> (+1.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6a61f0e...7f5e526. Read the comment docs.

Copy link
Member

@cpfiffer cpfiffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No further comments from me! Looks solid as all get-out.

@cpfiffer cpfiffer merged commit fa2d534 into master May 21, 2020
@delete-merged-branch delete-merged-branch bot deleted the tor/prediction branch May 21, 2020 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants