Functions to augment the user’s dataset with information from official sources.
gingado provides data augmentation functionalities that can help users to augment their datasets with a time series dimension. This can be done both on a stand-alone basis as the user incorporates new data on top of the original dataset, or as part of a scikit-learnPipeline that also includes other steps like data transformation and model estimation.
Data augmentation with SDMX
The Statistical Data and Metadata eXchange (SDMX) is an ISO standard comprising:
technical standards
statistical guidelines, including cross-domain concepts and codelists
an IT architecture and tools
SDMX is sponsored by the Bank for International Settlements, European Central Bank, Eurostat, International Monetary Fund, Organisation for Economic Co-operation and Development, United Nations, and World Bank Group.
More information about the SDMX is available on its webpage.
gingado uses SDMX to augment user datasets through the transformer AugmentSDMX.
For example, the code below is a simple illustration of AugmentSDMX augmentation under two scenarios: without a variance threshold (ie, including all data regardless if they are constants) or with a relatively high variance threshold (such that no data is actually added).
In both cases, the object is using the default data flow, which is the daily series of monetary policy rates set by central banks.
These AugmentSDMX objects are used to augment a data frame with simulated data for illustrative purposes. In real life, this data would be the user’s original data.
Querying data from BIS's dataflow 'WS_CBPOL_D' - Policy rates daily...
No columns added to original data because no feature in x meets the variance threshold 10.00000
/Users/douglasaraujo/Coding/.venv_gingado/lib/python3.10/site-packages/sklearn/feature_selection/_variance_threshold.py:104: RuntimeWarning: Degrees of freedom <= 0 for slice.
self.variances_ = np.nanvar(X, axis=0)
X augmented with data from SDMX with the same number of samples but more columns
Compatibility with scikit-learn
As mentioned above, gingado’s transformers are built to be compatible with scikit-learn. The code below demonstrates this compatibility.
First, we create the example dataset. In this case, it comprises the daily foreign exchange rate of selected currencies to the Euro. The Brazilian Real (BRL) is chosen for this example as the dependent variable.
from gingado.utils import load_SDMX_data, Lagfrom sklearn.model_selection import TimeSeriesSplit
X = load_SDMX_data( sources={'ECB': 'EXR'}, keys={'FREQ': 'D', 'CURRENCY': ['EUR', 'AUD', 'BRL', 'CAD', 'CHF', 'GBP', 'JPY', 'SGD', 'USD']}, params={"startPeriod": 2003} )# drop rows with empty valuesX.dropna(inplace=True)# adjust column names in this simple example for ease of understanding:# remove parts related to source and dataflow namesX.columns = X.columns.str.replace("ECB__EXR_D__", "").str.replace("__EUR__SP00__A", "")X = Lag(lags=1, jump=0, keep_contemporaneous_X=True).fit_transform(X)y = X.pop('BRL')# retain only the lagged variables in the X variableX = X[X.columns[X.columns.str.contains('_lag_')]]
Querying data from ECB's dataflow 'EXR' - Exchange Rates...
2023-06-18 11:29:19,756 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
Next, the data augmentation object provided by gingado adds more data. In this case, for brevity only one dataflow from one source is listed. If users want to add more SDMX sources, simply add more keys to the dictionary. And if users want data from all dataflows from a given source provided the keys and parameters such as frequency and dates match, the value should be set to 'all', as in {'ECB': ['CISS'], 'BIS': 'all'}.
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
Querying data from BIS's dataflow 'WS_CBPOL_D' - Policy rates daily...
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
Querying data from BIS's dataflow 'WS_CBPOL_D' - Policy rates daily...
2023-06-18 11:31:53,806 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
2023-06-18 11:33:30,655 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
This is the dataset now after this particular augmentation:
print(f"No of columns: {len(X_train__fit_transform.columns)}{X_train__fit_transform.columns}")X_train__fit_transform
Tuning the data augmentation to enhance model performance
And since AugmentSDMX can be included in a Pipeline, it can also be fine-tuned by parameter search techniques (such as grid search), further helping users make the best of available data to enhance performance of their models.
Tip
Users can cache the data augmentation step to avoid repeating potentially lengthy data downloads. See the memory argument in the sklearn.pipeline.Pipeline documentation.
Fitting 2 folds for each of 2 candidates, totalling 4 fits
[Pipeline] ...... (step 1 of 3) Processing augmentation, total= 0.0s
[Pipeline] ............... (step 2 of 3) Processing imp, total= 0.0s
[Pipeline] ............ (step 3 of 3) Processing forest, total= 0.7s
[CV] END ...........................augmentation=passthrough; total time= 0.7s
[Pipeline] ...... (step 1 of 3) Processing augmentation, total= 0.0s
[Pipeline] ............... (step 2 of 3) Processing imp, total= 0.0s
[Pipeline] ............ (step 3 of 3) Processing forest, total= 1.3s
[CV] END ...........................augmentation=passthrough; total time= 1.3s
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
[Pipeline] ...... (step 1 of 3) Processing augmentation, total= 9.7s
[Pipeline] ............... (step 2 of 3) Processing imp, total= 0.2s
[Pipeline] ............ (step 3 of 3) Processing forest, total= 1.8s
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
[CV] END ..augmentation=AugmentSDMX(sources={'ECB': 'CISS'}); total time= 21.1s
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
[Pipeline] ...... (step 1 of 3) Processing augmentation, total= 18.3s
[Pipeline] ............... (step 2 of 3) Processing imp, total= 0.4s
[Pipeline] ............ (step 3 of 3) Processing forest, total= 4.4s
Querying data from ECB's dataflow 'CISS' - Composite Indicator of Systemic Stress...
[CV] END ..augmentation=AugmentSDMX(sources={'ECB': 'CISS'}); total time= 41.7s
[Pipeline] ...... (step 1 of 3) Processing augmentation, total= 0.0s
[Pipeline] ............... (step 2 of 3) Processing imp, total= 0.0s
[Pipeline] ............ (step 3 of 3) Processing forest, total= 2.0s
2023-06-18 11:37:46,240 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
2023-06-18 11:37:58,171 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
2023-06-18 11:38:07,471 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
2023-06-18 11:38:30,335 pandasdmx.reader.sdmxml - INFO: Use supplied dsd=… argument for non–structure-specific message
grid.best_params_
{'augmentation': 'passthrough'}
print(f"In this particular case, the best model was achieved by {'not 'if grid.best_params_['augmentation'] =='passthrough'else''}using the data augmentation.")
In this particular case, the best model was achieved by not using the data augmentation.
print(f"The last value in the training dataset was {y_train.tail(1).to_numpy()}. The predicted value was {y_pred_grid}, and the actual value was {y_test.to_numpy()}.")
The last value in the training dataset was [5.2244]. The predicted value was [5.236705], and the actual value was [5.279].
Sources of data
gingado seeks to only lists realiable data sources by choice, with a focus on official sources. This is meant to provide users with the trust that their dataset will be complemented by reliable sources. Unfortunately, it is not possible at this stage to include all official sources given the substantial manual and maintenance work. gingado leverages the existence of the Statistical Data and Metadata eXchange (SDMX), an organisation of official data sources that establishes common data and metadata formats, to download data that is relevant (and hopefully also useful) to users.
The function list_SDMX_sources returns a list of codes corresponding to the data sources available to provide gingado users with data through SDMX.
You can also see what the available dataflows are. The code below returns a dictionary where each key is the code for an SDMX source, and the values associated with each key are the code and name for the respective dataflows.
from gingado.utils import list_all_dataflows
dflows = list_all_dataflows()dflows
2023-06-18 11:43:54,715 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
2023-06-18 11:43:58,219 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
2023-06-18 11:44:19,840 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
2023-06-18 11:44:20,874 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
2023-06-18 11:44:25,735 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
2023-06-18 11:44:26,799 pandasdmx.reader.sdmxml - DEBUG: Truncate sub-microsecond time in <Prepared>
ABS_XML ABORIGINAL_POP_PROJ Projected population, Aboriginal and Torres St...
ABORIGINAL_POP_PROJ_REMOTE Projected population, Aboriginal and Torres St...
ABS_ABORIGINAL_POPPROJ_INDREGION Projected population, Aboriginal and Torres St...
ABS_ACLD_LFSTATUS Australian Census Longitudinal Dataset (ACLD):...
ABS_ACLD_TENURE Australian Census Longitudinal Dataset (ACLD):...
...
UNSD DF_UNData_UNFCC SDMX_GHG_UNDATA
WB DF_WITS_Tariff_TRAINS WITS - UNCTAD TRAINS Tariff Data
DF_WITS_TradeStats_Development WITS TradeStats Devlopment
DF_WITS_TradeStats_Tariff WITS TradeStats Tariff
DF_WITS_TradeStats_Trade WITS TradeStats Trade
Name: dataflow, Length: 3354, dtype: object
For example, the dataflows from the World Bank are: