On Wednesday, May 23, 2012 8:46:34 AM UTC-5, Douglas Bates wrote:
It happens that I started with discrete distributions which, it turns out, are a bit more complicated than continuous distributions. I think that the DiscreteDistribution type needs to be other than
abstract DiscreteDistribution{T<:Integer} <: Distribution{T}
because the type T<:Integer just tells you the positions of the point masses in the probability mass function (pmf). Often the parameters of the distribution are T1<:Real and many of the functions associated with the distribution (cdf, quantile, pmf) are defined over the entire real line or the interval [0,1]. I have also defined methods for mean, variance, stddev, ... functions for individual distributions and they should return a type <:Real. In terms of types of arguments and returned values it is only the random variate generator, which I have defined as methods for the rand() generic, that uses T<:Integer for discrete distributions and even that is not a hard and fast rule (often it is convenient to think of the value of a binomial as the proportion of successes).
I will cobble something up with non-parameterized abstract types until this can be discussed in more detail.
So this is where I am now for a discrete distribution. Shall we take this discussion to an Issue and cut down the noise on the list?
type Binomial <: DiscreteDistribution
size::Integer
prob::Real
Binomial(n, p) = n <= 0 ? error("size must be positive") : (0. <= p <= 1. ? new(n, p) : error("prob must be in [0,1]"))
end
variance(d::Binomial) = d.size * d.prob * (1. - d.prob)
mean(d::Binomial) = d.size * d.prob
function rand(d::Binomial)
ccall(dlsym(_jl_libRmath, :rbinom), Float64, (Float64, Float64), d.size, d.prob)
end
function pmf(d::Binomial, x::Real)
ccall(dlsym(_jl_libRmath, :dbinom), Float64, (Float64,Float64,Float64,Int32),
x, d.size, d.prob, 0)
end
function lpmf(d::Binomial, x::Real) # log of pmf calculated directly (more precise)
ccall(dlsym(_jl_libRmath, :dbinom), Float64, (Float64,Float64,Float64,Int32),
x, d.size, d.prob, 1)
end
function cdf(d::Binomial, x::Real)
ccall(dlsym(_jl_libRmath, :pbinom), Float64, (Float64,Float64,Float64,Int32,Int32),
x, d.size, d.prob, 1, 0)
end