# Using Turing as a replacement of Klara

**URL:** https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152
**Category:** General Usage
**Created:** [December 11, 2019, 2:39pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152 "2019-12-11T14:39:02Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 11, 2019, 2:39pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/1 "2019-12-11T14:39:02Z")

</div>

i used to use `Klara` for MCMC sampler where i just call back the log density of the target function and it was working the way i wanted and now when i want to go back where i’m using it i found problems since i can’t use `Klara` anymore so i decided to look for a package that has maybe the same interface of `Klara` i found `Turing` and many more as an active options and i have already checked the post about MCMC landscape, i decided for the moment to go with `Turing` to sample arrays using `MH` sampler as you can see below:

```julia
using Random
using Distributions
using StatsPlots
using QuantumRelay # a module where it has all the called functions in this code
using DataFrames
using Turing
using LinearAlgebra
function qrelay(alpha, delta, name)
    n = 6
    chi = fill(sqrt(0.06), n)
    phi = [im * tanh(chi[i]) for i=1:n]
    omega = [1.0 / prod(cosh(chi[j]))^2 for j=1:n]
    syms, op = qrelay_op(n, phi, alpha, delta) #an exported function that is working in a made package
    op_a, op_ab, mat, coef = op_mat(op) #an exported function that is working in a made package

    op_q2 = [syms.apH[1], syms.apV[1], syms.bpH[end], syms.bpV[end]]
    op_q1 = [syms.apH[2:end]..., syms.apV[2:end]..., syms.bpH[1:end-1]..., syms.bpV[1:end-1]...]
    mask_q1 = [op in op_q1 for op in op_a];
    mask_q2 = [op in op_q2 for op in op_a];
    qq = [x in syms.apH || x in syms.bpV ? 1 : 0 for x in op_a]
    
    pdet0 = pdet_maker(0.04, 1e-5) #a function of generating the detection probability for detectors with transmittance eta(first argument) and dark count(second one) probability
    qrs = QRelaySampler(mat, coef, omega, pdet0)  

    targetcache = Dict{Vector{Int}, Float64}()
    
    @model plogtarget(na) = begin
        log(qrs.prob(qq, na, mask_q1) * qrs.prob(na)) #prob is the distribution function with two methods inside the QRelaySampler
    end
    c1=sample(plogtarget(zeros(qq)),MH(Dict{Symbol, Any}(:p=>qrs.psetproposal)),1000) #sampling using metropolis hasting sampler
    

    funcQ(v)=qrs.prob(qq, v, mask_q2)
    return qrs, c1, funcQ
end

```

qrelay (generic function with 1 method)

```julia
dataname="data3"
results = []
for i = 0:12
    beta = i*pi/12
    name = string(i)
    mkpath("$dataname/$name")
    qrs, c1, funcQ = qrelay(pi/4, beta, name)
    println("beta:", beta)

    
    push!(results, (qrs, c1, funcQ))
end

```

now when i try to run the code it shows me :

```julia
MethodError: no method matching zeros(::Array{Int64,1})
Closest candidates are:
  zeros(!Matched::Union{Integer, AbstractUnitRange}...) at array.jl:440
  zeros(!Matched::Type{StaticArrays.SArray{Tuple{N},T,1,N} where T}) where N at /Users/midow/.julia/packages/StaticArrays/1g9bq/src/SVector.jl:29
  zeros(!Matched::Type{StaticArrays.MArray{Tuple{N},T,1,N} where T}) where N at /Users/midow/.julia/packages/StaticArrays/1g9bq/src/MVector.jl:27
  ...

Stacktrace:
 [1] qrelay(::Float64, ::Float64, ::String) at ./In[7]:31
 [2] top-level scope at ./In[9]:4774:

```

i know the error is in `zeros(qq)`which is the initial value since i am sampling arrays .  
here you will find the github link of the same code using `Klara`, to see how it was working,  
[https://github.com/marouanehanhasse/quantum\_relay\_sampler/blob/master/quantumopticssampler(1).ipynb](https://github.com/marouanehanhasse/quantum_relay_sampler/blob/master/quantumopticssampler(1).ipynb)  
and i do apology if this post is long but i couldn’t reduce it or simplify it since everything is related and if you want to know something that is unclear on this post please ask.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 11, 2019, 2:48pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/2 "2019-12-11T14:48:12Z")

</div>

You need to use `zero(qq)`. If you transition your code from pre Julia 1.0 to 1.0 you should run it through Julia 0.7, which would have told you:

```julia
julia> zeros([1])
┌ Warning: `zeros(a::AbstractArray)` is deprecated, consider `zero(a)`, `fill(0, size(a))`, `fill!(copy(a), 0)`, or `fill!(similar(a), 0)`. Where necessary, use `fill!(similar(a), zero(eltype(a)))`.
│ caller = top-level scope at none:0
└ @ Core none:0
1-element Array{Int64,1}:
 0

```

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 11, 2019, 3:00pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/3 "2019-12-11T15:00:23Z")

</div>

i guess you are right but still get this error

```julia
KeyError: key 1 not found

Stacktrace:
 [1] getindex at ./dict.jl:477 [inlined]
 [2] MH(::Dict{Symbol,Any}) at /Users/midow/.julia/packages/Turing/LQFio/src/inference/mh.jl:55
 [3] qrelay(::Float64, ::Float64, ::String) at ./In[10]:31
 [4] top-level scope at ./In[11]:7 

```

it is run on jupyter notebook

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 11, 2019, 3:04pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/4 "2019-12-11T15:04:41Z")

</div>

Looks like you’re not passing in the right type of arguments to the sampler. But I don’t know Turing, so I cannot help.

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 11, 2019, 3:06pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/5 "2019-12-11T15:06:57Z")

</div>

what about if i want to use `KissMCMC` for this one, will it work? because i have to use a discrete sampling

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 11, 2019, 3:18pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/6 "2019-12-11T15:18:11Z")

</div>

You mean that the parameters which are sampled are discrete? If so, I don’t think KissMCMC works.

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 11, 2019, 3:18pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/7 "2019-12-11T15:18:19Z")

</div>

i guess the error that i’m getting is in the `Dict` now

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [December 11, 2019, 10:15pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/8 "2019-12-11T22:15:25Z")

</div>

@marouane1994 I don’t think you are using Turing correctly. Please read the docs [Getting Started](https://turing.ml/dev/docs/using-turing/get-started).

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 12, 2019, 5:40pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/9 "2019-12-12T17:40:43Z")

</div>

i already did and i’m trying to understand that but i don’t know why this one doesn’t work i’m trying to se it on a customized distribution that was created in a module called `QuantumRelay` and i want to sample the arrays using the metropolis hasting sampler with a function of distribution: `qrs.prob(qq, na, mask_q1) * qrs.prob(na)` and i want to call back the log density of this function i read the doc but there is no paragraph about such a thing i guess.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [December 12, 2019, 7:41pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/10 "2019-12-12T19:41:24Z")

</div>

The issue here is your model definition:

```julia
    @model plogtarget(na) = begin
        log(qrs.prob(qq, na, mask_q1) * qrs.prob(na)) #prob is the distribution function with two methods inside the QRelaySampler
    end

```

What you have here won’t do anything in Turing’s `@model` macro. You’ve written a simple log density function, which is far simpler than what Turing is meant for – if you want to generate the posterior for just a likelihood like this, I’d recommend one of Turing’s satellite packages to work with the likelihood function directly.

[AdvancedMH.jl](https://github.com/TuringLang/AdvancedMH.jl), which is currently unreleased, is built to do this with Metropolis-Hastings algorithms. You can take a look over there if you want to use a simple log density function.

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 13, 2019, 9:52am UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/11 "2019-12-13T09:52:48Z")

</div>

actually that’s exactly what i want only to generate only a simple likelhood for this distribution function as for AdvancedMH.jl do you have any information when will it be released? and is there any way to do it with `Turing`

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 13, 2019, 9:58am UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/12 "2019-12-13T09:58:38Z")

