Skip to content

BUG/REF/ENH copula for 0.13 follow-up #7629

Open

Description

@josef-pkt

current copula modules still have parts that need to be cleaned up or finished for 0.13

several items listed in merged PR #7408
following copied from #7408 (comment)

insuffient unit test coverage in several modules https://codecov.io/gh/statsmodels/statsmodels/tree/copula_7254_rebased/statsmodels/distributions/copula

  • copulas.py still contains old code, not covered
  • other_copulas.rvs_kernel added in last commit has not unit tests
  • archimedean.py missing test coverage mainly for tau related methods
  • elliptical.py tau, rho related methods and tail
  • depfunc_ev.py mainly no coverage for _check_args
  • extreme_value 100% coverage
  • is_completly_monotonic only method is_completly_monotonic is not covered

distributions.tools.py no coverage for _ecdf_mv function

_ecdf_mv returns counts and not plotting positions.


  • naming convention, renames
    • random -> rvs ?
    • IndependentCopula -> IndependenceCopula
    • d -> k_dim (or something like that)
    • ...
  • cleanup, enhance theta methods
  • empirical copula, beta copula need review, (It is where I stopped development for copula)

Activity

  1. added this to the 0.13 milestone on Aug 8, 2021
  2. josef-pkt commented on Aug 11, 2021

    @josef-pkt
    MemberAuthor

    specific copulas in docs

    class copula
    FrankCopula([theta]) Frank copula.
    ClaytonCopula([theta]) Clayton copula.
    GumbelCopula([theta]) Gumbel copula.
    GaussianCopula([corr]) Gaussian copula.
    StudentTCopula([df, corr]) Student copula.
    IndependenceCopula([d]) Independent copula.

    rst docs https://www.statsmodels.org/dev/distributions.html#copula should include available archimedean generators and extreme value pickands functions dep_func_ev
    available EV copulas are listed only in class docstring of ExtremeValueCopula

    class bv pdf mv pdf tau tau_inverse rvs cond cdf
    FrankCopula([theta]) x x? - x x x
    ClaytonCopula([theta]) x x? x x x -
    GumbelCopula([theta]) x x? x x x -
    GaussianCopula([corr]) x x x x x -
    StudentTCopula([df, corr]) x x x x x -
    IndependenceCopula([d]) x x x x x (*a)
    ArchimedeanCopula x - - - - -
    ExtremeValueCopula x - na na - -

    (*a) conditional is equal to marginal distribution

    update IndependenceCompula is the new name

  3. josef-pkt commented on Aug 12, 2021

    @josef-pkt
    MemberAuthor

    logpdf for Archimedean needs work
    only Frank has explicit logpdf and only for dim=2 bivariate

    ArchimedeanCopula class itself only supports 2-dim generically. Need more derivatives of transform function

    elliptical, Gaussian, T, only define pdf, logpdf inherited from base class Copula which returns np.log(self.pdf(u))

    ExtremeValueCopula uses np.log(self.pdf(u, args=args))

  4. josef-pkt commented on Aug 12, 2021

    @josef-pkt
    MemberAuthor

    args handling, args in methods

    In archimedean copulas: only Frank has args in methods. Other require frozen args. Estimation for copula parameter is through kendall's tau.

    We currently don't have any estimator for the EV Copula args.
    We need an MLE model or function to estimate args in asymmetric EV copulas
    two options:

    • use EV CopulaModel with uniform marginal distributions to fit; generic solution; we just need a helper function as wrapper.
    • use special case model for ev copula, that uses rank transformed data to estimate copula parameters. This requires some duplicate work and code.

    d or k_dim: need to add it consistently across copulas.

  5. josef-pkt commented on Aug 12, 2021

    @josef-pkt
    MemberAuthor

    elliptical, archimedean: corr_from_tau and theta_from_tau could/should be classmethods (or static methods) so we can call them before creating an instance. That's useful if we want to set tau in __init__

    elliptical t copula mixes up (spearman) rho and tau as temp variable that is returned

    base Copula class has _theta_from_tau and fit_theta method. However, elliptical copulas have inconsistent method name corr_from_tau

    Copula.fit_theta uses only the first two columns to compute kendalls_tau, i.e. assumes bivariate.
    for elliptical we also need multivariate tau. For multivariate archimedean it would be better to use tau averaged over pairs of r.v.

  6. josef-pkt commented on Aug 13, 2021

    @josef-pkt
    MemberAuthor

    We need attributes to know how many parameters and args a distribution or copula has.
    k_args is not necessarily the same as k_params

    EV dep funcs have two or three args,
    archimedean has one arg
    elliptical has one args but if k_dim >2, then the arg has several params, i.e. k_params >= k_args, k_params could be k * (k -1) for triangular submatrix of corr.

    in fit: k_params is needed for start_params, k_args is needed for tuple packing/unpacking (*args)

    We need this for the copula parameter, but similarly for marginal distribution paramaters. In the latter case k_args = k_params

    This is separate from vectorization across observations, e.g. we could have the single arg scale vectorized over observations when we have heteroscedasticity. Mean parameter will always be vectorized by obs in regression models.

    It gets messier when we allow for partial constraints, e.g. restrict some parameters and leave other parameters free for estimation.

    type of constraint combination

    • unconstrained: estimate copula and marginal distribution parameters
    • fixed marginal distribution parameters and estimate only copula parameters
    • fixed copula parameters and estimated marginal distribution parameters
    • all parameters fixed

    Currently I only have case 2, estimate only copula parameters, working for Frank and EV.
    Copula.logpdf and other methods do not yet support args for marginal distributions.

    more difficult will be partial constraints:

    • partial copula constraints, only relevant for EV (e.g. symmetry constraint) and possibly for correlation patterns in elliptical
    • partial constraints in marginal distributions for copula distribution, e.g. loc in R+ distribution, df in t-distribution
    • partial constraints in marginal model for copula regression model, depends on marginal Model.
  7. josef-pkt commented on Aug 17, 2021

    @josef-pkt
    MemberAuthor

    optimization problems in EV HR: copula = ExtremeValueCopula(transform=dep.HR())

    In unit tests for new CopulaModel I get optimization problems with HR or parameter estimates for marginal means are not close to true values. However, data is generated with Frank copula, We don't have rvs for EV copulas yet.
    Other EV copulas have marginal means reasonably close to true values.

    update found a bug the HR depfunc had hardcoded lamda=2 in the deriv.
    There is only one unit test case for HR depfunc and that has lamda=2

    now EV AsymLogistic has convergence failure in one test case: Frank copula with marginal normal, estimating mean and with fixed scale)
    Estimating both marginal means and scale converges

  8. josef-pkt commented on Sep 15, 2021

    @josef-pkt
    MemberAuthor

    missed one: unused and incomplete, conditional distribution

    FrankCopula has
    cdfcond_2g1(u[, args]) | Conditional cdf of second component given the value of first.
    ExtremeValueCopula has (with NotImplementedError)
    conditional_2g1(self, u, args=())
    inconsistent naming

    Requiring u as bivariate (2 columns) will not always make sense, e.g. when we wand to evaluate pdf or cdf for many u2 at one value of u1.
    Shapes of u1 and u2 need not match for all use cases, and, I guess we better separate them.

    initial usecase where we need conditional ppf is for inverse sampling (rvs using conditional ppf)
    Frank is the only copula that has explicit formula for ppfcond_2g1(self, q, u1, args=()) (AFAIK, or maybe only for archimedean)

  9. josef-pkt commented on Jan 31, 2023

    @josef-pkt
    MemberAuthor

    another limitation

    rvs for archimedean requires monotonicity (what's the name?)
    i.e. only for positive correlation, not extended to negative correlation.

    we might need to raise an explicit exception if condition on theta does not hold.
    Archimedean generator transform has is_completly_monotonic method that returns boolean.

    So far we don't handle "singular" components in copulas, i.e. we assume some kind of continuous wrt Lebesque measure.
    (I never really read those details.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions