# Issues implementing Copula regression using copula and Turing

**URL:** <https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907>\
**Category:** Probabilistic Programming\
**Tags:** question, turing, copula\
**Created:** [November 27, 2022, 3:45pm UTC](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907 "2022-11-27T15:45:14Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [December 9, 2022, 6:13pm UTC](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907/21 "2022-12-09T18:13:37Z")

</div>

It would help if you show us the error that pops out.

On my machine, after some massaging of your code, the error seems to be that OrderedLogisitc does not have a CDF, which is necessary for the copula object to provide a pdf (and therefore it wont work).

Also, the in this version, the `Y2[i] ~ OrderedLogistic(Y2st[i]+μ2[i],cutpoints)` line does nothing since it is not used later, right ? So you might just remove it.

Moreover, you may change

```julia
    # Regression parameters
    μ1 = zeros(typeof(θ₁), n)
    μ2 = zeros(typeof(θ₂), n)
     ρ = zeros(typeof(θ), n)
     Y2st ~ filldist(Logistic(0, 1.), n)

    # Implement Shrinkage Priors
    for i in 1:n
        μ1[i] = θ₁ .+ sum(X[i,:].* β1)
        μ2[i] = θ₂ .+ sum(X[i,:].* β2)
        ρ[i] = exp(θ .+ sum(X[i,:].* β))
        Y2[i] ~ OrderedLogistic(Y2st[i]+μ2[i],cutpoints)

        #Now the copula is between Y1 (observed) and Y2st (latent)
        Turing.Turing.@addlogprob! loglikelihood(SklarDist(SurvivalCopula(ClaytonCopula(2,ρ[i]),(1,2)), (Normal(μ1[i], 1.), Logistic(μ2[i],1.))), [Y1[i],Y2st[i]+μ2[i]] )

    end

```

to simply

```julia
    # Regression parameters
    # nothing here.

    # Implement Shrinkage Priors
    for i in 1:n
        μ1i = θ₁ .+ sum(X[i,:].* β1)
        μ2i = θ₂ .+ sum(X[i,:].* β2)
        ρi = exp(θ .+ sum(X[i,:].* β))

        #Now the copula is between Y1 (observed) and Y2st (latent)
        Turing.Turing.@addlogprob! loglikelihood(SklarDist(SurvivalCopula(ClaytonCopula(2,ρi),(1,2)), (Normal(μ1i, 1.), Logistic(μ2i,1.))), [Y1[i],Y2st[i]+μ2[i]] )

    end

```

Removing useless vectors will probably make your function a bit faster.

---

<div class="post-metadata">

**Author:** ![Kokora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kokora/32/7867_2.png) [@Kokora](https://discourse.julialang.org/u/Kokora)\
**Post date:** [December 9, 2022, 6:49pm UTC](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907/22 "2022-12-09T18:49:32Z")

</div>

Here is the error message I get. It seems confusing to me

```julia
ERROR: TaskFailedException

    nested task error: TaskFailedException
    
        nested task error: MethodError: no method matching loglikelihood(::OrderedLogistic{Float64, Vector{Float64}}, ::CategoricalValue{Int64, UInt32})
        Closest candidates are:
          loglikelihood(!Matched::Union{StatsModels.TableRegressionModel, StatsModels.TableStatisticalModel}, ::Any...; kwargs...) at ~/.julia/packages/StatsModels/fK0P3/src/statsmodel.jl:28
          loglikelihood(!Matched::DynamicPPL.Model, ::Any) at ~/.julia/packages/DynamicPPL/xKo8W/src/simple_varinfo.jl:647
          loglikelihood(::UnivariateDistribution, !Matched::Real) at ~/.julia/packages/Distributions/gggmX/src/univariates.jl:329
          ...

Y2[i] ~ OrderedLogistic(Y2st[i]+μ2[i],cutpoints)

```

I think that is the contribution of the ordinal categorical data to the likelihood, right?  
Otherwise it looks Ycat has not part in the likelihood.

---

<div class="post-metadata">

**Author:** ![Kokora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kokora/32/7867_2.png) [@Kokora](https://discourse.julialang.org/u/Kokora)\
**Post date:** [December 10, 2022, 3:29am UTC](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907/23 "2022-12-10T03:29:57Z")

</div>

I think I understand it now. I needed to convert Ycat to integer

```julia
convert.(Int,Ycat)

```

does the trick. but @lrnv this takes too long

```julia
Iterations = 1001:1:3000
Number of chains = 3
Samples per chain = 2000
Wall duration = 18177.01 seconds
Compute duration = 18174.29 seconds

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [December 10, 2022, 4:23pm UTC](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907/24 "2022-12-10T16:23:49Z")

</div>

Indeed that is a lot of time. How does it compare to the same thing without the copula (say with a gaussian noise with correlation rho instead of the Clayton copula) ? So that we can see if the time is eaten up by the copula code or by Turing.

Also you may have a look at Turing performance tips (google it) to check if your model is type stable, that might be the issue.

[Previous page](https://discourse.julialang.org/t/issues-implementing-copula-regression-using-copula-and-turing/90907.md?page=1)
