Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Ts Notebook
Ts Notebook
El objetivo de este análisis es identificar la influencia de las principales variables economicas en el tipo de cambio USD - DOP.
In [3]: df.head()
In [4]: df.info()
<class 'pandas.core.frame.DataFrame'>
Coeficientes de Variación
Determina la variable con mayor variabilidad
Exportaciones 0.18
Reservas 0.48
TPM 0.21
Turistas 0.32
Remesas 0.23
Out[7]: <AxesSubplot:>
Coeficientes de Variación sin periodo Covid
Determina la variable con mayor variabilidad excluyendo el 2020 pandemia.
Exportaciones 0.19
Reservas 0.48
TPM 0.19
Turistas 0.24
Remesas 0.23
Out[10]: <AxesSubplot:>
In [11]: df.set_index('Fecha',inplace=True)
promedio_movil = df.rolling(window=ventana).mean()
plt.figure(figsize=(15,5))
plt.title("Promedio movil\n ventana = {}\n {}".format(ventana,df.columns[0]))
plt.plot(promedio_movil,"g",label="Tendencia de Promedio Movil")
if intervalos:
error_absoluto_medio = mean_absolute_error(df[ventana:],df.rolling(window=ventana).mean()[ventana:])
desviacion_error = np.std(df[ventana:] - df.rolling(window=ventana).mean()[ventana:])
banda_inferior = df.rolling(window=ventana).mean()[ventana:] - (error_absoluto_medio + escala*desviacion_error)
banda_superior = df.rolling(window=ventana).mean()[ventana:] + (error_absoluto_medio + escala*desviacion_error)
plt.plot(banda_superior,"r--", label="Banda Superior /Banda Inferior")
plt.plot(banda_inferior,"r--")
if anomalias:
anomalias = pd.DataFrame(index=df.index, columns=df.columns)
anomalias[df[ventana:]<banda_inferior] = df[ventana:][df[ventana:]<banda_inferior]
anomalias[df[ventana:]>banda_superior] = df[ventana:][df[ventana:]>banda_superior]
plt.plot(anomalias,"ro",markersize=10)
Remesas
In [14]: GraficarPromedioMovil(remesas,30,intervalos=True,anomalias=True)
In [15]: decomposition = seasonal_decompose(remesas,period = 30)
tendencia = decomposition.trend
temporalidad = decomposition.seasonal
residuos = decomposition.resid
plt.figure(figsize=(15,12))
plt.subplot(411)
plt.plot(remesas, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(tendencia, label='Tendencia')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(temporalidad,label='Temporalidad')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residuos, label='Residuos')
plt.legend(loc='best')
plt.suptitle('Remesas',fontsize=15)
plt.tight_layout()
Exportaciones
In [16]: GraficarPromedioMovil(exportaciones,30,intervalos=True,anomalias=True)
In [17]: decomposition = seasonal_decompose(exportaciones,period = 30)
tendencia = decomposition.trend
temporalidad = decomposition.seasonal
residuos = decomposition.resid
plt.figure(figsize=(15,12))
plt.subplot(411)
plt.plot(exportaciones, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(tendencia, label='Tendencia')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(temporalidad,label='Temporalidad')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residuos, label='Residuos')
plt.legend(loc='best')
plt.suptitle('Exportaciones',fontsize=15)
plt.tight_layout()
Reservas
In [18]: GraficarPromedioMovil(reservas,30,intervalos=True,anomalias=True)
In [19]: decomposition = seasonal_decompose(reservas,period = 30)
tendencia = decomposition.trend
temporalidad = decomposition.seasonal
residuos = decomposition.resid
plt.figure(figsize=(15,12))
plt.subplot(411)
plt.plot(reservas, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(tendencia, label='Tendencia')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(temporalidad,label='Temporalidad')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residuos, label='Residuos')
plt.legend(loc='best')
plt.suptitle('Reservas',fontsize=15)
plt.tight_layout()
Llegada de Turistas
In [22]: GraficarPromedioMovil(turistas,30,intervalos=True,anomalias=True)
In [23]: decomposition = seasonal_decompose(turistas,period = 30)
tendencia = decomposition.trend
temporalidad = decomposition.seasonal
residuos = decomposition.resid
plt.figure(figsize=(15,12))
plt.subplot(411)
plt.plot(turistas, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(tendencia, label='Tendencia')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(temporalidad,label='Temporalidad')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residuos, label='Residuos')
plt.legend(loc='best')
plt.suptitle('Llegada de Turistas',fontsize=15)
plt.tight_layout()
Tipo de Cambio
In [24]: GraficarPromedioMovil(tc,30,intervalos=True,anomalias=True)
In [25]: decomposition = seasonal_decompose(tc,period = 30)
tendencia = decomposition.trend
temporalidad = decomposition.seasonal
residuos = decomposition.resid
plt.figure(figsize=(15,12))
plt.subplot(411)
plt.plot(tc, label='Original')
plt.legend(loc='best')
plt.subplot(412)
plt.plot(tendencia, label='Tendencia')
plt.legend(loc='best')
plt.subplot(413)
plt.plot(temporalidad,label='Temporalidad')
plt.legend(loc='best')
plt.subplot(414)
plt.plot(residuos, label='Residuos')
plt.legend(loc='best')
plt.suptitle('Tipo de Cambio USD-DOP',fontsize=15)
plt.tight_layout()
In [33]: plt.figure(figsize=(12,6))
plt.plot(reservas['normalizado'], label = 'Reservas')
plt.plot(exportaciones['normalizado'], label = 'Exportaciones')
plt.plot(tpm['normalizado'], label = 'Tasa Politica Monetaria')
plt.plot(turistas['normalizado'], label = 'Turistas')
plt.plot(remesas['normalizado'], label = 'Remesas')
plt.plot(tc['normalizado'], label = 'Tasa de Cambio USD-DOP', color='b')
plt.legend(loc='best')
plt.show()
Mapa de calor
In [58]: plt.figure(figsize=(10,6))
sns.heatmap(df.corr(), annot=True,cmap='coolwarm')
Out[58]: <AxesSubplot:>
In [59]: df.corr()['TC'].sort_values(ascending=False)[1:].plot(kind='bar')
Out[59]: <AxesSubplot:>
In [60]: df.corr()['TC'].sort_values(ascending=False)[1:]
ReservUSDMM 0.90
ExportUSDMM 0.77
Turistas -0.08
TPM -0.70
In [61]: sns.violinplot(y='Remesas',x='anio',data=df_orig)
plt.suptitle('Remesas')
plt.show()
In [62]: sns.violinplot(y='ExportUSDMM',x='anio',data=df_orig)
plt.suptitle('Exportaciones')
plt.show()
In [63]: sns.violinplot(y='ReservUSDMM',x='anio',data=df_orig)
plt.suptitle('Reservas')
plt.show()
In [64]: sns.violinplot(y='TPM',x='anio',data=df_orig)
plt.suptitle('TPM')
plt.show()
In [65]: sns.violinplot(y='Turistas',x='anio',data=df_orig)
plt.suptitle('Llegada de Turistas')
plt.show()
In [66]: sns.violinplot(y='TC',x='anio',data=df_orig)
plt.suptitle('Tasa de Cambios USD-DOP')
plt.show()
Out[67]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 478.89 542.83 602.19 563.79 537.73 581.19 677.30 745.73 541.26 740.37 812.01
mean 599.64 661.30 707.49 693.54 728.50 735.93 785.85 839.93 820.40 970.38 1,055.56
ExportUSDMM
std 76.04 68.69 58.36 63.80 74.43 77.49 57.31 55.69 114.88 96.38 122.35
max 757.49 782.42 786.56 800.23 796.22 843.28 873.43 921.50 924.04 1,114.46 1,214.42
In [68]: df_orig.groupby(['anio']).agg({'Remesas':['min','mean','std','max']}
).transpose().style.background_gradient(cmap='RdYlGn_r',axis='columns').format("{:,.2f}")
Out[68]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 263.52 262.32 288.50 338.76 355.07 440.93 455.88 537.03 395.00 760.96 748.81
mean 337.11 355.19 380.94 413.40 438.40 492.65 541.17 590.59 684.94 866.87 809.95
Remesas
std 38.41 54.84 47.78 47.60 43.78 41.41 45.34 46.16 140.25 71.68 48.57
max 409.43 436.61 459.76 486.89 504.03 563.90 603.93 665.49 872.31 994.89 888.13
In [69]: df_orig.groupby(['anio']).agg({'ReservUSDMM':['min','mean','std','max']}
).transpose().style.background_gradient(cmap='RdYlGn_r',axis='columns').format("{:,.2f}")
Out[69]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 2,980.10 3,017.80 3,449.50 4,369.80 4,938.60 5,821.83 6,597.96 6,980.51 6,689.18 11,963.16 12,373.98
mean 3,092.84 3,610.73 4,258.23 4,770.82 5,314.52 6,335.23 7,215.96 7,635.21 9,012.52 12,484.17 14,157.91
ReservUSDMM
std 94.75 420.73 500.26 212.92 339.35 260.71 433.00 630.75 1,469.00 366.65 817.88
max 3,245.15 4,386.50 5,162.00 5,195.05 6,046.70 6,780.40 8,050.15 8,781.40 10,751.62 13,060.29 14,849.76
In [70]: df_orig.groupby(['anio']).agg({'TPM':['min','mean','std','max']}
).transpose().style.background_gradient(cmap='RdYlGn_r',axis='columns').format("{:,.2f}")
Out[70]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 0.05 0.04 0.06 0.05 0.05 0.05 0.05 0.04 0.03 0.03 0.04
mean 0.06 0.05 0.06 0.05 0.05 0.05 0.05 0.05 0.04 0.03 0.06
TPM
std 0.01 0.01 0.00 0.01 0.00 0.00 0.00 0.00 0.01 0.00 0.01
max 0.07 0.06 0.06 0.06 0.06 0.06 0.06 0.06 0.04 0.04 0.07
In [71]: df_orig.groupby(['anio']).agg({'Turistas':['min','mean','std','max']}
).transpose().style.background_gradient(cmap='RdYlGn_r',axis='columns').format("{:,.2f}")
Out[71]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 298.53 287.63 324.69 373.73 404.99 360.46 422.15 385.02 1.38 224.00 607.40
mean 420.59 430.31 470.73 512.58 546.58 569.32 601.69 593.90 225.62 465.84 671.83
Turistas
std 74.99 79.56 75.50 76.66 82.85 96.62 95.09 103.81 217.78 159.98 67.48
max 511.65 537.04 581.48 633.71 679.39 702.86 731.82 714.09 636.88 788.41 800.94
In [72]: df_orig.groupby(['anio']).agg({'TC':['min','mean','std','max']}
).transpose().style.background_gradient(cmap='RdYlGn_r',axis='columns').format("{:,.2f}")
Out[72]: anio 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
min 38.86 40.58 42.86 44.46 45.53 46.69 48.33 50.29 53.04 56.27 54.47
mean 39.24 41.69 43.44 44.93 45.98 47.44 49.43 51.19 56.41 56.96 55.42
TC
std 0.41 0.79 0.40 0.30 0.34 0.38 0.53 1.00 2.30 0.53 1.11
max 40.17 42.61 44.14 45.44 46.59 48.11 50.15 52.84 58.36 58.07 57.52
In [80]: lr = LinearRegression()
In [81]: lr.fit(X_train,y_train)
Out[81]: LinearRegression()
In [82]: lr.intercept_
Out[82]: 45.359216989988944
Out[83]: Coeficientes
ExportUSDMM 0.204056
ReservUSDMM 9.004129
TPM -3.137765
Turistas -4.022236
Remesas 10.493490
In [85]: plt.scatter(y_test,predicciones)
In [88]: comparacion.plot()
plt.show()
In [92]: X = sm.add_constant(df_scale)
modelo = sm.OLS(y.reset_index(drop=True),X)
result = modelo.fit()
print(result.summary())
==============================================================================
Df Model: 5
===============================================================================
-------------------------------------------------------------------------------
==============================================================================
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [100]: lr = LinearRegression()
In [101]: lr.fit(X_train,y_train)
Out[101]: LinearRegression()
In [102]: lr.intercept_
Out[102]: 42.94967306067477
Out[103]: Coeficientes
ExportUSDMM 1.704006
ReservUSDMM 12.178299
TPM -3.544393
Turistas 1.030427
Remesas 3.798929
In [104]: predicciones = lr.predict(X_test)
In [105]: plt.scatter(y_test,predicciones)
In [109]: X = sm.add_constant(df_scale)
modelo = sm.OLS(y.reset_index(drop=True),X)
result = modelo.fit()
print(result.summary())
==============================================================================
Df Model: 5
===============================================================================
-------------------------------------------------------------------------------
==============================================================================
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Conclusión
Historicamente las variables seleccionadas tienen una correlación positiva con la variable objetivo, a excepción de la TPM
Las unicas variables que influyen negativamente, es decir, que aportan a la disminucion de la tasa de cambio, es la TPM y la llegada de turistas
Aunque no podemos evidenciar que las demas variables contribuyen positivamente (causalidad/correlacion) a la tasa de cambio, podemos notar que su incremento no crea una disminución, quizás contribuya a desacelerar el aumento,
pero los niveles observados no han contribuido a la disminución como tal