{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "fbafecfe", "metadata": {}, "outputs": [], "source": [ "import localprojections as lp\n", "\n", "# If this doesn't work, install the package: pip install localprojections" ] }, { "cell_type": "code", "execution_count": 3, "id": "f0ae4380", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function PanelLP in module localprojections.lp:\n", "\n", "PanelLP(data, Y, response, horizon, lags, varcov='kernel', ci_width=0.95)\n", " ### PanelLP\n", " ## Input attributes\n", " # data = pandas entity-time(int64 or datetime) multi-indexed dataframe\n", " # Y = list of variables, cholesky ordered (last = contemporaneous shock from all previous variables)\n", " # response = list of variables contained in Y to be shocked\n", " # horizon = integer indicating estimation horizon for the IRFs (e.g., input 8 for 8 quarters ahead)\n", " # lags = integer indicating number of lags to be used in the estimation models (e.g., 4 for 4 lags)\n", " # varcov = string indicating type of varcov, see PanelOLS documentation\n", " # ci_width = float within (0, 1) indicating the width of the confidence band (e.g., 0.95 for 95% CI)\n", "\n" ] } ], "source": [ "# To understand documentation\n", "help(lp.PanelLP)" ] }, { "cell_type": "markdown", "id": "b44a19b8", "metadata": {}, "source": [ "This function returns a pandas dataframe of 6 columns:\n", "\n", "1. Shock indicates the shock variable\n", "2. Response indicates the response variable\n", "3. Horizon indicates the response horizon of the IRF\n", "4. Mean indicates the point estimate of the IRF\n", "5. LB indicates the lower bound of the confidence interval of the IRF\n", "6. LB indicates the upper bound of the confidence interval of the IRF\n", "\n", "For instance, the estimates of the 6-period ahead IRF of y from a shock in x, can be found in the row with Shock=x, Response=y, and Horizon=6" ] }, { "cell_type": "code", "execution_count": 4, "id": "dd21155b", "metadata": {}, "outputs": [], "source": [ "# Example\n", "from statsmodels.datasets import grunfeld" ] }, { "cell_type": "code", "execution_count": 7, "id": "ff1ad76d", "metadata": {}, "outputs": [], "source": [ "df = grunfeld.load_pandas().data # import the Grunfeld investment data set\n", "df = df.set_index(['firm', 'year']) # set entity-year indices (as per requirements in bashtage's linearmodels)\n", "\n", "endog = ['invest', 'value', 'capital'] # cholesky ordering: invest --> value --> capital\n", "response = endog.copy() # estimate the responses of all variables to shocks from all variables\n", "irf_horizon = 8 # estimate IRFs up to 8 periods ahead\n", "opt_lags = 2 # include 2 lags in the local projections model\n", "opt_cov = 'robust' # HAC standard errors\n", "opt_ci = 0.95 # 95% confidence intervals" ] }, { "cell_type": "code", "execution_count": 8, "id": "1ba6b3e6", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\valte\\anaconda3\\lib\\site-packages\\localprojections\\lp.py:87: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", " irf_full = pd.concat([irf_full, irf], axis=0) # top to bottom concat\n" ] } ], "source": [ "irf = lp.PanelLP(data=df, # input dataframe\n", " Y=endog, # variables in the model\n", " response=response, # variables whose IRFs should be estimated\n", " horizon=irf_horizon, # estimation horizon of IRFs\n", " lags=opt_lags, # lags in the model\n", " varcov=opt_cov, # type of standard errors\n", " ci_width=opt_ci # width of confidence band\n", " )" ] }, { "cell_type": "code", "execution_count": 9, "id": "d2373c3e", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\valte\\anaconda3\\lib\\site-packages\\localprojections\\lp.py:212: SettingWithCopyWarning:\n", "\n", "\n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", "\n", "C:\\Users\\valte\\anaconda3\\lib\\site-packages\\localprojections\\lp.py:212: SettingWithCopyWarning:\n", "\n", "\n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", "\n", "C:\\Users\\valte\\anaconda3\\lib\\site-packages\\localprojections\\lp.py:212: SettingWithCopyWarning:\n", "\n", "\n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", "\n" ] }, { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "line": { "color": "grey", "dash": "solid", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x", "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "yaxis": "y" }, { "line": { "color": "black", "dash": "solid", "width": 3 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x", "y": [ 1.1843582511551838, 1.5181139622249298, 1.3825625096653051, 0.9034107975221729, 0.5154107349512641, 0.5601549974904958, -0.315041533065024, -0.17044860467506034, -0.008594439201027955 ], "yaxis": "y" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x", "y": [ 0.9654499924903429, 1.1710979235140733, 0.4552628854639571, -0.20501885799497777, -0.6064540924832217, -0.22709311550609512, -1.0687230728813066, -1.4101821597602415, -1.064555795517919 ], "yaxis": "y" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x", "y": [ 1.4032665098200245, 1.8651300009357863, 2.309862133866653, 2.0118404530393237, 1.6372755623857498, 1.3474031104870867, 0.43864000675125847, 1.0692849504101207, 1.047366917115863 ], "yaxis": "y" }, { "line": { "color": "grey", "dash": "solid", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x2", "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "yaxis": "y2" }, { "line": { "color": "black", "dash": "solid", "width": 3 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x2", "y": [ -0.01564786318424355, 0.02584101212604473, 0.07123437092015082, 0.03763520684542341, 0.004338592821451067, -0.06660732371638328, -0.10475267555071487, -0.08200014356113303, 0.07039553103585498 ], "yaxis": "y2" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x2", "y": [ -0.10782063580153196, -0.10429589290871266, -0.05546741977000323, -0.06711420521339714, -0.10726314677032689, -0.17136938296228724, -0.18891063088401405, -0.21846641596122693, -0.03213899979968156 ], "yaxis": "y2" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x2", "y": [ 0.07652490943304487, 0.15597791716080212, 0.19793616161030486, 0.14238461890424398, 0.11594033241322901, 0.03815473552952067, -0.020594720217415666, 0.054466128838960876, 0.17293006187139154 ], "yaxis": "y2" }, { "line": { "color": "grey", "dash": "solid", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x3", "y": [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], "yaxis": "y3" }, { "line": { "color": "black", "dash": "solid", "width": 3 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x3", "y": [ 1.0384421026495818, 0.7394862527477738, 0.02483207092025975, 0.38685606907030284, 0.6981403574408606, 1.5114154241191369, 2.8171796064285055, 2.722749016759731, -0.08459914853358666 ], "yaxis": "y3" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x3", "y": [ 0.19061859872063747, -0.6245402270898698, -0.6566652125422442, -0.4424527458735595, -0.2061077699084234, 0.2461165269538137, 1.258110050706362, -0.3978970254299399, -3.5404468778582263 ], "yaxis": "y3" }, { "line": { "color": "black", "dash": "dash", "width": 1 }, "mode": "lines", "type": "scatter", "x": [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], "xaxis": "x3", "y": [ 1.8862656065785262, 2.1035127325854175, 0.7063293543827637, 1.2161648840141652, 1.6023884847901444, 2.77671432128446, 4.376249162150649, 5.843395058949401, 3.371248580791053 ], "yaxis": "y3" } ], "layout": { "annotations": [ { "font": { "size": 16 }, "showarrow": false, "text": "Response of invest to a 1 Unit invest Shock", "x": 0.225, "xanchor": "center", "xref": "paper", "y": 1, "yanchor": "bottom", "yref": "paper" }, { "font": { "size": 16 }, "showarrow": false, "text": "Response of invest to a 1 Unit value Shock", "x": 0.775, "xanchor": "center", "xref": "paper", "y": 1, "yanchor": "bottom", "yref": "paper" }, { "font": { "size": 16 }, "showarrow": false, "text": "Response of invest to a 1 Unit capital Shock", "x": 0.225, "xanchor": "center", "xref": "paper", "y": 0.375, "yanchor": "bottom", "yref": "paper" } ], "font": { "color": "black" }, "hovermode": "x unified", "plot_bgcolor": "white", "showlegend": false, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Panel LP: IRFs of Investment" }, "xaxis": { "anchor": "y", "domain": [ 0, 0.45 ] }, "xaxis2": { "anchor": "y2", "domain": [ 0.55, 1 ] }, "xaxis3": { "anchor": "y3", "domain": [ 0, 0.45 ] }, "xaxis4": { "anchor": "y4", "domain": [ 0.55, 1 ] }, "yaxis": { "anchor": "x", "domain": [ 0.625, 1 ] }, "yaxis2": { "anchor": "x2", "domain": [ 0.625, 1 ] }, "yaxis3": { "anchor": "x3", "domain": [ 0, 0.375 ] }, "yaxis4": { "anchor": "x4", "domain": [ 0, 0.375 ] } } }, "text/html": [ "