\documentclass[11pt]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}

\newcommand{\T}{^{\top}}
\newcommand{\one}{\mathbf{1}}
\newcommand{\Z}{\mathbf{Z}}
\newcommand{\A}{\mathbf{A}}
\newcommand{\Lm}{\mathbf{L}}
\newcommand{\Id}{\mathbf{I}}
\newcommand{\E}{\mathbf{E}}
\newcommand{\Xhat}{\widehat{\mathbf{x}}}
\newcommand{\vhat}{\widehat{\mathbf{v}}}
\newcommand{\Smat}{\mathbf{S}}
\newcommand{\Wmat}{\mathbf{W}}
\newcommand{\Fmat}{\mathbf{F}}
\newcommand{\Mmat}{\mathbf{M}}

\begin{document}

\begin{center}
  {\LARGE\bfseries Value-Added Source Decomposition}\\[2pt]
  {\large The matrix algebra behind \texttt{va\_decomposition\_workflow.py}}\\[6pt]
  {\normalsize Pakistan vs.\ Developing Asia, ADB Input--Output Tables (2022)}\\[2pt]
  {\small Companion note for anyone running \texttt{va\_decomposition\_workflow.py}}
\end{center}
\vspace{4pt}
\hrule
\vspace{10pt}

\noindent
This note derives the decomposition that the workflow computes: for each sector
we attribute 100\% of its \emph{gross output value} to the domestic value added
created in every source sector, plus imported-input content, plus net product
taxes. The method is the standard demand-side Leontief model used throughout the
global value-chain (GVC) trade literature; see the references at the end.

\section{Where this comes from: the Leontief model}

An input--output table can be read in two directions, and the whole method rests
on the difference between them.

Reading \emph{across a row} $i$ is the market-clearing (quantity) balance:
sector $i$'s output is split between sales as intermediate inputs to other sectors
and sales to final demand $f_i$,
\begin{equation}
  x_i = \sum_{j} Z_{ij} + f_i,
  \qquad\Longrightarrow\qquad
  \mathbf{x} = \A\mathbf{x} + \mathbf{f},
  \qquad
  \mathbf{x} = (\Id-\A)^{-1}\mathbf{f},
  \label{eq:rowbalance}
\end{equation}
using $Z_{ij}=A_{ij}x_j$ (defined below). Reading \emph{down a column} $j$ is the
cost/value balance: the value of sector $j$'s output equals its intermediate-input
bill plus payments to primary factors (value added), imported inputs, and net
taxes -- this is the accounting identity \eqref{eq:identity} in the next section.

The behavioural content is the \emph{Leontief production function}: each sector
produces with inputs in fixed proportions, so making one unit of output $j$
requires a fixed amount $A_{ij}=Z_{ij}/x_j$ of every input $i$, independent of
scale and with no substitution between inputs. That single assumption turns the
$A_{ij}$ into structural technology parameters and makes both balances linear.
From the row balance, inverting $\Id-\A$ gives the Leontief inverse
$\Lm=(\Id-\A)^{-1}$: it converts final demand into the gross output needed to
support it, counting the direct requirement and every upstream round. We do not
use the row balance directly -- our object lives on the \emph{column} (value)
side -- but this is where $\Lm$ earns its meaning as the total-requirements
matrix.

The decomposition then asks a value-side question: of the money paid for one unit
of sector $j$'s output, how much ultimately accrues as value added created in
each source sector $i$, versus leaking to imported inputs or taxes? Since value
added is the primary-factor share $v_j=va_j/x_j$ left in each sector after its
intermediate bill, propagating $v_j$ through $\Lm$ answers exactly that. Sections
2--4 make this precise; Section 5 compares Pakistan to a developing-Asia
benchmark.

\section{Data and notation}

For the $n$ producing sectors of one country, the ADB table (sheet
\texttt{Table 1.17}) provides:

\begin{center}
\renewcommand{\arraystretch}{1.25}
\begin{tabular}{@{}c l l@{}}
\toprule
Symbol & Dimension & Meaning \\
\midrule
$\Z$              & $n\times n$ & intermediate transactions: $Z_{ij}$ = output of $i$ used as input by $j$ \\
$\mathbf{x}$      & $n\times 1$ & gross output; $\Xhat=\operatorname{diag}(\mathbf{x})$ \\
$\mathbf{va}\T$   & $1\times n$ & value added at basic prices \\
$\mathbf{im}\T$   & $1\times n$ & imported-input content \\
$\mathbf{tax}\T$  & $1\times n$ & net taxes (taxes less subsidies on products) \\
\bottomrule
\end{tabular}
\end{center}

\noindent
Let $\one$ denote the column vector of ones. Each sector's output value is
exhausted by intermediate purchases, value added, imports and net taxes, giving
the column accounting identity
\begin{equation}
  x_j \;=\; \sum_{i=1}^{n} Z_{ij} + va_j + im_j + tax_j,
  \qquad\text{i.e.}\qquad
  \mathbf{x}\T \;=\; \one\T\Z + \mathbf{va}\T + \mathbf{im}\T + \mathbf{tax}\T .
  \label{eq:identity}
\end{equation}

\section{Technical coefficients and the Leontief inverse}

The technical-coefficient matrix normalises each intermediate flow by the
\emph{total output} of the \emph{using} sector,
\begin{equation}
  \A \;=\; \Z\,\Xhat^{-1},
  \qquad
  A_{ij} \;=\; \frac{Z_{ij}}{x_j},
  \label{eq:A}
\end{equation}
so $A_{ij}$ is the amount of input $i$ required per unit of sector $j$'s output --
the fixed-proportions Leontief production function. This output denominator (not
an ``intermediates'' denominator) is what makes the total-requirements matrix,
the \emph{Leontief inverse},
\begin{equation}
  \Lm \;=\; (\Id-\A)^{-1} \;=\; \Id + \A + \A^2 + \cdots ,
  \label{eq:L}
\end{equation}
count all direct and indirect rounds of production.\footnote{The Neumann series
$\Id+\A+\A^2+\cdots$ converges iff the spectral radius $\rho(\A)<1$. Here that is
guaranteed by the Hawkins--Simon / productivity condition: every sector has
strictly positive value added, so by \eqref{eq:closure} each column of $\A$ sums
to $1-v_j-m_j-t_j<1$, and $\rho(\A)\le\lVert\A\rVert_1=\max_j\text{(column sum)}<1$.}
Define the per-unit-of-output
coefficient vectors
\begin{equation}
  \mathbf{v}\T = \mathbf{va}\T\Xhat^{-1},\qquad
  \mathbf{m}\T = \mathbf{im}\T\Xhat^{-1},\qquad
  \mathbf{t}\T = \mathbf{tax}\T\Xhat^{-1}.
  \label{eq:coeffs}
\end{equation}

\paragraph{Closure.} Post-multiplying \eqref{eq:identity} by $\Xhat^{-1}$ and using
\eqref{eq:A}--\eqref{eq:coeffs},
\begin{equation}
  \one\T\A + \mathbf{v}\T + \mathbf{m}\T + \mathbf{t}\T \;=\; \one\T,
  \qquad\Longrightarrow\qquad
  \mathbf{v}\T + \mathbf{m}\T + \mathbf{t}\T \;=\; \one\T(\Id-\A).
  \label{eq:closure}
\end{equation}
The column sums of $\A$, plus the value-added, import and tax coefficients, add to
one. This identity holds \emph{only} with the output denominator of \eqref{eq:A};
it is the engine that makes every column of the decomposition sum to 100\%.

\section{Source decomposition of gross output value}

One unit of sector $j$'s output embodies, through all upstream rounds, domestic
value added created in each source sector $i$:
\begin{equation}
  \E \;=\; \vhat\,\Lm,
  \qquad
  E_{ij} \;=\; v_i\,L_{ij}
  \quad(\vhat=\operatorname{diag}(\mathbf{v})),
  \label{eq:E}
\end{equation}
together with embodied imported inputs\footnote{With a single national table,
imports enter as a primary-input-like row and are pushed through the
\emph{domestic} inverse $\Lm$ -- the standard non-competitive-imports treatment.
``Imported inputs'' is therefore a \emph{terminal} source category: we measure how
much of each sector's output value leaks abroad, but not the foreign sector in
which that value originates. Tracing foreign content further would require a
multi-region table (MRIO), as in Koopman, Wang \& Wei (2014).} and embodied net
taxes,
\begin{equation}
  \boldsymbol{\mu}\T \;=\; \mathbf{m}\T\Lm,
  \qquad
  \boldsymbol{\tau}\T \;=\; \mathbf{t}\T\Lm .
  \label{eq:imptax}
\end{equation}
Stack these into a single $(n{+}2)\times n$ matrix whose rows are sources and
whose columns are using sectors:
\begin{equation}
  \mathbf{G}
  \;=\;
  \begin{bmatrix}
    \E \\[2pt] \boldsymbol{\mu}\T \\[2pt] \boldsymbol{\tau}\T
  \end{bmatrix}
  \;=\;
  \begin{bmatrix}
    \vhat\,\Lm \\[2pt] \mathbf{m}\T\Lm \\[2pt] \mathbf{t}\T\Lm
  \end{bmatrix}.
  \label{eq:G}
\end{equation}

\paragraph{Each column sums to one.} Summing the domestic block,
$\one\T\E=\one\T\vhat\Lm=\mathbf{v}\T\Lm$, so the column totals of $\mathbf{G}$ are
\begin{equation}
  \mathbf{v}\T\Lm + \mathbf{m}\T\Lm + \mathbf{t}\T\Lm
  \;=\;
  \big(\mathbf{v}\T+\mathbf{m}\T+\mathbf{t}\T\big)\Lm
  \;\overset{\eqref{eq:closure}}{=}\;
  \one\T(\Id-\A)\,(\Id-\A)^{-1}
  \;=\;\one\T .
  \label{eq:colsum}
\end{equation}
Every sector's output value is fully and exactly attributed to domestic value
added by source, imported inputs, and net taxes.

\section{Aggregation: 35 ADB sectors $\to$ 15 paper sectors}

Sources and using sectors aggregate differently. Let $g(\cdot)$ map each ADB
sector to one of the $G=15$ paper sectors.

\paragraph{Source rows -- summed.} With the $\{0,1\}$ selector
$\Smat\in\{0,1\}^{G\times n}$, $S_{g,j}=1 \iff g(j)=g$, the domestic block's rows
are summed within each group: $\Smat\,\E$. (Imports and net taxes are single rows
and carry over unchanged.)

\paragraph{Using columns -- output-weighted average.} Let
$\Wmat\in\mathbb{R}^{n\times G}$ with
\begin{equation}
  W_{j,h} \;=\;
  \begin{cases}
    \dfrac{x_j}{\sum_{k:\,g(k)=h} x_k}, & g(j)=h,\\[10pt]
    0, & \text{otherwise},
  \end{cases}
  \label{eq:W}
\end{equation}
so each column of $\Wmat$ holds output shares that sum to one. The aggregated
$(G{+}2)\times G$ matrix is
\begin{equation}
  \Fmat
  \;=\;
  \begin{bmatrix}
    \Smat\,\E \\[2pt] \boldsymbol{\mu}\T \\[2pt] \boldsymbol{\tau}\T
  \end{bmatrix}\Wmat .
  \label{eq:F}
\end{equation}
Row-summing (via $\Smat$) only regroups entries within a column, and each
aggregated column is a convex combination (weights sum to one) of columns that
already sum to one by \eqref{eq:colsum}; hence \emph{every column of $\Fmat$ still
sums to exactly one}. The final renormalisation to percentages,
\begin{equation}
  \Mmat \;=\; 100\cdot \Fmat\,\operatorname{diag}\!\big(\one\T\Fmat\big)^{-1},
  \label{eq:M}
\end{equation}
is therefore an identity up to floating-point error, not a correction. Two
zero-output cases must be separated. Individual sectors with $x_j=0$ are dropped
\emph{before} $\A$ is formed (the code keeps only $x>0$), so $\A$ and $\Lm$ are
never contaminated by a division by zero. The remaining case is a whole paper
\emph{group} all of whose member sectors have zero output: then that column of
$\Fmat$ is empty and \eqref{eq:M} returns an undefined ($\mathrm{NaN}$) column. In
this dataset that happens only for the Maldives (Petroleum \& Chemicals and
Machinery \& Electrical); the Maldives is excluded from the benchmark and is not
the focal country, and the cross-country median in \eqref{eq:median} uses a
$\mathrm{NaN}$-robust operator, so such a column never reaches a figure. The entry
$M_{ij}$ is the percentage of using-sector $j$'s gross output value that
originates in source $i$ (a paper sector, imported inputs, or net taxes), with
columns summing to $100$.

\paragraph{Why aggregate the results, not the table.} Everything above is
computed at the full 35-sector resolution, and only the resulting matrix is
aggregated at the very end. Collapsing $\Z$ to 15 sectors first and recomputing
$\A$ there would force a single technical coefficient onto each merged group and
generally yield different, biased multipliers -- the classic input--output
aggregation problem. Decomposing at full resolution and aggregating the
\emph{results} avoids it.

\section{Cross-country benchmark and the difference figure}

Applying \eqref{eq:A}--\eqref{eq:M} to every country $c$ yields
$\Mmat^{(c)}$. The developing-Asia benchmark is the \emph{cell-wise median} over
the benchmark set $\mathcal{C}$ (developed economies and structural outliers
excluded; Pakistan excluded from its own benchmark):
\begin{equation}
  M^{\text{bench}}_{ij} \;=\; \operatorname{median}_{c\in\mathcal{C}} M^{(c)}_{ij}.
  \label{eq:median}
\end{equation}
The median is \emph{not} additive, so the columns of $\Mmat^{\text{bench}}$ need
not sum to $100$. The comparison figure plots the difference
\begin{equation}
  \Delta \;=\; \Mmat^{\text{Pak}} - \Mmat^{\text{bench}},
  \label{eq:diff}
\end{equation}
where $\Delta_{ij}>0$ (red) means source $i$ contributes a larger share of sector
$j$'s output value in Pakistan than in the typical developing-Asia economy, and
$\Delta_{ij}<0$ (blue) the reverse.

Because the benchmark columns need not total $100$, the columns of $\Delta$ need
not total zero. A column can therefore read as net red simply because the
benchmark medians in it sum to less than $100$, not because Pakistan's sectoral
structure is unusual; each cell should be read on its own rather than balancing
red against blue within a column. Renormalising every benchmark column to $100$
before differencing would restore $\sum_i\Delta_{ij}=0$, but at the cost of
distorting the individual median cells, so we keep the raw medians undistorted.

\section*{Equation $\leftrightarrow$ code map}

\begin{center}
\renewcommand{\arraystretch}{1.25}
\begin{tabular}{@{}l l l@{}}
\toprule
Equation & Code (\texttt{va\_decomposition\_workflow.py}) & Object \\
\midrule
\eqref{eq:identity}          & \texttt{read\_iot} (Z, VA, X, IM, TAX)          & data \\
\eqref{eq:A}                 & \texttt{A = Zk / Xk[None, :]}                    & technical coefficients \\
\eqref{eq:L}                 & \texttt{L = np.linalg.inv(I - A)}               & Leontief inverse \\
\eqref{eq:coeffs}            & \texttt{v, m, t = VAk/Xk, IMk/Xk, TAXk/Xk}      & coefficient vectors \\
\eqref{eq:E}--\eqref{eq:imptax} & \texttt{E = v[:,None]*L;\ imp = m@L;\ tax = t@L} & embodied content \\
\eqref{eq:F} (rows)          & \texttt{S};\ \texttt{R = vstack([S@E, imp, tax])} & source aggregation \\
\eqref{eq:W}--\eqref{eq:F} (cols) & \texttt{w = Xk[cset]/Xk[cset].sum();\ R[:,cset]@w} & output-weighted columns \\
\eqref{eq:M}                 & \texttt{M = 100*F / F.sum(axis=0)}              & percentages \\
\eqref{eq:median}--\eqref{eq:diff} & \texttt{build\_difference()} (nanmedian; \texttt{pak - bench}) & benchmark \& $\Delta$ \\
\bottomrule
\end{tabular}
\end{center}

\appendix
\section{The exact 35\,$\to$\,15 sector mapping}

This is the mapping implemented in \texttt{va\_decomposition\_workflow.py} (the
\texttt{MAP} dictionary and the \texttt{PAPER} list). It refines the 11-sector
paper aggregation documented in \texttt{IOT\_sector\_mapping.docx} in exactly one
place: the single ``Non-food manufacturing'' bucket is split into five using
sectors (Textiles; Petrol.\ \& Chem.; Machinery \& Elec.; Transport eq.; Other
mfg) so the heatmaps show manufacturing detail. Every non-manufacturing sector is
identical to that document; ``Other mfg'' is the residual and also absorbs mining.

\renewcommand{\arraystretch}{1.15}
\begin{longtable}{@{}r >{\raggedright\arraybackslash}p{8.3cm} l@{}}
\toprule
\# & ADB IOT sector (35-sector table) & Paper sector \\
\midrule
\endhead
1  & Agriculture, hunting, forestry, and fishing & Agriculture \\
2  & Mining and quarrying & Other mfg \\
3  & Food, beverages, and tobacco & Food \\
4  & Textiles and textile products & Textiles \\
5  & Leather, leather products, and footwear & Other mfg \\
6  & Wood and products of wood and cork & Other mfg \\
7  & Pulp, paper, paper products, printing, and publishing & Other mfg \\
8  & Coke, refined petroleum, and nuclear fuel & Petrol.\ \& Chem. \\
9  & Chemicals and chemical products & Petrol.\ \& Chem. \\
10 & Rubber and plastics & Petrol.\ \& Chem. \\
11 & Other nonmetallic minerals & Other mfg \\
12 & Basic metals and fabricated metal & Other mfg \\
13 & Machinery, nec & Machinery \& Elec. \\
14 & Electrical and optical equipment & Machinery \& Elec. \\
15 & Transport equipment & Transport eq. \\
16 & Manufacturing, nec; recycling & Other mfg \\
17 & Electricity, gas, and water supply & Government \\
18 & Construction & Construction \\
19 & Sale, maintenance, and repair of motor vehicles; retail sale of fuel & Trade \\
20 & Wholesale trade and commission trade & Trade \\
21 & Retail trade; repair of household goods & Trade \\
22 & Hotels and restaurants & Other services \\
23 & Inland transport & Transport \\
24 & Water transport & Transport \\
25 & Air transport & Transport \\
26 & Other supporting and auxiliary transport activities & Transport \\
27 & Post and telecommunications & Prof.\ services \\
28 & Financial intermediation & Prof.\ services \\
29 & Real estate activities & Construction \\
30 & Renting of machinery and equipment and other business activities & Prof.\ services \\
31 & Public administration and defense; compulsory social security & Government \\
32 & Education & Education \\
33 & Health and social work & Health \\
34 & Other community, social, and personal services & Other services \\
35 & Private households with employed persons & Other services \\
\bottomrule
\end{longtable}

\noindent
The decomposition uses these $G=15$ paper sectors as the \emph{using} sectors
(columns of $\Mmat$) and as the domestic \emph{source} rows, plus two source-only
rows -- \emph{Imported inputs} and \emph{Net taxes} -- giving the $17\times15$
matrix of Section~4.

\begin{thebibliography}{9}
\small

\bibitem{leontief1936}
Leontief, W. (1936). Quantitative input and output relations in the economic
system of the United States. \emph{Review of Economics and Statistics}, 18(3),
105--125. \textit{[Origin of the input--output model and the inverse used here.]}

\bibitem{millerblair2009}
Miller, R. E., \& Blair, P. D. (2009). \emph{Input--Output Analysis: Foundations
and Extensions} (2nd ed.). Cambridge University Press. \textit{[The standard
textbook; Chs.\ 2 and 6 cover $\A=\Z\Xhat^{-1}$, the Leontief inverse, and
value-added multipliers.]}

\bibitem{hummels2001}
Hummels, D., Ishii, J., \& Yi, K.-M. (2001). The nature and growth of vertical
specialization in world trade. \emph{Journal of International Economics}, 54(1),
75--96.

\bibitem{johnson2012}
Johnson, R. C., \& Noguera, G. (2012). Accounting for intermediates: Production
sharing and trade in value added. \emph{Journal of International Economics},
86(2), 224--236. \textit{[Value-added exports via $\vhat(\Id-\A)^{-1}$.]}

\bibitem{koopman2014}
Koopman, R., Wang, Z., \& Wei, S.-J. (2014). Tracing value-added and double
counting in gross exports. \emph{American Economic Review}, 104(2), 459--494.
\textit{[Canonical decomposition of gross exports into domestic and foreign
value-added terms.]}

\bibitem{wang2013}
Wang, Z., Wei, S.-J., \& Zhu, K. (2013). Quantifying international production
sharing at the bilateral and sector levels. \emph{NBER Working Paper} No.\ 19677.

\bibitem{los2015}
Los, B., Timmer, M. P., \& de Vries, G. J. (2015). How global are global value
chains? A new approach to measure international fragmentation. \emph{Journal of
Regional Science}, 55(1), 66--92. \textit{[Hypothetical-extraction on the same
$\A$.]}

\bibitem{timmer2015}
Timmer, M. P., Dietzenbacher, E., Los, B., Stehrer, R., \& de Vries, G. J.
(2015). An illustrated user guide to the World Input--Output Database: the case
of global automotive production. \emph{Review of International Economics}, 23(3),
575--605. \textit{[Accessible walkthrough of the value-added multiplier used
above.]}

\bibitem{antras2018}
Antr\`as, P., \& Chor, D. (2018). On the measurement of upstreamness and
downstreamness in global value chains. In L.\ Y.\ Ing \& M.\ Yu (Eds.),
\emph{World Trade Evolution: Growth, Productivity and Employment} (pp.\
126--194). Routledge.

\end{thebibliography}

\vspace{4pt}
\noindent{\footnotesize These references are provided as reliable entry points to
the literature; please verify exact titles, years and page numbers against the
originals before formal citation.}

\end{document}
