TuringLang / docs

Documentation and tutorials for the Turing language
https://turinglang.org/docs/
MIT License
228 stars 98 forks source link

Add predict to online documentation #332

Open itsdfish opened 2 years ago

itsdfish commented 2 years ago

Hi all,

Turing includes a useful function predict for generating posterior predictive samples. Unfortunately, many users will not find this because it is not listed in the online documentation.

A secondary issue is plotting the posterior predictive distributions. Currently, predict returns a chain, which is not conducive to certain plots (e.g. a scatter plot in the case of regression). Would it better to return an array of predictions, which could be converted to an MCMCChain if needed? Here is an example based on the doc string:

 using Turing; Turing.setprogress!(false);

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

           for i ∈ eachindex(y)
               y[i] ~ Normal(β * x[i], σ)
           end
       end;

 σ = 0.1; f(x) = 2 * x + 0.1 * randn();

 Δ = 0.1; xs = 0:Δ:10; ys = f.(xs);

 model = linear_reg(xs, ys, σ);

 chain_lin_reg = sample(model, NUTS(100, 0.65), 200);

 m_pred = linear_reg(xs, Vector{Union{Missing, Float64}}(undef, length(ys)), σ);

 predictions = predict(m_pred, chain_lin_reg)
youainti commented 1 year ago

With regards to your first point, I think that would be simple to add to the current examples, maybe the current Linear Regression example would be a good place?

As far as plotting posterior predictive distributions goes, my initial inclination would be to have predict still return an MCMCChains struct, but build a posterior_predict function (or set of functions) that would allow easy comparison etc. In my mind I am envisioning a hypothetical TuringPredictiveAnalysis.jl package that has utilities and plot recipes for easy analysis. This seems like a separate issue so it might be good to open it as a different issue somewhere.