{
"cells": [
{
"cell_type": "markdown",
"id": "6fa254e4",
"metadata": {},
"source": [
"```{include} ../math-definitions.md\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bc1a1811",
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "33965f2b",
"metadata": {},
"source": [
"# El operador de rezagos\n",
"\n",
"## Definición de operador de series de tiempo\n",
"\n",
"Un operador de serie de tiempo es un “proceso” que transforma una o más series de tiempo en nuevas series de tiempo.\n",
"\n",
"Ejemplos:\n",
"\n",
"* Multiplicación escalar: $y_t = \\beta x_t$\n",
"* Suma: $y_t = x_t + w_t$\n",
"* Identidad: $y_t = 1y_t$\n",
"\n",
"Nótese que:\n",
"\\begin{equation*}\n",
"y_t = \\beta(x_t + w_t) = \\beta x_t + \\beta w_t\n",
"\\end{equation*}\n",
"\n",
"\n",
"\n",
"\n",
"## Operador de rezago\n",
"\n",
"El operador de rezago se denota por $\\Lag$ y se define como:\n",
"\n",
"\\begin{equation*}\n",
"\\Lag x_t \\equiv x_{t-1}\n",
"\\end{equation*}\n",
"\n",
"En general, se tiene que:\n",
"\n",
"\\begin{equation*}\n",
"\\Lag^k x_t = x_{t-k}\n",
"\\end{equation*}\n",
"\n",
"\n",
"{{ empieza_ejemplo }} Operador de rezagos y transformación de series {{ fin_titulo_ejemplo }}\n",
"Algunas de las transformaciones de la serie $y_t$ de la sección anterior pueden expresarse con el operador de rezagos:\n",
"\n",
"| Serie original | $y_t$ |\n",
"| :------------------------------- | :-------------------------------------------------------------------------------------- |\n",
"| Primera diferencia | $\\Delta y_t \\equiv y_t - y_{t-1} = y_t - \\Lag y_t = (1-\\Lag)y_t$ |\n",
"| Tasa de crecimiento | $\\Delta\\% y_t \\approx 100\\left(\\ln y_t - \\ln y_{t-1}\\right) = 100 (1-\\Lag)\\ln y_t$ |\n",
"| Diferencia interanual | $\\Delta_4 y_t \\equiv y_t - y_{t-4} = y_t - \\Lag^4 y_t = (1-\\Lag^4)y_t$ |\n",
"| Tasa de crecimiento interanual | $\\Delta_4\\% y_t \\approx 100\\left(\\ln y_t - \\ln y_{t-4}\\right) = 100 (1-\\Lag^4)\\ln y_t$ |\n",
"| Suavizada por media móvil | $y^s_t \\equiv \\tfrac{1}{4}\\left(y_t + y_{t-1} + y_{t-2} + y_{t-3}\\right) = \\tfrac{1}{4}\\left(1 + \\Lag + \\Lag^2 + \\Lag^3\\right)y_t$ |\n",
"{{ termina_ejemplo }}\n",
"\n",
"\n",
"(example-lags-and-diffs)=\n",
"{{ empieza_ejemplo }} Operador de rezagos {{ fin_titulo_ejemplo }}\n",
"El archivo `data/LandD.csv` tiene los siguientes datos ficticios"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e59f5af9",
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" y | \n",
"
\n",
" \n",
" trimestre | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-01-01 | \n",
" 10 | \n",
"
\n",
" \n",
" 2018-04-01 | \n",
" 13 | \n",
"
\n",
" \n",
" 2018-07-01 | \n",
" 10 | \n",
"
\n",
" \n",
" 2018-10-01 | \n",
" 8 | \n",
"
\n",
" \n",
" 2019-01-01 | \n",
" 15 | \n",
"
\n",
" \n",
" 2019-04-01 | \n",
" 16 | \n",
"
\n",
" \n",
" 2019-07-01 | \n",
" 14 | \n",
"
\n",
" \n",
" 2019-10-01 | \n",
" 11 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" y\n",
"trimestre \n",
"2018-01-01 10\n",
"2018-04-01 13\n",
"2018-07-01 10\n",
"2018-10-01 8\n",
"2019-01-01 15\n",
"2019-04-01 16\n",
"2019-07-01 14\n",
"2019-10-01 11"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"datos = pd.read_csv('https://github.com/randall-romero/econometria/raw/master/data/LandD.csv', index_col=0, parse_dates=True)\n",
"y = datos['y']\n",
"datos"
]
},
{
"cell_type": "markdown",
"id": "0fc4c069",
"metadata": {},
"source": [
"```{margin} Otras implementaciones\n",
"* {ref}`R`\n",
"```\n",
"Para calcular sus rezagos, diferencias, y diferencias estacionales con **Python**:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1e81504f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" y | \n",
" L_y | \n",
" L2_y | \n",
" D_y | \n",
" D2_y | \n",
" S_y | \n",
"
\n",
" \n",
" trimestre | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-01-01 | \n",
" 10 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2018-04-01 | \n",
" 13 | \n",
" 10.0 | \n",
" NaN | \n",
" 3.0 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 2018-07-01 | \n",
" 10 | \n",
" 13.0 | \n",
" 10.0 | \n",
" -3.0 | \n",
" -6.0 | \n",
" NaN | \n",
"
\n",
" \n",
" 2018-10-01 | \n",
" 8 | \n",
" 10.0 | \n",
" 13.0 | \n",
" -2.0 | \n",
" 1.0 | \n",
" NaN | \n",
"
\n",
" \n",
" 2019-01-01 | \n",
" 15 | \n",
" 8.0 | \n",
" 10.0 | \n",
" 7.0 | \n",
" 9.0 | \n",
" 5.0 | \n",
"
\n",
" \n",
" 2019-04-01 | \n",
" 16 | \n",
" 15.0 | \n",
" 8.0 | \n",
" 1.0 | \n",
" -6.0 | \n",
" 3.0 | \n",
"
\n",
" \n",
" 2019-07-01 | \n",
" 14 | \n",
" 16.0 | \n",
" 15.0 | \n",
" -2.0 | \n",
" -3.0 | \n",
" 4.0 | \n",
"
\n",
" \n",
" 2019-10-01 | \n",
" 11 | \n",
" 14.0 | \n",
" 16.0 | \n",
" -3.0 | \n",
" -1.0 | \n",
" 3.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" y L_y L2_y D_y D2_y S_y\n",
"trimestre \n",
"2018-01-01 10 NaN NaN NaN NaN NaN\n",
"2018-04-01 13 10.0 NaN 3.0 NaN NaN\n",
"2018-07-01 10 13.0 10.0 -3.0 -6.0 NaN\n",
"2018-10-01 8 10.0 13.0 -2.0 1.0 NaN\n",
"2019-01-01 15 8.0 10.0 7.0 9.0 5.0\n",
"2019-04-01 16 15.0 8.0 1.0 -6.0 3.0\n",
"2019-07-01 14 16.0 15.0 -2.0 -3.0 4.0\n",
"2019-10-01 11 14.0 16.0 -3.0 -1.0 3.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"datos['L_y'] = y.shift(1) # primer rezago\n",
"datos['L2_y'] = y.shift(2) # segundo rezago\n",
"datos['D_y'] = y.diff() # primera diferencia\n",
"datos['D2_y'] = y.diff().diff() # segunda diferencia\n",
"datos['S_y'] = y.diff(4) # diferencia estacional\n",
"datos"
]
},
{
"cell_type": "markdown",
"id": "58b5d124",
"metadata": {},
"source": [
"{{ termina_ejemplo }}\n",
"\n",
"\n",
"## Propiedades del operador de rezago\n",
"Sean $x_t, w_t$ dos series de tiempo. Entonces:\n",
"\n",
"* $\\Lag(\\beta x_t) = \\beta\\Lag x_t$\n",
"* $\\Lag(x_t + w_t) = \\Lag x_t + \\Lag w_t$\n",
"* $\\Lag(c) = c$\n",
"* $\\Lag^{-h} x_t = x_{t+h}$\n",
"* $\\Lag^{0} x_t = x_t$\n",
"* $(\\alpha \\Lag^{h} + \\beta\\Lag^k) x_t = \\alpha x_{t-h} + \\beta x_{t-k}$\n",
"\n",
"donde $\\alpha, \\beta, c$ son constantes.\n",
"\n",
"El operador tiene propiedades conmutativa, asociativa y distributiva.\n",
"\n",
"\n",
"\n",
"## Polinomio de rezagos\n",
"\n",
"El operador de rezagos sigue las reglas usuales de operaciones algebraicas. Por ejemplo:\n",
"\\begin{align*}\n",
"(a + b \\Lag)(c + d \\Lag)x_t &= (a + b \\Lag)(c x_t + d x_{t-1}) \\\\\n",
" &= a c x_t + a d x_{t-1} + bc x_{t-1} + bd x_{t-2} \\\\\n",
" &= \\left[ac + (ad+bc)\\Lag + bd \\Lag^2\\right]x_t\n",
"\\end{align*}\n",
"\n",
"Así, definimos un polinomio de rezagos de orden $p$:\n",
"\\begin{equation*}\n",
"\\left(1 + \\phi_1 \\Lag + \\phi_2 \\Lag^2 + \\dots + \\phi_p \\Lag^p\\right)x_t =\n",
"x_{t} + \\phi_1 x_{t-1} + \\phi_2 x_{t-2} + \\dots + \\phi_p x_{t-p}\n",
"\\end{equation*}\n",
"\n",
"\n",
"## Inverso de un polinomio de rezagos de grado 1 \n",
"\n",
"Considere la operación\n",
"\\begin{align*}\n",
"(1 - \\phi \\Lag)\\left(1 + \\phi \\Lag +\\dots + \\phi^k \\Lag^k\\right)x_t &= \\left(1 - \\phi^{k+1} \\Lag^{k+1}\\right)x_t\\\\\n",
" &= x_t - \\phi^{k+1}x_{t-k-1}\n",
"\\end{align*}\n",
"\n",
"Si $|\\phi| < 1$,\n",
"\\begin{equation*}\n",
"\\lim\\limits_{k\\to\\infty} \\phi^{k+1}x_{t-k-1} = 0\n",
"\\end{equation*}\n",
"\n",
"con lo que\n",
"\\begin{equation*}\n",
"(1 - \\phi \\Lag)\\left(1 + \\phi \\Lag + \\phi^2 \\Lag^2 + \\dots\\right)x_t = x_t\n",
"\\end{equation*}\n",
"\n",
"En este caso, escribimos\n",
"\\begin{equation*}\n",
"(1 - \\phi \\Lag)^{-1} = 1 + \\phi \\Lag + \\phi^2 \\Lag^2 + \\dots\n",
"\\end{equation*}\n",
"\n",
"\n",
"\n",
"\n",
"## Inverso de un polinomio de rezagos de grado $p$\n",
"\n",
"Consideremos el polinomio\n",
"\\begin{equation*}\n",
"\\Phi(L) \\equiv 1 - \\phi_1 \\Lag - \\dots - \\phi_p \\Lag^p\n",
"\\end{equation*}\n",
"\n",
"Si factorizamos el polinomio como\n",
"\\begin{equation*}\n",
"\\Phi(L) = (1-\\lambda_1\\Lag)(1-\\lambda_2\\Lag)\\cdots(1-\\lambda_p\\Lag)\n",
"\\end{equation*}\n",
"\n",
"Encontramos su inverso como\n",
"\\begin{align*}\n",
"\\Phi^{-1}(L) &= (1-\\lambda_1\\Lag)^{-1}\\cdots(1-\\lambda_p\\Lag)^{-1} \\\\\n",
" &=\\left(1 + \\lambda_1 \\Lag + \\lambda_1^2 \\Lag^2 + \\dots\\right)\\cdots\\left(1 + \\lambda_p \\Lag + \\lambda_p^2 \\Lag^2 + \\dots\\right)\n",
"\\end{align*}"
]
}
],
"metadata": {
"jupytext": {
"formats": "md:myst",
"text_representation": {
"extension": ".md",
"format_name": "myst"
}
},
"kernelspec": {
"display_name": "Python 3",
"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.13"
},
"source_map": [
17,
22,
25,
80,
85,
91,
98
],
"substitutions": {
"empieza_ejemplo": "\n
Ejemplo: \n",
"fin_titulo_ejemplo": "
",
"termina_ejemplo": "
"
}
},
"nbformat": 4,
"nbformat_minor": 5
}