Skip to content

Any plan to integrate Extreme Value Theory #7317

Open

Description

@pth91

Is there any plan for an integration of extreme value theory into statsmodels?

There is scikit-extremes but the project seems abandoned.

I just finished my master's with my thesis in Extreme Value Theory and think I could contribute a thing or two if that is in the scope of statsmodels.

Activity

  1. josef-pkt commented on Feb 10, 2021

    @josef-pkt
    Member

    methods for extreme value theory would fill well in statsmodels.

    I don't really have an overview of what methods should be added. scipy has the main univariate distributions.
    I currently started to look at some of it again for copulas and multivariate extreme value distributions. I used R evd package for this. Otherwise, there is currently no work going on in this area.

    see also #5185 and #6779

    It would be good to have something like the basic methods similar to R packages evd and extremes in statsmodels.
    (But, we cannot copy and translate code from them because their GPL license is not compatible with our BSD-3 license)
    The plan would be to add a new statsmodels.extremes directory with support for this. There will be some overlap with other parts of statsmodels like distributions

    Do you have a list of methods that you want to work on, or an outline of what we should add?

  2. pth91 commented on Feb 10, 2021

    @pth91
    Author

    I see that there was already a lot of thought put into these possible features. I would start by taking a closer look at the evd package of R to get the gist of the implementation - although I used it to some extend in my thesis, I did not take a look at the source code itself as it was a theoretical work.

    What I would do:

    1. Take a closer look at the two issues you posted, i..e the packages you linked in SUMM/ENH: extreme value distributions, pareto #5185
    2. Come up with a few methods to work on. Intuitively, I would separate this between blockmaxima and peaks over threshold methods. Hence the first would lead us to MLEs for the GEVs and the second to inference on the generalized Pareto distribution.
    3. I would have to take a look at the asymptotics but I think in the book Extreme Value Theory by Laurens de Haan there is a reasonable take on estimation of the extreme value index of the generalized Pareto distribution and hence a way to have at least some inference with finite data for the following theorem Pickands-Balkema-De Haan POT.

    As this would be my first real contribution to an open source project outside of university or work I think I would have to get familiar with a few best practices first, though.

  3. pth91 commented on Feb 27, 2021

    @pth91
    Author

    @josef-pkt Hi Josef, I had a closer look at the evd package. Now, I am thinking of an approach to start things. As far as I understand it, I should always compare the python implementation w/ the reference implementation which would be evd in that case. Is there any best practice regarding the comparison, i.e. how I could achieve a high test coverage of my code which orients itself on R? I think I am mostly talking about data here...

    Regards

  4. josef-pkt commented on Feb 27, 2021

    @josef-pkt
    Member

    Simple functions can be unit tested just at a few points. For example, I hardcoded some values for copula distributions at just a few points directly in the unit test module.

    For estimation we need a dataset for the examples in the unit tests. We have some in statsmodels.datasets but there might not be one that applies to EV. New datasets can be added, but we haven't done that in some time.
    Short dataset, like 20 numbers, can be added in modules.
    For larger datasets and for larger results data, e.g. residuals, predictions, we use mainly csv files in tests/results folders.

    you can look at current cases like statsmodels\stats\tests\results for examples.

    The comparison numbers for the results, e.g. estimated parameters, standard errors, pvalues, are either hardcoded by hand, or exported from R or Stata using helper functions that we had written.
    "\statsmodels\tools\R2nparray\R\R2nparray.R"
    (I haven't used the one for R in some time, because I had recently only cases with a few results numbers.)

    Once we have the comparison numbers, we use mostly assert_allclose from numpy.testing with rtol and atol specific to the case.

    If we call the new directory extremes, then we would have
    statsmodels.extremes.tests and statsmodels.extremes.tests.results for the unit tests.

    I'm not sure extremes is the best name for us, but we should avoid acronyms

  5. josef-pkt commented on Feb 27, 2021

    @josef-pkt
    Member

    Related:
    I usually set print precision in R to around 16 significant digits. For pure math like linalg, I set rtol=1e-13 and atol only if there are zeros.
    For optimization problems or approximations, the rtol might only be 1e-4 to 1e-6 if convergence tolerance is the limiting factor.

  6. pth91 commented on Jun 20, 2021

    @pth91
    Author

    Hi @josef-pkt I made a plan the past few days what I want to implement. It seems that scipy does have a Generalized Pareto and Generalized Extreme value distribution. Although neither of them has location and scale parameters as is the case for the implementations in the evd package. So I would start with both distributions w.r.t. to three (scale, location, shape) parameters instead of one parameter (shpae) in comparison to scipy. Then I would implement the maximum likelihoood estimator for the generalized Pareto distribution. A question regarding the MLE. Is there an estimator parent class for which i can define a child for the GPD MLE or is the estimator part of a GPD class?

    Edit: a more general question, there does not seem to be maximum likelihood estimation implemented for any distribution currently?!

  7. josef-pkt commented on Jun 20, 2021

    @josef-pkt
    Member

    in scipy all continuous distribution are loc-scale families. However, loc and scale are generically implemented and added by the wrapper methods and are not added in the specific distribution subclass.

    We have statsmodels.base.model.GenericLikelihoodModel as a base class for new or prototype distributions.
    I just implemented a estimation class for it but for discrete distributions based for a new subclass of scipy.distributions https://github.com/statsmodels/statsmodels/pull/7488/files#diff-67f8b6aec95fe9172af84b8bbc122cfd72948caac37af3d5a7f6c823f0843672R298

    Other examples and test cases are in statsmodels.miscmodels.
    e.g. MyPareto https://github.com/statsmodels/statsmodels/blob/main/statsmodels/miscmodels/tests/test_generic_mle.py#L17
    The student t-model in miscmodels should also be interesting. it includes keeping on of the parameters fixed during estimation.

    However I want to refactor GenericLikelihoodModel again and make it closer to our standard models, e.g. no nloglike just the regular loglike #4679 (some of those methods and attributes were experiments that didn't catch on are obsolete now)

    Over that last weeks, I was also looking more at tail and tail index estimation, but didn't write any code there, e.g. the older methods for estimating a power tail index.

    #5094
    We don't have any continuous non-gaussian distributions outside of GLM (gamma, inverse gaussian, ...)
    We have standard non-gaussian MLE for count models in discrete.
    We don't have any MLE models yet, where two distribution parameters can depend on explanatory variables.

    to the multivariate case
    For the copula PR #7408 I use multivariate genextreme as test case for the copula distribution class, unit tested to match R evd package. That PR does not yet include fitting distribution parameters.

  8. pth91 commented on Jun 20, 2021

    @pth91
    Author

    Okay, it makes way more sense to implement the standardization/normalization of the input independent of the underlying distribution, I did not think of that. Then I will take a look at the estimation and guess I will start to work on that. It is taking me some time to get into the code as I do not want to implement anything and reinvent the wheel, so to speak.

  9. pth91 commented on Nov 19, 2023

    @pth91
    Author

    Hi @josef-pkt,

    although it has been a long time, since our discussion. As far as I can see, there is no extreme value theory added to statsmodels yet?

    If so, I would (finally) try to start working on it in the next few weeks.

  10. josef-pkt commented on Nov 19, 2023

    @josef-pkt
    Member

    Hi @pth91

    Still no code for it in statsmodels.
    I was looking at it on and off, but did not do any actual work.
    I remember looking at some abandoned packages that would be a good starting point for the basic EVT, but don't remember details and I don't find a recent comment in issues.

    (What I remember seeing that would be useful to start with is extracting POT and block-maxima data from a pandas dataframe of timeseries data. Those could then be used by functions that compute EV statistics and estimate the distribution parameters.)

  11. pth91 commented on Nov 19, 2023

    @pth91
    Author

    Hi @josef-pkt,

    I was searching for packages for EVA and found this (pretty surely maintained) package.

    https://github.com/georgebv/pyextremes

    As I am not into the statsmodels "policies": is a "duplication" of existing functionalities in other modules part of statsmodel's scope?

    If not - alternatively - I found this Issue from two weeks ago:
    #9051

    I was thinking about taking a closer look at panel VAR and may be able to contribute something for that issue.

  12. josef-pkt commented on Nov 19, 2023

    @josef-pkt
    Member

    That's it. I was looking at pyextremes in Aug. 31 according to my browsing history. I had looked at all the main python modules of it.

    In general, I avoid duplicating other actively developed python packages.
    However, I don't mind overlap if there is a clear advantage to integrating it into statsmodels, and we can either add additional or other features or take advantage of reusing our existing code.
    example georgebv/pyextremes#30
    Large part of what I was looking at for EVT in statsmodels was related to regression model for EV distributions, and multivariate cases that take advantage of our EV copula models.

    (similar issue with complex survey data analysis.
    An existing python package covers the basics very well, but it would be an additional feature to integrate methods for complex survey analysis into our models. I guess implementing it would not be very difficult, but I don't have the necessary background to decide what we should or need to implement.)

    So, I think we could overlap in the basics with pyextremes, but then focus more on what statsmodels can add that's not in pyextremes.

    panel VAR is a difficult issue. We don't have a real maintainer for it. And I'm not familiar with it and not interested enough to spend a large amount of time to read up on it.
    However, what would be more interesting is seemingly unrelated regression SUR (overlapping with linearmodels) and VAR without restricted parameters, i.e. VAR when some lag coefficients are set to zero.
    Adding SUR would be more related to extending multivariate models, e.g. my PR #8919 (MultivariateLS is limited to having the same exog for all endog, similar to VAR.

  13. ByteMasterMike commented on Nov 12, 2026

    @ByteMasterMike
    Contributor

    @pth91, are you still planning the GPD MLE you mentioned in 2021, or do you have code I could build on? I'd rather collaborate than duplicate it. I work on wildfire loss modeling, where the tail drives pricing, so I'd like to help get this into statsmodels.

    @josef-pkt, my proposed first PR is a GPD model in statsmodels.extremes that subclasses GenericLikelihoodModel. Exceedances are endog, optional exog enters through log(scale), shape is constant, standard errors come from the Hessian, and summary() works like other statsmodels MLE models. Tests compare against hardcoded reference values, and no code comes from evd/extRemes. A simulated check recovers the parameters with roughly nominal CI coverage. I'd start with this instead of extraction because pyextremes already handles extraction and plain fits but has no covariates or likelihood standard errors, and this model covers the POT/genpareto item on #7472. Threshold selection, covariates on shape, GEV, and extraction stay out of this PR.

    If this sounds right, I'll open a draft PR with the model and tests.

    AI disclosure: I used Claude to research pyextremes and run the simulation check. I wrote this comment myself.

  14. pth91 commented on Nov 14, 2026

    @pth91
    Author

    @ByteMasterMike, I started twice and had to stop working on it because I do not have the time which I could dedicate to this. Good luck.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions