Skip to content

WIP ENH: add binned local polynomial regression - #3492

Open
josef-pkt wants to merge 3 commits into
statsmodels:mainfrom
josef-pkt:nonparametric_binned
Open

josef-pkt wants to merge 3 commits into
statsmodels:mainfrom
josef-pkt:nonparametric_binned

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Feb 19, 2017 •

Copy link
Copy Markdown
Member

this implements binning for local constant and local linear kernel regression following Fan and Marron 1994

It also adds a simple cross-validation with leave k-th bin out to choose the bandwidth
This undersmooths in small samples.

speed is very good, but there still might be a little bit of slack.
see https://groups.google.com/d/msg/pystatsmodels/KVmXLAJfMxU/lUaoV51oCwAJ

It works, but

  • API not settled
  • options not tested
  • needs better bandwidth choice in smaller samples, e.g. options for cross-validation setting, eg. evaluate at more points.
    Maybe replace MSE with something less quadratic, to not be influence so much by large residuals.
  • boundary handling
    If the half window is smaller than the number of explanatory variables, then the design matrix for the local regression is singular at the boundaries. I think this is the zero division warning when the window length was too small in the cross-validation, i.e. minimum window length is 3 for linear.
  • empty bins
    if the window length is small and there are empty bins, then the design matrix could also be singular if we don't have enough grid points with non zero weights.
  • leave k-th out cross validation related to empty bins: Should we require k > window_length ?

This uses only convolution. Additional results like effective degrees of freedom are not available. (that's why I used cross-validation for choosing window length given that the algorithm is fast)
Auxiliary quantities for binned observations in Turlach and Wand 1996 use the local regression representation and I have not tried to derive those for a convolution version. (diagonal of hat/smoother matrix for local constant looks ok, but for local linear regression it requires that we solve analytically a partitioned inverse, which I didn't try.
update see #3493 for follow-up idea of combining convolution and linalg for regression, which should solve this issue because we can use the linear algebra version with x.T W x directly, which means we can also calculate cov_params (x.T W x)**(-1) and some assumption, like homoscedasticity, on the error variance.

As byproduct of cross-validation we get a out-of-sample (leave many bins out) estimate of the mean squared prediction error MSE). But it's not used yet outside of cross-validation.

My plan is to merge this as basic smoother, and leave additional bandwidth selection methods and results statistic for the future, when I have again a week or three available.
I also haven't looked yet for references for a convolution version for local polynomial regression with order higher than linear.

This is a companion to #3380 which focuses on variance estimation with almost equal spaced regressor and nonparametric mean function.
One difference to #3380 and to Savitzky-Golay is that in this PR we handle frequency weights coming from the binning.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.008%) to 90.289% when pulling 4ecdd47 on josef-pkt:nonparametric_binned into 18983ea on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

fails on 2 older numpy

ERROR: statsmodels.nonparametric.tests.test_binned_regression.test_binned_regression_ex1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python2.7/site-packages/statsmodels-0.8.0-py2.7-linux-x86_64.egg/statsmodels/nonparametric/tests/test_binned_regression.py", line 25, in test_binned_regression_ex1
    yb = binn.bin_data(y)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python2.7/site-packages/statsmodels-0.8.0-py2.7-linux-x86_64.egg/statsmodels/nonparametric/binned_regression.py", line 234, in bin_data
    binned[:-1] += np.bincount(idx, x * (1 - rem), minlength=n_bins - 1)
ValueError: The first argument of bincount must be non-negative

not sure what's going on. idx should be something like
binn.idx
np.array([ 0, 1, 1, ..., 299, 299, 299], dtype=int64)

@josef-pkt

josef-pkt commented Feb 20, 2017 •

Copy link
Copy Markdown
Member Author

It looks like python 2.7 incompatibility, not related to numpy

The division from the future solved it.

@josef-pkt
josef-pkt force-pushed the nonparametric_binned branch from 4ecdd47 to 7b05449 Compare February 20, 2017 00:53
@coveralls

coveralls commented Feb 20, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 90.282% when pulling 7b05449 on josef-pkt:nonparametric_binned into 18983ea on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

runtime warning during cross-validation (I think). I did not investigate, I'm not sure whether and how empty bins are causing it.

statsmodels.nonparametric.tests.test_binned_regression.test_binned_regression_ex
1 ... M:\...\statsmodels\nonpar
ametric\binned_regression.py:196: RuntimeWarning: invalid value encountered in t
rue_divide
  m1 = (s2 * t0 - s1 * t1) / (s2 * s0 - s1**2)
M:\...\statsmodels\nonparametri
c\binned_regression.py:196: RuntimeWarning: divide by zero encountered in true_d
ivide
  m1 = (s2 * t0 - s1 * t1) / (s2 * s0 - s1**2)

@josef-pkt

Copy link
Copy Markdown
Member Author

last commit adjust the cross-validation window length choice in a semi ad-hoc way.

  • given the reasonable speed window length search goes to 50 by default
  • if n_bins is small, the we use full leave k-th out cross-validation, i.e. full set of start, this increases the number of out of sample predictions for mse
  • at least some options now to influence bandwidth and cross-validation choice. (Note this is supposed to be the convenience function, users can always go back and use individual functions or classes and options.)

We bias the choice of window length: Because cross-validation undersmooths in all the examples with smaller sample sizes, we increase the bandwidth up to an acceptable loss in terms of MSE, given by shift_loss. By default this is now 10 % of acceptable loss in MSE.

(shift_loss is an awful name, but I couldn't think of anything short and more precise)

@coveralls

coveralls commented Feb 20, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 90.282% when pulling a9ca7cf on josef-pkt:nonparametric_binned into 18983ea on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

my script files based on modification date of files

"\scripts\try_nonparametric_binned.py"
"\scripts\try_kernel_least_squares.py"
"\scripts\ex_nonparametric_binned2.py"
"\scripts\ex_nonparametric_binned.py"
plots look nice
I did not use notebooks at the time, AFAICS

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.

2 participants