GithubHelp home page GithubHelp logo

juliastats / conjugatepriors.jl Goto Github PK

View Code? Open in Web Editor NEW
46.0 46.0 19.0 75 KB

A Julia package to support conjugate prior distributions.

License: Other

Julia 100.00%
julia prior-distribution statistics

conjugatepriors.jl's People

Contributors

andreasnoack avatar ararslan avatar getzdan avatar hassaku avatar iainnz avatar kleinschmidt avatar kmsquire avatar krisdm avatar krylea avatar lindahua avatar mschauer avatar tkelman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

conjugatepriors.jl's Issues

Exporting distribution types and methods

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?

Labelling Improper Distributions (API Feature Request)

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.

disagreement in `mode` of NormalInverseChisq and NormalInverseGamma

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.

`Pkg.add("ConjugatePriors"); using ConjugatePriors` fails during precompilation.

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$ 

TagBot trigger issue

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!

rand(::NormalWishart) is broken

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.

Change to canonical form for NormalGamma and NormalWishart?

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.

can't infer type in some constructors

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

function NormalInverseGamma(mu::Real, v0::Real, sh::Real, r::Real)
T = promote_type(typeof(mu), typeof(v0), typeof(sh), typeof(r))
return NormalInverseGamma{T}(T(mu),T(v0),T(sh),T(r))
end

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).

Abandoned?

Is this effort abandoned or moved elsewhere perhaps? Just curious.

nu > 0

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.

Stop lying about eltype

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.