*! version 4.0 21juin2021

Title

complexity computes several metrics of Complexity of specialization pattern (See ECI/PCI from Hidalgo et al. 2009, 20017, or fitness Tachela et al 2017).
For generalization purposes, we refer to individuals (rather than countries), and nodes (from any network rather than goods from product space).
complexity only requires a matrix of individuals' specialization over nodes as input - either expressed in raw performance or in Revealed Comparative Advantage (RCA) (see Haussman & Hidalgo, 2012).
This input can be either specified as Stata varlist, Stata matrix or mata matrix, with inidividuals in row and nodes in columns.
Three alternative methodologies are available. By default, the eigenvalue method is followed as detailed at the OEC methodology webpage.
The alternative methods are the inital Method of Reflection (MR, Hidalgo & Hausmann, 2009), and the Fitness index (Tacchela et al, 2012).

Syntax

complexity
,
varlist()
matrix()
matsource()
method()
rca
iterations()
projection()
transpose
xvar

Required
varlist() Varlist indicating the "nodes" or activities of specialization.

OR

matrix() Name of input matrix.

matsource() Indicates the input matrix type. Can be matrix for a matrix stored in Stata or mata for a mata matrix (default).

Optional

method() Indicates the algorithm followed. Can be eigenvalue (default if empty).
Alternative methods are mr to follow instead the Method of Reflection as in Hidalgo & Hausmann, 2009,
or fitness to follow the Fitness Complexity Method as detailed in Tacchella et al.(2012). Note that fitness at the node level is the reverse of its complexity.

rca To be specified if varlist() or matsource() correspond to Revealed Comparative Advantage matrix.

iterations Sets the number of iterations (for MR or fitness methods). If reached, the optimal level of iteration (i.e. when the final ranking doesn't vary anymore) is chosen. Iterations must be of even order.

projection() Indicates which complexity index to return. indiv would return the individuals' complexity (e.g. countries ECI), while nodes returns the nodes' complexity (e.g. product PCI).
In any case both are computed. If none are indicated, individuals' complexity is returned by default.

transpose Transpose initial matrix if individuals were in columns and nodes in rows.

xvar Doesn't return a Stata variable (only Stata and mata matrices are stored).

Stored result

complexity saves the following in r():

Vectors
r(Complexity_individual) Column vector of Individuals' complexity values
r(Complexity_node) Column vector of nodes' complexity values
r(Diversity) Column vector of Individuals' diversity (nb of nodes with RCA>=1 for each indiv)
r(Ubiquity) Column vector of nodes' Ubiquity (nb of individuals with RCA>=1 in this node)

The same vectors are stored in Mata, respectively under the names:
comp_i
comp_n
Diversity
Ubiquity

if the fitness method is specified the vectors returned are
r(fitness_individual) Column vector of Individuals' complexity values
r(fitness_node) Column vector of nodes' fitness values
r(complexity_node) Column vector of nodes' fitness values
and respective mata vectors are
fitness_i
fitness_n
complexity_n

Scalars
r(iterations) : Number of iterations used in MR or fitness method

Examples

To compute complexity index on a real set of data, please see this link:
This Statalist thread

Otherwise, on random example values:

Using varlist() input

. set obs 15

. gen n1=runiform()*10

. gen n2=runiform()*10

. gen n3=runiform()*10

. gen n4=runiform()*10

. complexity, varlist(n*)

. complexity, varlist(n*) rca method(mr)

Using matsource() input

. mata mat=(0.1,0.2,3,1.2 \ 0.5, 1, 1.5 , 1 \ 2.1 , 0 , 5, 0.5)

. mata st_matrix("Smat", mat)

. complexity, matsource(matrix) mat(Smat)

. complexity, matrix(mat) pro(nodes)

. complexity, matrix(mat) method(mr)

Notes

Requires moremata (available on SSC) package to run.

References

On the Method of Reflection for ECI/PCI indexes: Hidalgo & Hausmann, 2009
On the Eigenvector Method for ECI/PCI indexes: OEC methodology
On the Fitness index: Tacchella et al.(2012)

Author

Charlie Joyez, Université Côte d'Azur, France
charlie.joyez@univ-cotedazur.fr

Acknowledgment

This program benefited from fruitful discussions with Mauricio Vargas.