0% found this document useful (0 votes)
4 views13 pages

L0B-Matrix Operations and Python

The document provides a lesson on using matrices in finance with Python, including installation instructions for Anaconda and Spyder. It discusses asset allocation using matrices, portfolio returns, and variance calculations, with examples and Python code snippets for implementation. Key concepts include matrix multiplication and the interpretation of investment allocations in a portfolio context.

Uploaded by

peterleewang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

L0B-Matrix Operations and Python

The document provides a lesson on using matrices in finance with Python, including installation instructions for Anaconda and Spyder. It discusses asset allocation using matrices, portfolio returns, and variance calculations, with examples and Python code snippets for implementation. Key concepts include matrix multiplication and the interpretation of investment allocations in a portfolio context.

Uploaded by

peterleewang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lesson 0B: Matrices in Finance and Python

Economics of Finance

School of Economic, UNSW1


Python

• Install the Anaconda navigator from here.


• A step-by-step instruction can be found here.
• Once the Anaconda navigator is launched, you will find
access to install Spyder, the Scientific Python Development
Environment.
• Install, and launch it.

2 / 13
Tables as Matrices

It is often desirable to think of matrices as the “inside” of


tables. For example, matrix Q might contain the values of the
following table

Bond Stock
Mon 54 21
Tue 55 18
Wed 56 27

This is a matrix with dimensions days × assets, in this case


3 × 2. In Python we’ll write
>> Q = np.array([[54,21],[55,18],[56,27]]);

3 / 13
Example: Asset allocation with investment funds

Funds Allocation, matrix A:


Fund A Fund B Fund C
Domestic Bonds 0.60 0.20 0.00
Current
Domestic Stocks 0.40 0.50 0.30
Foreign Stocks 0.00 0.30 0.70

Portfolio, vector x:
Fund A 0.20
Fund B 0.30
Fund C 0.50

4 / 13
Asset allocation

Question:
• What is the Investor’s current allocation among the three
major asset classes?
• We know that Ax = b:
    
0.6 0.2 0.0 0.2 0.18
 0.4 0.5 0.3   0.3  =  0.38 
0.0 0.3 0.7 0.5 0.44
How do we interpret b?

5 / 13
Fund Allocations

Funds Allocation, matrix A:


Fund A Fund B Fund C
Dom B 0.60 0.20 0.00
Dom S 0.40 0.50 0.30
For S 0.00 0.30 0.70
Desired Allocation, vector b:
Dom B 0.15
Dom S 0.35
For S 0.50
Question:
• What should be the portfolio in terms of fund’s investment
to obtain the desired allocation?
• Ax = b ⇒ A−1 Ax = A−1 b ⇒ x = A−1 b

6 / 13
Desirable Portfolio

Now let’s suppose there is only two funds in the market:


Fund A Fund B
Dom B 0.60 0.20
Dom S 0.40 0.50
For S 0.00 0.30
Desired Allocation, vector b:
Dom B 0.15
Dom S 0.35
For S 0.50
Question:
• What should be the portfolio in terms of fund’s investment
to obtain the desired allocation?
• not possible to answer: A is not square

7 / 13
Return and Variance

For a portfolio with each asset i weights Wi :


P∞ T
• Portfolio Return = i=1 Wi Ri =W R
P ∞ P T
• Portfolio Variance = i=1 j=1 Wi Wj σij =W σW

where
• σij is covariance of asset i with asset j;
• Ri is the return of each asset

Ri = P1 /P0 − 1

8 / 13
Example

An investor is constructing a 3-asset portfolio, denoted as X,


with allocation of $0.25 million, $0.10 million and $0.65 million
on assets A, B and C respectively. The expected returns for A,
B and C are 10%, 11% and 12% respectively.
The covariance matrix of the portfolio is given below:

Asset A B C
A 0.25 0.078 0.09
σ=
B 0.078 0.35 0.053
C 0.09 0.053 0.48

Formulate a Python command to determine the portfolio return


and standard deviation.

9 / 13
Expected return

Weight matrix and returns can be found as follows:

WT = 0.25 0.10 0.65




 
0.10
R =  0.11 
0.12
So, the return of portfolio:

RX = WT R = 0.114 = 11.4% (1)

10 / 13
Variance

Given that:  
0.25 0.078 0.09
σ =  0.078 0.35 0.053 
0.09 0.053 0.48
and  
0.25
W =  0.10 
0.65
Variance of the portfolio can be found by:

WT σW = 0.2620

Homework: use Python to do the calculation, and then check


the next page.

11 / 13
Check point: Python

Use the following Python code:


import numpy as np
W = np.array([0.25,0.1,0.65])
R = np.array([0.10,0.11,0.12])
cov=np.array([[0.25,0.078,0.09],[0.078,0.35,0.053]
,[0.09,0.053,0.48]])
Er=np.matmul(W,R.transpose())
V=np.matmul(np.matmul(W,cov),W.transpose())
print(Er)
print(V)

12 / 13
Python Notes

• ‘import numpy as np’ to declare the usage of ‘numpy’


library
• Python uses ‘np.array([])’ to define vector
• Matrix is writen as nesting of vectors, i.e.,
‘np.array([[],[],...,[]])’
• Matrix multiplication: np.matmul()
• Python does not allow multiplication of a sequence of more
than two matrices: np.matmul(A,B,C) is invalid
• Nesting, however, is more than welcome:
np.matmul(np.matmul(A,B),C).

13 / 13

You might also like