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
{{ message }}
Repository navigation
ENH: windowed regression using linalg ufunctions #3493
one part of followup to #3492 binned local polynomial regression
In #3492 I copied the formulas from Fan Marron for local constant and local linear regression.
However, those are just explicitly solving a small linear system of equations for each observations.
We can do this more generally and fast by using the vectorized linear algebra functions.
I.e. use convolution (vectorized over moments - scipy signal instead of numpy ?) to get the moment equations for each observation, then use linalg solve or inv vectorized over observations.
compared to Savitzky-Golay this reverses the order of linalg solve/inv and the convolution. savgol does linalg first and then convolves. If we have weights as in the binned local linear regression, then we cannot convolve the linalg solution because it is observation or freq_weight specific.
difference to explicit solution in 3492 is that there we only solve for one of the parameters and don't need the full solution.
simple windowed/kernel regression with original exog
windowed/kernel regression with local exog, i.e exog for observation i is vander(x_j - x_i) for j in window
#3380 contains a brute force loop method for 1., a quick trial code for vectorized linalg below
#3492 uses 2., In the general version we have a 2-dimensional-vectorized window weights kernel(dx) * vander(dx) that needs to be convoluted with count and with endog
a quick version with convolution of the moment matrices (without proper boundary handling where moment matrix x.T W x might be singular.)
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 21 17:04:25 2017
Author: Josef Perktold
"""
import numpy as np
from numpy.testing import assert_allclose
x = np.random.randn(10, 3)
xtx = x[:,None, :] * x[:, :, None]
y = np.ones(10)
xty = x * y[:, None]
xtxc = np.cumsum(xtx, axis=0)
xtyc = np.cumsum(xty, axis=0)
r = 1e-12 * np.eye(3) # regularize for invalid/singular edge boundary cases
p = np.linalg.solve(xtxc + r, xtyc)
p1 = np.asarray([np.linalg.solve(x[:k].T.dot(x[:k]) + r, x[:k].T.dot(y[:k]))
for k in range(4, x.shape[0]+1)])
assert_allclose(p[3:], p1, rtol=1e-13)
from scipy.signal import convolve # works vectorized
mode = 'valid'
xtxw = convolve(xtx, np.array([1,1,1,1])[:, None, None], mode=mode)
xtyw = convolve(xty, np.array([1,1,1,1])[:, None], mode=mode)
pw = np.linalg.solve(xtxw + r, xtyw)
p1w = np.asarray([np.linalg.solve(x[k:k+4].T.dot(x[k:k+4]) + r,
x[k:k+4].T.dot(y[k:k+4]))
for k in range(0, x.shape[0]+1 - 4)])
assert_allclose(pw[:], p1w, rtol=1e-13)
Activity
josef-pkt commentedon Feb 22, 2017
Note there are two different versions
vander(x_j - x_i)for j in window#3380 contains a brute force loop method for 1., a quick trial code for vectorized linalg below
#3492 uses 2., In the general version we have a 2-dimensional-vectorized window weights
kernel(dx) * vander(dx)that needs to be convoluted with count and with endoga quick version with convolution of the moment matrices (without proper boundary handling where moment matrix x.T W x might be singular.)