Skip to content

Instantly share code, notes, and snippets.

@robintux
Created April 2, 2024 16:37
Show Gist options
  • Save robintux/a531b800c0c2e01988d3462544d34abb to your computer and use it in GitHub Desktop.
Save robintux/a531b800c0c2e01988d3462544d34abb to your computer and use it in GitHub Desktop.
Clase 17 : Series de tiempo (EPC)
# Gráfico de correlograma
def plot_correlogram(x, lags=None, title=None):
lags = min(10, int(len(x)/5)) if lags is None else lags
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14, 8))
x.plot(ax=axes[0][0], title='Time Series')
x.rolling(21).mean().plot(ax=axes[0][0], c='k', lw=1)
q_p = np.max(q_stat(acf(x, nlags=lags), len(x))[1])
stats = f'Q-Stat: {np.max(q_p):>8.2f}\nADF: {adfuller(x)[1]:>11.2f}'
axes[0][0].text(x=.02, y=.85, s=stats, transform=axes[0][0].transAxes)
probplot(x, plot=axes[0][1])
mean, var, skew, kurtosis = moment(x, moment=[1, 2, 3, 4])
s = f'Mean: {mean:>12.2f}\nSD: {np.sqrt(var):>16.2f}\nSkew: {skew:12.2f}\nKurtosis:{kurtosis:9.2f}'
axes[0][1].text(x=.02, y=.75, s=s, transform=axes[0][1].transAxes)
plot_acf(x=x, lags=lags, zero=False, ax=axes[1][0])
plot_pacf(x, lags=lags, zero=False, ax=axes[1][1])
axes[1][0].set_xlabel('Lag')
axes[1][1].set_xlabel('Lag')
fig.suptitle(title, fontsize=14)
sns.despine()
fig.tight_layout()
fig.subplots_adjust(top=.9)
# Unit Root Test
def test_unit_root(df):
return df.apply(lambda x: f'{pd.Series(adfuller(x)).iloc[1]:.2%}').to_frame('p-value')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment