<jupyter_start><jupyter_text>Portfolio Optimisation with Linear Algebra  This notebook pre-supposes some familiarity with linear algebra from school. But for the older among us, just to bring back some of the good ole memories, let's try to solve a set of linear equations.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Let's solve the following equations:* 2x + 3y + 4z = 11* 10x + y + z = 32* 5x + 7y + 8z = 20 We represent the equation above as Ax = b<jupyter_code>A = np.array([[2, 3, 4], [10, 1, 1], [5, 7, 8]])
b = np.array([[11], [32], [20]])
A
b<jupyter_output><empty_output><jupyter_text>And solve for x<jupyter_code>x = np.linalg.solve(A, b)
x<jupyter_output><empty_output><jupyter_text>Linear Algebra for a Simple Portfolio Optimisation -  We want to build a simplified portfolio of CASH, EQUITIES and BONDS.The assumptions/constraints are that - * Proportion of CASH, EQUITIES, and BONDS should add up to 1 (i.e. 100%)* The mix of CASH, EQUITIES and BONDS should give us a total return of 0.05 (i.e. 5%)* CASH returns = 0.01, EQUITIES returns = 0.1, BOND returns = 0.05* Proportion of CASH should be 0.2 (20%) So the equations are -- CASH + EQUITIES + BONDS = 1- 0.01\*CASH + 0.1\*EQUITIES + 0.05\*BONDS = 0.05- CASH = 0.2<jupyter_code>import numpy as np
A = np.array([[1,1,1], [0.01,0.1,0.05], [1,0,0]])
A
B = np.array([1,0.05,0.2])
B<jupyter_output><empty_output><jupyter_text>To solve and get the proportion of CASH, EQUITIES, and BONDS that we need to meet the constraints above<jupyter_code>print(np.linalg.solve(A,B))<jupyter_output>[ 0.2   0.16  0.64]<jupyter_text>Common Linear Algebra Operations  Other than the above, there are a range of linear algebra operations that we can undertake using Numpy Dot product is just what we had learned in school. If you do not remember, it's basically the sum of row x col in each of the resultant cells<jupyter_code>a = np.array([[1, 0],[0, 1]])
b = np.array( [[4, 1],[2, 2]])
print(a)
print(b)<jupyter_output>[[1 0]
 [0 1]]
[[4 1]
 [2 2]]<jupyter_text>So we take (1x4+0x2)=4 for the top left cell ...<jupyter_code>np.dot(a,b)
# 1x3 + 1x5 + 1x1 = 9
a = np.array([1, 1, 1])
b = np.array([3, 5, 1])
np.inner(a,b)
# First row is 1x3, 1x5, 1x1; ditto for the rest
np.outer(a,b)
# This is a L2-norm, basically the sq root of (0^2+1^2+2^2)
a = np.arange(3)
np.linalg.norm(a)
# Det is ABS(A) = a*d - b*c; so here it is 1*4-2*3=4-6=2
a = np.array([[1,2],[3,4]])
np.linalg.det(a)
# for [[a,b],[c,d]], you switch a and d, make b and c negative and divide by the det 
a = np.array([[1,2],[3,4]])
np.linalg.inv(a)
# Decomposing a matrix into A = QR
a = np.array([[1,2],[3,4]])
np.linalg.qr(a)
# Sum of the diagonal element
np.trace(np.arange(6).reshape(2,3))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15045207956600362, 'fraction_numerical': 0.058589511754068714, 'mean_word_length': 4.587878787878788, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '180016', 'license': 'MIT', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1125, 'n_words': 362, 'path': 'Portfolio Optimisation with Linear Algebra.ipynb', 'repo_name': 'playgrdstar/linear_algebra_portfolio_optimisation', 'size': '2765'}