Help with creating a custom distribution

Hi,

I’m very new (2 days) into Julia and I’m having trouble defining and plotting a new distribution using StatPlots. Here’s my attempt (creating Normal(mu, 2*sigma) just to make things easy):

using Distributions, Plots, StatPlots
importall Distributions

type MyDist <: ContinuousUnivariateDistribution
    mu::Float64
    sigma::Float64
    MyDist(mu, sigma) = new(Float64(mu), Float64(sigma))
end

pdf(d::MyDist, x::Float64) = pdf(Normal(d.mu, 2*d.sigma), x)
rand(d::MyDist) = rand(Normal(d.mu, 2*d.sigma))
sampler(d::MyDist) = sampler(Normal(d.mu, 2*d.sigma))
logpdf(d::MyDist, x::Real) = logpdf(Normal(d.mu, 2*d.sigma), x)
cdf(d::MyDist, x::Real) = cdf(Normal(d.mu, 2*d.sigma), x)
quantile(d::MyDist, q::Real) = quantile(Normal(d.mu, 2*d.sigma), q)
minimum(d::MyDist) = -Inf
maximum(d::MyDist) = Inf
insupport(d::MyDist, x::Real) = insupport(Normal(d.mu, 2*d.sigma), x)
mean(d::MyDist) = mean(Normal(d.mu, 2*d.sigma))
var(d::MyDist) = var(Normal(d.mu, 2*d.sigma))

When I try plot(MyDist(0, 1)), I get the following error: MethodError: no method matching start(::MyDist). If someone could help that would be awesome. Thanks a lot!

2 Likes

You can’t just plot an instance of MyDist. What would you expect it to plot? The pdf or the cdf?

You can try plotting the pdf like so

plot(x->pdf(MyDist(0,1), x))

By the way, what plotting package are you using (Plots.jl?) and what version of Julia is this? type is no longer valid in Julia 1.0, it’s mutable struct (or just struct).

2 Likes

I’m using StatPlots package, which support plotting a distribution, and Julia 0.6.4… Anyways, what I ended up doing was plotting the pdf as you suggested. Thanks!

Can you plot any of the distributions from Distributions?

Yupyup. Please refer to this: https://github.com/JuliaPlots/StatPlots.jl#distributions

I didn’t mean “can you” as in is it possible, more like: “if fishix10 tries to plot their own distribution type it doesn’t work, but what if they plot(Normal()):slight_smile:

Oh sorry. Yes I could. :stuck_out_tongue: