Skip to content

ENH/BUG: work on copulas - #7257

Merged
josef-pkt merged 2 commits into
statsmodels:masterfrom
josef-pkt:copula
Jan 20, 2021
Merged

josef-pkt merged 2 commits into
statsmodels:masterfrom
josef-pkt:copula

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

getting started with unit tests and bug fixes for copulas in sandbox

extreme value copulas are compared to R evd package (except ev-t).
other copulas are in R package copula

@pep8speaks

pep8speaks commented Jan 15, 2021 •

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-01-20 16:11:10 UTC

@josef-pkt

Copy link
Copy Markdown
Member Author

numbers are copied from an R session, for the record here is part of it, using library(evd)

> abvevd(c(0., 0.25, 0.5, 0.75, 1), dep = 0.5, asy = c(0.25, 0.75), model = "alog")
[1] 1.0000000000000000 0.9409615711335886 0.8952847075210475 0.8901650429449552
[5] 1.0000000000000000
> abvevd(0.75, dep = 0.5, asy = c(0.25, 0.75), model = "alog")
[1] 0.8901650429449552
> abvevd(0.75, dep = 0.5, asy = c(0.5, 0.5), model = "alog")
[1] 0.8952847075210475
> abvevd(c(0., 0.25, 0.5, 0.75, 1), dep = 0.5, asy = c(0.5, 0.5), model = "alog")
[1] 1.0000000000000000 0.8952847075210475 0.8535533905932737 0.8952847075210475
[5] 1.0000000000000000
> abvevd(c(0., 0.25, 0.5, 0.75, 1), dep = 0.5, asy = c(0.75, 0.25), model = "alog")
[1] 1.0000000000000000 0.8901650429449552 0.8952847075210475 0.9409615711335886
[5] 1.0000000000000000
> pbvevd(c(0.5,0.9), alpha = 0.25, beta = 0.05, model = "amix")
[1] 0.393453820991562
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.25, beta = 0.05, model = "amix")
[1] 0.464357480263932
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.25, beta = 0.05, model = "bilog")
[1] 0.5
> pbvevd(qgev(c(0.5,0.5)), alpha = 0.25, beta = 0.05, model = "bilog")
[1] 0.4603469540522427
> pbvevd(qgev(c(0.999,0.999)), alpha = 0.25, beta = 0.05, model = "bilog")
[1] 0.9988808602338192
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.75, beta = 0.25, model = "bilog")
[1] 0.4988077515720172
> pbvevd(qgev(c(0.5,0.9)), dep=0.5, model = "hr")
[1] 0.4551235014298542
> abvevd(0.75, dep = 0.5, model = "hr")
[1] 0.9808443476674621
> abvevd(c(0., 0.25, 0.5, 0.75, 1), dep = 0.5, model = "hr")
[1] 1.0000000000000000 0.9808443476674621 0.9772498680518208 0.9808443476674621
[5] 1.0000000000000000
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.25, beta = 0.05, model = "amix")
[1] 0.464357480263932
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.5, beta = 0.25, model = "amix")
[1] 0.4916117128670654
> abvevd(c(0., 0.25, 0.5, 0.75, 1), alpha = 0.5, beta = 0.25, model = "amix")
[1] 1.00000000 0.84765625 0.78125000 0.82421875 1.00000000
> pbvevd(c(0.5,0.9), alpha = 0.5, beta = 0.25, model = "amix")
[1] 0.4519820720233402
> pbvevd(qgev(c(0.5,0.9)), alpha = 0.5, beta = 0.25, model = "amix")
[1] 0.4916117128670654
> pbvevd(qgev(c(0.9, 0.5)), alpha = 0.5, beta = 0.25, model = "amix")
[1] 0.48340673415789
> pbvevd(qgev(c(0.5, 0.9)), dep = 0.5, asy = c(0.5, 0.5), model = "aneglog")
[1] 0.462440667016731
> pbvevd(qgev(c(0.5, 0.9)), dep = 0.5, asy = c(0.75, 0.25), model = "aneglog")
[1] 0.4579672324993965
> pbvevd(qgev(c(0.5, 0.9)), dep = 0.25, asy = c(0.5, 0.75), model = "aneglog")
[1] 0.4543698299835434
> pbvevd(qgev(c(0.9, 0.5)), dep = 0.25, asy = c(0.5, 0.75), model = "aneglog")
[1] 0.4539773435983587
> abvevd(c(0., 0.25, 0.5, 0.75, 1), dep = 0.25, asy = c(0.5, 0.75), model = "aneglog")
[1] 1.0000000000000000 0.9845519769229971 0.9809613830669038 0.9836738469328421
[5] 1.0000000000000000

@josef-pkt

Copy link
Copy Markdown
Member Author

bug in clayton inverse power should be -1/theta, not -theta.
then matches R copula for copula cdf and pdf

and more bugs

now fixed in latest commit. All generated copulas should be verified now (at least at a few points)

@josef-pkt

josef-pkt commented Jan 16, 2021 •

Copy link
Copy Markdown
Member Author

interface design ?

currently
bv ev copula use u, and v as separate arguments, currently only cdf available
archimedean copulas use a single vector/array u, in both cdf and pdf

CopulaDistribution cannot handle both patterns. Either we split CopulaDistribution into a BV and an MV class or use vector u everywhere.

Both R packages, evd and copula use vector u

with 2-dim array u with need to split u, v by columns not by unpacking

CopulaDistribution.cdf only works for bivariate and uses tuple unpacking, i.e. is wrong (bug)

CopulaArchimedean has axis argument in cdf, pdf.
It's easily vectorized, but axis option makes it potentially more work for other things that is not worth it. i.e. hard code axis=-1 and drop argument.

broadcasting ?

  • iid observations: 2-dim observations u but scalar params, not broadcasting needed in base case
  • regression case: 2-dim observations u, scalar copula params, but some vector params of marginal distributions, e.g. mean.
    I'm not sure yet how this will work, params for estimation will be vector with fixed length independent of nobs.

I thought of adding CopulaModel similarly to CopulaDistribution

  • CopulaDistribution takes list of marginal distributions as __init__ argument, for iid case.
  • CopulaModel takes list of model instances, e.g. GLM or new regression MLE classes for other distributions,, as __init__ argument. Then, conversion from regression parameters to distribution parameters like mean and scale, are handled by the marginal model, and the copula part doesn't need to know about the details.

i.e. Copula distribution should only need "regression" or "model" params,

edit
current CopulaDistribution take a list of cdf functions. That's not enough, we need distributions or model instances.
see also comment #5076 (comment)

design params

list of marginal params
copula package uses keywords as params for each marginal
I guess we use args, i.e. each marginal param is array_like, tuple or list

for consistency with MLE model
We could define loglike separate from logpdf, which does the params unpacking. In optimization we need a single params vector.
add methods for params packing, unpacking or transformation.
e.g. miscmodel.TModel uses it also to optionally fix df

@josef-pkt

Copy link
Copy Markdown
Member Author

we are missing amh as one popular archimedean
but this PR is mainly for fixing sandbox code

@josef-pkt

Copy link
Copy Markdown
Member Author

usage of checkargs

  • EV transform function: raises exception if invalid copula params
  • archimedean generator transforms: is implemented but not used by transform methods

If we want to estimate copula params by MLE, then we cannot have an exception during optimization.
The problem is that in some cases the restriction on copula params are not simple and cannot be worked around easily with link transformations.

@josef-pkt

Copy link
Copy Markdown
Member Author

I need a glossary for the special cases/copulas: W, M, P. Which is which?

@josef-pkt

josef-pkt commented Jan 16, 2021 •

Copy link
Copy Markdown
Member Author

limitation:
I'm not sure: the current CopulaArchimedean.pdf is maybe only correct for bivariate copulas.
need to check

after checking with R:
pdf is wrong for dim=3, computed pdf value is negative in a Gumbel example.
cdf for trivariate Gumbel agrees with copula package

> cop <- gumbelCopula(2,dim=3)

> pCopula(c(0.5, 0.9, 0.6), cop)
[1] 0.4200146617837097
> dCopula(c(0.5, 0.9, 0.6), cop)
[1] 0.7507987484870147

> pCopula(c(0.5, 0.9, 0.25), cop)
[1] 0.2115061554169671
> dCopula(c(0.5, 0.9, 0.25), cop)
[1] 0.2119179178640158
cg = CopulaArchimedean(cop.TransfGumbel())
cg.cdf([0.5, 0.9, 0.6], args=(2,)), cg.pdf([0.5, 0.9, 0.6], args=(2,))
(0.4200146617837097, -0.3320492133053681)

cg.cdf([0.5, 0.9, 0.25], args=(2,)), cg.pdf([0.5, 0.9, 0.25], args=(2,))
(0.2115061554169671, -0.25927496948369333)

@josef-pkt josef-pkt mentioned this pull request Jan 16, 2021
1 of 3 tasks
@josef-pkt

Copy link
Copy Markdown
Member Author

all green, merging

this is bug fix PR, next will be moving and refactoring.

@josef-pkt
josef-pkt merged commit 789c194 into statsmodels:master Jan 20, 2021
@josef-pkt josef-pkt mentioned this pull request Jan 27, 2021
4 tasks
@josef-pkt
josef-pkt deleted the copula branch February 21, 2021 02:53
@josef-pkt josef-pkt added this to the 0.13 milestone Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants