Solving linear equations using matrices

In the previous section, we looked at solving a system of linear equations with inequality constraints. If a set of systematic linear equations has constraints that are deterministic, we can represent the problem as matrices and apply matrix algebra. Matrix methods represent multiple linear equations in a compact manner while using the existing matrix library functions.

Suppose we would like to build a portfolio consisting of 3 securities, Solving linear equations using matrices, Solving linear equations using matrices and Solving linear equations using matrices. The allocation of the portfolio must meet certain constraints: it must consist of 6 units of a long position in security a. With every 2 units of security a, 1 unit of security b, and 1 unit of security c invested, the net position must be 4 units long. With every 1 unit of security a, 3 units of security b, and 2 units of security c invested, the net position must be long 5 units.

To find out the number of securities to invest in, we can frame the problem mathematically as follows:

Solving linear equations using matrices
Solving linear equations using matrices
Solving linear equations using matrices

With all of the coefficients visible, the equations are as follows:

Solving linear equations using matrices
Solving linear equations using matrices
Solving linear equations using matrices

Taking the coefficients of the equations and representing them in a matrix form:

Solving linear equations using matrices

The linear equations can now be stated as follows:

Solving linear equations using matrices

To solve for the vector x that contains the number of securities to invest in, the inverse of matrix A is taken and the equation is written as follows:

Solving linear equations using matrices

Using the NumPy arrays, the A and B matrices are assigned as follows:

""" Linear algebra with NumPy matrices """
import numpy as np

A = np.array([[2, 1, 1],
              [1, 3, 2],
              [1, 0, 0]])
B = np.array([4, 5, 6])

We can use the linalg.solve function of NumPy to solve a system of linear scalar equations:

>>> print np.linalg.solve(A, B )
[  6.  15. -23.] 

The portfolio would require a long position of 6 units of security a, 15 units of security b, and a short position of 23 units of security c.

In portfolio management, we can use the matrix system of equations to solve for optimal weight allocations of securities, given a set of constraints. As the number of securities in the portfolio increases, the size of matrix A increases and it becomes computationally expensive to compute the matrix inversion of A. Thus, one may consider methods such as the Cholesky decomposition, LU decomposition, QR decomposition, the Jacobi method, or the Gauss-Seidel method to break down matrix A into simpler matrices for factorization.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset