Пример #1
0
 def test_se_no_fit(self):
     my_pwlf_0 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     try:
         my_pwlf_0.standard_errors()
         self.assertTrue(False)
     except AttributeError:
         self.assertTrue(True)
Пример #2
0
 def test_break_point_diff_x0_2(self):
     # check if my duplicate is in a different location
     x0 = self.x_small.copy()
     my_fit4 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x0[1] = 1.1
     ssr4 = my_fit4.fit_with_breaks(x0)
     self.assertTrue(np.isclose(ssr4, 0.0))
Пример #3
0
 def test_pv_no_fit(self):
     my_pwlf_0 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     try:
         my_pwlf_0.prediction_variance(self.x_small)
         self.assertTrue(False)
     except AttributeError:
         self.assertTrue(True)
Пример #4
0
 def test_p(self):
     # check to see if it will let me calculate p-values
     my_pwlf = pwlf.PiecewiseLinFitTF(np.linspace(0, 100, num=100),
                                      np.linspace(0, 100, num=100))
     my_pwlf.fitfast(2)
     my_pwlf.p_values()
     self.assertTrue(True)
Пример #5
0
 def test_break_point_spot_on_r2(self):
     # test r squared value with known solution
     my_fit1 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x0 = self.x_small.copy()
     my_fit1.fit_with_breaks(x0)
     rsq = my_fit1.r_squared()
     self.assertTrue(np.isclose(rsq, 1.0))
Пример #6
0
 def test_r2_no_fit(self):
     my_pwlf_0 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     try:
         my_pwlf_0.r_squared()
         self.assertTrue(False)
     except AttributeError:
         self.assertTrue(True)
Пример #7
0
 def test_se(self):
     # check to see if it will let me calculate standard errors
     my_pwlf = pwlf.PiecewiseLinFitTF(np.linspace(0, 100, num=100),
                                      np.linspace(0, 100, num=100))
     my_pwlf.fitfast(2)
     my_pwlf.standard_errors()
     self.assertTrue(True)
Пример #8
0
 def test_single_force_break_point2(self):
     my_fit = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x_c = [2.0]
     y_c = [1.5]
     my_fit.fit_with_breaks_force_points([0.2, 1.0], x_c, y_c)
     yhat = my_fit.predict(x_c)
     self.assertTrue(np.isclose(y_c, yhat))
Пример #9
0
 def test_predict(self):
     my_pwlf = pwlf.PiecewiseLinFitTF(self.xk, self.yk)
     my_pwlf.fit_with_breaks([0.5])
     x = np.linspace(self.xk.min(), self.xk.max(), 10)
     yHat = my_pwlf.predict(x)
     e = np.sum(np.abs(yHat - self.yk))
     self.assertTrue(np.isclose(e, 0.0))
Пример #10
0
 def test_pvalue_no_fit(self):
     my_pwlf_0 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     try:
         my_pwlf_0.p_values()
         self.assertTrue(False)
     except AttributeError:
         self.assertTrue(True)
Пример #11
0
 def test_fast_false(self):
     # check that I can fit when break points spot on a
     my_fit1 = pwlf.PiecewiseLinFitTF(self.x_small,
                                      self.y_small,
                                      fast=False)
     x0 = self.x_small.copy()
     ssr = my_fit1.fit_with_breaks(x0)
     self.assertTrue(np.isclose(ssr, 0.0))
Пример #12
0
 def test_pv(self):
     # check to see if it will let me calculate prediction variance for
     # random data
     my_pwlf = pwlf.PiecewiseLinFitTF(np.linspace(0, 100, num=100),
                                      np.linspace(0, 100, num=100))
     my_pwlf.fitfast(2)
     my_pwlf.prediction_variance(np.random.random(20))
     self.assertTrue(True)
Пример #13
0
 def test_custom_opt(self):
     my_pwlf = pwlf.PiecewiseLinFitTF(self.xk, self.yk)
     my_pwlf.use_custom_opt(3)
     x_guess = np.array((0.7, 0.9))
     from scipy.optimize import minimize
     with tf.Session():
         res = minimize(my_pwlf.fit_with_breaks_opt, x_guess)
     self.assertTrue(np.isclose(res['fun'], 0.0))
Пример #14
0
 def test_single_precision(self):
     # check that I can fit when break points spot on a
     my_fit1 = pwlf.PiecewiseLinFitTF(self.x_small,
                                      self.y_small,
                                      dtype='float32')
     x0 = self.x_small.copy()
     ssr = my_fit1.fit_with_breaks(x0)
     self.assertTrue(np.isclose(ssr, 0.0))
Пример #15
0
 def test_predict_with_custom_param(self):
     # check to see if predict runs with custom parameters
     x = np.random.random(20)
     my_pwlf = pwlf.PiecewiseLinFitTF(x, np.random.random(20))
     my_pwlf.predict(x,
                     beta=np.array((1e-4, 1e-2, 1e-3)),
                     breaks=np.array((0.0, 0.5, 1.0)))
     self.assertTrue(True)
Пример #16
0
 def test_break_point_diff_x0_0(self):
     # check diff loc
     my_fit2 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x0 = self.x_small.copy()
     x0[1] = 1.00001
     x0[2] = 1.50001
     ssr2 = my_fit2.fit_with_breaks(x0)
     self.assertTrue(np.isclose(ssr2, 0.0))
Пример #17
0
 def test_assembly_list(self):
     # check that I can fit when break points spot on a
     my_fit = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x0 = self.x_small.copy()
     A = my_fit.assemble_regression_matrix(list(x0), my_fit.x_data)
     Asb = np.array([[1., 0., 0., 0.], [1., x0[1] - x0[0], 0., 0.],
                     [1., x0[2] - x0[0], x0[2] - x0[1], 0.],
                     [1., x0[3] - x0[0], x0[3] - x0[1], x0[3] - x0[2]]])
     with tf.Session():
         A = A.eval()
     self.assertTrue(np.allclose(A, Asb))
Пример #18
0
 def test_opt_fit_with_force_points(self):
     # I need more data points to test this function because of
     # ill conditioning in the least squares problem...
     x = np.linspace(0.0, 1.0, num=100)
     y = np.sin(6.0 * x)
     my_fit = pwlf.PiecewiseLinFitTF(x, y, disp_res=True)
     np.random.seed(1231)
     x_c = [0.0]
     y_c = [0.0]
     my_fit.fit(3, x_c, y_c, popsize=2, maxiter=2, disp=False, polish=False)
     yhat = my_fit.predict(x_c)
     self.assertTrue(np.isclose(y_c, yhat))
Пример #19
0
 def test_fit_guess_kwrds(self):
     my_pwlf = pwlf.PiecewiseLinFitTF(self.x, self.y)
     breaks = my_pwlf.fit_guess([6.0],
                                m=10,
                                factr=1e2,
                                pgtol=1e-05,
                                epsilon=1e-6,
                                iprint=-1,
                                maxfun=1500000,
                                maxiter=150000,
                                disp=None)
     self.assertTrue(np.isclose(breaks[1], 5.99819559))
Пример #20
0
 def test_multi_start_fitfast(self):
     my_pwlf = pwlf.PiecewiseLinFitTF(self.xk, self.yk)
     my_pwlf.fitfast(2)
     self.assertTrue(np.isclose(my_pwlf.ssr, 0.0))
Пример #21
0
 def test_diff_evo(self):
     my_pwlf = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     np.random.seed(12)
     my_pwlf.fit(3, atol=1e-6)
     self.assertTrue(np.isclose(my_pwlf.ssr, 0.0))
Пример #22
0
 def test_fit_guess(self):
     # x = np.array([4., 5., 6., 7., 8.])
     # y = np.array([11., 13., 16., 28.92, 42.81])
     my_pwlf = pwlf.PiecewiseLinFitTF(self.x, self.y)
     breaks = my_pwlf.fit_guess([6.0])
     self.assertTrue(np.isclose(breaks[1], 5.99819559))
Пример #23
0
 def test_pv_list(self):
     my_fit1 = pwlf.PiecewiseLinFitTF(self.x_small, self.y_small)
     x0 = self.x_small.copy()
     _ = my_fit1.fit_with_breaks(x0)
     my_fit1.prediction_variance(list(self.x_small))
Пример #24
0
breaks = np.linspace(0.0, 10.0, num=21)

n = np.logspace(3, 7, num=15, dtype=np.int)
n_repeats = 10
run_times = np.zeros((3, n.size, n_repeats))

for i, n_data in enumerate(n):
    # set random seed
    np.random.seed(256)
    # generate sin wave data
    x = np.linspace(0, 10, num=n_data)
    y = np.sin(x * np.pi / 2)
    # add noise to the data
    y = np.random.normal(0, 0.05, size=n_data) + y
    for j in range(n_repeats):
        # normal PWLF fit
        t0 = time()
        my_pwlf = pwlf.PiecewiseLinFit(x, y)
        ssr = my_pwlf.fit_with_breaks(breaks)
        t1 = time()
        # PWLF TF fit
        t2 = time()
        my_pwlf = pwlf.PiecewiseLinFitTF(x, y)
        ssr = my_pwlf.fit_with_breaks(breaks)
        t3 = time()
        run_times[0, i, j] = t1 - t0
        run_times[1, i, j] = t3 - t2

np.save('bench_run_times/20_break_times.npy', run_times)
np.save('bench_run_times/n.npy', n)
    7.03872000e-03, 1.85494500e-02, 3.00926700e-02, 4.17617000e-02,
    5.37279600e-02, 6.54941000e-02, 7.68092100e-02, 8.76596300e-02,
    9.80525800e-02, 1.07961810e-01, 1.17305210e-01, 1.26063930e-01,
    1.34180360e-01, 1.41725010e-01, 1.48629710e-01, 1.55374770e-01,
    0.00000000e+00, 1.65610200e-02, 3.91016100e-02, 6.18679400e-02,
    8.30997400e-02, 1.02132890e-01, 1.19011260e-01, 1.34620080e-01,
    1.49429370e-01, 1.63539960e-01, -0.00000000e+00, 1.01980300e-02,
    3.28642800e-02, 5.59461900e-02, 7.81388400e-02, 9.84458400e-02,
    1.16270210e-01, 1.31279040e-01, 1.45437090e-01, 1.59627540e-01,
    0.00000000e+00, 1.63404300e-02, 4.00086000e-02, 6.34390200e-02,
    8.51085900e-02, 1.04787860e-01, 1.22120350e-01, 1.36931660e-01,
    1.50958760e-01, 1.65299640e-01, 1.79942720e-01
])

# initialize piecewise linear fit with your x and y data
my_pwlf = pwlf.PiecewiseLinFitTF(x, y, disp_res=True)

# fit the function with four line segments
# force the function to go through the data points
# (0.0, 0.0) and (0.19, 0.16)
# where the data points are of the form (x, y)
x_c = [0.0, 0.19]
y_c = [0.0, 0.2]
breaks = [0.00711605, 0.12014667, 0.1799223]
L = my_pwlf.fit_with_breaks_force_points(breaks, x_c, y_c)

# predict for the determined points
xHat = np.linspace(min(x), 0.19, num=10000)
yHat = my_pwlf.predict(xHat)

# plot the results