Example #1
0
    def partial_correlation(data_y: [float], data_x: [float],
                            eliminate: [[float]]) -> float:
        Statistics.__validate(data_y)
        Statistics.__validate(data_x)
        if len(data_y) != len(data_x):
            raise ValueError("Data Y & X should be of same dimension!!" +
                             str(len(data_y)) + " != " + str(len(data_x)))
        Matrix.validate(eliminate)
        n = len(data_y)
        if n != len(eliminate[0]):
            raise ValueError(
                "Data Y, X & Eliminate should be of same dimension!!" +
                str(n) + " != " + str(len(eliminate[0])))

        m = len(eliminate) + 2
        data = [[0.0 for x in range(n)] for x in range(m)]
        for i in range(m):
            if i == 0:
                data[i] = data_y
            elif i == 1:
                data[i] = data_x
            else:
                data[i] = eliminate[i - 2]
        mat = Statistics.partial_correlation_matrix(data)
        return mat[0][1]
    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 __validate(self, data:[[float]], y:[float]):
     if data is None or len(data) == 0:
         raise ValueError("Data could not be None or Empty!!")
     elif y is None or len(y) == 0:
         raise ValueError("Y could not be None or Empty!!")
     Matrix.validate(data)
     if len(data[0]) != len(y):
         raise ValueError("No of Data Points should be eqaul to " + str(len(y)) + " !!")
Example #4
0
    def correlation_matrix(data: [[float]]) -> [[float]]:
        Matrix.validate(data)
        m = len(data)

        corr_m = [[None for x in range(m)] for x in range(m)]

        for j in range(m):
            for k in range(m):
                if corr_m[j][k] is None:
                    cov, r = Statistics.covariance(data[j], data[k])
                    corr_m[j][k] = r
                    corr_m[k][j] = r
        return corr_m
Example #5
0
    def partial_correlation_matrix(data: [[float]]) -> [[float]]:
        m = len(data)
        corr_m = Statistics.correlation_matrix(data)
        corr_mi = Matrix.inverse(corr_m)

        par_corr_m = [[None for x in range(m)] for x in range(m)]

        for j in range(m):
            for k in range(m):
                if j == k:
                    par_corr_m[j][k] = 1.0
                else:
                    den = -1 * math.sqrt(corr_mi[j][j] * corr_mi[k][k])
                    r = corr_mi[j][k] / den
                    par_corr_m[j][k] = r

        return par_corr_m
Example #6
0
 def test_multiply2(self):
     m1 = [[3, 2, 1, 5], [9, 1, 3, 0]]
     m2 = [[2, 9, 0], [1, 3, 5], [2, 4, 7], [8, 1, 5]]
     actual = [[50, 42, 42], [25, 96, 26]]
     expected = Matrix.multiply(m1, m2)
     self.assertEqual(expected, actual)
Example #7
0
 def test_addition_2corss2_m1_m2(self):
     m1 = [[1, 2], [3, 4]]
     m2 = [[5, 6], [0, -2]]
     actual = [[6, 8], [3, 2]]
     expected = Matrix.add(m1, m2)
     self.assertEqual(expected, actual)
Example #8
0
 def test_matrix_inverse_validation(self):
     with self.assertRaises(ValueError):
         Matrix.inverse([[1, 2], [3, 4], [5, 6]])
Example #9
0
 def test_row_to_col_transform(self):
     m1 = [[1, 4], [2, 5], [3, 6]]
     actual = [[1, 2, 3], [4, 5, 6]]
     expected = Matrix.transpose(m1)
     self.assertEqual(expected, actual)
Example #10
0
 def test_square_transform(self):
     m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
     actual = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
     expected = Matrix.transpose(m1)
     self.assertEqual(expected, actual)
Example #11
0
 def test_multiply_single(self):
     m1 = [[5]]
     m2 = [[5]]
     actual = [[25]]
     expected = Matrix.multiply(m1, m2)
     self.assertEqual(expected, actual)
Example #12
0
 def test_multiply_not_exist(self):
     m1 = [[1, 2, 3]]
     m2 = [[1, 2, 3], [3, 4, 5], [6, 7, 8]]
     with self.assertRaises(ValueError):
         Matrix.multiply(m2, m1)
Example #13
0
 def test_matrix_validation(self):
     with self.assertRaises(ValueError):
         Matrix.validate([])
     with self.assertRaises(ValueError):
         Matrix.validate(None)
     with self.assertRaises(ValueError):
         Matrix.validate([None, [1]])
     with self.assertRaises(ValueError):
         Matrix.validate([[], [1]])
     with self.assertRaises(ValueError):
         Matrix.validate([[1], None])
     with self.assertRaises(ValueError):
         Matrix.validate([[1], []])
     with self.assertRaises(ValueError):
         Matrix.validate([[1, 2], [1]])
     with self.assertRaises(ValueError):
         Matrix.validate([[1, 2], [1, 2, 3]])
Example #14
0
 def test_scalar_multiply_square_2corss2_m1_m2(self):
     m1 = [[1, 2], [3, 4]]
     s = 5
     actual = [[5, 10], [15, 20]]
     expected = Matrix.scalar_multiply(m1, s)
     self.assertEqual(expected, actual)
Example #15
0
 def test_multiply1(self):
     m1 = [[1, 2, 3], [4, 5, 6]]
     m2 = [[7, 8], [9, 10], [11, 12]]
     actual = [[58, 64], [139, 154]]
     expected = Matrix.multiply(m1, m2)
     self.assertEqual(expected, actual)
Example #16
0
 def test_multiply_square_2cross2_m2_m1(self):
     m1 = [[1, 2], [3, 4]]
     m2 = [[5, 6], [0, -2]]
     actual = [[23, 34], [-6, -8]]
     expected = Matrix.multiply(m2, m1)
     self.assertEqual(expected, actual)
Example #17
0
 def test_multiply_square_2corss2_m1_m2(self):
     m1 = [[1, 2], [3, 4]]
     m2 = [[5, 6], [0, -2]]
     actual = [[5, 2], [15, 10]]
     expected = Matrix.multiply(m1, m2)
     self.assertEqual(expected, actual)
Example #18
0
 def test_multiply3(self):
     m1 = [[1, 4, 6]]
     m2 = [[2, 3], [5, 8], [7, 9]]
     actual = [[64, 89]]
     expected = Matrix.multiply(m1, m2)
     self.assertEqual(expected, actual)
Example #19
0
 def test_identity(self):
     m = 3
     actual = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
     expected = Matrix.identity(m)
     self.assertEqual(expected, actual)