# Am I creating distributions wrong in Turing?

**URL:** <https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [April 28, 2021, 3:20am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142 "2021-04-28T03:20:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 28, 2021, 3:20am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/1 "2021-04-28T03:20:35Z")

</div>

Hello, I’m using Turing and having some issues. To reproduce this, I’ll provide about as simple of an example as I can generate. Let’s take the basic example from the docs ([Probabilistic Programming in Thirty Seconds](https://turing.ml/dev/docs/using-turing/quick-start)) and modify it only slightly by including a user-generated distribution, with the exact specifications from the docs ([Advanced Usage](https://turing.ml/dev/docs/using-turing/advanced#1-define-the-distribution-type)). Then, all that needs to be done is to include these lines of code (defining the struct, and the two functions) and to modify the model declaration so that `p ~ Beta(1,1)` changes to `p ~ CustomUniform()`. When I run this code, I get the following error:

`LoadError: MethodError: no method matching iterate(::CustomUniform)`

What I know so far (disclaimer: I’m fairly new to Julia with more experience in some other languages) is that `iterate` is not meant to be defined for a distribution, but may be useful for something like an `AbstractRNG`. Is there a reason I’m getting this error? Maybe something I’ve typed in wrong? I can copy and paste my roughly 40 lines of code but they are quite literally copied and pasted from the docs… anyways, I’d really appreciate any feedback!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 28, 2021, 3:36am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/2 "2021-04-28T03:36:29Z")

</div>

What versions of libraries do you have? If they’re old, that could explain it.

---

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 28, 2021, 4:02am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/3 "2021-04-28T04:02:33Z")

</div>

According to `Pkg.status` I have Turing 0.13.0 installed. I do see that the latest release is 0.15.18, so this may be the underlying issue. Interestingly though, when I run `] update` I don’t get a newer version. Is that maybe something wrong with what’s installed on my device or should there be another way I am updating the packages?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 28, 2021, 4:26am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/4 "2021-04-28T04:26:44Z")

</div>

Might not be compatible with your Julia version. Are you on 1.3 or newer?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 28, 2021, 5:02am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/5 "2021-04-28T05:02:42Z")

</div>

Try `] add Turing@0.15` to see what’s preventing the update

---

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 28, 2021, 1:42pm UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/6 "2021-04-28T13:42:45Z")

</div>

I’m using version 1.6. When I run that line of code it returns an error: `ERROR: Could not determine command`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 28, 2021, 3:46pm UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/7 "2021-04-28T15:46:21Z")

</div>

What exactly did you do to get that error? Can you post your REPL input and output?

---

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 28, 2021, 4:02pm UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/8 "2021-04-28T16:02:58Z")

</div>

OK, I figured out the versioning issue. I had another library (that I wasn’t using but was installed) that depended on an older version of `Distributions`, and `Turing` couldn’t install a new version without a newer version of `Distributions`. Removing the other library (I don’t need it anyway) allows me to install `Turing` version 15.18.

That being said, the code still does not work. Maybe there is another versioning issue? I tried `] update` in the REPL in case another update was missing and it still does not work. Thoughts?

---

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 28, 2021, 7:41pm UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/9 "2021-04-28T19:41:24Z")

</div>

Alright, I think I’ve figured it out! I went and read more about the `iterate` function and found that it is called inside the `maximum`, `mean`, `std`, and other functions of various types. When I called `maximum` on my made-up distribution, the same error about not having an `iterate` function popped up. When I manually defined `maximum` and `minimum` as functions for my distribution type, the code now runs without errors (and appears to work properly). As a result of this, if anyone involved in the development of Turing is reading this, I would advise a modification of the docs (particularly the section on using a user-generated distribution) to indicate that in addition to `rand` and `logpdf`, `maximum` and `minimum` may also be required.

PS - if you would like, I have found that making your own `iterate` function also works if this is sensible for your distribution (e.g., if you have a discrete distribution maybe this makes sense). However, this is much slower than just supplying values for `maximum` and `minimum`.

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [April 29, 2021, 7:47am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/10 "2021-04-29T07:47:53Z")

</div>

Hey! So it’s technically already there, though given this issue I agree that it’s not clear enough and def needs improvement!

I’m referring to “Step 3” in the process of creating custom distribution for use in Turing.jl:  
[https://turing.ml/dev/docs/using-turing/advanced#3-define-helper-functions](https://turing.ml/dev/docs/using-turing/advanced#3-define-helper-functions)

In particular, it says:

> Alternatively, for `UnivariateDistribution` we can define the `minimum` and `maximum` of the distribution

```julia
Distributions.minimum(d::CustomUniform) = 0.
Distributions.maximum(d::CustomUniform) = 1.

```

Hope that clears things up:)

---

<div class="post-metadata">

**Author:** ![pthompson234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthompson234/32/24339_2.png) [@pthompson234](https://discourse.julialang.org/u/pthompson234)\
**Post date:** [April 29, 2021, 7:12pm UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/11 "2021-04-29T19:12:49Z")

</div>

I see! That clears it up. Thanks so much!

---

<div class="post-metadata">

**Author:** ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)\
**Post date:** [May 4, 2021, 1:21am UTC](https://discourse.julialang.org/t/am-i-creating-distributions-wrong-in-turing/60142/12 "2021-05-04T01:21:24Z")

</div>

I made this video recently on adding a distribution to Distributions.jl and then seeing if it works in Turing. I too got the iterate error, but find that implementing all the eight methods in the video makes it go away. Hope you like it 🙂

[![](https://global.discourse-cdn.com/julialang/original/3X/b/3/b3f73fde38b9dab702ba8817b03184498aa94528.jpeg "Extending Julia Functions - Adding a New Distribution to Distributions.jl for a Turing.jl Workflow") ](https://www.youtube.com/watch?v=7J_VFr2e_U8)
