{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we will demonstrate how to use `ivxj` package to compute the IVXJ estimates and the corresponding \\\\(t\\\\)-statistics for unbalanced panel data under a simple regression specification." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use the baseline sample of [Greenwood et al. (2022)](https://onlinelibrary.wiley.com/doi/full/10.1111/jofi.13105), which is an unbalanced panel that covers 42 countries from 1950 to 2016 annually. They investigate the empirical association via the panel local projection:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{math}\n", "y_{i,t+h} = \\mu_{y,i}^{(h)} + \\beta^{(h)*} \\cdot x_{i,t}^{\\mathrm{diff}} + e_{i,t+h},\\quad h=1,2,3.\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The dependent variable \\\\(y_{i,t+h}\\\\) is a binary indicator of financial crisis occurring in country \\\\(i\\\\) in any year between year \\\\(t+1\\\\) and year \\\\(t+h\\\\), and the regressor \\\\(x_{i,t}^{\\mathrm{diff}}\\\\) is the three-year debt growth relative to either GDP or CPI. And we run the regression when \\\\(h = 1\\\\) in this example.\n", "\n", "Four measures of normalized debt are used, and in this example, we use this measure:\n", "Ratio of private debt to GDP (\\\\(\\mathit{Debt}^{\\mathit{Priv}}/\\mathit{GDP}\\\\)).\n", "\n", "\n", "For IVX we set \\\\(c_{z}=-1\\\\) and \\\\(\\theta=0.95\\\\) as in our paper, thereby \\\\(\\rho_{z}=1-1 / \\max_{i \\leq n} T_{i}^{0.95}\\\\), where \\\\(T_{i}\\\\) is the time span of country \\\\(i\\\\) in this unbalanced panel.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1.0\n" ] } ], "source": [ "import ivxj\n", "\n", "print(ivxj.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Processing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data is downloaded from [here](https://onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2Fjofi.13105&file=jofi13105-sup-0002-ReplicationCode.zip). The Data.csv can be found in this folder." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "data = pd.read_csv('Data.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dependent variable is `crisis_ind_bvx` and the regressor is `debt_to_gdp_private_d3`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data_selected = data[[\"country\", \"year\", \"crisis_ind_bvx\", \"debt_to_gdp_private_d3\"]].dropna()\n", "\n", "# delete individual time series less than 3 observations\n", "country_counts = data_selected[\"country\"].value_counts()\n", "valid_countries = country_counts[country_counts > 2].index\n", "data_filtered = data_selected[data_selected[\"country\"].isin(valid_countries)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Estimation" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "rhoz = np.float64(0.980174328998269)\n", "btaHat, btaHatDebias, se, rhoHat = ivxj.ivxj(data_filtered, rhoz)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`btaHat` is the IVX estimate for \\\\(\\beta^{(h)*}\\\\)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(0.0029654842208844436)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "btaHat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IVXJ estimate is `btaHatDebias`. Below, we print both the IVXJ estimate and its corresponding standard error `se`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IVXJ Estimate: 0.003058926878738318\n", "Standard Error: 0.0007471981592453964\n" ] } ], "source": [ "print(\"IVXJ Estimate:\", btaHatDebias)\n", "print(\"Standard Error:\", se)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we calculate the \\\\(t\\\\)-statistic for the hypothesis test." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "t-statistics: 4.093862974485325\n" ] } ], "source": [ "print(\"t-statistics:\", btaHatDebias / se)" ] } ], "metadata": { "kernelspec": { "display_name": "ivxjenv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 4 }