def test_predict(self): """ Tests the predict method using known model params """ X, y, params, exp_y = self.solve_model() # use cost function predict method cost = cost_functions.GaussianCostFunction(X, y) obs_y = cost.predict(X, params) self.assertEqual(exp_y.shape, obs_y.shape)
def test_error(self): """ Tests the mean-squared error implementation """ X, y, _, pred_y = self.solve_model() # use cost function predict method cost = cost_functions.GaussianCostFunction(X, y) obs_mse = cost._mse(y, pred_y) exp_mse = mean_squared_error(y, pred_y) self.assertAlmostEqual(obs_mse, exp_mse)
def test_cost(self): """ Tests the cost function. """ X, y, params, pred_y = self.solve_model() exp_mse = mean_squared_error(y, pred_y) # use cost function predict method cost_func = cost_functions.GaussianCostFunction(X, y) cost = cost_func.cost(params) self.assertAlmostEqual(cost, exp_mse)
def test_optim(self): """ Test optimizing the cost function. """ X, y, exp_params, exp_pred_y = self.solve_model() exp_mse = mean_squared_error(y, exp_pred_y) cost_func = cost_functions.GaussianCostFunction(X, y) optimizer = optim.Optimizer(0.1, 10000, 1e-4, 1e-4) # use a mean of 0, variance of 1 to start initial_params = np.array([0.0, 1.0]) optim_params, n_iters = optimizer.optimize(cost_func, initial_params) print(n_iters) print(exp_params) print(optim_params) self.assertAlmostEqual(optim_params[0], exp_params[0], 1) self.assertAlmostEqual(optim_params[1], exp_params[1], 1)