statisticalrethinkingjulia / sr2turingpluto.jl Goto Github PK
View Code? Open in Web Editor NEWStatisticalRethinking notebook project using Turing and Pluto notebooks (derived from Max Lapan's Jupyter project)
License: MIT License
StatisticalRethinking notebook project using Turing and Pluto notebooks (derived from Max Lapan's Jupyter project)
License: MIT License
This is outside the scope of this repository (i.e. the error, I believe, is in the book itself), but it looks like the normalization for the grid approximation might be done incorrectly?
It doesn't seem to take into account the number of points in the grid / the thickness of each approximated section, so the normalization isn't actually normalizing the integral to be 1, just the raw probabilities?
This means changing the number of points changes the scale y-axis of the approximation significantly!
Plotting it over the analytical solution, we have, uncorrected (i.e. as written in the book):
But with the * (1/size)
correction applied in the normalization step:
The height is still a little off there, since that Riemann sum isn't perfect, but it approaches a perfect approximation with infinite points โ either way, I believe this is the correct way to normalize a grid approximation? At least one random person on the internet seems to agree: https://stats.stackexchange.com/a/609243
For the actually actionable part of this issue: if this is a mistake, should be correct it in these notebooks and leave a short note mentioning that it might be slightly wrong in the book?
Let me know what you think!
Hi Rob!
Recently I sumbled on a strange issue related to the model m7.1 in the book. The problem is log_sigma, which converges to the different distribution when I use Turing versus numpyro/quap.
Turing model converges to this:
Very similar to R results are produced by numpyro version of this model (https://fehiepsi.github.io/rethinking-numpyro/07-ulysses-compass.html):
The difference is significant enough to produce different LPPD values (my model have almost 2 times lower than reported in the book).
If I enforce the log_sigma close to the book (by changing priors, for example), everything becomes the same. But after tons of experimentation with different samplers I still getting this -1.39 mean :)
As both R and numpyro are the some, I have a feeling that this is some subtle Turing problem, but run out of ideas how it could be :).
As I can see in the comments, you also have "fun" with chapter 7, so, if you have any ideas/suggestions about this issue, they will be very appreciated.
Looking for something like StatisticalRethinking.jl, but for Turing code.
This repo name suggests this is a right place, but in fact it is a set of notebooks snippets rather than reusable package.
I'm getting this error when I try to instantiate. I'm a bit lost on how to troubleshoot it. I'm running on Julia v1.5.3.
(StatisticalRethinkingTuring) pkg> instantiate
ERROR: TypeError: in typeassert, expected VersionNumber, got a value of type Pkg.Types.VersionSpec
Stacktrace:
[1] load_urls(::Pkg.Types.Context, ::Array{Pkg.Types.PackageSpec,1}) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\Operations.jl:510
[2] #download_source#54 at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\Operations.jl:686 [inlined]
[3] download_source at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\Operations.jl:685 [inlined]
[4] instantiate(::Pkg.Types.Context; manifest::Nothing, update_registry::Bool, verbose::Bool, platform::Pkg.BinaryPlatforms.Windows, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\API.jl:875
[5] instantiate(::Pkg.Types.Context) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\API.jl:795
[6] #instantiate#169 at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\API.jl:791 [inlined]
[7] instantiate() at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\API.jl:791
[8] do_cmd!(::Pkg.REPLMode.Command, ::REPL.LineEditREPL) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\REPLMode\REPLMode.jl:401
[9] do_cmd(::REPL.LineEditREPL, ::String; do_rethrow::Bool) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\REPLMode\REPLMode.jl:382
[10] do_cmd at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\REPLMode\REPLMode.jl:377 [inlined]
[11] (::Pkg.REPLMode.var"#24#27"{REPL.LineEditREPL,REPL.LineEdit.Prompt})(::REPL.LineEdit.MIState, ::Base.GenericIOBuffer{Array{UInt8,1}}, ::Bool) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Pkg\src\REPLMode\REPLMode.jl:546
[12] #invokelatest#1 at .\essentials.jl:710 [inlined]
[13] invokelatest at .\essentials.jl:709 [inlined]
[14] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\LineEdit.jl:2355
[15] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\REPL\src\REPL.jl:1144
[16] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at .\task.jl:356
When running Clip-00-04-05t.jl
in Pluto or in REPL I get this error:
julia> m = lm(@formula(height ~ weight), df)
ERROR: LoadError: UndefVarError: @formula not defined
in expression starting at REPL[7]:1
Hello,
I realize that this package is under development. I was looking through, and note that the section 12 models file includes the zero-inflated models from the book, e.g.
## R code 12.9 m12.3 <- ulam( alist( y ~ dzipois( p , lambda ), logit(p) <- ap, log(lambda) <- al, ap ~ dnorm( -1.5 , 1 ), al ~ dnorm( 1 , 0.5 ) ) , data=list(y=y) , chains=4 )
However, these do not seem to appear in the Julia / Turing.jl files. I was wondering whether there were resources for running zero-inflated models in Turing, or whether you had managed to implement them.
Best,
Eric
Hello!
Pushing into Julia via this repo, thanks for putting it together!
I'm running Julia 1.5.3
, and following the installation instructions in repo README.md, I am hitting a TypeError when executing the instantiate
command. Stacktrace, here:
(@v1.5) pkg> activate .
Activating environment at `~/repos/StatisticalRethinkingTuring.jl/Project.toml`
(StatisticalRethinkingTuring) pkg> instantiate
Installing known registries into `~/.julia`
######################################################################## 100.0%
Added registry `General` to `~/.julia/registries/General`
ERROR: TypeError: in typeassert, expected VersionNumber, got a value of type Pkg.Types.VersionSpec
Stacktrace:
[1] load_urls(::Pkg.Types.Context, ::Array{Pkg.Types.PackageSpec,1}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Operations.jl:510
[2] #download_source#54 at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Operations.jl:686 [inlined]
[3] download_source at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Operations.jl:685 [inlined]
[4] instantiate(::Pkg.Types.Context; manifest::Nothing, update_registry::Bool, verbose::Bool, platform::Pkg.BinaryPlatforms.MacOS, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:875
[5] instantiate(::Pkg.Types.Context) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:795
[6] #instantiate#169 at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:791 [inlined]
[7] instantiate() at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:791
[8] do_cmd!(::Pkg.REPLMode.Command, ::REPL.LineEditREPL) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:401
[9] do_cmd(::REPL.LineEditREPL, ::String; do_rethrow::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:382
[10] do_cmd at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:377 [inlined]
[11] (::Pkg.REPLMode.var"#24#27"{REPL.LineEditREPL,REPL.LineEdit.Prompt})(::REPL.LineEdit.MIState, ::Base.GenericIOBuffer{Array{UInt8,1}}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:546
[12] #invokelatest#1 at ./essentials.jl:710 [inlined]
[13] invokelatest at ./essentials.jl:709 [inlined]
[14] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/REPL/src/LineEdit.jl:2355
[15] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:1144
[16] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at ./task.jl:356
Searching, I found this discourse discussion on the same exception, and mentions that there is possibly incompatibility in Mainfest.toml across different versions of Julia. I've inspected the Manifest.toml
in the repo, and don't see any reference to a specific Julia version, and being unfamiliar with Julia, don't know where else to go. Any pointers would be helpful.
Thanks much in advance!
Hello Rob,
I have been looking for Turing models that use multivariate normal priors. Here is an example in Stan, but I am not sure exactly how to translate it to Turing.
I noticed that Chapter 14 in Statistical Rethinking has examples, but those have not been implemented in this repository. What is the status for these models?
The above line produces a TypeError
when I try to instantiate
the ๐ฆ environment due to the hardcoded local path.
It is likely that the below line will produce a similar error.
Here is the TypeError
produced on a Mac:
ERROR: TypeError: in typeassert, expected VersionNumber, got a value of type Pkg.Types.VersionSpec
Stacktrace:
[1] load_urls(::Pkg.Types.Context, ::Array{Pkg.Types.PackageSpec,1}) at /Users/julia/buildbot/worker/package_macos64/build/usr/sha
re/julia/stdlib/v1.5/Pkg/src/Operations.jl:510
[2] #download_source#54 at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Operations.jl:68
6 [inlined]
[3] download_source at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/Operations.jl:685 [i
nlined]
[4] instantiate(::Pkg.Types.Context; manifest::Nothing, update_registry::Bool, verbose::Bool, platform::Pkg.BinaryPlatforms.MacOS,
kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/julia/buildbot/worker/package_macos64/buil
d/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:875
[5] instantiate(::Pkg.Types.Context) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API
.jl:795
[6] #instantiate#169 at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:791 [inlined
]
[7] instantiate() at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/API.jl:791
[8] do_cmd!(::Pkg.REPLMode.Command, ::REPL.LineEditREPL) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/std
lib/v1.5/Pkg/src/REPLMode/REPLMode.jl:401
[9] do_cmd(::REPL.LineEditREPL, ::String; do_rethrow::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/
stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:382
[10] do_cmd at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:377 [in
lined]
[11] (::Pkg.REPLMode.var"#24#27"{REPL.LineEditREPL,REPL.LineEdit.Prompt})(::REPL.LineEdit.MIState, ::Base.GenericIOBuffer{Array{UI
nt8,1}}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Pkg/src/REPLMode/REPLMode.jl:546
[12] #invokelatest#1 at ./essentials.jl:710 [inlined]
[13] invokelatest at ./essentials.jl:709 [inlined]
[14] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /Users/julia/buildbo
t/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/REPL/src/LineEdit.jl:2355
[15] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/juli
a/stdlib/v1.5/REPL/src/REPL.jl:1144
[16] (::REPL.var"#38#42"{REPL.LineEditREPL,REPL.REPLBackendRef})() at ./task.jl:356
Apparently some things changed and Chapter_04_part_1.jl Code 4.16 doesn't work.
Substituting logpdf
to broadcasted .logpdf
does the trick.
For univariate distributions, calling pdf on an array of values is deprecated. Nevertheless it should not throw an error but only display a deprecation warning (if they are enabled). see issue in Distributions.jl
... but as of 18 Mar 2022, throws an error...
julia> pdf(b1, x)
ERROR: MethodError: pdf(::Beta{Float64}, ::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}) is ambiguous. Candidates:
pdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real, M}) where {N, M} in Distributions at E:\Julia.julia\packages\Distributions\vcKQj\src\common.jl:251
pdf(d::UnivariateDistribution, X::AbstractArray) in Distributions at deprecated.jl:70
Possible fix, define
pdf(::UnivariateDistribution, ::AbstractArray{T, M} where T<:Real) where M
Fix: insert period to enable broadcasting
julia> pdf.(b1, x)
Despite instantiating julia using the Project.toml, I got the following error:
Error requiring `Turing` from `StatisticalRethinking`
exception
LoadError: UndefVarError: `ModeResult` not defined
in expression starting at /Users/blum/.julia/packages/StatisticalRethinking/RYYWV/src/require/turing/turing_optim_sample.jl:5
in expression starting at /Users/blum/.julia/packages/StatisticalRethinking/RYYWV/src/require/turing/turing.jl:7
top-level scope@turing_optim_sample.jl:5
include(::Module, ::String)@Base.jl:457
include(::String)@StatisticalRethinking.jl:1
top-level [email protected]:7
include(::Module, ::String)@Base.jl:457
include(::String)@StatisticalRethinking.jl:1
top-level [email protected]:40
[email protected]:370[inlined]
[email protected]:1[inlined]
(::StatisticalRethinking.var"#3#12")()@require.jl:101
macro [email protected]:393[inlined]
err(::Any, ::Module, ::String, ::String, ::Any)@require.jl:47
(::StatisticalRethinking.var"#2#11")()@require.jl:100
withpath(::Any, ::String)@require.jl:37
(::StatisticalRethinking.var"#1#10")()@require.jl:99
listenpkg(::Any, ::Base.PkgId)@require.jl:20
macro [email protected]:98[inlined]
__init__()@StatisticalRethinking.jl:31
register_restored_modules(::Core.SimpleVector, ::Base.PkgId, ::String)@loading.jl:1115
_include_from_serialized(::Base.PkgId, ::String, ::String, ::Vector{Any})@loading.jl:1061
_require_search_from_serialized(::Base.PkgId, ::String, ::UInt128)@loading.jl:1506
_require(::Base.PkgId, ::String)@loading.jl:1783
_require_prelocked(::Base.PkgId, ::String)@loading.jl:1660
macro [email protected]:1648[inlined]
macro [email protected]:267[inlined]
require(::Module, ::Symbol)@loading.jl:1611
top-level scope@[Other: 13](http://localhost:1235/edit?id=253bb52c-a005-11ee-0a0f-6d20735f295d#)
โ
WARNING: could not import Turing.ModeResult into StatisticalRethinking
I guess this is a versioning problem? I will try other versions of Turing as soon as I get to it, I am just setting this repository up.
When I try to run Fig2.5.2t.jl
file
@model globe_toss(W, L) = begin
p ~ Uniform(0, 1)
W ~ Binomial(W + L, p)
end
m = globe_toss(6, 3);
r = quap(m)
I got an error message:
MethodError: no method matching quap(::DynamicPPL.Model{typeof(globe_toss), (:W, :L), (), (), Tuple{Int64, Int64}, Tuple{}, DynamicPPL.DefaultContext}) Closest candidates are: quap(!Matched::DataFrame) at C:\Users\frank\.julia\packages\StatisticalRethinking\3rZsp\src\quap.jl
And I checked up the file, the parameter it takes is a DataFrame instead of a Turing model
PS:
Plots
and StatsPlots
could not precompile in my PC so I comment out the lines including the using StatisticalRethinkingPlots
Could anyone help me with that?
Hi again,
I can see that quap()
is defined in StatisticalRethinking.jl but it does not seem to be used in these notebooks here (e.g. Chapter 2, Code chunk 2.6). Is that intentional?
Would it be useful if I tried update the relevant notebooks to using quap()
? Are there any complications to be expected? No promise but I could give it a go...
All the best,
Hannes
This line trying to show summary statistics https://github.com/StatisticalRethinkingJulia/StatisticalRethinkingTuring.jl/blob/98ee9d1efd865f5d04e5b5a42e9985add25fc7cc/notebooks/03/clip-03-02-05t.jl#L53
currently doesn't seem to work inside Pluto, at least not for me.
Similar to what was done in another notebook it can be fixed with:
Text(sprint(show, "text/plain", summarize(chn)))
Do you want a PR for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.