def test_same_result_with_offset_index(): xx = np.arange(20, dtype=np.int64) y = 3 * xx + 9 sol1 = l1_fit(xx, y) xfit1 = sol1["x"] xx = xx + 999 sol2 = l1_fit(xx, y) xfit2 = sol2["x"] assert (xfit1 == xfit2).all()
def test_gaps_work_on_line(): xx = np.arange(20, dtype=np.int64) y = 3 * xx + 9 sol = l1_fit(xx, y) xfit = sol["x"] # this one has a big gap xx = np.array(range(7) + range(15, 20)) y = 3 * xx + 9 sol = l1_fit(xx, y) xfit2 = sol["x"] for i, j in enumerate(xx): diff = abs(xfit[j] - xfit2[i]) assert diff < 1e-6
def test_l1_fit_rand_with_permissive( beta_d2=4.0, beta_d1=1.0, beta_seasonal=1.0, beta_step=2.5, period=12, noise=0, seed=3733, doplot=True, sea_amp=0.05 ): # print "seed=%s,noise=%s,beta_d2=%s,beta_d1=%s,beta_step=%s," \ # "beta_seasonal=%s" % (seed,noise,beta_d2,beta_d1,beta_step,beta_seasonal) mock = make_l1tf_mock2(noise=noise, seed=seed, sea_amp=sea_amp) y = mock["y_with_seasonal"] xx = mock["x"] step_permissives = [(30, 0.5)] sol = l1_fit( xx, y, beta_d2=beta_d2, beta_d1=beta_d1, beta_seasonal=beta_seasonal, beta_step=beta_step, period=period, step_permissives=step_permissives, ) if doplot: plt.clf() plt.plot(xx, y, linestyle="-", marker="o", markersize=4) plt.plot(xx, sol["xbase"], label="base") plt.plot(xx, sol["steps"], label="steps") plt.plot(xx, sol["seas"], label="seasonal") plt.plot(xx, sol["x"], label="full") plt.legend(loc="upper left")
def test_l1_fit_monthly(): tol = 1e-12 xx = np.arange(4, dtype=np.int64) y = 3 * xx + 9 sol1 = l1_fit(xx, y) xfit1 = sol1["x"] xx = np.array([date(2014, 1, 2), date(2014, 2, 5), date(2014, 3, 17), date(2014, 4, 1)]) sol2 = l1_fit_monthly(xx, y) xfit2 = sol2["x"] diff = abs(xfit1 - xfit2).max() assert diff < tol
def test_l1_fit_linearity(): mock = make_l1tf_mock2(noise=0.05, seed=48457) y = mock["y_with_seasonal"] xx = mock["x"] step_permissives = [(30, 0.5)] sol = l1_fit(xx, y, period=6, step_permissives=step_permissives) # some linear transformation should effect things only linearly scale = 17.0 offset = -56.0 y = y * scale + offset sol2 = l1_fit(xx, y, period=6, step_permissives=step_permissives) x = sol["x"] x2 = sol2["x"] x_unscaled = (x2 - offset) / scale tol = 1e-10 assert abs(x - x_unscaled).max() < tol
def test_l1_fit(beta_d2=4.0, beta_d1=1.0, beta_seasonal=1.0, beta_step=2.5, period=12, noise=0, seed=3733): mock = make_l1tf_mock2(noise=noise, seed=seed) y = mock["y_with_seasonal"] xx = mock["x"] plt.clf() plt.plot(xx, y, linestyle="-", marker="o", markersize=4) sol = l1_fit( xx, y, beta_d2=beta_d2, beta_d1=beta_d1, beta_seasonal=beta_seasonal, beta_step=beta_step, period=period ) plt.plot(xx, sol["xbase"], label="base") plt.plot(xx, sol["steps"], label="steps") plt.plot(xx, sol["seas"], label="seasonal") plt.plot(xx, sol["x"], label="full") plt.legend(loc="upper left")
def test_l1_fit_step(): # test that the place of the step and the specification of the step # permissive do not have a off-by-one error # note our steps occur from right to left # the first one (right to left) that has jumped is the place # where h is non-zero n = 55 xx = np.arange(n) y = xx * 0.0 y[xx <= 20] = 1000.0 # this means that y[20] includes the step-up and so 20 is where 'h' should be non-zero sol = l1_fit(xx, y, beta_step=100.0, step_permissives=[(20, 0.1)]) print abs(sol["h"][19]) < 1e-10 print abs(sol["h"][21]) < 1e-10 print abs(sol["h"][20] - 1000.0) < 1e-10
def test_l1_fit_speed(beta_d2=4.0, beta_d1=1.0, beta_seasonal=1.0, beta_step=2.5, period=12, n=50, num=100): mock = make_l1tf_mock2() y = mock["y_with_seasonal"] xx = mock["x"] start = time.time() for i in xrange(num): sol = l1_fit( xx[0:n], y[0:n], beta_d2=beta_d2, beta_d1=beta_d1, beta_seasonal=beta_seasonal, beta_step=beta_step, period=period, ) fin = time.time() runtime = fin - start rate = num / runtime print "num: %s, runtime: %s seconds, rate: %s per sec for %s points" % (num, runtime, rate, n) return runtime