Skip to content

ENH Kernel covariance - #4726

Merged
kshedden merged 6 commits into
statsmodels:masterfrom
kshedden:kernel_cov
Jun 28, 2018
Merged

kshedden merged 6 commits into
statsmodels:masterfrom
kshedden:kernel_cov

Conversation

@kshedden

@kshedden kshedden commented Jun 7, 2018

Copy link
Copy Markdown
Contributor

This implements a simple method of moments estimator of a spatial covariance function for multivariate data, using the approach discussed on page 7 of this paper:

Genton M, W Kleiber (2015). Cross covariance functions for multivariate geostatics. Statistical Science 30(2). https://arxiv.org/pdf/1507.08017.pdf

@kshedden kshedden changed the title Kernel covariance ENH Kernel covariance Jun 7, 2018
@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 1 alert when merging dcc7e6d into 8928f8d - view on LGTM.com

new alerts:

  • 1 for Signature mismatch in overriding method

Comment posted by LGTM.com

Comment thread statsmodels/stats/correlation_tools.py Outdated
return np.exp(-(x - loc)**2 / (2 * self.bw2)).sum(1) / self.bwk


def kernel_covariance(exog, loc, group, kernel=None, bw=None):

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.

we use plural groups

@josef-pkt

Copy link
Copy Markdown
Member

For now, I have mainly a question about the use case for this and related PRs:
Do you have the within group number of observations small enough to use a dense covariance matrix?
My impression from the outside was that many spatial applications have to move to using sparse matrices to handle the number of spatial or within group observations.

@kshedden

kshedden commented Jun 7, 2018 •

Copy link
Copy Markdown
Contributor Author

Regarding sparse versus dense, this returns a function cov(x, y) that computes the cross-covariance matrix between Z(x) and Z(y), where Z(.) is a process on R^p (possibly with repeated independent observations). The dimension of Z(.) will generally be small (e.g. for human subject studies, height, weight, blood pressure at age x). cov(x, y) in this case would return a 3x3 matrix, which is a covariance matrix if x=y and a cross-covariance matrix otherwise. The underlying covariance function is infinite dimensional but we only ever evaluate it at one point.

This could be used for spatial data, but it is also useful for longitudinal or similarly-indexed data with many repeated observations, e.g. growth curves on individuals. Since the observations can be made at arbitrary time points, and there can be different numbers of them per subject, it doesn't always work well with MixedLM and GEE.

The spatial application would normally not have repeated observations. You could set groups to the same value for all rows of exog and this should still work, at least from a mechanical point of view. But statistically, you would need fairly dense observations to get a useful estimate of the covariance.

When there are replicates, any one subject can have just a few observations, because the underlying infinite-dimensional covariance function is shared by everyone, and we can pool data to gain power.

Comparing to ProcessRegression, this uses kernels rather than a parametric model for the covariance, and does not estimate the mean and covariance parameters jointly (here you would have to estimate the mean first and subtract it out). The advantage of this approach is that it might be more flexible than the parametric approaches, and it can handle multivariate observations (e.g. joint model of temperature and humidity) rather than just one variable at a time.

@josef-pkt

Copy link
Copy Markdown
Member

Thanks for the application.
So the basic structure is similar to GEE, except for the difference in estimators, and more "dimensions".

I was thinking of spatial panel data like what pysal handles, i.e. each group is a spatial cross section at a point or period in time, and we have many time periods.

@josef-pkt

Copy link
Copy Markdown
Member

aside: I added topic-covtype as additional label
We are currently missing a cov_type for spatial correlation that uses a kernel for averaging. This sounds a bit similar. Currently, we have kernels in HAC but that is only for a single dimension, i.e. time. An extension that I thought about but didn't implement is to use the kernel on a multivariate dimension, e.g. applied to some spatial distance measure.
The idea is similar to cluster robust standard errors, but with kernel averaging when the clusters are too large to do unweighted averaging. Also In one dimension like HAC, we currently assume equal spacing for the kernel averaging.)

@kshedden

kshedden commented Jun 7, 2018

Copy link
Copy Markdown
Contributor Author

One use-case is imputation, taking values that are observed at arbitrary time points for different individuals, then imputing values on a common grid of time points for additional analyses. Hence #4722 to make the covariance modeling and imputation work together a bit better.

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 2 alerts when merging 0bfcee2 into 8928f8d - view on LGTM.com

new alerts:

  • 1 for Signature mismatch in overriding method
  • 1 for First argument of a method is not named 'self'

Comment posted by LGTM.com

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 83.581% when pulling 0bfcee2 on kshedden:kernel_cov into 8928f8d on statsmodels:master.

@coveralls

coveralls commented Jun 23, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 83.552% when pulling d0b0cd1 on kshedden:kernel_cov into 4c601f6 on statsmodels:master.

@codecov-io

codecov-io commented Jun 23, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4726 into master will increase coverage by <.01%.
The diff coverage is 87.82%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4726      +/-   ##
==========================================
+ Coverage   81.08%   81.09%   +<.01%     
==========================================
  Files         579      580       +1     
  Lines       89521    89625     +104     
  Branches     9975     9992      +17     
==========================================
+ Hits        72592    72682      +90     
- Misses      14654    14664      +10     
- Partials     2275     2279       +4
Impacted Files Coverage Δ
statsmodels/stats/correlation_tools.py 90.27% <82.43%> (-3.06%) ⬇️
statsmodels/stats/tests/test_correlation.py 97.56% <97.56%> (ø)
statsmodels/stats/descriptivestats.py 24.13% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c601f6...d0b0cd1. Read the comment docs.

j2 = ii[j2.flat]
w = kx[j1] * ky[j2]

cm += np.einsum("ij,ik,i->jk", exog[j1, :], exog[j2, :], w)

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.

in GLM according to timing by @thequackdaddy using something like the following was faster than einsum, but it was for a matrix version (GLM.hessian IIRC)
(exog[j1, :] * w).dot(exog[j2, :])`

@josef-pkt

Copy link
Copy Markdown
Member

I think this is fine, and you can merge when ready.

I read through most of Genton, Kleiber to get a basic idea (but not more than that).

I think the current location in correlation_tools is fine because it returns a (nested) helper function instead of doing some full covariance computation.
We can move it if needed if or when we get some more general support for computing and using this kind of covariance and correlation matrices/functions. (I have no clear idea yet where to put all the correlation and covariance related function for general usage, stats, multivariate, robust, nonparametric, ...)

@josef-pkt

Copy link
Copy Markdown
Member

This pull request introduces 2 alerts when merging 658ca7f into 8928f8d - view on LGTM.com

new alerts:

  • 1 for Signature mismatch in overriding method
  • 1 for First argument of a method is not named 'self'

Comment posted by LGTM.com

@kshedden
kshedden merged commit 8f8376b into statsmodels:master Jun 28, 2018
@josef-pkt josef-pkt added this to the 0.10 milestone Sep 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants