In [1]:
import numpy as np
from scipy import linalg

Diversified economy

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)


[1700  850]

Leontiev model

We assume that cost is proportional to production $$ x_{ij}=a_{ij}x_j \implies a_{ij}=\frac{x_{ij}}{x_j} $$


In [5]:
A = X / x
print(A)


[[ 0.29411765  0.35294118]
 [ 0.08823529  0.23529412]]

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)


[ 2208.84353741  1301.02040816]

Part 2

Given matrix $A$


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)


[ 1.    -0.9    0.1    0.028]

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)


0.7

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)])


[ 0.57735027  0.57735027  0.57735027]
[ 0.93080864  0.08864844  0.35459377]

Full costs matrix

Can be computed as


In [13]:
B = np.linalg.inv(np.eye(A.shape[0]) - A)
print(B)


[[ 2.63157895  1.05263158  1.57894737]
 [ 0.0877193   1.53508772  0.21929825]
 [ 0.61403509  0.74561404  1.53508772]]

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)


It took 14 steps to converge.
[[ 2.62407473  1.04512739  1.57144316]
 [ 0.08700461  1.53437301  0.21858356]
 [ 0.61117634  0.74275528  1.53222897]]

Prices (by Leontiev)

Additional costs vector $s$ equals

$$ p = pA + s $$$$ p(E - A) = s $$$$ p = s(E - A) ^{-1} $$$$ p = sB $$

In [15]:
s = np.array([0.2, 0.3, 0.4])

In [16]:
p = s @ B
print(p)


[ 0.79824561  0.96929825  0.99561404]