# Help with creating a custom distribution

**URL:** <https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229>\
**Category:** New to Julia\
**Created:** [August 11, 2018, 5:58am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229 "2018-08-11T05:58:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fishix10](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fishix10](https://discourse.julialang.org/u/fishix10)\
**Post date:** [August 11, 2018, 5:58am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/1 "2018-08-11T05:58:10Z")

</div>

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

```julia
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!

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [August 11, 2018, 8:07am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/2 "2018-08-11T08:07:31Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![fishix10](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fishix10](https://discourse.julialang.org/u/fishix10)\
**Post date:** [August 11, 2018, 8:42am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/3 "2018-08-11T08:42:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [August 11, 2018, 9:25am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/4 "2018-08-11T09:25:42Z")

</div>

Can you plot any of the distributions from Distributions?

---

<div class="post-metadata">

**Author:** ![fishix10](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fishix10](https://discourse.julialang.org/u/fishix10)\
**Post date:** [August 11, 2018, 9:46am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/5 "2018-08-11T09:46:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [August 11, 2018, 9:54am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/6 "2018-08-11T09:54:41Z")

</div>

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())`” 🙂

---

<div class="post-metadata">

**Author:** ![fishix10](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fishix10](https://discourse.julialang.org/u/fishix10)\
**Post date:** [August 11, 2018, 10:02am UTC](https://discourse.julialang.org/t/help-with-creating-a-custom-distribution/13229/7 "2018-08-11T10:02:13Z")

</div>

Oh sorry. Yes I could. 😛
