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

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. It’s a model like this:

eaea578273513ab8dfac4257ec6f10b613981cf2

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:

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:

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.

2 Likes

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:

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.

1 Like

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. You may access it this way.