Skip to content

Nonparametric density - #408

Closed
gpanterov wants to merge 79 commits into
statsmodels:masterfrom
gpanterov:nonparametric-density
Closed

gpanterov wants to merge 79 commits into
statsmodels:masterfrom
gpanterov:nonparametric-density

Conversation

@gpanterov

Copy link
Copy Markdown
Contributor

Nonparametric Density estimators. Conditional and Unconditional multivariable mixed data estimation available with plug-in (normal-reference) and data-driven bandwidth selection methods (cross-validation least squares and cross-validation maximum likelihood).

gpanterov and others added 30 commits May 26, 2012 05:18
Doctests can be run with:

    from statsmodels import nonparametric
    nonparametric.test(doctests=True)

Or "nosetests --with-doctest" in the nonparametric dir.

Note that some other doctests seem to be failing at the moment.
CKDE method still to do.  Also clean up some docstrings and code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the definition in the docstring and ref [1], the abs calls in the two lines above are incorrect. It's abs(Xi-x), not abs(abs(Xi) - abs(x)). Removing them in my speedup branch.

@josef-pkt

Copy link
Copy Markdown
Member

I still don't really like one letter names in the classes. Users can use the formula interface if they don't want to see endog/exog.

5c987e0#diff-1

@rgommers

Copy link
Copy Markdown
Member

Josef, given the number of people being in favor for X/Y (both on-list and in Skippers tutorials) and virtually no one except you liking endog/exog, I really hope you'll reconsider your opinion on this.

That issue is orthogonal to this PR though. So I propose to not discuss it further here, and if the status quo is still endog/exog at merge time of this PR, we should use it in this code.

@rgommers

Copy link
Copy Markdown
Member

@gpanterov: can you comment on the renaming proposals above? I'd like to get those out of the way, then we're pretty much done with this PR.

@rgommers

Copy link
Copy Markdown
Member

Time of nonparametric.test() is down to 16 seconds, nonparametric.test('full') to 80 seconds on my machine. So this is getting more or less acceptable for merging I think. There's more that can be shaved off still, but it's OK-ish now I think.

@gpanterov

Copy link
Copy Markdown
Contributor Author

On the naming conventions:
tydat, txdat, eydat and exdat were borrowed from R's np package.
I like the idea of changing them so that they are more in line with statsmodels naming conventions. I like the brevity of X and Y and if this is more in line with the rest of statsmodels I propose that we go ahead with this. However, we should also consider renaming the arguments of the KDE.cdf() and COnditionalKDE.cdf() and pdf() which are currenty exdat and eydat (e standing for evaluating as opposed to training data (t) ). I propose eX and eY ?

@josef-pkt

Copy link
Copy Markdown
Member

eX, eY is still uninformative, I never managed to guess what the e stands for.

our terminology in general is fit and predict

shorthand names can sometimes be useful inside a function, but for the outside I prefer descriptive names.

we don't have exdat yet in other models, just exog in predict. The full name would be :)
exog_predict

@rgommers

Copy link
Copy Markdown
Member

exog_predict is pretty uninformative too imho. Note that the already existing kdensity and kdensity_fft do use X already:

