Example #1
0
    def test003(self):
        p = 5
        lmbda = 0.05

        for _ in range(10):
            R, X = self._create_case(p, T=1000)
            B_hat, eps = solve_lasso(X, p=p, lmbda=lmbda,
                                     maxiter=250, eps=-np.inf)
            self.assertTrue(eps > 0)
            J_star = exact_cost_function(B_hat, X, lmbda=lmbda)
            for _ in range(10):
                J = exact_cost_function(
                    B_hat + 0.025 * np.random.normal(size=B_hat.shape),
                    X, lmbda=lmbda)
                self.assertTrue(J_star < J)
        return
Example #2
0
 def test_exact_cost_function001(self):
     X = np.array([[1.0, 2.0],
                   [-1.0, -2.0]])
     B = np.array([[[2.2, 3.1],
                    [-1.8, 2.6]]])
     cost_xp = (1. / 4) * (np.sum((X[1] - B[0] @ X[0])**2) +
                           np.sum(X[0]**2))
     cost = exact_cost_function(B, X, lmbda=0.0, W=1.0)
     self.assertAlmostEqual(cost, cost_xp)
     return
Example #3
0
 def test_exact_cost_function002(self):
     for _ in range(10):
         X = np.random.normal(size=(3, 2))
         B = np.array([[[2.2, 3.1],
                        [-1.8, 2.6]]])
         cost_xp = (1. / 6) * (np.sum((X[2] - B[0] @ X[1])**2) +
                               np.sum((X[1] - B[0] @ X[0])**2) +
                               np.sum(X[0]**2))
         cost = exact_cost_function(B, X, lmbda=0.0, W=1.0)
         self.assertAlmostEqual(cost, cost_xp)
     return
Example #4
0
 def test_exact_cost_function004(self):
     for _ in range(10):
         X = np.random.normal(size=(3, 2))
         W = np.random.normal(size=(2, 2))
         B = np.random.normal(size=(2, 2, 2))
         cost_xp = ((1. / 6) * (
             np.sum((X[2] - B[0] @ X[1] - B[1] @ X[0])**2) +
             np.sum((X[1] - B[0] @ X[0])**2) +
             np.sum(X[0]**2)) +
                    2 * np.sum(np.abs(W * B)))
         cost = exact_cost_function(B, X, lmbda=2.0, W=W)
         self.assertAlmostEqual(cost, cost_xp)
     return