</div>

Alternatively, if you have coded up a posterior already,

[https://github.com/tpapp/DynamicHMC.jl/](https://github.com/tpapp/DynamicHMC.jl/)

may also work for you. The docs has a [worked example](https://tamaspapp.eu/DynamicHMC.jl/latest/worked_example/).

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [December 13, 2019, 10:00am UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/13 "2019-12-13T10:00:09Z")

</div>

i’ll check it hopefully it will work for this case

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [December 13, 2019, 3:11pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/14 "2019-12-13T15:11:00Z")

</div>

> [@marouane1994](#):
>
> and is there any way to do it with `Turing`

No, not for your case at the moment. Turing is for much more complex models where you can’t or don’t want to write up the likelihood.

> [@marouane1994](#):
>
> do you have any information when will it be released?

Probably soon, but it doesn’t need to be released for you to use it. You can call

```julia
] dev https://github.com/TuringLang/AdvancedMH.jl

```

to install the package.

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [January 6, 2020, 3:20pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/15 "2020-01-06T15:20:18Z")

</div>

Does it support discrete sampling?

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [January 6, 2020, 3:49pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/16 "2020-01-06T15:49:02Z")

</div>

I initially thought so, but I think I’ve made some design decisions that made it so discrete sampling won’t work. I can fix that though. Thanks for asking, I wouldn’t have noticed that until it was too late!

EDIT: It does support discrete sampling, but the functionality is kind of limited by your ability to define a proposal distribution. If your model has both discrete and continuous variables, there’s not really (as far as I know) a good way to describe them using both using the same multivariate distribution.

@torfjelde does Bijectors have some way of allowing for multivariate draws from like a `[Categorical(5), Normal(0,1)]` distribution?

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [January 7, 2020, 2:50am UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/17 "2020-01-07T02:50:42Z")

</div>

Because i am using a struct of: `struct OrthoNNDist <: DiscreteMultivariateDistribution` .  
The sampling that i’m trying to perform is discrete one and a long one with a lot of measurement, and i need a package that can perform discrete sampling

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [January 7, 2020, 4:41am UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/18 "2020-01-07T04:41:42Z")

</div>

> [@cpfiffer](#):
>
> does Bijectors have some way of allowing for multivariate draws from like a `[Categorical(5), Normal(0,1)]` distribution?

`Distributions.Product` should be able to support this imo. It currently doesn’t but I think that’s a bug.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [January 7, 2020, 2:34pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/19 "2020-01-07T14:34:45Z")

</div>

In AdvancedMH, uou can do discrete sampling as long as you have a proposal distribution that returns something you can add to your last sample. I’m assuming the results of `rand(::OrthoNNDist)` are integer valued? If so, you can basically just set it up with something like this:

```julia
# The first argument is a vector of initial parameterizations,
# and the second is the proposal distribution.
spl = MetropolisHastings(zeros(n_params), Categorical(5)) 

```

You have to catch cases where your samples are out of bounds in your model. From the README, here’s a quick way to do this for a discrete model:

```julia
# Define the components of a basic model.
insupport(θ) = θ[2] >= 0
dist(θ) = Normal(θ[1], θ[2])

# This is the function we use as our log density.
density(θ) = insupport(θ) ? sum(logpdf.(dist(θ), data)) : -Inf

```

---

<div class="post-metadata">

### Author: ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)
#### Post date: [January 14, 2020, 4:25pm UTC](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152/20 "2020-01-14T16:25:12Z")

</div>

of course i have my own i have created a customized proposal distribution and yes you’re right the `rand(::OrthoNNdist)` gives valued integers, but here what is the `data` ? i know it is the generation of the of the proposal, here in this case how can i generate them in this case. waht you have suggested is working but i want to put everything in a chain.

[Next page](https://discourse.julialang.org/t/using-turing-as-a-replacement-of-klara/32152.md?page=2)
