juliastats / conjugatepriors.jl Goto Github PK
View Code? Open in Web Editor NEWA Julia package to support conjugate prior distributions.
License: Other
A Julia package to support conjugate prior distributions.
License: Other
Due to the upper bound, having this package installed causes a Distributions to break.
It sure would be nice to have some documentation.
Right now there is no Docstrings, nor is there anything in the readme.
Maybe there should be a link to http://distributionsjl.readthedocs.io/en/latest/conjugates.html#ref-conj ?
Hi, I'm a Julia newbie.
I understand that it's necessary to export distribution types and methods in ConjugatePriors.jl for calling them without prefix like "ConjugatePriors.NormalWishart".
What do you think this idea and the necessity of PR for them?
I have a need for a correct implementation of improper priors for Turing-based Bayesian inference. By improper I mean that the distribution does not integrate to 1, and in some cases you don't want to disallow the construction of such distributions, but you want to bar the user from sampling from it. The simplest case is Uniform(-Inf, Inf)
. See the wikipedia entry for more details.
I looked here in Distributions to see if there was a function in the API to tell if a distribution was improper or not. Since I did not find one, I am proposing such an interface:
isproper(distribution, lower, upper) = ... # to be implemented for each distribution
with helper method
isproper(d::Distribution) = isproper(d, minimum(d), maximum(d))
and some example implementations:
isproper(d::Uniform, lower, upper) = isfinite(max(lower, d.a)) & isfinite(min(upper, d.b))
isproper(d::Normal, _, _) = isfinite(d.σ)
isproper(d::Beta, _, _) = !iszero(d.α) | !iszero(d.β)
# last one relies on Beta being modified to allow for zero-valued α, β
As with the Beta
example above, this may require some restructuring of the allowed parameter values for some distributions.
I am asking for feedback on this interface before I start a PR.
These are two equivalent parametrizations of the conjugate prior for a normal with unknown mean and variance. Both are based on Murphy, and the mode for NIX2 was taken directly from Murphy, while the mode for NIG isn't given explicitly.
julia> using Distributions
julia> using ConjugatePriors: NormalInverseChisq, NormalInverseGamma
julia> d = NormalInverseGamma(0., 1., 2, 2)
ConjugatePriors.NormalInverseGamma{Float64}(mu=0.0, v0=1.0, shape=2.0, scale=2.0)
julia> d2 = NormalInverseChisq(d)
ConjugatePriors.NormalInverseChisq{Float64}(μ=0.0, σ2=1.0, κ=1.0, ν=4.0)
julia> mode(d2)
(0.0, 1.3333333333333333)
julia> mode(d)
(0.0, 0.6666666666666666)
They do agree if the formula that Murphy gives for the mode of the marginal posterior of the variance is used:
julia> margmode(d::NormalInverseChisq) = d.ν*d.σ2 / (d.ν+2)
margmode (generic function with 1 method)
julia> margmode(d2)
0.6666666666666666
Which suggests to me that the NIG formula is also using the marginal posterior.
This doesn't happen if I Pkg.checkout("ConjugatePriors")
before using
. Could you please put a version tag on current master so I can pin it?
NB: The same thing happens with julia v0.4.5. See discussion here.
user@julia:~/src/cat.jl$ rm -rf ~/.julia
user@julia:~/src/cat.jl$ julia -e 'Pkg.add("ConjugatePriors"); using ConjugatePriors'
INFO: Initializing package repository /home/user/.julia/v0.5
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
INFO: Cloning cache of ArrayViews from https://github.com/JuliaLang/ArrayViews.jl.git
INFO: Cloning cache of Compat from https://github.com/JuliaLang/Compat.jl.git
INFO: Cloning cache of ConjugatePriors from https://github.com/JuliaStats/ConjugatePriors.jl.git
INFO: Cloning cache of Distributions from https://github.com/JuliaStats/Distributions.jl.git
INFO: Cloning cache of PDMats from https://github.com/JuliaStats/PDMats.jl.git
INFO: Cloning cache of StatsBase from https://github.com/JuliaStats/StatsBase.jl.git
INFO: Cloning cache of StatsFuns from https://github.com/JuliaStats/StatsFuns.jl.git
INFO: Installing ArrayViews v0.6.4
INFO: Installing Compat v0.7.20
INFO: Installing ConjugatePriors v0.1.2
INFO: Installing Distributions v0.8.9
INFO: Installing PDMats v0.3.6
INFO: Installing StatsBase v0.8.1
INFO: Installing StatsFuns v0.2.2
INFO: Package database updated
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: deprecated syntax "call(::Type{Base.ByteString}, ...)".
Use "(::Type{Base.ByteString})(...)" instead.
WARNING: `@unix_only` is deprecated, use `@static if is_unix()` instead
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in @unix_only(::Any) at ./deprecated.jl:1226
in include_from_node1(::String) at ./loading.jl:426
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Compat/src/Compat.jl, in expression starting on line 132
WARNING: Method definition (::Type{ArrayViews.UnsafeContiguousView})(Ptr{#T<:Any}, Int64, Tuple{Vararg{Int64, #N<:Any}}) in module ArrayViews at /home/user/.julia/v0.5/ArrayViews/src/arrviews.jl:18 overwritten at /home/user/.julia/v0.5/ArrayViews/src/arrviews.jl:26.
WARNING: Base.SparseMatrix is deprecated.
likely near /home/user/.julia/v0.5/PDMats/src/chol.jl:9
WARNING: Base.SparseMatrix is deprecated.
likely near /home/user/.julia/v0.5/PDMats/src/chol.jl:9
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:199 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariates.jl, in expression starting on line 193
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:200 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariates.jl, in expression starting on line 193
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in @_delegate_statsfuns(::Any, ::Any, ::Symbol, ::Vararg{Symbol,N}) at /home/user/.julia/v0.5/Distributions/src/univariates.jl:316
in include_from_node1(::String) at ./loading.jl:426
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:408 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariate/discrete/binomial.jl, in expression starting on line 71
WARNING: Method definition (::Type{Distributions.MvNormalKnownCov})(#Cov<:PDMats.AbstractPDMat) in module Distributions at /home/user/.julia/v0.5/Distributions/src/multivariate/mvnormal.jl:148 overwritten at /home/user/.julia/v0.5/Distributions/src/multivariate/mvnormal.jl:151.
INFO: Recompiling stale cache file /home/user/.julia/lib/v0.5/Distributions.ji for module Distributions.
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:199 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariates.jl, in expression starting on line 193
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:200 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariates.jl, in expression starting on line 193
WARNING: symbol is deprecated, use Symbol instead.
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in symbol(::String, ::Vararg{String,N}) at ./deprecated.jl:30
in @_delegate_statsfuns(::Any, ::Any, ::Symbol, ::Vararg{Symbol,N}) at /home/user/.julia/v0.5/Distributions/src/univariates.jl:316
in include_from_node1(::String) at ./loading.jl:426
in macro expansion; at /home/user/.julia/v0.5/Distributions/src/univariates.jl:408 [inlined]
in anonymous at ./<missing>:?
in include_from_node1(::String) at ./loading.jl:426 (repeats 2 times)
in macro expansion; at ./none:2 [inlined]
in anonymous at ./<missing>:?
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/Distributions/src/univariate/discrete/binomial.jl, in expression starting on line 71
WARNING: Method definition (::Type{Distributions.MvNormalKnownCov})(#Cov<:PDMats.AbstractPDMat) in module Distributions at /home/user/.julia/v0.5/Distributions/src/multivariate/mvnormal.jl:148 overwritten at /home/user/.julia/v0.5/Distributions/src/multivariate/mvnormal.jl:151.
WARNING: Module PDMats uuid did not match cache file
This is likely because module PDMats does not support precompilation but is imported by a module that does.
ERROR: LoadError: __precompile__(true) but require failed to create a precompiled cache file
in require(::Symbol) at ./loading.jl:366
in include_from_node1(::String) at ./loading.jl:426
in eval(::Module, ::Any) at ./boot.jl:225
in require(::Symbol) at ./loading.jl:357
in eval(::Module, ::Any) at ./boot.jl:225
in process_options(::Base.JLOptions) at ./client.jl:243
in _start() at ./client.jl:322
while loading /home/user/.julia/v0.5/ConjugatePriors/src/ConjugatePriors.jl, in expression starting on line 5
user@julia:~/src/cat.jl$
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
It seems the random generator for NormalWishart
is broken after the code has been separated from Distributions:
mu0 = [2., 3.]
kappa0 = 3.
nu0 = 4.
T0 = eye(2)
T0[1,2] = T0[2,1] = .5
pri = ConjugatePriors.NormalWishart(mu0, kappa0, T0, nu0)
ConjugatePriors.rand(pri)
ERROR: `rand` has no method matching rand(::Wishart{PDMat})
you may have intended to import Base.rand
in rand at /home/jaak/.julia/v0.3/ConjugatePriors/src/normalwishart.jl:78
One solution is to replace rand
into Distributions.rand
in lines normalwishart.jl:78 and 79. Maybe some other solution is better.
Currently, the NormalGamma distrbution returns (mu, tau) when sampled. This distribution, however, is only used (as far as I know) as the conjugate prior to the NormalCanon, which needs the parameters in the canonical form (mu*tau, tau). This is also the case (I believe) for the NormalWishart and MvNormalCanon.
This is obviously not a massive issue as it can simply be converted when needed, so I understand if it is low priority. It is causing me some annoyances with typing and dispatch, however, since I cannot simply use the samples from the prior as the parameters for the likelihood (whereas with other distributions I can). What are your thoughts on changing this? I can submit a pull request with the changes if you are okay with it.
julia> @code_warntype ConjugatePriors.NormalInverseGamma(1., 1., 1., 1.)
Variables:
#self# <optimized out>
mu::Float64
v0::Float64
sh::Float64
r::Float64
T::Any
Body:
begin
T::Any = (Base.promote_type)(Float64, (Base.promote_type)(Float64, (Base.promote_type)(Float64, Float64)::Any)::Any)::Any # line 20:
return ((Core.apply_type)(ConjugatePriors.NormalInverseGamma, T::Any)::Type{ConjugatePriors.NormalInverseGamma{_}} where _)((T::Any)(mu::Float64)::Any, (T::Any)(v0::Float64)::Any, (T::Any)(sh::Float64)::Any, (T::Any)(r::Float64)::Any)::ConjugatePriors.NormalInverseGamma{_} where _
end::ConjugatePriors.NormalInverseGamma{_} where _
I think this comes from having constructors like
ConjugatePriors.jl/src/normalinversegamma.jl
Lines 18 to 21 in 36fe174
where the type is computed at runtime (with promote_type
), but I could be wrong about that.
When all the arguments are of the same type, adding a method like NormalInverseGamma(::T, ::T, ::T, ::T) where {T<:Real})
gets rid of the type instability. Another possible fix would be to use the inner constructor with the explicit type parameter whenever possible (e.g., in posterior_canon
, which is where I was bitten by this).
The current specification in REQUIRE is "julia 0.3".
Is this effort abandoned or moved elsewhere perhaps? Just curious.
Is there a reason nu is required to be strictly greater than 0 in the inner constructor? I don't see any reason that it needs to be, and when I was using my own code I often set nu=0 for testing and it didn't have any problems.
Hello,
Is there a reason that the NormalWishart type extends the UnivariateDistribution type rather than MultivariateDistribution type?
The eltype of, for instance, NormalInverseGamma
should be Tuple{Float64, Float64}
, but instead is given as Float64
. This means that rand
chokes because it allocates an array of floats instead of an array of tuples:
julia> d = ConjugatePriors.NormalInverseGamma(0., 1., 2, 2)
ConjugatePriors.NormalInverseGamma{Float64}(mu=0.0, v0=1.0, shape=2.0, scale=2.0)
julia> rand(d)
(-0.6216462947065958, 0.7004872075936253)
julia> rand(d, 2)
ERROR: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Stacktrace:
[1] _rand!(::ConjugatePriors.NormalInverseGamma{Float64}, ::Array{Float64,1}) at /home/dave/.julia/v0.6/Distributions/src/genericrand.jl:37
[2] rand(::ConjugatePriors.NormalInverseGamma{Float64}, ::Int64) at /home/dave/.julia/v0.6/Distributions/src/univariates.jl:182
julia> eltype(d)
Float64
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.