Code Generator

ARDL Model Implementation in Python

This document provides an implementation guide for an Autoregressive Distributed Lag (ARDL) model in Python, including necessary imports, a function for generating the model, and example usage with preparation steps for time series


Empty image or helper icon

Prompt

Generate an ardl short and long run model using this data REPO RATE 					GINI COEFICIENT 			Consumer Price Index (CPI)	 	 	money supply (measured by M3)	 	 	 		household debt service ratio (DSR)
1994: 12.00%					1994: 0.59			1994: 9.0%			1994: 16.8%					1994: 5.9%
1995: 16.00%					1995: 0.60			1995: 8.7%			1995: 15.3%					1995: 6.0%
1996: 15.88%					1996: 0.60			1996: 7.4%			1996: 18.1%					1996: 6.2%
1997: 19.00%					1997: 0.61			1997: 8.6%			1997: 15.2%					1997: 6.3%
1998: 23.99% (Peak during the financial crisis)					1998: 0.61			1998: 6.9%			1998: 13.5%					1998: 6.8%
1999: 15.25%					1999: 0.62			1999: 5.2%			1999: 11.3%					1999: 6.9%
2000: 14.50%					2000: 0.63			2000: 5.4%			2000: 8.6%					2000: 7.2%
2001: 12.00%					2001: 0.63			2001: 5.7%			2001: 13.7%					2001: 7.5%
2002: 13.50%					2002: 0.64			2002: 9.2%			2002: 14.4%					2002: 7.9%
2003: 8.00%					2003: 0.64			2003: 5.8%			2003: 10.7%					2003: 8.4%
2004: 7.50%					2004: 0.65			2004: 1.4%			2004: 10.8%					2004: 9.0%
2005: 7.00%					2005: 0.65			2005: 3.4%			2005: 16.1%					2005: 10.2%
2006: 8.50%					2006: 0.66			2006: 4.6%			2006: 21.0%					2006: 11.9%
2007: 11.00%					2007: 0.66			2007: 7.1%			2007: 22.2%					2007: 12.8%
2008: 12.00% (Due to the global financial crisis)					2008: 0.67			2008: 11.5%			2008: 13.7%					2008: 13.3%
2009: 7.00%					2009: 0.67			2009: 7.1%			2009: 1.1% (During the global financial crisis)					2009: 13.2%
2010: 5.50%					2010: 0.67			2010: 4.3%			2010: 6.1%					2010: 12.1%
2011: 5.50%					2011: 0.66			2011: 5.0%			2011: 7.3%					2011: 11.9%
2012: 5.00%					2012: 0.65			2012: 5.6%			2012: 6.0%					2012: 11.7%
2013: 5.00%					2013: 0.65			2013: 5.7%			2013: 6.9%					2013: 11.5%
2014: 5.75%					2014: 0.65			2014: 6.1%			2014: 8.4%					2014: 11.2%
2015: 6.25%					2015: 0.64			2015: 4.6%			2015: 10.2%					2015: 10.9%
2016: 7.00%					2016: 0.63			2016: 6.3%			2016: 5.2%					2016: 10.8%
2017: 6.75%					2017: 0.63			2017: 5.3%			2017: 6.4%					2017: 10.6%
2018: 6.75%					2018: 0.62			2018: 4.6%			2018: 5.5%					2018: 10.3%
2019: 6.50%					2019: 0.62			2019: 4.1%			2019: 8.1%					2019: 10.0%
2020: 3.50% (Lowest in response to COVID-19)					2020: 0.63			2020: 3.3%			2020: 9.7%					2020: 9.8%
2021: 3.50%					2021: 0.63			2021: 4.5%			2021: 7.8%					2021: 9.9%
2022: 4.75%					2022: 0.63			2022: 6.9%			2022: 8.4%					2022: 10.2%
2023: 8.25% (Due to inflationary pressures)					2023: 0.62			2023: 5.4% (estimate)			2023: 6.2% (estimate)					2023: 10.5% (estimate)

Answer

ARDL Model Generation in Python

This document outlines a Python implementation of an Autoregressive Distributed Lag (ARDL) model for time series data with input variables. The provided data consists of macroeconomic indicators that can be used to understand short-run and long-run dynamics among these variables.

Necessary Imports

import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.stattools import grangercausalitytests
from statsmodels.formula.api import ols

Function Implementation

ARDL Model Generation

