**Matrix Algebra to solve Linear Equations with Python**

**Background**

Most of us have solved *set of linear equations* at a certain point of time in our lives — be it in school, undergraduate or postgraduate level. These type of problems were commonly referred to a set of *simultaneous equations*. The result of solving such equations is to arrive at the values of coefficients of these equations.

Such equations can be solved by hand, however, this manual approach to solving such problems by hand can get tedious if there are multiple equations that need to be solved simultaneously. An approach to solving such problems efficiently and accurately is by using **matrix algebra**. Matrices enable not only to visualize the system of “n” equations clearly but also solve these equations very systematically. Therefore, usage of matrices is highly relevant for industry applications.

In this article, we will discuss a few popular techniques of applying linear algebra for solving a set of linear equations. We begin with a basic approach and then transition to the idea of *matrix decomposition* to solve problems. Further, we will attempt to implement the same via Python. Towards the end of this article, we will discuss an industry application where this technique may be applicable.

**Problem Statement**

We wish to construct a portfolio consisting of three assets (financial securities) a,b and c. We aim to find the number of each of the securities to invest in. We are given a certain set of constraints that are to be satisfied at all times.

i. With every 2 units of asset

, 1 unit of assetaand 2 units of assetbinvested, the net long position should be equal to 4cii. With every 1 unit of asset

, 3 units of asseta, and 2 units of assetbinvested, the net position must be long 5 units.ciii. 6 units to be invested in asset

a

Mathematically, the above problem is formulated as below:

2a + 1b + c = 4

1a + 3b + 2c = 5

1a = 6

Let’s attempt to solve the above set of equations using 3 techniques and review the results (the results must tally, because we are solving the same set problem statement only with different techniques!)

**Approach 1: Basic Matrix Algebra**

**Approach 2: LU Decomposition**

**Approach 3: QR Decomposition**

This approach is another useful technique to solve a set of linear equations. In this approach, we have A = QR i.e., (Matrix A is a product of an orthogonal matrix Q and upper triangular matrix R)

The original equation was Ax = B

We restate the above in the form below:

QR. x = B

Rx = Q(inverse). B (note: Q (transpose) is the same as Q(inverse))

The answer tallies with the previous 2 approaches.

**Application to Asset Management**

Asset management firms may want to use a model for determining the optimal asset allocation based on the client requirement and constraints as may have been agreed in their **investment policy statement (IPS)** with their respective clients. An investor’s return requirement and constraints can be translated in the form of a set of linear equations. These equations may be solved via one of the techniques discussed in this article. The output of each of the above three approaches gives the optimal number of assets to invest in each of the asset categories.

**Conclusion**

In this article we have demonstrated three techniques that may be used to solve a set of linear equations via matrix algebra and decomposition techniques. In this article we have explored only equations that were in the form of equalities. There could be scenarios whereby we could may have to solve a system of linear equations that may have inequalities. In that case we will need to use the **linear programming problem** approach to solve for the optimal values of coefficients.

Another point for readers to pursue could be to explore **Cholesky decomposition** technique from the point of view of generation of **correlated random processes** that are frequently used in modelling randomness in assets.