Import the library
In [1]:
import pyvinecopulib as pv
import numpy as np
In [2]:
# Specify pair-copulas
bicop = pv.Bicop(pv.BicopFamily.bb1, 90, [3, 2])
pcs = [[bicop, bicop], [bicop]]
# Specify R-vine matrix
mat = np.array([[1, 1, 1], [2, 2, 0], [3, 0, 0]])
# Set-up a vine copula
cop = pv.Vinecop(mat, pcs)
print(cop)
Showcase some methods
In [3]:
u = cop.simulate(n=10, seeds=[1, 2, 3])
fcts = [cop.pdf, cop.rosenblatt, cop.inverse_rosenblatt,
cop.loglik, cop.aic, cop.bic]
[f(u) for f in fcts]
Out[3]:
Create a vine copula model
Different ways to fit a copula (when the families and structure are known)...
In [5]:
u = cop.simulate(n=1000, seeds=[1, 2, 3])
# Define first an object to control the fits:
# - pv.FitControlsVinecop objects store the controls
# - here, we only restrict the parametric family
# - see help(pv.FitControlsVinecop) for more details
controls = pv.FitControlsVinecop(family_set=[pv.BicopFamily.bb1])
print(controls)
# Create a new object an select family and parameters by fitting to data
cop2 = pv.Vinecop(mat, pcs)
cop2.select(data=u, controls=controls)
print(cop2)
# Otherwise, create directly from data
cop2 = pv.Vinecop(data=u, matrix=mat, controls=controls)
print(cop2)
When nothing is known, there are also two ways to fit a copula...
In [6]:
# Create a new object and select strucutre, family, and parameters
cop3 = pv.Vinecop(d=3)
cop3.select(data=u)
print(cop3)
# Otherwise, create directly from data
cop3 = pv.Vinecop(data=u)
print(cop3)
In [7]:
# create a C-vine structure with root node 1 in first tree, 2 in second, ...
cvine = pv.CVineStructure([4, 3, 2, 1])
# specify pair-copulas in every tree
tree1 = [pv.Bicop(pv.BicopFamily.gaussian, 0, [0.5]),
pv.Bicop(pv.BicopFamily.clayton, 0, [3]),
pv.Bicop(pv.BicopFamily.student, 0, [0.4, 4])]
tree2 = [pv.Bicop(pv.BicopFamily.indep),
pv.Bicop(pv.BicopFamily.gaussian, 0, [0.5])]
tree3 = [pv.Bicop(pv.BicopFamily.gaussian)]
# instantiate C-vine copula model
cop = pv.Vinecop(cvine, [tree1, tree2, tree3])
print(cop)
In [ ]: