Repository navigation
Conversation
|
Hello @tupui! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2021-04-07 18:23:37 UTC |
bdfad23 to
b03a4b2
Compare
b03a4b2 to
6e65144
Compare
|
|
| self.rng = np.random.default_rng(seed) | ||
|
|
||
| @abstractmethod | ||
| def random(self, n): |
There was a problem hiding this comment.
Needs to accept an argument random_state that can handle a random_state or BitGenerator object.
There was a problem hiding this comment.
This is like what scipy does.
Seed shouldn't be part of the class initialization.
There was a problem hiding this comment.
ok sure. I was not sure about the architecture here. If you think something else would be better in general let me know.
There was a problem hiding this comment.
For the accepted values, I propose to not talk about RandomState as we do in stats.qmc to strive people to not use it. Let me know if I should do otherwise.
There was a problem hiding this comment.
It seems fine to me to prefer Generator objects. The problem is that a RandomState has to be default until statsmodels requires NumPy 1.17.
There was a problem hiding this comment.
Is there somewhere a function to check the random state? Something like scipy._lib._util.check_random_state. I could not find anything like this. I will use this for now.
|
|
||
|
|
||
| class StudentCopula(Copula): | ||
| """Student copula.""" |
There was a problem hiding this comment.
docstrings should have a reasonable amount of mathematicaal detail similar to what scipy does.
There was a problem hiding this comment.
ok I will see to add at least the definition of C. But I am not a mathematician and I would just copy paste. So if you know better, it's better if you do it.
There was a problem hiding this comment.
ok I will see to add at least the definition of C. But I am not a mathematician and I would just copy paste. So if you know better, it's better if you do it.
Copy-paste from Wiki is a good start.
|
|
This pull request introduces 2 alerts when merging 7dba6ff into 56838f2 - view on LGTM.com new alerts:
|
|
@bashtage I added |
| return self.density.cdf(x) | ||
|
|
||
| def pdf(self, x): | ||
| return self.mv_density.pdf(x) |
There was a problem hiding this comment.
This is not correct, we need the copula pdf and not the pdf of the multivariate t distribution.
I guess we can implement logpdf (loglike) and get pdf through exp(logpdf)
|
@tupui you need to add references about from where you took the formulas for individual copula pdf, cdf |
Of course sure, I will do this next time. I will soon push a fit function for theta. Still thinking a bit more about the interface before proposing you something. |
|
I just found your references in the Copula class. I will start with the Copula hierarchy, see #7257 now that I have the generator functions fixed and verified. The archimedean copulas will subclass the sandbox your plot functions for 2-dim. archimedean and elliptical can be multivariate, but I haven't looked at explicit, non-generated cdf and pdf for those. |
|
I added more ref about Seeing all your work on sandbox in the other issue, I guess I will wait for you to merge this and I will rebase onto that. If you want me to proceed differently let me know. |
| return fig | ||
|
|
||
| @property | ||
| def tau(self): |
There was a problem hiding this comment.
tau should be analytical or numerical integration
There was a problem hiding this comment.
I will have to do some research here. TBH my knowledge about all this is all here.
|
did you look at how to use quasi-random numbers for copula sampling? I think in the past I only looked at univariate and multivariate normal quasi-random numbers based on halton sequences, where we only need univariate |
The only thing I've ever used is the Pinging some experts from OpenTURNS who might be interested to help/give a hand: @regislebrun @mbaudin47 |
|
What you need is the conditional quantile function (quantile=ppf). It means, the inverse of the following function: You should have a look here for a possible implementation. It is very easy to implement QMC sampling once you have the conditional quantile, which is not so easy to implement efficiently in a generic way. IMO you should consider to rely on OpenTURNS for this kind of computation, but in democracy everybody is free to reinvent the wheel, as we also did in many parts of OpenTURNS ;-)! |
You know I like the way it's done in OpenTURNS 😉 But the license is not compatible with statsmodels. So if we want to propose something here, it has to be done here (or from an OK license). |
|
it's mainly the last commit, that changed things too much from my pattern |
|
@tupui from where did you get the default values for archimedean theta, e.g. I think there shouldn't be any default values. The removal of args in archimedean and in unit tests can be reversed by amending last commit. |
|
still open question: args tuple versus single args, consistency versus convenience ? specific archimedean classes don't have uniy test coverage, and according to codecov, test coverage for pdf, logpdf in Frank copula would be removed. |
|
@josef-pkt I can add back For the default I took an arbitrary default value. So you want the code to raise if we don't specify |
|
We need arguments in the functions, otherwise it's messy to create a model that use copulas where the copula parameters are estimated. My guess is that copulas are in many cases not the relevant class for user. Copula distributions and copula (regression) models. Named parameters are not generic enough for library use. We need a simple common pattern for args/params, so we can build additional features that do not rely on the parameterization of specific copulas. The details are still open, we don't have enough models to have much experience with many different parameters, except for packing them in E.g. it's more important that we are able to write a multivariate extreme value model based on generic copula interface, than having a userfriendly but inconsistent interface to copulas. We can allow scalar args in the case of copulas with only a single parameter, but the problem if we go away from tuples is unpacking if there are too many options for how to specify the parameters, and interpretation of broadcasting/shape if we have array parameters. |
|
OK then your call. I will re-introduce the TBH I am not a fan of how |
statsmodels is built on dual use, library and interactive, so we have two obvious ways, one for each use case :) (Pandas was for a long time focused on interactive use and a pain in library use. Most complaints about tidy verse are that it's a pain for code development/maintenance and long term use. The basic structure of statsmodels has lasted for over 10 year and still works well.) |
|
possible interface for args: The first part will need to be made more general when only some, but not all parameters of copula are fixed, needed for EV copulas For elliptical copulas we have two possible cases
Using full MLE for elliptical cov will not be coming soon, because there are too many tools missing to estimate patterned cov matrices outside of GEE. |
You mean this is to be used for the methods right? If so I propose to have a common validation function for it so we avoid duplication.
No sure what this means but I will implement the validation part and you can add stuff there.
I don't have this I guess as for now I coded something which needs the matrix. Or I misunderstand what you mean. |
|
to the last point for elliptical cov: about partially fixed params: The asymmetric EV copulas can have two asymmetry and one correlation parameter. When used for the standard symmetric/exchangable case, then the asymmetry parameters can be fixed to some values or restricted so that both asymmetry parameters are the same. So, partial fixing becomes necessary in fitting when we have a smaller number of free parameters. |
|
@josef-pkt I put back We still need to decide what to do about Let me know what you think about all that. |
|
@josef-pkt friendly reminder. Let me know how you want to proceed 😃 |
|
@josef-pkt Shall I close this one in favour of the new PR? |
|
Leave it open for now, we close it if and when the new PR gets merged. I'm trying to write an introductory overview notebook to see how well things work. |
If that can help, I had things like these in a notebook. import seaborn as sns
from statsmodels.sandbox.distributions.copula import *
from statsmodels.distributions.joint_distribution import *
copula = GumbelCopula(2)
sample = copula.random(10000)
# some plot
h = sns.jointplot(sample[:, 0], sample[:, 1], kind='hex')
h.set_axis_labels('X1', 'X2', fontsize=16)
# another one
copula = ClaytonCopula(0.3)
_ = copula.plot_pdf(10)
# and this
dists = [stats.gamma(2), stats.norm]
copula = GaussianCopula(mean=[0, 0], cov=[[1., 0.5],
[0.5, 1.]])
sample = copula.random(10000)
h = sns.jointplot(sample[:, 0], sample[:, 1], kind='hex')
h.set_axis_labels('X1', 'X2', fontsize=16)
joint_dist = JointDistribution(dists, copula)
sample = joint_dist.random(512)
h = sns.jointplot(sample[:, 0], sample[:, 1], kind='scatter')
h.set_axis_labels('X1', 'X2', fontsize=16) |
|
do you have your notebook in a gist or somewhere online? |
I just had this one online: https://gist.github.com/tupui/d1f2aa721b3994e59283059233a50962 |
|
I have mostly notebooks for extreme value copulas from when I was working on those, and bernstein copula. |
|
I will close my PR in favor of #7408 as its now merged. |
This PR's intent is to allow the construction of correlated multivariate distributions.