JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.12k stars 418 forks source link

Trouble implementing custom distribution #528

Open michaellindon opened 8 years ago

michaellindon commented 8 years ago

I am following the docs to implement a multivariate distribution, however, once defining _rand! I assumed rand would also work, but this turns out not to be the case:

julia> _rand!(foo,Array{Float64}(10))
10-element Array{Any,1}:
  0.824614 
  0.349095 
 -0.0143556
  0.908455 
  0.101736 
 -0.0347107
  0.0881389
  0.037218 
  0.790759 
  0.903383 

julia> rand(foo)
ERROR: MethodError: `_rand!` has no method matching _rand!(::lassofc, ::Array{Float64,1})
Closest candidates are:
  _rand!(::Distributions.InverseGamma, ::AbstractArray{T,N})
  _rand!{T<:Real}(::Union{Distributions.Dirichlet,Distributions.DirichletCanon}, ::AbstractArray{T<:Real,1})
  _rand!{T<:Real}(::Distributions.Multinomial, ::AbstractArray{T<:Real,1})
  ...
 in rand at /home/michael/.julia/v0.4/Distributions/src/multivariates.jl:17

julia> typeof(foo)
lassofc

According to the error message _rand! has no method matching _rand!(::lassofc, ::Array{Float64,1}), but then why does my first line work perfectly? Im confused.

andreasnoack commented 8 years ago

Did you import _rand!?

ararslan commented 8 years ago

What Andreas said.

Also, what's the output of lassofc <: Sampleable? Using the current API, rand is only defined for sampleable distributions. If you need to be able to sample from your distribution, I think you'll have to declare the type as type lassofc <: Sampleable{Univariate} (or Multivariate or whatever, also immutable rather than type as appropriate). Could be wrong on that though.

michaellindon commented 8 years ago

I tried

julia> import Base._rand!
WARNING: could not import Base._rand! into Main

and lassofc appears to be a subtype of Sampleable

julia> lassofc <: Sampleable
true
ararslan commented 8 years ago

You'd have to import Distributions._rand!