Codigo Phyton

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

{

"cells": [
{
"cell_type": "markdown",
"id": "94aff61b",
"metadata": {},
"source": [
"Realizar análisis univariado (cada variable) de cada serie por separado. \n",
"\n",
"Decidir transformaciones y uso de cada una de ellas. \n",
"\n",
"Deben evaluar los diagramas de autocorrelación para definir los rezagos del modelo."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b79e75b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Consumo PIB\n",
"Fecha \n",
"2005-01-01 102.729 126.279\n",
"2005-04-01 104.878 128.499\n",
"2005-08-01 104.916 128.968\n",
"2005-12-01 106.554 131.107\n",
"2006-01-01 108.827 133.876\n"
]
},
{
"data": {
"image/png": " ",
"text/plain": [
"<Figure size 1400x700 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<Figure size 1000x400 with 0 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": " ",
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<Figure size 1000x400 with 0 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": " ",
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"\n",
"# a. Cargar datos desde Excel\n",
"#df1 = pd.read_excel(\"Consumo y PIB.xlsx\", sheet_name=1, header=0, index_col=0)\n",
"#df1\n",
"\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from statsmodels.graphics.tsaplots import plot_acf\n",
"\n",
"# Datos\n",
"data = {\n",
" 'Fecha': [\n",
" '01/01/2005', '01/04/2005', '01/08/2005', '01/12/2005',\n",
" '01/01/2006', '01/04/2006', '01/08/2006', '01/12/2006',\n",
" '01/01/2007', '01/04/2007', '01/08/2007', '01/12/2007',\n",
" '01/01/2008', '01/04/2008', '01/08/2008', '01/12/2008',\n",
" '01/01/2009', '01/04/2009', '01/08/2009', '01/12/2009',\n",
" '01/01/2010', '01/04/2010', '01/08/2010', '01/12/2010',\n",
" '01/01/2011', '01/04/2011', '01/08/2011', '01/12/2011',\n",
" '01/01/2012', '01/04/2012', '01/08/2012', '01/12/2012',\n",
" '01/01/2013', '01/04/2013', '01/08/2013', '01/12/2013',\n",
" '01/01/2014', '01/04/2014', '01/08/2014', '01/12/2014',\n",
" '01/01/2015', '01/04/2015', '01/08/2015', '01/12/2015',\n",
" '01/01/2016', '01/04/2016', '01/08/2016', '01/12/2016',\n",
" '01/01/2017', '01/04/2017', '01/08/2017', '01/12/2017',\n",
" '01/01/2018', '01/04/2018', '01/08/2018', '01/12/2018',\n",
" '01/01/2019', '01/04/2019', '01/08/2019', '01/12/2019',\n",
" '01/01/2020', '01/04/2020', '01/08/2020', '01/12/2020',\n",
" '01/01/2021', '01/04/2021', '01/08/2021', '01/12/2021',\n",
" '01/01/2022', '01/04/2022'\n",
" ],\n",
" 'Consumo': [\n",
" 102.729, 104.878, 104.916, 106.554, 108.827, 110.885, 111.968, 113.293,\n",
" 115.383, 116.903, 119.542, 120.763, 121.22, 122.59, 123.808, 124.889,\n",
" 124.917, 125.324, 126.042, 127.133, 129.771, 131.157, 133.187, 134.887,\n",
" 135.936, 139.378, 140.698, 142.981, 144.719, 146.576, 148.403, 149.997,\n",
" 152.467, 154.831, 155.865, 158.103, 159.721, 160.973, 162.123, 165.317,\n",
" 166.34, 167.581, 170.426, 165.854, 168.776, 170.155, 170.679, 171.491,\n",
" 172.436, 174.055, 175.089, 175.393, 179.039, 181.139, 181.988, 182.482,\n",
" 184.897, 188.924, 190.109, 191.825, 192.817, 164.873, 175.386, 191.272,\n",
" 198.173, 202.975, 209.171, 214.692, 221.503, 228.431\n",
" ],\n",
" 'PIB': [\n",
" 126.279, 128.499, 128.968, 131.107, 133.876, 135.915, 138.828, 149.815,\n",
" 143.092, 145.191, 148.245, 149.929, 150.237, 151.62, 153.324, 150.532,\n",
" 151.298, 152.44, 153.858, 155.021, 156.758, 159.16, 160.368, 163.865,\n",
" 167.213, 169.45, 173.107, 174.858, 175.887, 178.081, 177.753, 179.695,\n",
" 182.342, 186.768, 188.144, 190.685, 193.329, 194.022, 195.876, 198.362,\n",
" 199.137, 200.794, 202.757, 202.004, 203.538, 204.785, 205.959, 207.207,\n",
" 206.659, 207.473, 208.759, 209.765, 210.784, 212.41, 214.869, 215.945,\n",
" 217.539, 219.312, 221.795, 222.579, 218.061, 183.445, 202.975, 214.634,\n",
" 221.699, 216.792, 230.741, 237.339, 240.557, 244.116\n",
" ]\n",
"}\n",
"\n",
"# Crear DataFrame\n",
"df = pd.DataFrame(data)\n",
"df['Fecha'] = pd.to_datetime(df['Fecha'], format='%d/%m/%Y')\n",
"df.set_index('Fecha', inplace=True)\n",
"\n",
"# Visualizar las primeras filas del DataFrame\n",
"print(df.head())\n",
"\n",
"# Realizar gráficos de las series temporales\n",
"plt.figure(figsize=(14, 7))\n",
"plt.subplot(2, 1, 1)\n",
"plt.plot(df['Consumo'], color='blue')\n",
"plt.title('Serie de Consumo')\n",
"plt.ylabel('Consumo')\n",
"plt.xlabel('Fecha')\n",
"\n",
"plt.subplot(2, 1, 2)\n",
"plt.plot(df['PIB'], color='red')\n",
"plt.title('Serie de PIB')\n",
"plt.ylabel('PIB')\n",
"plt.xlabel('Fecha')\n",
"\n",
"plt.tight_layout()\n",
"plt.show()\n",
"\n",
"# Función para visualizar los diagramas de autocorrelación\n",
"def plot_autocorrelation(series, title):\n",
" plt.figure(figsize=(10, 4))\n",
" plot_acf(series, lags=20, title=title)\n",
" plt.xlabel('Lag')\n",
" plt.ylabel('Autocorrelation')\n",
" plt.show()\n",
"\n",
"# Visualizar los diagramas de autocorrelación\n",
"plot_autocorrelation(df['Consumo'], 'Autocorrelación de Consumo')\n",
"plot_autocorrelation(df['PIB'], 'Autocorrelación de PIB')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef99677f",
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'Consumo y PIB.xlsx'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call
last)",
"",
"File ",
"File ",
"File ",
"File ",
"\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'Consumo
y PIB.xlsx'"
]
}
],
"source": [
"# Importar pandas\n",
"import pandas as pd\n",
"\n",
"# a. Cargar datos desde Excel\n",
"df1 = pd.read_excel(\"Consumo y PIB.xlsx\", sheet_name=0, header=0, index_col=0)\n",
"df1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "276ebce7",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like