You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
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.
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.
@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...
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
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.
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?!
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.
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.
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.
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.)
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.
@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.
Activity
josef-pkt commentedon Feb 10, 2021
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
evdpackage 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
distributionsDo you have a list of methods that you want to work on, or an outline of what we should add?
pth91 commentedon Feb 10, 2021
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:
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.
pth91 commentedon Feb 27, 2021
@josef-pkt Hi Josef, I had a closer look at the
evdpackage. 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 beevdin 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
josef-pkt commentedon Feb 27, 2021
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.datasetsbut there might not be one that applies to EV. Newdatasetscan 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/resultsfolders.you can look at current cases like
statsmodels\stats\tests\resultsfor 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_allclosefrom numpy.testing with rtol and atol specific to the case.If we call the new directory
extremes, then we would havestatsmodels.extremes.testsandstatsmodels.extremes.tests.resultsfor the unit tests.I'm not sure
extremesis the best name for us, but we should avoid acronymsjosef-pkt commentedon Feb 27, 2021
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.
pth91 commentedon Jun 20, 2021
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
evdpackage. 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?!
josef-pkt commentedon Jun 20, 2021
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
nloglikejust the regularloglike#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
evdpackage. That PR does not yet include fitting distribution parameters.pth91 commentedon Jun 20, 2021
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.
pth91 commentedon Nov 19, 2023
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.
josef-pkt commentedon Nov 19, 2023
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.)
pth91 commentedon Nov 19, 2023
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.
josef-pkt commentedon Nov 19, 2023
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.
ByteMasterMike commentedon Nov 12, 2026
@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.extremesthat subclassesGenericLikelihoodModel. Exceedances areendog, optionalexogenters throughlog(scale), shape is constant, standard errors come from the Hessian, andsummary()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.GenericLikelihoodModelnow, or build against the refactor in SUMM/REF/ENH: GenericLikelihoodModel needs updating #4679?statsmodels.extremeswork, or would you like a different name?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.
pth91 commentedon Nov 14, 2026
@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.