def test_fit_returns_min_1d_grid(): N = 100 D = 3 m = 1 omega = np.random.randn(D, m) u = np.random.uniform(0, 2 * np.pi, m) X = np.random.randn(N, D) C = compute_C_memory(X, omega, u) b = compute_b_memory(X, omega, u) theta = fit(X, omega, u) J = objective(X, theta, omega, u, b, C) thetas_test = np.linspace(theta - 3, theta + 3) Js = np.zeros(len(thetas_test)) for i, theta_test in enumerate(thetas_test): Js[i] = objective(X, np.array([theta_test]), omega, u, b, C) # plt.plot(thetas_test, Js) # plt.plot([theta, theta], [Js.min(), Js.max()]) # plt.title(str(theta)) # plt.show() assert_almost_equal(Js.min(), J, delta=thetas_test[1] - thetas_test[0]) assert_almost_equal(thetas_test[Js.argmin()], theta[0], delta=thetas_test[1] - thetas_test[0])
def test_fit(): N = 100 D = 3 m = 10 omega = np.random.randn(D, m) u = np.random.uniform(0, 2 * np.pi, m) X = np.random.randn(N, D) C = compute_C_memory(X, omega, u) b = compute_b_memory(X, omega, u) theta = fit(X, omega, u) theta_manual = np.linalg.solve(C, b) assert_allclose(theta, theta_manual)
def test_fit_returns_min_random_search(): N = 100 D = 3 m = 10 omega = np.random.randn(D, m) u = np.random.uniform(0, 2 * np.pi, m) X = np.random.randn(N, D) C = compute_C_memory(X, omega, u) b = compute_b_memory(X, omega, u) theta = fit(X, omega, u) J = objective(X, theta, omega, u, b, C) for noise in [0.0001, 0.001, 0.1, 1, 10, 100]: for _ in range(10): theta_test = np.random.randn(m) * noise + theta J_test = objective(X, theta_test, omega, u, b, C) assert_less_equal(J, J_test)