In [1]:
from pylab import *
import pandas as pd
import pandas.tools.plotting as pt
import datetime as dt
from pandas.stats import moments as mm
from sklearn import linear_model as lin
In [2]:
nino34=pd.read_csv("http://www.cpc.ncep.noaa.gov/products/analysis_monitoring/ensostuff/detrend.nino34.ascii.txt", sep="\s+")
link = pd.read_csv("https://raw.githubusercontent.com/johncarlosbaez/el-nino/master/R/average-link-strength-1948-2013.txt",
                   sep="\s+", header=None, names=["N", "S"])
link.S-=link.S.mean()
dates = dt.date(1948,01,01)+vectorize(dt.timedelta)(days=10*link["N"]+729)
del link["N"]
#dates
In [3]:
link.shape
Out[3]:
(2337, 1)
In [4]:
nino34.ANOM.plot()
Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff985a83690>
In [5]:
link.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff984832c50>
In [6]:
zz=specgram(link.T.values[-1])
In [7]:
zz=psd(link.T.values[-1], NFFT=2**9, noverlap=2**8)
In [8]:
figure(figsize=(5,5))
plot(log2(zz[1][1:]),log2(zz[0][1:]))
Out[8]:
[<matplotlib.lines.Line2D at 0x7ff982d51090>]
In [9]:
nino34.T
Out[9]:
0 1 2 3 4 5 6 7 8 9 ... 768 769 770 771 772 773 774 775 776 777
YR 1950.00 1950.00 1950.00 1950.00 1950.00 1950.00 1950.00 1950.00 1950.00 1950.00 ... 2014.00 2014.00 2014.00 2014.00 2014.00 2014.00 2014.00 2014.00 2014.00 2014.00
MON 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 ... 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00
TOTAL 24.84 25.22 26.04 26.38 26.20 26.53 26.41 25.98 25.78 25.95 ... 26.03 26.08 26.87 27.68 28.15 27.81 27.29 26.83 27.01 27.17
ClimAdjust 26.26 26.53 27.09 27.50 27.58 27.31 26.91 26.48 26.30 26.22 ... 26.68 26.84 27.34 27.81 27.91 27.69 27.28 26.92 26.83 26.79
ANOM -1.42 -1.31 -1.04 -1.12 -1.38 -0.78 -0.50 -0.50 -0.52 -0.27 ... -0.64 -0.76 -0.47 -0.13 0.24 0.12 0.02 -0.10 0.18 0.38

5 rows × 778 columns

