# How can I avoid insatisfient random variable in Turing.jl?

**URL:** https://discourse.julialang.org/t/how-can-i-avoid-insatisfient-random-variable-in-turing-jl/66397
**Category:** General Usage
**Tags:** turing, distributions, hmm, probability
**Created:** [August 14, 2021, 4:15pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-insatisfient-random-variable-in-turing-jl/66397 "2021-08-14T16:15:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dddd1007](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dddd1007/32/18391_2.png) [@dddd1007](https://discourse.julialang.org/u/dddd1007)
#### Post date: [August 14, 2021, 4:15pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-insatisfient-random-variable-in-turing-jl/66397/1 "2021-08-14T16:15:25Z")

</div>

Hi!

I want to ask how to keep the suitable beta distribution parameters in the HMM model in Turing.jl . The details as follow:

I am trying to implement [Bayesian Learner Model of Tim Behrens](https://static-content.springer.com/esm/art%3A10.1038%2Fnn1954/MediaObjects/41593_2007_BFnn1954_MOESM7_ESM.pdf). It’s a model like this:

![eaea578273513ab8dfac4257ec6f10b613981cf2](https://global.discourse-cdn.com/julialang/original/3X/5/4/5474934a5ec60b98f043644daf44bdea5f039067.png)

Where r is a beta distribution followed by this formula:

\mathrm{p}\left(\mathrm{r}\_{\mathrm{i}+1} \mid \mathrm{r}\_{\mathrm{i}}, \mathrm{v}\right) \sim \beta\left(\mathrm{r}\_{\mathrm{i}}, \mathrm{V}\right)

I used Turing.jl to implement this model like this:

```julia
using Turing
y = [0,1,1,0]

@model function bayesianLearner(y)
	    # The number of observations.
	    N = length(y)
		v = Vector(undef, N)
		r = Vector(undef, N)
	    
		k ~ Normal()
		v[1] ~ Normal(0.05, exp(k))
		r[1] ~ Beta(1,1)
	    y[1] ~ Bernoulli(r[1])
		
	    for i in 2:N
		    v[i] ~ Normal(v[i-1], exp(k))
			tmp1 = r[i-1]
			tmp2 = exp(v[i-1])
			r[i] ~ Beta(-1 * ((-tmp1 + 2*tmp1*tmp2)/(tmp1 - tmp2)), 
						((-1 + tmp1) * (-1 + 2*tmp2))/(tmp1 - tmp2))
			
	        y[i] ~ Bernoulli(r[i])
	    end
	end

chain = sample(bayesianLearner(y), SMC(), 1000)

```

Unfortunately, I always meet this error:

```julia
ArgumentError: Beta: the condition α > zero(α) && β > zero(β) is not satisfied.

```

I have used PyMC3 to implement this model. In PyMC3, I can give a `testval` as the init value of the sampler to solve this question. But I don’t know how to solve this issue in Turing.jl without the same solution.

---

<div class="post-metadata">

### Author: ![Stephen\_Wild](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_wild/32/19061_2.png) [@Stephen\_Wild](https://discourse.julialang.org/u/Stephen_Wild)
#### Post date: [August 18, 2021, 11:24pm UTC](https://discourse.julialang.org/t/how-can-i-avoid-insatisfient-random-variable-in-turing-jl/66397/2 "2021-08-18T23:24:46Z")

</div>

I have no idea how about the calculation you are using for the \alpha and \beta parameters in the beta distribution, as I did not see it in the paper you linked, but I suggest changing the algorithm you use in your sampler. Rather than `SMC()`, try `NUTS()`. I would also suggest ensuring that your Vectors are type stable. The following code worked for me, and seems to align with equations 1 and 2:

```julia
using Turing
y = [0,1,1,0]

@model function bayesianLearner(y, ::Type{T} = Float64) where {T}
	    # The number of observations.
	    N = length(y)
		v = Vector{T}(undef, N)
		r = Vector{T}(undef, N)
	    
		k ~ Normal()
		v[1] ~ Normal(0.05, exp(k))
		r[1] ~ Beta(1,1)
	    y[1] ~ Bernoulli(r[1])
		
	    for i in 2:N
		    v[i] ~ Normal(v[i-1], exp(k))
			tmp1 = r[i-1]
			tmp2 = exp(v[i-1])
			r[i] ~ Beta(tmp1, tmp2)
			
	        y[i] ~ Bernoulli(r[i])
	    end
	end

chain = sample(bayesianLearner(y), NUTS(), 1000)

```

I hope that helps.

---

<div class="post-metadata">

### Author: ![dddd1007](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dddd1007/32/18391_2.png) [@dddd1007](https://discourse.julialang.org/u/dddd1007)
#### Post date: [August 19, 2021, 2:52am UTC](https://discourse.julialang.org/t/how-can-i-avoid-insatisfient-random-variable-in-turing-jl/66397/3 "2021-08-19T02:52:41Z")

</div>

Hi Stephen, Thanks for your help!

I think Behrens uses the \mu parameter in the beta distribution rather than the \alpha and \beta because the mu can represent the coin flip probability. \alpha and \beta are the shape parameters, so they cannot describe the probability information. In my opinion, it is crucial.

I will share this paper via my [Google Drive.](https://drive.google.com/file/d/16gavn_Am3MNJvHANTLK1LoXUPYRdhuo2/view?usp=sharing) You may access it this way.
