Example #1
0
    def test_result(self):
        a_matrix_1 = matrices.Matrix([[1, 2, 3], [4, 5, 6]])
        a_matrix_2 = matrices.Matrix([[7, 8], [9, 10], [11, 12]])
        bad_matrix = matrices.Matrix([[1, 1, 7], [2, 3, 4]])

        result_list = [[58, 64], [139, 154]]
        result_matrix = matrices.multiplication(a_matrix_1, a_matrix_2)

        with self.assertRaises(matrices.NonValidMatricesForMultiplicationError):
            matrices.multiplication(a_matrix_1, bad_matrix)

        self.assertEqual(result_list, result_matrix.matrix)
Example #2
0
def coeficents_estimation(regressand_vector_matrix_raw, regressor_matrix):
    """
       Estimates the optimal coeficents in the linear regression model
       using the method of least squares.
       -
       For more information:
       http://en.wikipedia.org/wiki/
       Linear_least_squares_%28mathematics%29#Computation
       -
    """
    regressand_matrix = regressand_vector_matrix_raw.transposed()
    regressor_matrix = add_to_matrix_for_intercept(regressor_matrix)

    transposed = regressor_matrix.transposed()
    D = matrices.multiplication(transposed, regressor_matrix)
    C = D.find_inverse()
    F = matrices.multiplication(C, transposed)
    K = matrices.multiplication(F, regressand_matrix)

    return K.transpose().matrix