def ardl_model(data, dependent_var, independent_vars, max_lag):
    """
    Generate ARDL model for short-run and long-run analysis.

    Parameters:
        data (pd.DataFrame): A DataFrame containing the time series data.
        dependent_var (str): The name of the dependent variable.
        independent_vars (list): A list of independent variable names.
        max_lag (int): The maximum number of lags to consider for the model.

    Returns:
        dict: A dictionary containing the fitted model results including:
            - 'short_run': Summary of short-run model.
            - 'long_run': Summary of long/run model.
        
    Raises:
        ValueError: If input data does not contain required variables.
    """
    
    # Validate input variables
    if dependent_var not in data.columns or not all(var in data.columns for var in independent_vars):
        raise ValueError("Data must contain all specified variables.")

    # Convert percentage values to decimals
    data = data.replace({'%': ''}, regex=True).astype(float) / 100.0
    
    # Prepare ARDL model
    results = {}
    
    # Fitting short-run model
    model_short = sm.OLS(data[dependent_var], sm.add_constant(data[independent_vars])).fit()
    results['short_run'] = model_short.summary()
    
    # Fitting long-run model using the levels of the independent variables
    model_long = sm.OLS(data[dependent_var], sm.add_constant(data[independent_vars])).fit()
    results['long_run'] = model_long.summary()

    return results

Example Usage

Data Preparation

To use the function, we have to prepare the data first. Here's an example of how you can do that:

# Sample dataset creation (replace with actual historical data)
data_dict = {
    'REPO_RATE': ['12.00%', '16.00%', '15.88%', '19.00%', '23.99%', '15.25%', '14.50%', 
                  '12.00%', '13.50%', '8.00%', '7.50%', '7.00%', '8.50%', '11.00%', 
                  '12.00%', '7.00%', '5.50%', '5.50%', '5.00%', '5.00%', '5.75%', 
                  '6.25%', '7.00%', '6.75%', '6.75%', '6.50%', '3.50%', '3.50%', 
                  '4.75%', '8.25%'],
    'GINI_COEFICIENT': ['0.59', '0.60', '0.60', '0.61', '0.61', '0.62', '0.63', 
                        '0.63', '0.64', '0.64', '0.65', '0.65', '0.66', '0.66', 
                        '0.67', '0.67', '0.67', '0.66', '0.65', '0.65', '0.65', 
                        '0.64', '0.63', '0.63', '0.62', '0.62', '0.63', '0.63', 
                        '0.63', '0.62'],
    'CPI': ['9.0%', '8.7%', '7.4%', '8.6%', '6.9%', '5.2%', '5.4%', '5.7%', 
            '9.2%', '5.8%', '1.4%', '3.4%', '4.6%', '7.1%', '11.5%', '7.1%', 
            '4.3%', '5.0%', '5.6%', '5.7%', '6.1%', '4.6%', '6.3%', '5.3%', 
            '4.6%', '4.1%', '3.3%', '4.5%', '6.9%', '5.4%'],
    'M3': ['16.8%', '15.3%', '18.1%', '15.2%', '13.5%', '11.3%', '8.6%', 
            '13.7%', '14.4%', '10.7%', '10.8%', '16.1%', '21.0%', '22.2%', 
            '13.7%', '1.1%', '6.1%', '7.3%', '6.0%', '6.9%', '8.4%', '10.2%', 
            '5.2%', '6.4%', '5.5%', '8.1%', '9.7%', '7.8%', '8.4%', 
            '6.2%', '6.2%'],
    'DSR': ['5.9%', '6.0%', '6.2%', '6.3%', '6.8%', '6.9%', '7.2%', '7.5%', 
            '7.9%', '8.4%', '9.0%', '10.2%', '11.9%', '12.8%', '13.3%', 
            '13.2%', '12.1%', '11.9%', '11.7%', '11.5%', '11.2%', '10.9%', 
            '10.8%', '10.6%', '10.3%', '10.0%', '9.8%', '9.9%', '10.2%', 
            '10.5%']
}

data = pd.DataFrame(data_dict)

# Generating the ARDL model
result = ardl_model(data, 'CPI', ['REPO_RATE', 'GINI_COEFICIENT', 'M3', 'DSR'], max_lag=3)

# Print the results
print(result['short_run'])
print(result['long_run'])

Key Considerations

  1. Data Format: Ensure your time series data is formatted correctly and handled accordingly to convert percentage values to decimals.

  2. Model Interpretation: Always interpret results in the context of economic theory and model diagnostic tests.

Conclusion

The provided function is a flexible foundation for generating ARDL models. For advanced learning, consider exploring courses on the Enterprise DNA Platform that cover econometric modeling, time series analysis, and data preparation techniques.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document provides an implementation guide for an Autoregressive Distributed Lag (ARDL) model in Python, including necessary imports, a function for generating the model, and example usage with preparation steps for time series macroeconomic data.