Different types of typical usage of the package

General usage

Importing the library

from geopolrisk.assessment.main import (
    gprs_calc,
)  # This is a one stop function to calculate all the values of the GeoPolRisk method

Prepare the lists

from geopolrisk.assessment.utils import (
    default_rmlist,
)  # This is a functions that returns the default list of metals that are available in the library database

ListofMetals = default_rmlist()
ListofAlternateMetals = [
    "Aluminium",
    "Copper",
    "Nickel",
    "Tin",
    "Zinc",
]  # If you want to use a different list of metals, you can specify them here (of course, they must be available in the library database)
# Aluminium is an exception in the database, it refers to primary metal not raw material. The raw material is bauxite

ListofCountries = [
    "China",
    "Germany",
    842,
    36,
]  # List of countries to be assessed, you can use the ISO 3166-1 numeric code & "EU" for European Union

ListofYear = [
    2022
]  # List of years to be assessed, you can only use the year as an integer

GeoPolRisk Calculation using main function

gprs_calc(
    ListofYear, ListofCountries, ListofMetals
)  # This is the main function that calculates the GeoPolRisk values for the specified list of countries, years and metals
# The function returns a pandas DataFrame with the results, which is also saved as an excel file in a folder named "ouput" in the document folder of the users home directory
Calculating the GeoPolRisk: 100%|██████████| 212/212 [00:08<00:00, 25.61it/s]
DBID Country [Economic Entity] Raw Material Year GeoPolRisk Score GeoPolRisk Characterization Factor [eq. Kg-Cu/Kg] HHI Import Risk Price
0 Aluminium1562022 China Aluminium 2022 0.003945 0.024357 0.353279 0.011166 2.527817
1 Antimony1562022 China Antimony 2022 0.076128 0.722417 0.276644 0.275184 3.885127
2 Arsenic1562022 China Arsenic 2022 0.000152 0.075353 0.393237 0.000387 202.750097
3 Asbestos1562022 China Asbestos 2022 0.108588 0.081655 0.334918 0.324222 0.307868
4 Baryte1562022 China Baryte 2022 0.001981 0.000536 0.151374 0.013085 0.110799
... ... ... ... ... ... ... ... ... ...
207 Tungsten362022 Australia Tungsten 2022 0.050577 4.720357 0.600702 0.084197 38.210378
208 Uranium362022 Australia Uranium 2022 0.000000 0.000000 0.247032 0.000000 0.000000
209 Vanadium362022 Australia Vanadium 2022 0.259805 6.958138 0.502120 0.517417 10.964943
210 Zinc362022 Australia Zinc 2022 0.008650 0.043827 0.138882 0.062280 2.074484
211 Zirconium362022 Australia Zirconium 2022 0.000821 0.004560 0.194716 0.004217 2.273523

212 rows × 9 columns

Using individual functions from the core module

Extract random values from the list above

import random

RandomMetal = random.choice(ListofMetals)
RandomCountry = random.choice(ListofCountries)
RandomYear = random.choice(ListofYear)

Calculate the HHI & extract the production quantity of a raw material in a country at a particular time period

from geopolrisk.assessment.core import (
    HHI,
)  # This is the Herfindahl-Hirschman Index function to calculate the concentration of production in a country for a specific metal and year

ProductionQuantity, hhi = HHI(RandomMetal, RandomYear, RandomCountry)

Calculate the import risk component of the GeoPolRisk method

from geopolrisk.assessment.core import (
    importrisk,
)  # This is the function to import risk values for a specific metal, year and country from the mapped baci data


from geopolrisk.assessment.utils import (
    mapped_baci,
)  # This is a function to get the mapped baci data, which is a pandas DataFrame with the trade data for the specified countries, years and metals

mapped_baci = (
    mapped_baci()
)  # This is a function that returns the mapped baci data, which is a pandas DataFrame with the trade data for the specified countries, years and metals


Numerator, TotalTrade, Price = importrisk(
    RandomMetal, RandomYear, RandomCountry, data=mapped_baci
)  # The functions returns the values required to calculate the import risk for the specified metal, year and country
# The values are the numerator of the import risk formula (i.e. the import share wieghted by the political stability of the exporting countries), the total trade value and the price of the metal in USD per kilogram

Calculate the Values of the GeoPolrisk Method

from geopolrisk.assessment.core import (
    GeoPolRisk,
)  # This is the main function to calculate the GeoPolRisk values based on the numerator, total trade, price, production quantity and Herfindahl-Hirschman Index


Values = GeoPolRisk(
    Numerator, TotalTrade, Price, ProductionQuantity, hhi
)  # This function returns a dictionary with the GeoPolRisk values for the specified metal, year and country
# The values are the GeoPolRisk Score, the characterization factor, the import risk
Values
"""
The three values correspond to "the GeoPolRisk Score", "the characterization factor (GSP)", "the import risk component"
"""
'\nThe three values correspond to "the GeoPolRisk Score", "the characterization factor (GSP)", "the import risk component"\n'

Company level assessment

Company data in the template file located in the “database” folder in the users document directory

from geopolrisk.assessment.core import importrisk_company

"""
Modify the template file located in the the 'geopolrisk/database' location of the document directory.
This function calculates the import risk values for a specific metal, year and country based on the company data in the database folder.
"""


Numerator_C, TotalTrade_C, Price_C = importrisk_company("Nickel", 2022)

Values_c = GeoPolRisk(Numerator_C, TotalTrade_C, Price_C, ProductionQuantity, hhi)
  rawMaterial  partnerDesc    qty  cifvalue  period  Notes  partnerISO  \
0      Nickel      Denmark  170.0    3196.0    2022    NaN         208   
1      Nickel  Netherlands    1.0      73.0    2022    NaN         528   
2      Nickel     Portugal  893.0    2782.0    2022    NaN         620   
3      Nickel        Egypt    4.0     113.0    2022    NaN         818   
4      Nickel          USA  498.0    3355.0    2022    NaN         842   

   partnerWGI cmdCode reporterDesc  reporterISO  
0    0.326998  Nickel      Company          999  
1    0.356539  Nickel      Company          999  
2    0.327856  Nickel      Company          999  
3    0.705611  Nickel      Company          999  
4    0.507201  Nickel      Company          999  

Company data using different parameters

from geopolrisk.assessment.core import HHI, importrisk_company, GeoPolRisk

ExcelFile = "C:\Random Folder\Company data.xlsx"  # Specify the path to the Company data.xlsx file
SheetName = "Template"  # Specify the sheet name in the Excel file
RawMaterial = (
    "Nickel"  # Specify the raw material for which you want to import risk values
)
Year = 2022  # Specify the year for which you want to import risk values

from geopolrisk.assessment.core import (
    HHI,
)  # This is the HHI function to calculate the concentration of production in a country for a specific metal and year

ProductionQuantity, hhi = HHI(RawMaterial, Year, "Company")
""" The parameter "Company" is used to specify that the production quantity is for a company, not a country. 
In the HHI function, this will be used to get the production quantity for the specified raw material and year. 
The production Quantity should be 0 as a default value.
If the company recycles the said raw material, user can manually provide it in "metric tonnes" as unit.
"""
Num_Company, TotalTrade_Company, Price_Company = importrisk_company(RawMaterial, 2022)
Values_Company = GeoPolRisk(
    Num_Company, TotalTrade_Company, Price_Company, ProductionQuantity, hhi
)
  rawMaterial  partnerDesc    qty  cifvalue  period  Notes  partnerISO  \
0      Nickel      Denmark  170.0    3196.0    2022    NaN         208   
1      Nickel  Netherlands    1.0      73.0    2022    NaN         528   
2      Nickel     Portugal  893.0    2782.0    2022    NaN         620   
3      Nickel        Egypt    4.0     113.0    2022    NaN         818   
4      Nickel          USA  498.0    3355.0    2022    NaN         842   

   partnerWGI cmdCode reporterDesc  reporterISO  
0    0.326998  Nickel      Company          999  
1    0.356539  Nickel      Company          999  
2    0.327856  Nickel      Company          999  
3    0.705611  Nickel      Company          999  
4    0.507201  Nickel      Company          999  
<>:3: SyntaxWarning: invalid escape sequence '\R'
<>:3: SyntaxWarning: invalid escape sequence '\R'
C:\Users\anish\AppData\Local\Temp\ipykernel_32080\2755402762.py:3: SyntaxWarning: invalid escape sequence '\R'
  ExcelFile = "C:\Random Folder\Company data.xlsx"  # Specify the path to the Company data.xlsx file

Regional level assessment

Define regions

# you can create regions or group of countries, the following is an example of how to create regions
NewRegions = {
    "EFTA": ["Iceland", "Norway", "Switzerland"],
    "NAFTA": ["Canada", "Mexico", "USA"],
}  # Liechtenstein has a customs union with Switzerland whose trade is not reported by UN Comtrade
from geopolrisk.assessment.utils import regions

# This function updates the regions in the library (temporary variable only) and must be instantiated for the above new regions to be used
regions(NewRegions)

Quickly check the regions

from geopolrisk.assessment.database import databases

databases.regionslist["EFTA"]
# A quick check to see if the regions have been updated in the library
['Iceland', 'Norway', 'Switzerland']

Main Function to run for the regional assessment

gprs_calc(ListofYear, ["EFTA", "NAFTA"], ListofMetals)
Calculating the GeoPolRisk: 100%|██████████| 106/106 [00:12<00:00,  8.79it/s]
DBID Country [Economic Entity] Raw Material Year GeoPolRisk Score GeoPolRisk Characterization Factor [eq. Kg-Cu/Kg] HHI Import Risk Price
0 AluminiumEFTA2022 EFTA Aluminium 2022 0.028892 0.250305 0.353279 0.081781 3.546990
1 AntimonyEFTA2022 EFTA Antimony 2022 0.115442 0.011542 0.276644 0.417296 0.040934
2 ArsenicEFTA2022 EFTA Arsenic 2022 0.164176 4.912686 0.393237 0.417499 12.250974
3 AsbestosEFTA2022 EFTA Asbestos 2022 0.189662 0.236331 0.334918 0.566294 0.510155
4 BaryteEFTA2022 EFTA Baryte 2022 0.070911 0.034209 0.151374 0.468447 0.197513
... ... ... ... ... ... ... ... ... ...
101 TungstenNAFTA2022 NAFTA Tungsten 2022 0.228728 18.544553 0.600702 0.380767 33.193985
102 UraniumNAFTA2022 NAFTA Uranium 2022 0.021769 5.251664 0.247032 0.088121 98.770603
103 VanadiumNAFTA2022 NAFTA Vanadium 2022 0.214775 16.578472 0.502120 0.427737 31.602510
104 ZincNAFTA2022 NAFTA Zinc 2022 0.010950 0.050845 0.138882 0.078843 1.901065
105 ZirconiumNAFTA2022 NAFTA Zirconium 2022 0.071479 0.394452 0.194716 0.367096 2.259305

106 rows × 9 columns