import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data from https://tassidicambio.bancaditalia.it/
ex2017=pd.read_csv('2017.csv')
ex2017.columns
ex2017.info
We deal mostly with numbers, so we convert to numpy array to handle the data.
ex2017reduced_np=np.array(ex2017[ ['Rate',"Reference date (CET)"] ])
ex2017.shape[0]
ex2017reduced_np.shape
ex2017.shape
Pandas already offers similar functionality, but our goal was to play with lists!
ex2017[ ['Rate'] ].rolling(window=1).mean()
ex2017reduced_np[0:1]
np.zeros(0)
DEBUG=False
averages=np.array([[]])
for window_size in range(ex2017reduced_np.shape[0]):
#windows can have size bbetween 1 and the total length of the array
if DEBUG: print('window size=',window_size)
first=0
last=window_size+1
res=np.zeros(window_size)
while last <= ex2017reduced_np.shape[0]:
#res=np.zeros(first)
new=np.mean(ex2017reduced_np[first:last,0])
res = np.concatenate((res, [new]), axis=0)
if DEBUG: print(res)
first+=1
last+=1
if DEBUG: print('computed averages=',len(res))
if DEBUG: print('computed non vanishing averages=',last-first)
#averages=np.insert(averages,0,res,axis=0)
res=np.array([res])
if DEBUG: print(res.shape)
if DEBUG: print(averages.shape)
if np.size(averages)==0:
averages=res
if DEBUG: print('moving averages: ',averages)
else:
averages=np.append(averages,res,axis=0)
if DEBUG: print('moving averages: ',averages)
np.shape(averages)
np.min(averages[averages>0])
np.max(averages[averages>0])
levels = np.sort(np.concatenate((np.arange(1.08, 1.18, 0.02),[1.1537])) )
levels
#plt.figure(figsize=(8,8))
fig, ax = plt.subplots()
im=ax.imshow(averages,cmap="YlGn",vmin=np.min(averages[averages>0]),vmax=np.max(averages[averages>0]))
levels=np.arange(1.08, 1.18, 0.02)
CS=ax.contour(averages, levels, colors='k')#cmap='bwr')
ax.clabel(CS, levels, inline=1, fmt='%1.2f', fontsize=14)
plt.xlabel('windows size + start date')
plt.ylabel('start date')
fig.colorbar(im, orientation='vertical', shrink=0.99)