In [10]:
linktmp=link[arange(len(link))%73 != 0]
ll= len(linktmp)
print ll
linkmnth= linktmp.groupby(arange(ll)//3).mean()
linkmnth.reset_index()
linkmnth.T
linkmnth.shape
2304

Out[10]:
(768, 1)
In [11]:
linkx=linkmnth
df=nino34.iloc[:len(linkx)]
df.index=range(df.shape[0])
print ll, len(linkx), df.shape
df["link"]=linkmnth
2304 768 (768, 5)

In [12]:
df.to_csv("ANOMALY.csv")
df.T
Out[12]:
0 1 2 3 4 5 6 7 8 9 ... 758 759 760 761 762 763 764 765 766 767
YR 1950.000000 1950.00000 1950.000000 1950.000000 1950.000000 1950.000000 1950.000000 1950.000000 1950.000000 1950.000000 ... 2013.000000 2013.00000 2013.000000 2013.000000 2013.000000 2013.000000 2013.000000 2013.000000 2013.000000 2013.000000
MON 1.000000 2.00000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 ... 3.000000 4.00000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000
TOTAL 24.840000 25.22000 26.040000 26.380000 26.200000 26.530000 26.410000 25.980000 25.780000 25.950000 ... 26.890000 27.81000 27.780000 27.380000 26.950000 26.550000 26.550000 26.650000 26.540000 26.200000
ClimAdjust 26.260000 26.53000 27.090000 27.500000 27.580000 27.310000 26.910000 26.480000 26.300000 26.220000 ... 27.340000 27.81000 27.910000 27.690000 27.280000 26.920000 26.830000 26.790000 26.740000 26.690000
ANOM -1.420000 -1.31000 -1.040000 -1.120000 -1.380000 -0.780000 -0.500000 -0.500000 -0.520000 -0.270000 ... -0.450000 0.00000 -0.140000 -0.300000 -0.320000 -0.370000 -0.280000 -0.140000 -0.200000 -0.490000
link -0.013189 -0.11609 -0.178518 -0.209169 -0.170269 -0.120167 -0.049324 0.001743 -0.003429 -0.009383 ... -0.165093 -0.11449 -0.104858 -0.061627 0.031293 0.117316 0.166109 0.236409 0.274647 0.326008

6 rows × 768 columns

In [13]:
df.plot(x="link", y="ANOM", kind="scatter", figsize=(9,9), alpha=0.3, marker="o")
Out[13]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff982d677d0>
In [14]:
figure(figsize=(9,9))
scatter(df["link"].iloc[:-6], y=df["ANOM"].iloc[6:], alpha=0.3, marker="o")
Out[14]:
<matplotlib.collections.PathCollection at 0x7ff982c21490>

ANOM vs ANOM, 6 month lag

In [15]:
figure(figsize=(9,9))
scatter(df.ANOM[:-6], y=df.ANOM[6:], alpha=0.3, marker="o")
Out[15]:
<matplotlib.collections.PathCollection at 0x7ff982b40690>
In [16]:
reg = lin.LinearRegression()
reg.fit(df[["ANOM","link"]].iloc[:-6], df["ANOM"].iloc[6:])
print reg.coef_, reg.intercept_
print reg.score(df[["ANOM","link"]].iloc[:-6], df["ANOM"].iloc[6:])
[ 0.36617213  0.65108817] 0.0130854161203
0.222001841322

In [17]:
figure(figsize=(9,9))
scatter(reg.predict(df[["ANOM","link"]].iloc[:-6]),
        df["ANOM"].iloc[6:],
        alpha=0.3, marker="o")
Out[17]:
<matplotlib.collections.PathCollection at 0x7ff984649c10>

ANOM auto courrelation

In [18]:
figure(figsize=(20,3))
zz=acorr(df.ANOM, maxlags=None)
In [19]:
figure(figsize=(20,3))
zz=acorr(df.ANOM, maxlags=48)
In [20]:
figure(figsize=(20,3))
zz=acorr(df.link, maxlags=None )
In [21]:
figure(figsize=(20,3))
zz=acorr(df.link, maxlags=48 )
In [22]:
figure(figsize=(20,3))
zz=xcorr(df.link, df.ANOM, maxlags=None)
In [23]:
mx= argmin(zz[1]), argmax(zz[1])
print mx,  zz[0][mx[0]],zz[1][mx[0]], zz[0][mx[1]],  zz[1][mx[1]]
(600, 758) -167 -0.30203655691 -9 0.323732824151

In [24]:
figure(figsize=(20,3))
zz=xcorr(df.link, df.ANOM, maxlags=48)
In [25]:
mx= argmin(zz[1]), argmax(zz[1])
print mx,  zz[0][mx[0]],zz[1][mx[0]], zz[0][mx[1]],  zz[1][mx[1]]
(6, 39) -42 -0.151493651164 -9 0.323732824151

In [26]:
print zz[1][zz[0]==0]
[ 0.13226321]

Ignore below

In [27]:
oni = mm.rolling_mean(nino34.ANOM.values, 3)[2:]
elnino = (mm.rolling_min(oni, 5)[4:] - 0.5)
ludnino = (mm.rolling_min(nino34.ANOM, 5)[4:] - 0.5)
oni -= oni.mean()
elnino -= elnino.mean()
ludnino -= ludnino.mean()
In [28]:
zz=acorr(elnino,maxlags=None)
In [29]:
zz=acorr(ludnino,maxlags=None)
In [30]:
zz=xcorr(ludnino,maxlags=None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-8ba679138f6a> in <module>()
----> 1 zz=xcorr(ludnino,maxlags=None)

TypeError: xcorr() takes at least 2 arguments (2 given)
In []:
aa=pd.DataFrame({"link":linkmnth["S"], "elnino": elnino[:linkmnth.shape[0]]})
aa.plot()#, maxlags=None)
In []:
zz=pt.scatter_matrix(df, alpha=0.2, diagonal="kde", figsize=(15,15))
In []:
figure(figsize=(20,3))
zz=cohere(df.ANOM, df.link, NFFT=64, noverlap=32)
In []:
xcorr(aa.link, aa.elnino,maxlags=48)
In []:
linkmnth.shape
In []:
figure(figsize=(10,10))
plot(elnino[:-6], elnino[6:],marker=".", alpha=0.2)
In []:
plot(linkmnth["S"], elnino[:linkmnth.shape[0]], marker=".", alpha=0.2)
In []:
figure(figsize=(10,10))
plot(linkmnth["S"], ludnino[2:2+linkmnth.shape[0]], marker=".", alpha=0.2)
In []:
scatter(nino34.ANOM[6:], elnino)
In []:
z=arange(-10,10,0.1)
k=20
df=pd.DataFrame({"a": sin(z[k:]), "b":sin(z[:-k])})
df.plot("a","b")
In []:
df.plot("a","b",kind="scatter")
In []:
zz=xcorr(df.a, df.b, maxlags=40)
In []: