Skip to content

Commit

Permalink
adds relative relatedness
Browse files Browse the repository at this point in the history
Adds the relative_relatedness function, based on the `relative-relatedness` branch.
  • Loading branch information
hermosillajelmy authored May 1, 2024
1 parent a11e1af commit 476f30b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions economic_complexity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
proximity,
relatedness,
similarity,
relative_relatedness
)
from .rca import rca
from .subnational import complexity_subnational
Expand All @@ -33,4 +34,5 @@
"pgi",
"peii",
"complexity_subnational",
"relative_relatedness"
)
45 changes: 45 additions & 0 deletions economic_complexity/product_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,48 @@ def peii(
peii = _pmi(tbl=tbl, rcas=rcas, measure=emissions, cutoff=cutoff)
peii.rename(columns={peii.columns[0]: name}, inplace=True)
return peii


def relative_relatedness(
rcas: pd.DataFrame,
*,
cutoff: float = 1,
proximities: Optional[pd.DataFrame] = None,
) -> pd.DataFrame:
"""Calculates the Relative Relatedness, given a matrix of RCAs for the economic
activities of a location, and a matrix of Proximities.
### Args:
* rcas (pd.DataFrame) -- Matrix of RCAs for a certain location.
### Keyword Args:
* cutoff (float, optional) -- Set the cutoff threshold value.
Internally, RCA values under it will be set to zero, one otherwise.
Default value: `1`.
* proximities (pd.DataFrame, optional) -- Matrix with the proximity between the elements.
If not provided, will be calculated using the "max" procedure, and
the same cutoff value for this call.
### Returns:
(pd.DataFrame) -- A matrix with the probability that a location generates
comparative advantages in a economic activity.
"""

opp = rcas.copy()

if cutoff == 0:
opp = 1
else:
opp[opp >= cutoff] = pd.NA
opp[opp < cutoff] = 1

if proximities is None:
wcp = relatedness(rcas, cutoff)
else:
wcp = relatedness(rcas, cutoff, proximities)

wcp_opp = opp*wcp
wcp_mean = wcp_opp.mean(axis=1)
wcp_std = wcp_opp.std(axis=1)

return wcp.transform(lambda x: (x-wcp_mean)/wcp_std)
4 changes: 4 additions & 0 deletions tests/test_product_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ def test_pgi(df_global_exports, df_rca):

def test_peii():
pass

def test_relative_relatedness(df_rca):
relt = ec.relative_relatedness(df_rca)
pass

0 comments on commit 476f30b

Please sign in to comment.