### Install py-ecomplexity via terminal Source: https://github.com/harvard-growth-lab/py-ecomplexity/blob/master/README.md Commands to install the package using pip. ```bash pip install ecomplexity ``` ```bash pip install git+https://github.com/cid-harvard/py-ecomplexity@develop ``` -------------------------------- ### Calculate Continuous Product Proximity Matrix using Correlation Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt This example demonstrates calculating product proximity using a continuous correlation-based method instead of discrete co-occurrence. Set 'continuous=True' and 'presence_test' to 'rca' to use the correlation of RCA values. ```python import pandas as pd from ecomplexity import proximity data = pd.DataFrame({ 'year': [2020] * 12, 'country': ['USA', 'USA', 'USA', 'USA', 'CHN', 'CHN', 'CHN', 'CHN', 'DEU', 'DEU', 'DEU', 'DEU'], 'product': ['A', 'B', 'C', 'D'] * 3, 'exports': [100, 80, 60, 40, 50, 120, 90, 70, 80, 60, 100, 50] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Calculate continuous proximity using RCA correlations prox_df = proximity( data, trade_cols, continuous=True, presence_test='rca' ) print(prox_df) ``` -------------------------------- ### Calculate complexity and proximity with Python Source: https://github.com/harvard-growth-lab/py-ecomplexity/blob/master/README.md Example usage of the ecomplexity and proximity functions with trade data. ```python from ecomplexity import ecomplexity from ecomplexity import proximity # Import trade data from CID Atlas data_url = "https://intl-atlas-downloads.s3.amazonaws.com/country_hsproduct2digit_year.csv.zip" data = pd.read_csv(data_url, compression="zip", low_memory=False) data = data[['year','location_code','hs_product_code','export_value']] # Calculate complexity trade_cols = {'time':'year', 'loc':'location_code', 'prod':'hs_product_code', 'val':'export_value'} cdata = ecomplexity(data, trade_cols) # Calculate proximity matrix prox_df = proximity(data, trade_cols) ``` -------------------------------- ### Calculate ecomplexity with Continuous Proximity and KNN Density Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt This example calculates economic complexity using continuous product proximities (correlation-based) and limits density calculations to k-nearest neighbors. Ensure 'continuous' is set to True for correlation-based proximity and specify 'knn' for density calculation. ```python import pandas as pd from ecomplexity import ecomplexity # Trade data data = pd.DataFrame({ 'year': [2020] * 12, 'country': ['USA', 'USA', 'USA', 'CHN', 'CHN', 'CHN', 'DEU', 'DEU', 'DEU', 'JPN', 'JPN', 'JPN'], 'product': ['A', 'B', 'C'] * 4, 'exports': [100, 80, 20, 50, 200, 150, 120, 40, 90, 80, 100, 60] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Calculate with continuous proximity (correlation-based) and KNN density cdata = ecomplexity( data, trade_cols, continuous=True, asymmetric=False, knn=2, verbose=False ) print(cdata[['country', 'product', 'rca', 'density', 'coi', 'cog']]) ``` -------------------------------- ### Initialize and Calculate Complexity Metrics Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Demonstrates the workflow for initializing a ComplexityData object, calculating RCA and MCP, and deriving diversity and ubiquity metrics. ```python data = pd.DataFrame({ 'year': [2020, 2020, 2020, 2020], 'country': ['USA', 'USA', 'CHN', 'CHN'], 'product': ['tech', 'food', 'tech', 'food'], 'exports': [100, 50, 80, 120] }) cols_input = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Initialize ComplexityData object cdata = ComplexityData(data, cols_input, val_errors_flag='coerce') # Access cleaned data with standardized column names print("Cleaned data:") print(cdata.data) # Create full rectangular dataframe for specific time period cdata.create_full_df(t=2020) # Calculate RCA cdata.calculate_rca() print("\nRCA matrix shape:", cdata.rca_t.shape) print("RCA values:\n", cdata.rca_t) # Calculate MCP based on RCA threshold cdata.calculate_mcp( rca_mcp_threshold_input=1.0, rpop_mcp_threshold_input=1.0, presence_test='rca', pop=None, t=2020 ) print("\nMCP matrix:\n", cdata.mcp_t) # Calculate diversity and ubiquity diversity = np.nansum(cdata.mcp_t, axis=1) ubiquity = np.nansum(cdata.mcp_t, axis=0) print(f"\nDiversity (products per country): {diversity}") print(f"Ubiquity (countries per product): {ubiquity}") ``` -------------------------------- ### Calculate Complexity and Inspect Output Columns Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Shows how to run the ecomplexity function and iterate through the resulting DataFrame to inspect available complexity metrics. ```python import pandas as pd from ecomplexity import ecomplexity # Sample calculation data = pd.DataFrame({ 'year': [2020] * 6, 'country': ['USA', 'USA', 'CHN', 'CHN', 'DEU', 'DEU'], 'product': ['X', 'Y', 'X', 'Y', 'X', 'Y'], 'exports': [100, 50, 60, 150, 80, 70] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} result = ecomplexity(data, trade_cols, verbose=False) # Available output columns: output_columns = { 'diversity': 'Number of products country exports with comparative advantage (k_c,0)', 'ubiquity': 'Number of countries exporting the product with comparative advantage (k_p,0)', 'rca': "Revealed Comparative Advantage (Balassa's RCA index)", 'rpop': 'Exports per capita relative to world (only if presence_test != "rca")', 'mcp': 'Binary comparative advantage indicator (1 if RCA >= threshold)', 'eci': 'Economic Complexity Index - measures productive capabilities of countries', 'pci': 'Product Complexity Index - measures sophistication/complexity of products', 'density': 'Network density around product - connectedness in product space', 'coi': 'Complexity Outlook Index - potential for complexity growth', 'cog': 'Complexity Outlook Gain - expected gain from acquiring new product' } for col, desc in output_columns.items(): if col in result.columns: print(f"{col}: {desc}") print(f" Sample values: {result[col].head(3).tolist()}\n") ``` -------------------------------- ### Initialize ComplexityData Class Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt This snippet shows how to import and initialize the ComplexityData class, which is used internally for data processing, cleaning, and intermediate calculations in the ecomplexity library. It can be used for custom workflows. ```python import pandas as pd import numpy as np from ecomplexity.ComplexityData import ComplexityData ``` -------------------------------- ### Calculate ecomplexity with Pre-computed Binary MCP Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Use this function when you have pre-computed binary MCP values (0 or 1) for products and countries. The 'manual' presence test treats the 'val' column as binary MCP. ```python import pandas as pd from ecomplexity import ecomplexity # Data with pre-computed binary MCP values (0 or 1) data = pd.DataFrame({ 'year': [2020, 2020, 2020, 2020, 2020, 2020], 'country': ['USA', 'USA', 'CHN', 'CHN', 'DEU', 'DEU'], 'product': ['aerospace', 'textiles', 'aerospace', 'textiles', 'aerospace', 'textiles'], 'mcp_binary': [1, 0, 0, 1, 1, 0] # Pre-computed comparative advantage }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'mcp_binary'} # Use manual presence test with pre-computed MCP cdata = ecomplexity( data, trade_cols, presence_test='manual' # Treats 'val' column as binary MCP ) print(cdata[['year', 'country', 'product', 'mcp', 'diversity', 'ubiquity', 'eci', 'pci']]) ``` -------------------------------- ### Calculate Asymmetric Product Proximity Matrix Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Generate an asymmetric proximity matrix where the proximity from product A to product B may differ from B to A. This is useful for understanding directional relationships in co-export patterns. ```python import pandas as pd from ecomplexity import proximity data = pd.DataFrame({ 'year': [2020] * 9, 'country': ['USA', 'USA', 'USA', 'CHN', 'CHN', 'CHN', 'DEU', 'DEU', 'DEU'], 'product': ['tech', 'auto', 'pharma'] * 3, 'exports': [200, 150, 100, 300, 80, 50, 100, 250, 180] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Calculate asymmetric proximity # Asymmetric proximity measures: given country exports product A, # what's the probability it also exports product B? prox_df = proximity( data, trade_cols, asymmetric=True # Generate asymmetric proximity matrix ) # Proximity from tech to auto may differ from auto to tech print(prox_df[['year', 'product_1', 'product_2', 'proximity']]) ``` -------------------------------- ### Use RPOP Presence Test for Comparative Advantage Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Calculate complexity indices using RPOP (exports per capita relative to world average) as the presence test for comparative advantage. Requires providing population data separately. The 'pop' argument takes a DataFrame with population information. ```python import pandas as pd from ecomplexity import ecomplexity # Trade data trade_data = pd.DataFrame({ 'year': [2020, 2020, 2020, 2020], 'country': ['USA', 'USA', 'CHN', 'CHN'], 'product': ['tech', 'food', 'tech', 'food'], 'exports': [500, 200, 800, 600] }) # Population data (must have time, location, population columns in that order) pop_data = pd.DataFrame({ 'year': [2020, 2020], 'country': ['USA', 'CHN'], 'population': [330000000, 1400000000] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Calculate using RPOP-based presence test cdata = ecomplexity( trade_data, trade_cols, presence_test='rpop', rpop_mcp_threshold=1.0, pop=pop_data ) # Output includes both rca and rpop columns print(cdata[['year', 'country', 'product', 'rca', 'rpop', 'mcp', 'eci']]) ``` -------------------------------- ### Provide Manual MCP for Presence Test Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Calculate complexity indices by providing pre-computed binary comparative advantage values (MCP) directly. This bypasses the need for RCA or RPOP calculations. ```python import pandas as pd from ecomplexity import ecomplexity ``` -------------------------------- ### Calculate Proximity Matrix Source: https://github.com/harvard-growth-lab/py-ecomplexity/blob/master/README.md This endpoint calculates the product proximity matrix, indicating how likely products are to be co-exported by countries. It supports both symmetric and asymmetric calculations and options for nearest neighbors. ```APIDOC ## POST /calculate/proximity ### Description Calculates the product proximity matrix based on co-export data. ### Method POST ### Endpoint /calculate/proximity ### Parameters #### Request Body - **data** (pandas dataframe) - Required - DataFrame containing production or trade data with variables for time, location, product, and value. - **cols_input** (dict) - Required - Dictionary mapping column names to 'time', 'loc', 'prod', and 'val'. Example: `{'time':'year', 'loc':'origin', 'prod':'hs92', 'val':'export_val'}`. - **continuous** (bool) - Optional - If True, uses correlation for product proximities. If False (default), uses product co-occurrence. - **asymmetric** (bool) - Optional - If True, generates an asymmetric proximity matrix. If False (default), generates a symmetric matrix. - **knn** (int) - Optional - Number of nearest neighbors to use for density calculation. If None (default), uses the entire proximity matrix. ### Request Example ```json { "data": "[pandas dataframe]", "cols_input": {"time":"year", "loc":"location_code", "prod":"hs_product_code", "val":"export_value"}, "continuous": false, "asymmetric": false, "knn": null } ``` ### Response #### Success Response (200) - **proximity_matrix** (dict) - Dictionary representing the calculated proximity matrix. #### Response Example ```json { "proximity_matrix": { "product_a": {"product_b": 0.8, "product_c": 0.2}, "product_b": {"product_a": 0.7, "product_c": 0.5} } } ``` ``` -------------------------------- ### Calculate Symmetric Product Proximity Matrix Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt This function calculates product proximity matrices based on co-export patterns. Use this to understand how related products are. The default is a symmetric matrix, meaning proximity(A, B) == proximity(B, A). ```python import pandas as pd from ecomplexity import proximity # Trade data across multiple years data = pd.DataFrame({ 'year': [2019, 2019, 2019, 2020, 2020, 2020, 2019, 2019, 2019, 2020, 2020, 2020], 'country': ['USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'CHN', 'CHN', 'CHN', 'CHN', 'CHN', 'CHN'], 'product': ['cars', 'planes', 'chips', 'cars', 'planes', 'chips', 'cars', 'planes', 'chips', 'cars', 'planes', 'chips'], 'exports': [100, 200, 150, 110, 210, 160, 80, 50, 300, 90, 55, 350] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'exports'} # Calculate proximity matrix prox_df = proximity(data, trade_cols) # Output has columns: year, product_1, product_2, proximity # Each row represents proximity between a pair of products for a given year print(prox_df) # Filter to see specific product relationships cars_proximity = prox_df[prox_df['product_1'] == 'cars'] print(f"\nProximity of other products to 'cars':\n{cars_proximity}") ``` -------------------------------- ### Configure RCA Threshold for Comparative Advantage Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Calculate complexity indices using a custom RCA threshold to define comparative advantage. Products with RCA above the specified threshold are considered part of a country's export basket. Set verbose=True for detailed output. ```python import pandas as pd from ecomplexity import ecomplexity # Sample trade data data = pd.DataFrame({ 'year': [2020, 2020, 2020, 2020, 2020, 2020], 'country': ['USA', 'USA', 'CHN', 'CHN', 'DEU', 'DEU'], 'product': ['cars', 'electronics', 'cars', 'electronics', 'cars', 'electronics'], 'export_value': [100, 50, 80, 200, 150, 30] }) trade_cols = {'time': 'year', 'loc': 'country', 'prod': 'product', 'val': 'export_value'} # Use higher RCA threshold (more stringent comparative advantage) cdata = ecomplexity( data, trade_cols, presence_test='rca', rca_mcp_threshold=1.5, # Only count RCA >= 1.5 as comparative advantage verbose=True ) print(cdata[['year', 'country', 'product', 'rca', 'mcp', 'eci', 'pci']]) ``` -------------------------------- ### Calculate Economic Complexity Source: https://github.com/harvard-growth-lab/py-ecomplexity/blob/master/README.md This endpoint calculates the Economic Complexity Index (ECI) and Product Complexity Index (PCI) from trade data. It allows customization of parameters such as presence test, thresholds, and population data. ```APIDOC ## POST /calculate/ecomplexity ### Description Calculates the Economic Complexity Index (ECI) and Product Complexity Index (PCI) from provided trade data. ### Method POST ### Endpoint /calculate/ecomplexity ### Parameters #### Request Body - **data** (pandas dataframe) - Required - DataFrame containing production or trade data with variables for time, location, product, and value. - **cols_input** (dict) - Required - Dictionary mapping column names to 'time', 'loc', 'prod', and 'val'. Example: `{'time':'year', 'loc':'origin', 'prod':'hs92', 'val':'export_val'}`. - **presence_test** (str) - Optional - Test used for industry presence in a location. Options: "rca" (default), "rpop", "both", or "manual". - **val_errors_flag** (str) - Optional - Error handling for numeric conversion. Options: 'coerce', 'ignore', 'raise'. Default: 'coerce'. - **rca_mcp_threshold** (numeric) - Optional - RCA threshold for MCP calculation. Default: 1. - **rpop_mcp_threshold** (numeric) - Optional - RPOP threshold for MCP calculation. Default: 1. Used only if presence_test is not "rca". - **pop** (pandas df) - Optional - DataFrame with time, location, and population data. Not required if presence_test is "rca". ### Request Example ```json { "data": "[pandas dataframe]", "cols_input": {"time":"year", "loc":"location_code", "prod":"hs_product_code", "val":"export_value"}, "presence_test": "rca", "val_errors_flag": "coerce", "rca_mcp_threshold": 1, "pop": null } ``` ### Response #### Success Response (200) - **eci_pci_data** (dict) - Dictionary containing calculated ECI and PCI values. - **mcp_data** (dict) - Dictionary containing calculated Modified Competitive Position (MCP) values. #### Response Example ```json { "eci_pci_data": { "eci": [ ... ], "pci": [ ... ] }, "mcp_data": { "mcp": [ ... ] } } ``` ``` -------------------------------- ### Calculate Economic Complexity Indices Source: https://context7.com/harvard-growth-lab/py-ecomplexity/llms.txt Primary function to compute ECI, PCI, RCA, and other complexity metrics from trade data. Requires a pandas DataFrame with time, location, product, and value columns. Uses RCA-based presence test by default. ```python import pandas as pd from ecomplexity import ecomplexity # Load trade data (example using CID Atlas data) data_url = "https://intl-atlas-downloads.s3.amazonaws.com/country_hsproduct2digit_year.csv.zip" data = pd.read_csv(data_url, compression="zip", low_memory=False) data = data[['year', 'location_code', 'hs_product_code', 'export_value']] # Define column mapping trade_cols = { 'time': 'year', 'loc': 'location_code', 'prod': 'hs_product_code', 'val': 'export_value' } # Calculate complexity indices with default settings (RCA-based presence test) cdata = ecomplexity(data, trade_cols) # Output columns include: # - diversity: number of products country exports with RCA >= 1 # - ubiquity: number of countries that export the product with RCA >= 1 # - rca: Revealed Comparative Advantage (Balassa index) # - mcp: Binary indicator (1 if RCA >= threshold) # - eci: Economic Complexity Index (country-level) # - pci: Product Complexity Index (product-level) # - density: Network density around each product # - coi: Complexity Outlook Index # - cog: Complexity Outlook Gain print(cdata[['year', 'location_code', 'hs_product_code', 'eci', 'pci', 'rca', 'density']].head()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.