Skip to content

SUMM/ENH: resurrecting copulas #4046

Closed
Closed

Description

@josef-pkt

(no immediate plans, but we don't have an issue, it's mentioned in just a few issues)

application to pairs trading https://www.quantopian.com/posts/statistical-arbitrage-using-gaussian-copula
(comment recommends ICA https://www.hindawi.com/journals/isrn/2013/434832 )
some background on pairs trading using cointegration or copulas https://www.quantconnect.com/tutorials/pairs-trading-strategy/

https://statsletters.com/2016/10/19/trying-out-copula-packages-in-python-i/
https://statsletters.com/2016/11/16/trying-out-copula-packages-in-python-ii/
https://github.com/lia-statsletters/notebooks/blob/master/Copula%20packages%20continuation.ipynb
the second blog post, (ii), uses the old code in statsmodels sandbox
quote: "However as it is, is quite flexible. Is good that the developers decided to keep it anyway."
(an argument for not just deleting stale code in the sandbox)
(Note: the notebook repo has a GPL license)

There would be a long way to catch up with more recent developments which went more into vines and nested copulas (last time I checked) which are a bit more difficult to implement (efficiently).

However, it would be possible to get the basics. Either following up on the sandbox with unit tests comparing to R, or emphasis on special cases, e.g. bivariate normal and t copulas.
(Integration code of ... is now BSD licensed, scipy has only the generic multivariate normal cdf Fortran code, but we should be able to get relatively efficient/fast bivariate normal and t cdf.)

Of interest besides financial applications are cases with discrete or mixed continuous-discrete multivariate models.

Activity

  1. added this to the Someday milestone on Oct 18, 2017
  2. josef-pkt commented on Oct 18, 2017

    @josef-pkt
    MemberAuthor
  3. josef-pkt commented on Oct 18, 2017

    @josef-pkt
    MemberAuthor

    adding a reference because it showed up on the google search, I haven't looked at it beyond title

    Irannezhad, Elnaz, Carlo G. Prato, Mark Hickman, and Afshin Shariat Mohaymany. A Copula-Based Joint Discrete-Continuous Model of Road Vehicle Type and Shipment Size. No. 17-05892. 2017.
    http://docs.trb.org/prp/17-05892.pdf

  4. josef-pkt commented on Oct 18, 2017

    @josef-pkt
    MemberAuthor

    and another stale issue
    http://jpktd.blogspot.ca/2012/06/non-linear-dependence-measures-distance.html

    I was initially looking at and working on copula based tests of 'independence' (?) or uncorrelated it terms of some non-linear distance measure. (including auto-correlation, serial independence)
    (I have no idea where the code or notes are.)

  5. lia-statsletters commented on Jan 24, 2018

    @lia-statsletters

    Hi! I wrote the notebooks and came here to say: resurrecting copulas? yes please! There is also very good material in this book: "Quantitative Risk Management: Concepts, Techniques and Tools" by Alexander J. McNeil, Rüdiger Frey and Paul Embrechts, published by Princeton University Press in 2015" and in qrmtutorial.org.

  6. jmatthewpeters commented on Aug 30, 2018

    @jmatthewpeters

    I'd like to help with this if I can. I would love to have something like [https://cran.r-project.org/web/packages/copula/copula.pdf]( R copula Package ) [https://datascienceplus.com/modelling-dependence-with-copulas/] I'm trying to replicate a project that was written in R that uses that package and while I think what is here is good enough, I'd be willing to help out if I can (I haven't built many python libraries).

  7. josef-pkt commented on Aug 30, 2018

    @josef-pkt
    MemberAuthor

    It would be great if you (and anyone else) would move this forward.

    One of the first things that would be needed are unit tests against R to check that those functions are correct. Also a rough roadmap for missing pieces would be useful to see what is needed to make this useful. When I wrote this I mainly focused on pdf and cdf, and the general setup for Archimedean and extreme value copulas. I guess adding fit (and rvs ?) would be important.
    Then it would be easier to incrementally add more features.

    I just looked for a while through my old (2011) folders to see what else I did. I have one more commit in my local repo that is not in the statsmodels sandbox. I don't find any additional example scripts or code that I might have written.
    (I have 500 pdf files with copula in the name in my old folders, but it looks like I did more reading articles than writing code. )

  8. josef-pkt commented on Aug 30, 2018

    @josef-pkt
    MemberAuthor

    A general warning:
    We are not allowed to translate code that is released under GPL or any other license that is not BSD compatible, unless we have explicit permission to do so.

  9. jmatthewpeters commented on Aug 30, 2018

    @jmatthewpeters

    Ok, I will need some guidance on how you want the tests to look, if there is some standards already for statsmodels I can look them up or if you want to point me to them. I can't imagine just translating anything, I don't really like the structure for the R code. I like the approach so some of the more complicated functions, but they used mathematica to derive some of the functions and I can use sympy for it. I can't guarantee how fast I can get stuff done but at least I can work on it.

  10. josef-pkt commented on Aug 30, 2018

    @josef-pkt
    MemberAuthor

    (I found a file where I had used sympy to try out obtaining derivatives for some copula functions. Mathematica online is also pretty good.)

    for unit tests
    background reading is the numpy testing guidelines which is roughly followed by most packages in this area, although there are idiosyncratic differences, e.g. we have many classes
    https://github.com/numpy/numpy/blob/master/doc/TESTS.rst.txt

    for many unit test we use test classes when we need to check many different attributes or results.
    However, the simplest cases are just hardcoding the corresponding number from R and compare them to the statsmodels function with assert_allclose from numpy testing.
    e.g. https://github.com/statsmodels/statsmodels/blob/master/statsmodels/stats/tests/test_proportion.py#L500

    For example for the current copula code you could get the pdf and cdf at several values from R.
    If there are many cases and it gets repetitive, then storing the results separately and using a loop or parameterized test will reduce copy-paste tasks. (e.g. in Stata and R I use export scripts if there are too many cases to do it by hand.)

  11. josef-pkt commented on Aug 30, 2018

    @josef-pkt
    MemberAuthor

    I added the commit that I found with the example for some of the Archimedean copulas
    #5076

    It adds the derivatives to the archimedean copula transform classes, but I don't remember why we need them.

  12. josef-pkt commented on Aug 31, 2018

    @josef-pkt
    MemberAuthor

    I checked that the basic things in copula are working, but didn't verify by comparing to other packages.
    I merged the extra commit in #5076, and added some comments on my interpretation of the copula code.

    I need to go back to other projects, but I can spend more time on this if there are questions or if I need to help with implementing parts of it.

  13. jmatthewpeters commented on Aug 31, 2018

    @jmatthewpeters

    Thank you. I'm slowly getting up to speed on some of its and will work on some tests and comparisons to R. I'm just going to need some time to figure out how to replicate the test cases in R and then translate that to here.

  14. 35 remaining items

  15. tupui commented on May 13, 2021

    @tupui
    Contributor

    We can use and compare our values to the output, but the documentation is also licensed. Most people don't bother to use a proper licensing for their doc and just license the whole thing with the same license, but still. If having a look at a licensed code to figure something out and code yourself the solution is not ok, then the same apply for the doc if covered under the same terms.

  16. josef-pkt commented on May 13, 2021

    @josef-pkt
    MemberAuthor

    No, we can read documentation without problems. Copyright for documentation means we cannot copy the documentation itself. (e.g. We can read scientific articles and use them, but we cannot plagiarize them.)

    It does not affect writing code after reading the documentation, otherwise we would never be allowed to look at documentation of proprietary packages like Matlab, SAS, SPSS, Stata and ...

  17. tupui commented on May 13, 2021

    @tupui
    Contributor

    Well nobody will come here to annoy us... So just for the sack of the discussion. But if the documentation is licensed under GPL's terms, it's GPL. There are other license which are more appropriate to properly distribute doc, and almost no package have a separate license for the doc. What is commonly done by people is another story I am afraid. As for the articles, here as well it's the far west as nobody really reads the copyright of any paper which is different from one journal to the other.

  18. josef-pkt commented on May 13, 2021

    @josef-pkt
    MemberAuthor

    But facts and definitions cannot be copyrighted. Whatever copyright is assigned, doesn't matter.
    e.g. random google find: copyrightability of short phrases https://fairuse.stanford.edu/2003/09/09/copyright_protection_for_short/

    for example from copBasic surfuncCOP docstring:

    Compute the joint survival function for a copula (Nelsen, 2006, p. 33), which is defined as
    C_bar(u; v) = Pr[U > u; V > v] = 1 - ...
    

    Nothing original, copyrightable in there.

  19. ChadFulton commented on May 13, 2021

    @ChadFulton
    Member

    But facts and definitions cannot be copyrighted.

    Another good example of this is recipes for food. Anyone is free to publish their own cookbook with a collection of any recipes.

    This is also one of the reasons why companies maintain trade secrets - the fundamental elements of many trade secrets (e.g. algorithms, these days) often can't be copyrighted.

  20. tupui commented on May 13, 2021

    @tupui
    Contributor

    See this for the recipe, not that simple 😉: https://www.copyright.gov/circs/circ33.pdf As always, this is a question of understanding and subjective to the appreciation of the judge. Also remember that copyright laws are different from one country to another.

    But I just wanted to ensure you saw this R library was under GPL and hence it should be dealt with care.

  21. bashtage commented on May 13, 2021

    @bashtage
    Member

    In the US at least, the Google vs Oracle made it pretty clear that API documentation is not copyrighted. So if one wanted to write a work-alike package, that would probably be OK under current US case law.

  22. tupui commented on May 13, 2021

    @tupui
    Contributor

    In the US at least, the Google vs Oracle made it pretty clear that API documentation is not copyrighted. So if one wanted to write a work-alike package, that would probably be OK under current US case law.

    Sure sure, but here we are not talking about naming things the same. But read the doc or have a look at some other work to figure out how it works and how we can replicate. Which is quite different.

  23. josef-pkt commented on May 13, 2021

    @josef-pkt
    MemberAuthor

    That's the same as in the recipe case in your linked pdf.

    The original, artistic presentation of a recipe can be copyrighted, but not the ingredients and procedure to replicate the dish.

    Also, except for a few computational hints, e.g. for corner cases, nothing in the documentation of packages like copula, copBasic is original methods. If there were a copyright on methods/procedures/definitions, then it would be copyright be the original authors. However, that (methods, definitions, theorems, ...) is not copyrightable because it would kill science.

  24. josef-pkt commented on Mar 3, 2023

    @josef-pkt
    MemberAuthor

    closing

    we still have updated open issues
    general:
    #8635 missing parts and extensions
    #7258 some notes and references
    and other issues for specific extensions

  25. modified the milestones: Someday, 0.13 on Mar 3, 2023
  26. modified the milestones: 0.13, 0.14 on Apr 14, 2023
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions