def fit(self, data:[[float]], y:[float]) -> ([{}], [float], [float]): logging.info("log: Linear Regression Model Fit Invoked.") self.__validate(data, y) self.__variables__ = len(data) merge_data = [[None for x in range(len(y))] for x in range(len(data)+1)] for i in range(len(merge_data)): if i == 0: merge_data[i] = y else: merge_data[i] = data[i-1] self.__corr_mat__ = Statistics.correlation_matrix(merge_data) self.__partial_mat__ = Statistics.partial_correlation_matrix(merge_data) self.__stats__ = self.__stats(data, y) m1, m2 = self.__regression_matrix(data, y) m1i = Matrix.inverse(m1) m1it = Matrix.transpose(m1i) m2t = Matrix.transpose([m2]) params = Matrix.multiply(m1it, m2t) params = Matrix.transpose(params) params = params[0] self.__params__ = params self.__ycap__ = self.predicts(data) self.__model_stats__ = self.__model_stats(y, self.__ycap__) return (self.__stats__, self.__params__, self.__ycap__)
def test_correlation_matrix_4data(self): x1 = [75, 83, 85, 85, 92, 97, 99] x2 = [1.85, 1.25, 1.5, 1.75, 1.15, 1.75, 1.6] x3 = [16, 20, 25, 27, 32, 48, 48] x4 = [14, 21, 29, 16, 21, 47, 47] mat = Statistics.correlation_matrix([x1, x2, x3, x4]) actual = [[ 1.0, -0.15534261150561485, 0.9627972334240521, 0.8520652907262152 ], [ -0.15534261150561485, 1.0, 0.10551846545168846, 0.13506331370887886 ], [0.9627972334240521, 0.10551846545168846, 1.0, 0.9088795720039011], [ 0.8520652907262152, 0.13506331370887886, 0.9088795720039011, 1.0 ]] self.assertEqual(mat, actual)