In [1]:
import load_data, plots
import pandas as pd
from matplotlib import pyplot as plt
import statsmodels.api as sm

In [2]:
data= load_data.load_data()
plots.plot_data(data)



In [10]:
value = data['usd-blue-lanacion'].keys()[0]

In [11]:
value?

In [3]:
df = pd.DataFrame(data)

In [19]:
df['usd-bna/buy'].describe()


Out[19]:
count    2613.000000
mean        3.575617
std         0.698774
min         2.780000
25%         3.020000
50%         3.180000
75%         4.000000
max         5.705000
dtype: float64

In [20]:
df['usd-bna/buy'].plot()


Out[20]:
<matplotlib.axes.AxesSubplot at 0x106fb7d90>

In [21]:
df['usd-bna/buy'].hist()


Out[21]:
<matplotlib.axes.AxesSubplot at 0x106fe5090>

In [22]:
df['usd-bna/buy'].apply(np.log10).hist()


Out[22]:
<matplotlib.axes.AxesSubplot at 0x107056510>

In [4]:
serie = df['usd-bna/buy']

In [31]:
pd.stats.moments.rolling_mean(serie, 1).plot()


Out[31]:
<matplotlib.axes.AxesSubplot at 0x10739ec10>

In [33]:
pd.stats.moments.rolling_mean(serie.apply(np.log10), 2).plot()


Out[33]:
<matplotlib.axes.AxesSubplot at 0x10781dd10>

In [35]:
(serie/serie.shift(1)).plot()


Out[35]:
<matplotlib.axes.AxesSubplot at 0x1078cf210>

In [50]:
(serie/serie.shift(1)).hist(bins=50)


Out[50]:
<matplotlib.axes.AxesSubplot at 0x108ee49d0>

In [41]:
(serie/serie.shift(30)).plot()


Out[41]:
<matplotlib.axes.AxesSubplot at 0x107c5b350>

In [42]:
(serie/serie.shift(360)).plot()


Out[42]:
<matplotlib.axes.AxesSubplot at 0x107eac310>

In [49]:
(serie/serie.shift(30)).hist(bins=50)


Out[49]:
<matplotlib.axes.AxesSubplot at 0x108e21990>

In [46]:
(serie/serie.shift(5)).plot()


Out[46]:
<matplotlib.axes.AxesSubplot at 0x1085ccc10>

In [54]:
fig = (serie/serie.shift(5)).hist(bins=50)
fig.set_title('a')


Out[54]:
<matplotlib.text.Text at 0x109b71c90>

In [64]:
for i in [5, 30, 360]:
    f, (ax1, ax2) = plt.subplots(2)
    ax1.hist((serie/serie.shift(i)).dropna(), bins=50)
    ax1.set_title('cada %d dias' % i)
    ax2.plot((serie/serie.shift(i)).dropna())
    ax2.set_title('cada %d dias' % i)
    f.savefig('plot-%d.pdf' % i, format = 'pdf')



In [57]:
plt.plot((serie/serie.shift(i)).dropna())


Out[57]:
[<matplotlib.lines.Line2D at 0x10b080050>]

In [67]:
for i in [5, 30, 360]:
    f, (ax1, ax2) = plt.subplots(2)
    ax1.hist((serie/serie.shift(i)).apply(np.log10).dropna(), bins=50)
    ax1.set_title('cada %d dias' % i)
    ax2.plot((serie/serie.shift(i)).apply(np.log10).dropna())
    ax2.set_title('cada %d dias' % i)



In [5]:
def difference(serie, shift):
    return serie/serie.shift(i)

In [69]:
for i in [5, 30, 360]:
    f, (ax1, ax2) = plt.subplots(2)
    ax1.hist(difference(serie, i).dropna(), bins=50)
    ax1.set_title('cada %d dias' % i)
    ax2.plot(difference(serie, i).dropna())
    ax2.set_title('cada %d dias' % i)



In [70]:
for i in [5, 30, 360]:
    f, (ax1, ax2) = plt.subplots(2)
    ax1.hist(difference(difference(serie, i), i).dropna(), bins=50)
    ax1.set_title('cada %d dias' % i)
    ax2.plot(difference(difference(serie, i), i).dropna())
    ax2.set_title('cada %d dias' % i)



In [7]:
logged = serie.apply(np.log10)

In [73]:
(logged - logged.shift(1)).plot()


Out[73]:
<matplotlib.axes.AxesSubplot at 0x10b0f6810>

In [6]:
df1 = lambda x, t=1: x - x.shift(t)

In [82]:
df1(logged, 5).plot()


Out[82]:
<matplotlib.axes.AxesSubplot at 0x10c36e050>

In [84]:
df1(df1(logged, 5), 5).plot()


Out[84]:
<matplotlib.axes.AxesSubplot at 0x10caf0890>

In [79]:
df1(df1(df1(logged))).hist()


Out[79]:
<matplotlib.axes.AxesSubplot at 0x10b52d150>

In [29]:
log_diff = df1(df1(logged, 5), 5)

In [32]:
arma_mod = sm.tsa.ARMA(log_diff.dropna(),order=(2,2), freq='D')

In [33]:
arma_res = arma_mod.fit()

In [99]:
arma_res.fittedvalues.plot()


Out[99]:
<matplotlib.axes.AxesSubplot at 0x10ee58e50>

In [104]:
forecast = arma_res.forecast(100)[0]

In [108]:
arima = sm.tsa.ARMA(logged.dropna(), order = (2,2,2), freq='D')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-bda19b204028> in <module>()
----> 1 arima = sm.tsa.ARMA(logged.dropna(), order = (2,2,2), freq='D')

TypeError: __init__() got an unexpected keyword argument 'order'

In [111]:
df1(df1(logged, 30), 30).plot()


Out[111]:
<matplotlib.axes.AxesSubplot at 0x10f9364d0>

In [17]:
sm.graphics.tsa.plot_acf(df1(df1(logged, 5), 5).dropna(), lags=10)


Out[17]:

In [18]:
sm.graphics.tsa.plot_pacf(df1(df1(logged, 5), 5).dropna(), lags=10)


Out[18]:

In [22]:
sm.graphics.tsa.plot_acf(df1(df1(df1(logged, 5), 5), 5).dropna(), lags=20)


Out[22]:

In [26]:
sm.graphics.tsa.plot_acf(df1(logged, 5).dropna(), lags=20)


Out[26]:

In [27]:
sm.graphics.tsa.plot_pacf(df1(logged, 5).dropna(), lags=20)


Out[27]:

In [36]:
sm.graphics.tsa.plot_pacf(logged.dropna(), lags=20)


Out[36]:

In [38]:
sm.graphics.tsa.plot_acf(logged.dropna(), lags=100)


Out[38]:

Esto indicaría un modelo ARIMA(0,2,9)


In [46]:
sm.graphics.tsa.plot_acf(df1(df1(logged, 5), 5).dropna(), lags=30)
sm.graphics.tsa.plot_pacf(df1(df1(logged, 5), 5).dropna(), lags=30)


Out[46]:

In [47]:
sm.graphics.tsa.plot_acf(logged.dropna(), lags=100)
sm.graphics.tsa.plot_pacf(logged.dropna(), lags=100)


Out[47]:

In [51]:
df1(df1(logged, 5), 5).hist(bins=40)


Out[51]:
<matplotlib.axes.AxesSubplot at 0x10c0f1c10>

In [49]:
df1(df1(logged, 5), 5).plot()


Out[49]:
<matplotlib.axes.AxesSubplot at 0x10c074ad0>

In [ ]: