def test_lstsqjackknifeslow(self): x = np.atleast_2d(np.arange(10)).T y = np.atleast_2d(2 * np.arange(10)).T reg = jk.LstsqJackknifeSlow(x, y, n_blocks=10) regnn = jk.LstsqJackknifeSlow(x, y, n_blocks=10, nn=True) assert_array_almost_equal(reg.est, [[2.]]) assert_array_almost_equal(regnn.est, [[2.]])
def test_eq_slow(self): x = np.atleast_2d(np.random.normal(size=(100, 2))) y = np.atleast_2d(np.random.normal(size=(100, 1))) print(x.shape) for n_blocks in range(2, 49): b1 = jk.LstsqJackknifeFast(x, y, n_blocks=n_blocks).est b2 = jk.LstsqJackknifeSlow(x, y, n_blocks=n_blocks).est assert_array_almost_equal(b1, b2)
def irwls(cls, x, y, update_func, n_blocks, w, slow=False, separators=None): ''' Iteratively re-weighted least squares (IRWLS). Parameters ---------- x : np.matrix with shape (n, p) Independent variable. y : np.matrix with shape (n, 1) Dependent variable. update_func: function Transforms output of np.linalg.lstsq to new weights. n_blocks : int Number of jackknife blocks (for estimating SE via block jackknife). w : np.matrix with shape (n, 1) Initial regression weights. slow : bool Use slow block jackknife? (Mostly for testing) separators : list or None Block jackknife block boundaries (optional). Returns ------- jknife : jk.LstsqJackknifeFast Block jackknife regression with the final IRWLS weights. ''' (n, p) = x.shape if y.shape != (n, 1): raise ValueError( 'y has shape {S}. y must have shape ({N}, 1).'.format( S=y.shape, N=n)) if w.shape != (n, 1): raise ValueError( 'w has shape {S}. w must have shape ({N}, 1).'.format( S=w.shape, N=n)) w = np.sqrt(w) for i in range(2): # update this later new_w = np.sqrt(update_func(cls.wls(x, y, w))) if new_w.shape != w.shape: print('IRWLS update:', new_w.shape, w.shape) raise ValueError('New weights must have same shape.') else: w = new_w x = cls._weight(x, w) y = cls._weight(y, w) if slow: jknife = jk.LstsqJackknifeSlow(x, y, n_blocks, separators=separators) else: jknife = jk.LstsqJackknifeFast(x, y, n_blocks, separators=separators) return jknife