%% The Leontief Supply-Production Model
%
% The model captures the basic production/demand economic relationship. The basic idea is to determine whether the system itself has the requisite resources to meet a specified demand. the production at time t is determined by the state vector p(t) \in R^n.  The demand will be described by d(t) \in R^n. The dimension n of the states corresponds to the number of good and resources being tracked.
% The demand d(t) can be met if the net production
% p(t) - A(t) p(t) \ge d(t)        (3)
% where A \in R^{n \times n} is the cost matrix. We are allowing for the cost matrix to change in time. The cost matrix is (usually) known.  Supposing the cost matrix is constant in time and we want to just meet the demand,
% (I-A) p = d                        (4)
% puts constraints on p, if d is given. That would be the case if we ask what does p have to be so that we can meet d, given fixed production costs Ap? In that case,
% p =(I-A)^{-1} d
% Perhaps a more fundamental question is what are the conditions on (I-A) that guarantee that  p and d be non-negative?
% The Leontief model in (4) is called a open. The closed model is simply
% p = Ap.
% There is no external demand; we want to know if the system is self-supporting.
%
% Consider the following table:
%                              |         Inputs consumer per unit of output
% Purchased from  |   Manufacturing         Agriculture     Services
% -----------------------------------------------------------------------------------
% Manufacturing      |          0.5                          0.4                0.2
% Agriculture            |         0.2                          0.3                 0.1
% Services               |          0.1                          0.1                0.3
%
% The consumption matrix
% A =\left( \begin{array}{ccc}
% 0.5 & 0.4 & 0.2 \\
% 0.2 & 0.3 & 0.1 \\
% 0.1 & 0.1 & 0.3
% \end{array}
%  \right)
% (unlike the Markov chain case, the cost matrix does not have to row-add to 1, though the case in which they add to 1 is used to study the closed problem Ap = p)
% Can we guarantee that (I-A)^{-1} is exists, and that it lead to p and d non-negative?

clear, close all
A = [0.5,0.4,0.2;0.2,0.3,0.1;0.1,0.1,0.3];
M=eye(3)-A;
ev = eig(A);
for ii = 1:3
 fprintf('************************************************\n')
end
fprintf('THE LEONTIEF MODEL \n')
fprintf('The cost matrix')
A
fprintf('Eigenvalues of A: %f, %f, %f \n',ev(1),ev(2),ev(3))

% None of the eigenvalues is 1, hence (I-A) has an inverse. Furthermore, the eigenvalues are all positive and less than 1, which means that if d is positive, then p is as well.
%
% Can we meet a demand of $50M from manufacturing, $30M from agriculture, and $20M services?


d = [50,30,20]';
p =(eye(3)-A)\d;
fprintf('Manufacturing production level: $ %fM \n',p(1))
fprintf('Agriculture production level: $ %fM \n',p(2))
fprintf('Services production level: $ %fM \n',p(3))