def test_covariance(self): data_x_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] data_y_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] actual = (7.5, 1) expected = Statistics.covariance(data_x_1, data_y_1) self.assertEqual(expected, actual) with self.assertRaises(ValueError): Statistics.covariance([1, 2, 3], [1, 2]) data_x_1_mean = Statistics.mean(data_x_1) data_y_1_mean = Statistics.mean(data_y_1) expected = Statistics.covariance(data_x_1, data_y_1, data_x_1_mean, data_y_1_mean) self.assertEqual(expected, actual)
def pcf_data(ip, n): op_list = [] for i in range(1, n+1): lag_op = shift(ip, i) op_tmp = list(Statistics.covariance(ip[i:], lag_op[i:])) op_list.append([i, op_tmp[1]]) return op_list
def test_none_data(self): with self.assertRaises(ValueError): Statistics.mean(None) with self.assertRaises(ValueError): Statistics.mean([]) with self.assertRaises(ValueError): Statistics.variance(None) with self.assertRaises(ValueError): Statistics.variance([]) with self.assertRaises(ValueError): Statistics.covariance(None, [1]) with self.assertRaises(ValueError): Statistics.covariance([], [1]) with self.assertRaises(ValueError): Statistics.covariance([1], None) with self.assertRaises(ValueError): Statistics.covariance([1], []) with self.assertRaises(ValueError): Statistics.covariance([1], [1, 2])
def __stats(self, data: [[float]], y: [float]) -> []: stats = [{} for i in range(len(data) + 1)] y_stats = Statistics.describe(y) (y_variance, y_std) = Statistics.variance(y, y_stats["mean"]) y_stats["covariance"] = y_variance y_stats["r"] = 1.0 y_stats["pr"] = 1.0 stats[0] = y_stats for i in range(len(data)): s = Statistics.describe(data[i]) (covariance, r) = Statistics.covariance(data[i], y, s["mean"], y_stats["mean"]) s["covariance"] = covariance s["r"] = r s["pr"] = self.__partial_mat__[0][i + 1] stats[i + 1] = s return stats
def test_partial_correlation_coefficient_greater_than_r(self): #https://stackoverflow.com/questions/51236206/partial-correlation-values-are-larger-than-normal-correlation-in-r hl = [7, 15, 19, 15, 21, 22, 57, 15, 20, 18] disp = [ 0.000, 0.964, 0.000, 0.000, 0.921, 0.000, 0.000, 1.006, 0.000, 1.011 ] deg = [9, 2, 3, 4, 1, 3, 1, 3, 6, 1] BC = [ 1.78e-02, 1.05e-06, 1.37e-05, 7.18e-03, 0.00e+00, 0.00e+00, 0.00e+00, 4.48e-03, 2.10e-06, 0.00e+00 ] var, r = Statistics.covariance(hl, disp) r = round(r, 5) self.assertEqual(r, -0.23787) pr = Statistics.partial_correlation(hl, disp, [deg, BC]) pr = round(pr, 5) self.assertEqual(pr, -0.67209) mat = Statistics.partial_correlation_matrix([hl, disp, deg, BC]) mat_pr = round(mat[0][1], 5) self.assertEqual(mat_pr, -0.67209)