{ "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": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "irfplot = lp.IRFPlot(irf=irf, # take output from the estimated model\n", " response=['invest'], # plot only response of invest ...\n", " shock=endog, # ... to shocks from all variables\n", " n_columns=2, # max 2 columns in the figure\n", " n_rows=2, # max 2 rows in the figure\n", " maintitle='Panel LP: IRFs of Investment', # self-defined title of the IRF plot\n", " show_fig=True, # display figure (from plotly)\n", " save_pic=False # don't save any figures on local drive\n", " )" ] }, { "cell_type": "markdown", "id": "1cf0a209", "metadata": {}, "source": [ "## Time Series Local Projections" ] }, { "cell_type": "code", "execution_count": 10, "id": "709f987c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function TimeSeriesLP in module localprojections.lp:\n", "\n", "TimeSeriesLP(data, Y, response, horizon, lags, newey_lags=4, ci_width=0.95)\n", " ### TimeSeriesLP\n", " ## Input attributes\n", " # This is almost identical procedurally with the Panel version\n", " # data = pandas dataframe (each row = 1 period)\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", " # newey_lags = max lags for Newey-West HAC SEs\n", " # ci_width = float within (0, 1) indicating the width of the confidence band (e.g., 0.95 for 95% CI)\n", "\n" ] } ], "source": [ "help(lp.TimeSeriesLP)" ] }, { "cell_type": "code", "execution_count": 11, "id": "6d583f27", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\valte\\anaconda3\\lib\\site-packages\\localprojections\\lp.py:166: FutureWarning:\n", "\n", "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", "\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", "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": { "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.2738281344461584, 1.5839805920044405, 2.239800089482526, 2.551943587350621, 2.1250995241293893, 1.8027561939533037, -0.9388171671513561, -4.066848682500548, 7.764096451050332 ], "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.1193046198444339, 1.1602486173753195, 1.1788448601972534, 0.673272782480473, 0.2454234828122639, 0.8638470154926444, -2.0008971810784573, -5.062420212893805, 7.605695430466929 ], "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.428351649047883, 2.0077125666335616, 3.3007553187677985, 4.430614392220769, 4.004775565446515, 2.741665372413963, 0.12326284677574484, -3.071277152107291, 7.922497471633735 ], "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.04636850528603936, -0.039625463855456164, 0.027890676545248183, -0.013986220843540426, 0.057314617439851306, 0.0021840752922778206, -0.15578951202603813, -0.268168043193618, 0.2648337611707484 ], "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.1894844695524651, -0.26575485758862805, -0.16770798328983058, -0.2017591793502154, -0.15883266105483684, -0.2007089806878094, -0.2775847708308693, -0.4245985626152968, 0.21082458560409262 ], "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.09674745898038636, 0.18650392987771575, 0.22348933638032695, 0.17378673766313454, 0.27346189593453946, 0.20507713127236504, -0.033994253221206944, -0.11173752377193913, 0.3188429367374042 ], "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": [ 0.5448740026969056, -0.22667135898545632, -0.9429603853285123, 0.07306552702149377, 0.2706599753955812, 1.584148674007118, 4.109566141711654, 4.466807373015488, -2.5241687304771814 ], "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.6705931616075061, -2.1949133557565483, -2.4487203203275363, -1.0022086387077767, -0.2459588511407519, 0.8189081131887944, 2.7622181044064815, 1.657561620981272, -3.7880327433333836 ], "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.7603411670013172, 1.7415706377856357, 0.5627995496705115, 1.148339692750764, 0.7872788019319142, 2.3493892348254417, 5.4569141790168265, 7.276053125049704, -1.2603047176209794 ], "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": "Single Entity Time Series 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": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = grunfeld.load_pandas().data # import the Grunfeld investment data set\n", "df = df[df['firm'] == 'General Motors'] # keep only one entity (as an example of a single entity time series setting)\n", "df = df.set_index(['year']) # set time variable as index\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\n", "\n", "# Use TimeSeriesLP for the single entity case\n", "irf = lp.TimeSeriesLP(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", " newey_lags=2, # maximum lags when estimating Newey-West standard errors\n", " ci_width=opt_ci # width of confidence band\n", " )\n", "irfplot = lp.IRFPlot(irf=irf, # take output from the estimated model\n", " response=['invest'], # plot only response of invest ...\n", " shock=endog, # ... to shocks from all variables\n", " n_columns=2, # max 2 columns in the figure\n", " n_rows=2, # max 2 rows in the figure\n", " maintitle='Single Entity Time Series LP: IRFs of Investment', # self-defined title of the IRF plot\n", " show_fig=True, # display figure (from plotly)\n", " save_pic=False # don't save any figures on local drive\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "10bb60ea", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.18" } }, "nbformat": 4, "nbformat_minor": 5 }