In [1]:
import numpy as np
from scipy import linalg
Matrix $X$ - production needs, where $x_{ij}$ is how much of $i$-th product is needed to make $j$-th product
In [2]:
X = np.array([[500, 300], [150, 200]])
Vector $y$ - consumer needs, where $y_i$ shows how much of $i$-th product people buy(non-production needs)
In [3]:
y = np.array([900, 500])
Total production can be calculated as $$ x_i = \sum_{j=1}^n x_{ij} + y_i $$
In [4]:
x = np.sum(X, axis=1) + y
print(x)
In [5]:
A = X / x
print(A)
When our consumer needs changed
In [6]:
y1 = np.array([1100, 800])
We can approximate production changes needed to satisfy our customers $$ x' = Ax' + y' $$
$$ (A - E)x' = -y' $$
In [7]:
x1 = np.linalg.solve(A - np.eye(A.shape[0]), -y1)
print(x1)
In [8]:
A = np.array([[0.5, 0.1, 0.5], [0, 0.3, 0.1], [0.2, 0.3, 0.1]])
We can find coefficients of charactiristic polynomial using numpy.poly
In [9]:
coefs = np.poly(A)
print(coefs)
To compute eigenvalues and eigenvectors we can use scipy.linalg.eig
In [10]:
vals, left, right = linalg.eig(A, left=True, right=True)
Frobenius number is by definition the largest of eigenvalues
In [11]:
print(np.max(vals).real)
Because Frobenius number is less than 1 we can say that technology matrix is productive.
Also left and right frobenius vectors are
In [12]:
print(left[:, np.argmax(vals)])
print(right[:, np.argmax(vals)])
In [13]:
B = np.linalg.inv(np.eye(A.shape[0]) - A)
print(B)
We can also approximate matrix $B$ with limit $$ \lim_{n \to \infty} (E + \sum_{i=1}^n A^n) = \lim_{n \to \infty} E + A + A^2 + ... + A^n = B $$
In [14]:
B1 = P = np.eye(A.shape[0])
for i in range(100):
P = P @ A
B1 = B1 + P
if np.max(np.abs(B - B1)) < 1e-2:
print('It took {} steps to converge.'.format(i))
break
print(B1)
In [15]:
s = np.array([0.2, 0.3, 0.4])
In [16]:
p = s @ B
print(p)