# Simple Turing model fails for InexactError

**URL:** <https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [December 31, 2020, 11:42am UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655 "2020-12-31T11:42:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BradGroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradgroff/32/21156_2.png) [@BradGroff](https://discourse.julialang.org/u/BradGroff)\
**Post date:** [December 31, 2020, 11:42am UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/1 "2020-12-31T11:42:21Z")

</div>

Hi all,

The model is a simple binomial on binary data:

```julia
@model function coinflip_binomial_broken(y::Array{Int64})
    # prior on p
    p ~ Beta(1, 1)
    
    # updates on p
    heads = sum(y)
    heads ~ Binomial(length(y), p)
end

```

When run, I get `InexactError: Int64(3.0555688441753306)` (the value changes each time) which seems to be coming from the logpdf calculation for the Binomial.

I can make it work by pulling the `sum` and `length` functions out of the model block:

```julia
@model function coinflip_binomial(heads::Int64, flips::Int64)
    # prior on p
    p ~ Beta(1, 1)
    
    # update on p
    heads ~ Binomial(flips, p)
end

```

That said, **why does the first one not work???**

It seems to be due to `sum(y)` (rather than `length(y)`), which I deduced by redundantly passing the `heads` variable to the model instead of calculating it within the block while leaving the `length` call as is.

As a beginner to Turing and to Julia, I’d really like to understand what’s happening here. I suspect my understanding of how to write and interpret code in an AD setting is where my gap is. Any ideas on the above or suggestions for resources would be much appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 31, 2020, 2:58pm UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/2 "2020-12-31T14:58:58Z")

</div>

The stack trace would help as we can’t see where your error happens. But I’d take a guess and say you have a Vector{Int} somewhere and calculate a Float64 which you then assign to that vector. That causes an InexactError because you can’t convert any float to an integer. Or any other container parameterized with Int.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 31, 2020, 3:32pm UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/3 "2020-12-31T15:32:31Z")

</div>

I am unable to produce your result. Please be sure to post a working example that reproduces the error that you find. Also, please indicate which version of Turing you are using.

On my system, using Turing 0.15.8, I experience tons of gradient errors. I am guessing that the macro is not parsing the length(y) in the Binomial function.

```julia
using Turing, Random

@model function coinflip_binomial_broken(y::Array{Int64})
    # prior on p
    p ~ Beta(1, 1)
    
    # updates on p
    heads = sum(y)
    heads ~ Binomial(length(y), p)
end

Random.seed!(5484)
y = [1,0,0,1]
chain = sample(coinflip_binomial_broken(y), NUTS(1000,.65), 2000)

```

Output:

```julia
Summary Statistics
  parameters mean std naive_se mcse ess rhat 
      Symbol Float64 Float64 Float64 Float64 Float64 Float64 

       heads 2.0000 0.0000 0.0000 0.0000 NaN NaN
           p 0.2672 0.0000 0.0000 0.0000 7.1985 1.0187

Quantiles
  parameters 2.5% 25.0% 50.0% 75.0% 97.5% 
      Symbol Float64 Float64 Float64 Float64 Float64 

       heads 2.0000 2.0000 2.0000 2.0000 2.0000
           p 0.2672 0.2672 0.2672 0.2672 0.2672

```

As you can see, the sampler does not move beyond its initial value.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 31, 2020, 3:35pm UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/4 "2020-12-31T15:35:39Z")

</div>

You are correct. It is not `length`, but rather `heads = sum(y)` that is the culprit. The following works.

```julia
using Turing, Random

@model function coinflip_binomial_broken(y::Array{Int64})
    # prior on p
    p ~ Beta(1, 1)
    
    # updates on p
    heads = sum(y)
    2 ~ Binomial(length(y), p)
end

Random.seed!(5484)
y = [1,0,0,1]
chain = sample(coinflip_binomial_broken(y), NUTS(1000,.65), 2000)

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 31, 2020, 3:44pm UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/5 "2020-12-31T15:44:57Z")

</div>

I opened up an [issue](https://github.com/TuringLang/Turing.jl/issues/1508) because it seems like a bug.

---

<div class="post-metadata">

**Author:** ![BradGroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradgroff/32/21156_2.png) [@BradGroff](https://discourse.julialang.org/u/BradGroff)\
**Post date:** [December 31, 2020, 7:04pm UTC](https://discourse.julialang.org/t/simple-turing-model-fails-for-inexacterror/52655/6 "2020-12-31T19:04:55Z")

</div>

Thank you! I’ll follow along in the issue thread.
