Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty generating independent copula samples and also evaluating density and cumulative functions for Fréchet–Hoeffding copula bounds #35

Closed
Santymax98 opened this issue Oct 24, 2023 · 2 comments · Fixed by #36

Comments

@Santymax98
Copy link
Contributor

Santymax98 commented Oct 24, 2023

Hello, nice to talk to you.

I have a problem when I want to generate random samples and also to calculate the "pdf" and the "cdf" of the independent copula and the Fréchet–Hoeffding bounds. I know that densities for the bounds do not exist, however the cumulative function should be able to be calculated. In the first code I show a small example where it works well for the Gaussian copula. It involves generating a 3D graph of the density and cumulative function of the copula. To change between density and cumulative function it is only necessary to comment and uncomment

####################################################################

using Copulas, Distributions, Plotly

###Define a valid correlation matrix for the bivariate Gaussian copula
Σ = [1.0 0.5; 0.5 1.0] # example

###Create the bivariate Gaussian copula with the correlation matrix Σ
G = GaussianCopula(Σ)
#G = IndependentCopula(2)
#G = MCopula(2)
###Generate a grid of two-dimensional values (u, v) in [0, 1]x[0, 1]
u = range(0, stop=1, length=100)
v = range(0, stop=1, length=100)
grid = [(ui, vi) for ui in u, vi in v]
###Calculate the cumulative function or density of the copula in the grid matrix
density = zeros(length(grid))
###acumulate = zeros(length(grid))
for (i, (ui, vi)) in enumerate(grid)
###acumulate[i] = cdf(G, [ui, vi])
density[i] = pdf(G, [ui, vi])
end

####Reformat the density into a two-dimensional array
z_data = reshape(density, 100, 100)

layout = Layout(
title="Gaussian Copula",
autosize=false,
scene_camera_eye=attr(x=1.87, y=0.88, z=-0.64),
width=500, height=500,
margin=attr(l=65, r=50, b=65, t=90)
)

plot(surface(
z=z_data,
contours_z=attr(
show=true,
usecolormap=true,
highlightcolor="limegreen",
project_z=true
)
), layout)

###############################################################################

The second code is a little more basic, it simply consists of simulating random samples of the independent copula and the Fréchet–Hoeffding bounds. Now, as you can notice, when simulating the Gaussian copula I have no problems, however for the independent copula I get the error
" MethodError: no method matching Random.Sampler(::Type{Random.TaskLocalRNG}, :Random.SamplerType{IndependentCopula{2}}, ::Val{1})"

##########################

using Copulas, Distributions

###Gaussian copula simulation
Σ = [1.0 0.5; 0.5 1.0]
G = GaussianCopula(Σ)
simu1 = rand(G,1)

###independent copula simulation
I = IndependentCopula(2)
simu2 = rand(I,1) #Does not generate samples and gives the mentioned error

###Mcopula simulation
M = MCopula(2)
simu3 = rand(M,1)

###WCopula simulation
W = Copulas.WCopula(2)
simu4 = rand(W,1)

###########################

The code does not generate samples of the independent copula, nor does it generate samples of "WCopula(2)" since an error of the type "UndefVarError: WCopula not defined" appears. I could solve it by changing "WCopula(2) for Copulas.WCopula(2) )" . However, the error of independent copulation continues. For "MCopula()" and the other elliptic and Archimedean copulas it works well.

Thank you very much for your attention and I look forward to your prompt response.

@lrnv
Copy link
Owner

lrnv commented Oct 24, 2023

Thanks a lot for taking the time to write the details of your issues.

I fixed the problem for sampling the IndependantCopula (a pair of parentheses was missing…) and the WCopula (I had a typo export Wcopula instead of export WCopula…). I also added a fast route for computation of the CDF and PDF of the independent copula, and fixed the implementation of the CDF of MCopula.

I could not install Plotly and thus was not able to run your graph, but all the errors you mention should be gone as soon as the new modifications are released.

EDIT: It got released as v0.1.11 in the registry. Tell me if you still experience these issues on the new version, and if everything is alright we'll close this.

@lrnv lrnv reopened this Oct 24, 2023
lrnv referenced this issue Oct 24, 2023
…01-18-32-623-01712040392

CompatHelper: bump compat for MvNormalCDF to 0.3, (keep existing compat)
@Santymax98
Copy link
Contributor Author

I used the mods in the package and now everything works perfectly. It couples very well with "Ploty" and can effectively obtain samples for the aforementioned copulations. Furthermore, the PDF and CDF functions for the independent copula and the CDF function for the Fréchet–Hoeffding bounds can be perfectly evaluated.

Thank you very much for the excellent work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants