def test_regress(): """Test regression.""" # Simple regression example, no weights # fit random walk y = np.cumsum(np.random.randn(1000, 1), axis=0) x = np.arange(1000)[:, None] x = np.hstack([x, x**2, x**3]) [b, z] = regress(y, x) # Simple regression example, with weights y = np.cumsum(np.random.randn(1000, 1), axis=0) w = np.random.rand(*y.shape) [b, z] = regress(y, x, w) # Downweight 1st half of the data y = np.cumsum(np.random.randn(1000, 1), axis=0) + 1000 w = np.ones(y.shape[0]) w[:500] = 0 [b, z] = regress(y, x, w) # # Multichannel regression y = np.cumsum(np.random.randn(1000, 2), axis=0) w = np.ones(y.shape[0]) [b, z] = regress(y, x, w) assert z.shape == (1000, 2) assert b.shape == (2, 1) # Multichannel regression y = np.cumsum(np.random.randn(1000, 2), axis=0) w = np.ones(y.shape) w[:, 1] == .8 [b, z] = regress(y, x, w) assert z.shape == (1000, 2) assert b.shape == (2, 3)
import config # plotting utils np.random.seed(9) ############################################################################### # Regression # ============================================================================= ############################################################################### # Simple regression example, no weights # ----------------------------------------------------------------------------- # We first try to fit a simple random walk process. x = np.cumsum(np.random.randn(1000, 1), axis=0) r = np.arange(1000)[:, None] r = np.hstack([r, r**2, r**3]) b, y = regress(x, r) plt.figure(1) plt.plot(x, label='data') plt.plot(y, label='fit') plt.title('No weights') plt.legend() plt.show() ############################################################################### # Downweight 1st half of the data # ----------------------------------------------------------------------------- # We can also use weights for each time sample. Here we explicitly restrict the # fit to the second half of the data by setting weights to zero for the first # 500 samples. x = np.cumsum(np.random.randn(1000, 1), axis=0) + 1000