def kdensity(X, kernel="gau", bw="scott", weights=None, gridsize=None,
             adjust=1, clip=(-np.inf,np.inf), cut=3, retgrid=True):
    """
    Rosenblatz-Parzen univariate kernel desnity estimator

    Parameters
    ----------
    X : array-like
        The variable for which the density estimate is desired.

I see a bunch of predict methods, but no parameters with _predict appended.

Using fit() for kicking off bandwidth estimation is a good point though.

@josef-pkt

Copy link
Copy Markdown
Member

No, we don't have _predict postfix, because we never needed it.
standard signature is just predict(exog) since none of the other models needs to keep the original exog from the fit around at the same time. (predict signature will get more complicated with formulas)

Kernel methods are currently the only ones where we don't just have "parameters", and where we need the full original sample for "prediction".

For density estimation there is no real endog and exog, just the data, so I don't really care much. Similar, I usually don't use the endog/exog terminology in statistical tests.
But kernel regression is similar to the other model in that it estimates a relationship between endog and exog under some assumptions on the process that generated the data.

@rgommers

Copy link
Copy Markdown
Member

OK, all the renames done in my speedup-nonparametric branch. I included exog_predict and even exog_nonparametric_predict, because I realised the most confusing part of exog_predict is not _predict. So why not, at least it's consistent.

Adding fit presents a slight problem, since the regression classes already have a fit method (for doing the actual regression and marginal effects). If fit() would be used for computing the bandwidth (still don't see a good use-case for why that's needed), what to call the current fit()?

@josef-pkt

Copy link
Copy Markdown
Member

Ralf, thanks for working through this
Can you create a pull request from your branch, so we can review your version?

The way it sounds like, I think we should merge it soon.

@gpanterov

Copy link
Copy Markdown
Contributor Author

Can we indicate somewhere that the CensoredReg, SemiLinear and SingleIndexModel classes are experimental? Or should we exclude them from the PR ? They are a unique feature of statsmodels i.e. they are not present in any other package to my knowledge. But because of this, I was not able to cross-check the output that they give.

@josef-pkt

Copy link
Copy Markdown
Member

I would prefer to merge everything instead of splitting up the pull request again.

If there are some tests that they work, then I would just add a comment to the docstrings and leave them in, even if they are not verified against other packages.
If they are unfinished or might have problems, then we could also just move them temporarily to the sandbox.

(I haven't looked at the code for those yet.)

@rgommers

Copy link
Copy Markdown
Member

Test coverage should be increased first though if we want to merge everything at once:

  • TestFForm and SingleIndexModel are completely untested
  • SemiLinear and CensoredReg have only one test.

I propose to rename TestFForm to FFormTest by the way. No name should start with Test.

George, do you have time to work on those tests in the near future? If not, I propose to leave those things out for now.

@rgommers

Copy link
Copy Markdown
Member

What do we do about current KDE class by the way? Rename to KDE_1d?

@jseabold

Copy link
Copy Markdown
Member

Is there a name clash? Is something called KDE in the PR? If so, I think it should be renamed given that KDE is in use already. If not, I'm ok to change KDE to KDE_1d (or whatever), but we'd have to go through a deprecation period for the current KDE.

@rgommers

Copy link
Copy Markdown
Member

The main class is called KDE (renamed from UKDE). Josef's suggestion from the mailing list:

I would spell it out ConditionalKDE,  or KDEConditional,
I think shortening UKDE to KDE is fine, if there is no problem with
several KDE, eg. rename the other one to KDEUnivariate or UnivariateKDE.

@jseabold

Copy link
Copy Markdown
Member

I think at least initially we would have to have KDEMultivariate or MultivariateKDE, then we can deprecate KDE and move to this if it's what we really want to do unless there are some plans to separate by namespace (still would be a bit confusing IMO).

@rgommers

Copy link
Copy Markdown
Member

Hmm, not sure what would be best. Basically, the current KDE is a specialization for 1-D continuous data. It's faster and has support for data on a finite domain. The new KDE can handle mixed continuous/ordered/unordered data and has cross-validated bandwidth selection. We should be able to add finite domain support to it (longer term, non-trivial). So it looks to me like KDE should become KDE_1d or UnivariateKDE. However, given the current state that's indeed not very practical. Alternative is to rename the new KDE and explain how the classes are related in the two docstrings.

Splitting the two between two namespaces doesn't make sense.

@josef-pkt

Copy link
Copy Markdown
Member

I don't think we want to separate by namespace, too confusing and there are no natural namespaces withing kde.

One possibility would be to keep all the KDE with qualifiers.

The KDE in this PR is using product kernels, so we could also call it KDEProduct to distinguish from scipy gaussian_kde which I think doesn't impose the product structure. KDEMutivariate is also fine (I'm not sure users care about "Product")

(other multivariate: nearest neighbor, rbf kernels ?)

@rgommers

Copy link
Copy Markdown
Member

Users indeed won't care much. Multivariate sounds better than Product.

@jseabold

Copy link
Copy Markdown
Member

+1

@josef-pkt

Copy link
Copy Markdown
Member

unless the untested parts will get tests soon, I prefer moving them to the sandbox.
requires less maintenance, avoids bitrot, and we don't have to struggle with git merges.
the only disadvantage is that it's easier to add github comments to a PR.

And it can be used even if it doesn't have a full test suite, and we can add tests incrementally.

(example: We got a bug report for survival2 which is in the sandbox, but the code in the sandbox is outdated and no one has worked on the new version the branch in 7 months.
Would have been easier to merge and add test coverage piece by piece.)

@josef-pkt

Copy link
Copy Markdown
Member

Ralf can you make a pull request from your branch

This branch doesn't contain the extra classes SingleIndexModel, Censored, ...

Also I would suggest splitting the module nonparametric2.py into at least two or three parts, density, regression and others? and rename nonparametric2 to "kernel_density.py" or something like that.

I only had a brief look at the extras. One of the main parts that might need expansion (later?) is to check what should be returned by fit, there are currently no results classes.

@rgommers

Copy link
Copy Markdown
Member

Yes, will get the last renames done and send a PR this week.

@gpanterov I'll follow Josef's suggestion on moving things that aren't tested to the sandbox; they can always be moved back as soon as we have tests.

@gpanterov

Copy link
Copy Markdown
Contributor Author

Please go ahead and move them to the sandbox Ralf. I was thinking along the same lines. I will have time to write some tests for them in the next few weeks. But I can't do it immediately ...

@rgommers

rgommers commented Nov 3, 2012

Copy link
Copy Markdown
Member

New PR opened, so closing this one.

@rgommers rgommers closed this Nov 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants