Workshop Matlab BITS Base

Post on 28-Nov-2014

866 views 0 download

description

Workshop Matlab Bank of Italy Time Series ToolboxWorkshop c/o Area Ricerca Banca d'ItaliaVilla Huffer21 Gennaio 2008

Transcript of Workshop Matlab BITS Base

BITS: Serie Storiche in Matlab

Emmanuele Somma

Supporto Informatico per l’Area RicercheBanca d’Italia

21 Gennaio 2008

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 1/96

Piano della presentazione

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 2/96

Le serie storiche in MATLAB

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 3/96

Serie storica Insieme di informazioni omogenee riferite a momenti diversiin successione temporale.

t1 v1

t2 v2...

...tn vn

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 4/96

Serie storica Insieme di osservazioni numeriche riferite a momenti diversiin successione temporale periodica.

tp v1

t2p v2...

...tnp vn

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 5/96

L’oggetto timeseries

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 6/96

Un array dei dati

Un array di dati

x = [-0.2 -0.3 13;-0.1 -0.4 15;NaN 2.8 17;0.5 0.3 NaN;-0.3 -0.1 15]

Un array casuale di dati

x = rand(4,4);

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 7/96

Un paio di timeseries

ts_pos = timeseries(x(:,1:2), 1:5, ’name’, ’Position’)getdatasamplesize(ts_pos)

ts_vel = timeseries(x(:,3), 1:5, ’name’, ’Velocity’);getdatasamplesize(ts_vel)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 8/96

Altre timeseries

%% Import the sample dataload count.dat

count1 = timeseries(count(:,1), 1:24,’name’, ’intersection1’);count2 = timeseries(count(:,2), 1:24,’name’, ’intersection2’);count3 = timeseries(count(:,3), 1:24,’name’, ’intersection3’);

get(count1)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 9/96

Definizione delle unita di misuracount1.DataInfo.Units = ’cars’;count1.TimeInfo.Units = ’hours’;count2.TimeInfo.Units = ’hours’;count3.TimeInfo.Units = ’hours’;

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 10/96

Definizione del metodo d’interpolazione

count1.DataInfo.Interpolation = tsdata.interpolation(’zoh’);% zero-order hold

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 11/96

Aggiunta di eventi

%% Construct and add the first event to all time seriese1 = tsdata.event(’AMCommute’,8);

% Construct the first event at 8 AMe1.Units = ’hours’; % Specify the time units of the timecount1 = addevent(count1,e1); % Add the event to count1count2 = addevent(count2,e1); % Add the event to count2count3 = addevent(count3,e1); % Add the event to count3

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 12/96

Aggiunta di eventi

%% Construct and add the second event to all time seriese2 = tsdata.event(’PMCommute’,18);

% Construct the first event at 6 PMe2.Units = ’hours’; % Specify the time units of the timecount1 = addevent(count1,e2); % Add the event to count1count2 = addevent(count2,e2); % Add the event to count2count3 = addevent(count3,e2); % Add the event to count3

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 13/96

Collezioni di timeseries

tsc = tscollection({count1 count2},’name’, ’count_coll’)

tsc = addts(tsc, count3)

tsc1 = removets(tsc1,’intersection3’)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 14/96

Ricampionamento della collezione

tsc1 = resample(tsc,1:2:24)tsc1 = resample(tsc,1:0.5:24)tsc1 = resample(tsc1,tsc1.Time)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 15/96

Modifica dei contenuti degli elementi della collezione

tsc1 = addsampletocollection(tsc1,’time’,3.25,...’intersection1’,5)

tsc1 = delsamplefromcollection(tsc1,’index’,...find(isnan(tsc1.intersection2.Data)));

tsc1 = addsampletocollection(tsc1,’time’,3.25,...’intersection1’,5);

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 16/96

Modifica dei contenuti degli elementi della collezione

plot(tsc1.intersection1); hold on;plot(tsc1.intersection2)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 17/96

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 18/96

L’oggetto fints

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 19/96

Creazione fints

dates = (datenum(’05/11/99’):datenum(’05/11/99’)+100)’;data_series1 = exp(randn(1, 101))’;data_series2 = exp(randn(1, 101))’;data = [data_series1 data_series2];myfts = fints(dates, data);myftsmyfts =desc: (none)freq: Unknown (0)’dates: (101)’ ’series1: (101)’ ’series2: (101)’’11-May-1999’ [ 0.6488] [ 0.1105]’12-May-1999’ [ 0.1891] [ 2.6814]’13-May-1999’ [ 1.1335] [ 0.5953][...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 20/96

Trasformazionesrs2 = myfts.series2

srs2_vec = fts2mat(myfts.series2)

format long g

srs2_mtx = fts2mat(myfts.series2, 1)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 21/96

Indicizzazione elementiformat shortmyfts(’05/11/99’)myfts.series2(’05/11/99’)myfts.series2({’05/11/99’, ’05/21/99’, ’05/31/99’})myfts (’05/11/99::05/15/99’)myfts.series2(1)myfts.series2([1, 3, 5])myfts.series2(16:20)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 22/96

Operazioniaddup = myfts1 + newftssubout = myfts1 - newftsaddscalar = myfts1 + 10000submtx = myfts1 - randn(20, 4)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 23/96

Operazioni

newfts2 = fints(myfts1.dates, fts2mat(myfts1/10000),...{’Rat1’,’Rat2’, ’Rat3’,’Rat4’}, 1, ’New FTS’)

addother = myfts1 + fts2mat(newfts2);

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 24/96

BITS - Bank of Italy Time Series

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 25/96

L’oggetto tsmat

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 26/96

Costruzione tsmat

TSMAT = tsmat(1980,1,12,count)tsmat( macro index (year), micro index (period), frequency, data matrix )

Parametri

macro index double (used as integer)micro index double (used as integer)frequency numero di elementi in una divisione macro

1 Year2 Semester4 Quarter12 Month52 Week365 Day

data matrix NOBS!NSERIESSERIES colonneOSSERV righe

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 27/96

Esempio

A = tsmat(1980,1,12,rand(20,1))Tsmat object

Size: [20 1]Frequency: 12

Start: 1980 1End: 1981 8

Labels: T1

B = tsmat(1980,1,12,rand(20,1))

BG = tsmat(1980,1,365,rand(220,1))Tsmat object

Size: [220 1]Frequency: 365

Start: Tue Jan-01-1980End: Thu Aug-07-1980

Labels: T1

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 28/96

Caricamento datiload count.datTSC = tsmat(1980,1,365,count)Tsmat object

Size: [24 3]Frequency: 365

Start: Tue Jan-01-1980End: Thu Jan-24-1980

Labels: T1 T2 T3

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 29/96

Strutturastruct(TSC)ans =

matdata: [24x3 double]start_year: 1980

start_period: 1freq: 365

last_year: 1980last_period: 24

meta: [1x1 struct]meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 30/96

I ValoriTSC.matdataans =

11 11 97 13 1114 17 2011 13 943 51 6938 46 7661 132 186[...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 31/96

I metadati di base

.freq frequenza delle osservazioni

.start indice dell’oss. iniziale (anno,periodo)

.start year anno dell’oss. iniziale

.start period periodo dell’oss. iniziale

.last indice dell’oss. finale (calcolato)

.last year anno dell’oss. finale (calcolato)

.last period periodo dell’oss. finale (calcolato)

.range vettore degli indici iniziale e finale

.dates vettore delle date in formato num. Matlab

.matdata matrice dei valori della collezione

Ad es.datestr(TSC.dates)ans =01-Jan-198002-Jan-198003-Jan-1980[...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 32/96

I metadati ’estesi’

.meta metadati della collezione

.meta cols metadati delle colonne

I metadati di colonna obbligatori

’label’ nome della serie

Ad es.TSC.metaans =

release: 733425TSC.meta_colsans =

label: {’T1’ ’T2’ ’T3’}

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 33/96

Visualizzare una serie con tstab

tstab(S1 [, S2 ...]) dove S1,...,S2 sono oggetti tsmatA=tsmat(1980,1,12,rand(5,1))B=tsmat(1980,3,12,rand(5,1))tstab(A,B)tsStart: 1980 1tsRelease: 18-Jan-2008Frequency: 12 (monthly)Date

T1 T118/01/08 18/01/08

1980-M-001 0.0377 NaN1980-M-002 0.8852 NaN1980-M-003 0.9133 0.26191980-M-004 0.7962 0.33541980-M-005 0.0987 0.67971980-M-006 NaN 0.13661980-M-007 NaN 0.7212

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 34/96

Creazione della tsmat

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 35/96

tsmat: Come si usa>>

ts = tsmat(1980,1,12,rand(3,4))Tsmat object

Size: [3 4]Start: 1980 1End: 1980 3

Labels: T1 T2 T3 T4>> get(ts)

matdata: [3x4 double]start_year: 1980

start_period: 1freq: 12

last_year: 1980last_period: 3

meta: [1x1 struct]meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 36/96

tsmat: Come si usa>> ts = tsmat(1980,1,12,rand(3,4))

Tsmat objectSize: [3 4]Start: 1980 1End: 1980 3

Labels: T1 T2 T3 T4>> get(ts)

matdata: [3x4 double]start_year: 1980

start_period: 1freq: 12

last_year: 1980last_period: 3

meta: [1x1 struct]meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 36/96

tsmat: Come si usa>> ts = tsmat(1980,1,12,rand(3,4))Tsmat object

Size: [3 4]Start: 1980 1End: 1980 3

Labels: T1 T2 T3 T4>>

get(ts)matdata: [3x4 double]

start_year: 1980start_period: 1

freq: 12last_year: 1980

last_period: 3meta: [1x1 struct]

meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 36/96

tsmat: Come si usa>> ts = tsmat(1980,1,12,rand(3,4))Tsmat object

Size: [3 4]Start: 1980 1End: 1980 3

Labels: T1 T2 T3 T4>> get(ts)

matdata: [3x4 double]start_year: 1980

start_period: 1freq: 12

last_year: 1980last_period: 3

meta: [1x1 struct]meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 36/96

tsmat: Come si usa>> ts = tsmat(1980,1,12,rand(3,4))Tsmat object

Size: [3 4]Start: 1980 1End: 1980 3

Labels: T1 T2 T3 T4>> get(ts)

matdata: [3x4 double]start_year: 1980

start_period: 1freq: 12

last_year: 1980last_period: 3

meta: [1x1 struct]meta_cols: [1x1 struct]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 36/96

tsmat: Come si usa>>

tstab(ts)tsStart: 1980 1tsRelease: 17-Jul-2007

RELEASES

Frequency: 12 (monthly)Date

LABELS

T1 T2 T3 T417/07/07 17/07/07 17/07/07 17/07/07

1980-M-001 0.6822 0.1509 0.8600 0.49661980-M-002 0.3028 0.6979 0.8537 0.89981980-M-003 0.5417 0.3784 0.5936 0.8216

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 37/96

tsmat: Come si usa>> tstab(ts)

tsStart: 1980 1tsRelease: 17-Jul-2007

RELEASES

Frequency: 12 (monthly)Date

LABELS

T1 T2 T3 T417/07/07 17/07/07 17/07/07 17/07/07

1980-M-001 0.6822 0.1509 0.8600 0.49661980-M-002 0.3028 0.6979 0.8537 0.89981980-M-003 0.5417 0.3784 0.5936 0.8216

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 37/96

tsmat: Come si usa>> tstab(ts)tsStart: 1980 1tsRelease: 17-Jul-2007

RELEASES

Frequency: 12 (monthly)Date

LABELS

T1 T2 T3 T417/07/07 17/07/07 17/07/07 17/07/07

1980-M-001 0.6822 0.1509 0.8600 0.49661980-M-002 0.3028 0.6979 0.8537 0.89981980-M-003 0.5417 0.3784 0.5936 0.8216

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 37/96

tsmat: Come si usa>> tstab(ts)tsStart: 1980 1tsRelease: 17-Jul-2007

RELEASES

Frequency: 12 (monthly)Date LABELS

T1 T2 T3 T417/07/07 17/07/07 17/07/07 17/07/07

1980-M-001 0.6822 0.1509 0.8600 0.49661980-M-002 0.3028 0.6979 0.8537 0.89981980-M-003 0.5417 0.3784 0.5936 0.8216

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 37/96

tsmat: Come si usa>> tstab(ts)tsStart: 1980 1tsRelease: 17-Jul-2007 RELEASESFrequency: 12 (monthly)Date

LABELS

T1 T2 T3 T417/07/07 17/07/07 17/07/07 17/07/07

1980-M-001 0.6822 0.1509 0.8600 0.49661980-M-002 0.3028 0.6979 0.8537 0.89981980-M-003 0.5417 0.3784 0.5936 0.8216

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 37/96

tsmat Etichette di colonnats = tsmat(1980,1,12,rand(3,4), ...

{’PRICE’, ’STOCK’, ’EXCHANGE’, ’UNIT’});

tstab(ts)tsStart: 1980 1tsRelease: 18-Jul-2007Frequency: 12 (monthly)Date

PRICE STOCK EXCHANGE UNIT18/07/07 18/07/07 18/07/07 18/07/07

1980-M-001 0.0495 0.5221 0.3751 0.59791980-M-002 0.5667 0.1171 0.8234 0.94921980-M-003 0.1219 0.7699 0.0466 0.2888

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 38/96

tsmat Etichette di colonna automatichets = tsmat(1980,1,12,rand(3,4),’PRICE’);

tstab(ts)tsStart: 1980 1tsRelease: 18-Jul-2007Frequency: 12 (monthly)Date

PRICE1 PRICE2 PRICE3 PRICE418/07/07 18/07/07 18/07/07 18/07/07

1980-M-001 0.8702 0.1923 0.9339 0.89521980-M-002 0.0269 0.7157 0.1372 0.94241980-M-003 0.5195 0.2507 0.5216 0.3351

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 39/96

tsmat Rilasci di colonnats = tsmat(1980,1,12,rand(3,4),’PRICE’, ...

{ ’01-Jan-2007’ ’02-Jan-2007’ ’07-Jan-2007’ ’09-Jan-2007’ });

tstab(ts)tsStart: 1980 1Frequency: 12 (monthly)Date

PRICE1 PRICE2 PRICE3 PRICE401/01/07 02/01/07 07/01/07 09/01/07

1980-M-001 0.5962 0.5972 0.9561 0.81211980-M-002 0.3290 0.1614 0.5955 0.61011980-M-003 0.4782 0.8295 0.0287 0.7015

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 40/96

tsmat Distribuzione dei rilasci su tutte le colonnets = tsmat(1980,1,12,rand(3,4), ...

{’PRICE’, ’STOCK’, ’EXCHANGE’, ’UNIT’}, ...’01-Jan-2002’ );

tstab(ts)tsStart: 1980 1tsRelease: 18-Jul-2007Frequency: 12 (monthly)Date

PRICE STOCK EXCHANGE UNIT01/01/02 01/01/02 01/01/02 01/01/02

1980-M-001 0.4374 0.1359 0.3987 0.86861980-M-002 0.4712 0.5325 0.3584 0.62641980-M-003 0.1493 0.7258 0.2853 0.2412

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 41/96

Indicizzazione per l’accesso

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 42/96

Indicizzazione

Metadati fondamentali tramite ’.’ (es. TSC.start)

Osservazione tramite ’()’ (es. TSC([2:5, [1:end])

Lead/Lag tramite ’{}’ (es. TSC{-1})

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 43/96

Accesso ai metadati: ’.’

.freq frequenza delle osservazioni

.start indice dell’oss. iniziale (anno,periodo)

.start year anno dell’oss. iniziale

.start period periodo dell’oss. iniziale

.last indice dell’oss. finale (calcolato)

.last year anno dell’oss. finale (calcolato)

.last period periodo dell’oss. finale (calcolato)

.range vettore degli indici iniziale e finale

.dates vettore delle date in formato num. Matlab

.matdata matrice dei valori della collezione

.meta dizionario dei metadati di collezione

.meta cols dizionari dei metadati di colonna

Ad es.datestr(TSC.dates)ans =01-Jan-198002-Jan-198003-Jan-1980[...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 44/96

Accesso alle osservazioni: ’()’

Doppio vettore di interi (2 elementi)righe colonne

osservazioni serieTSC( [2:5], [1:end])

tsStart: 1980 2tsRelease: 19-Jan-2008Frequency: 365 (daily)Date

T1 T2 T319/01/08 19/01/08 19/01/08

1980-D-002 02-Jan-1980 7.0000 13.0000 11.00001980-D-003 02-Jan-1980 14.0000 17.0000 20.00001980-D-004 02-Jan-1980 11.0000 13.0000 9.00001980-D-005 02-Jan-1980 43.0000 51.0000 69.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 45/96

Esempio: Una osservazione di tutte le serie nella collezione

righe colonneosservazioni serie

TSC( 1, [1:end])

tstab(TSC1)tsStart: 1980 1tsRelease: 19-Jan-2008Frequency: 365 (daily)Date

T1 T2 T319/01/08 19/01/08 19/01/08

1980-D-001 01-Jan-1980 11.0000 11.0000 9.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 46/96

Esempio: Tutte le osservazioni di una serie della collezione

righe colonneosservazioni serie

TSC( [1:end], 2)

tstab(TSC1)tsStart: 1980 1tsRelease: 19-Jan-2008Frequency: 365 (daily)Date

T119/01/08

1980-D-001 01-Jan-1980 11.00001980-D-002 01-Jan-1980 7.00001980-D-003 01-Jan-1980 14.00001980-D-004 01-Jan-1980 11.0000[...]1980-D-021 01-Jan-1980 35.00001980-D-022 01-Jan-1980 11.00001980-D-023 01-Jan-1980 13.00001980-D-024 01-Jan-1980 10.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 47/96

Accesso alle osservazioni: ’()’

Quattro Interi e un indice (5 elementi)anno periodo anno periodo colonne

iniziale finaleTSC( 1986, 1, 1996, 1, ’:’)TSC( 1986, 1, 1996, 1, [2 3])

tsStart: 1980 2tsRelease: 19-Jan-2008Frequency: 365 (daily)Date

T1 T2 T319/01/08 19/01/08 19/01/08

1980-D-002 02-Jan-1980 7.0000 13.0000 11.00001980-D-003 02-Jan-1980 14.0000 17.0000 20.00001980-D-004 02-Jan-1980 11.0000 13.0000 9.00001980-D-005 02-Jan-1980 43.0000 51.0000 69.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 48/96

Operazione di Lead e Lag: ’{}’TSC = tsmat(1980,1,365,count(:,1))tstab(TSC1,TSC1{-1},TSC1{1})

% == tstab(TSC,tslag(TSC,1),tslag(TSC,-1))tsStart: 1979 365tsRelease: 18-Jan-2008Frequency: 365 (daily)Date T1 T1 T1

18/01/08 18/01/08 18/01/081979-D-365 31-Dec-1979 NaN NaN 11.00001980-D-001 31-Dec-1979 11.0000 NaN 7.00001980-D-002 31-Dec-1979 7.0000 11.0000 14.0000[...]1980-D-023 31-Dec-1979 13.0000 11.0000 10.00001980-D-024 31-Dec-1979 10.0000 13.0000 NaN1980-D-025 31-Dec-1979 NaN 10.0000 NaN

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 49/96

Riorganizzazione

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 50/96

Concatenazione orizzontaleaa=tsmat(1980,1,4,reshape(-1:-1:-16,4,4))Tsmat object

Size: [4 4]Frequency: 4

Start: 1980 1End: 1980 4

Labels: T1 T2 T3 T4tstab(aa)tsStart: 1980 1tsRelease: 19-Jan-2008Frequency: 4 (quarterly)Date

T1 T2 T3 T419/01/08 19/01/08 19/01/08 19/01/08

1980-Q-001 -1.0000 -5.0000 -9.0000 -13.00001980-Q-002 -2.0000 -6.0000 -10.0000 -14.00001980-Q-003 -3.0000 -7.0000 -11.0000 -15.00001980-Q-004 -4.0000 -8.0000 -12.0000 -16.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 51/96

Concatenazione orizzontalebb=tsmat(1979,1,4,reshape(1:16,4,4))Tsmat object

Size: [4 4]Frequency: 4

Start: 1979 1End: 1979 4

Labels: T1 T2 T3 T4tstab(bb)tsStart: 1979 1tsRelease: 19-Jan-2008Frequency: 4 (quarterly)Date

T1 T2 T3 T419/01/08 19/01/08 19/01/08 19/01/08

1979-Q-001 1.0000 5.0000 9.0000 13.00001979-Q-002 2.0000 6.0000 10.0000 14.00001979-Q-003 3.0000 7.0000 11.0000 15.00001979-Q-004 4.0000 8.0000 12.0000 16.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 52/96

Concatenazione orizzontalecc=[aa,bb]Tsmat object

Size: [8 8]Frequency: 4

Start: 1979 1End: 1980 4

Labels: T1 T2 T3 T4 T1 T2 T3 T4tstab(cc)tsStart: 1979 1tsRelease: 19-Jan-2008Frequency: 4 (quarterly)Date

T1 T2 [...] T3 T419/01/08 19/01/08 [...] 19/01/08 19/01/08

1979-Q-001 NaN NaN [...] 9.0000 13.00001979-Q-002 NaN NaN [...] 10.0000 14.00001979-Q-003 NaN NaN [...] 11.0000 15.00001979-Q-004 NaN NaN [...] 12.0000 16.00001980-Q-001 -1.0000 -5.0000 [...] NaN NaN1980-Q-002 -2.0000 -6.0000 [...] NaN NaN1980-Q-003 -3.0000 -7.0000 [...] NaN NaN1980-Q-004 -4.0000 -8.0000 [...] NaN NaN

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 53/96

Concatenazione verticalecc=[aa;bb]Tsmat object

Size: [8 4]Frequency: 4

Start: 1979 1End: 1980 4

Labels: T1 T2 T3 T4tstab(cc)tsStart: 1979 1tsRelease: 19-Jan-2008Frequency: 4 (quarterly)Date

T1 T2 T3 T419/01/08 19/01/08 19/01/08 19/01/08

1979-Q-001 1.0000 5.0000 9.0000 13.00001979-Q-002 2.0000 6.0000 10.0000 14.00001979-Q-003 3.0000 7.0000 11.0000 15.00001979-Q-004 4.0000 8.0000 12.0000 16.00001980-Q-001 -1.0000 -5.0000 -9.0000 -13.00001980-Q-002 -2.0000 -6.0000 -10.0000 -14.00001980-Q-003 -3.0000 -7.0000 -11.0000 -15.00001980-Q-004 -4.0000 -8.0000 -12.0000 -16.0000

[aa ; bb] == extend(aa,bb,nan,true)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 54/96

Concatenazione verticalecc=[aa{-2};bb]Tsmat object

Size: [8 4]Frequency: 4

Start: 1979 1End: 1980 4

Labels: T1 T2 T3 T4tstab(cc)tsStart: 1979 1tsRelease: 19-Jan-2008Frequency: 4 (quarterly)Date

T1 T2 T3 T419/01/08 19/01/08 19/01/08 19/01/08

1979-Q-001 1.0000 5.0000 9.0000 13.00001979-Q-002 2.0000 6.0000 10.0000 14.00001979-Q-003 3.0000 7.0000 11.0000 15.00001979-Q-004 4.0000 8.0000 12.0000 16.00001980-Q-001 NaN NaN NaN NaN1980-Q-002 NaN NaN NaN NaN1980-Q-003 -1.0000 -5.0000 -9.0000 -13.00001980-Q-004 -2.0000 -6.0000 -10.0000 -14.00001981-Q-001 -3.0000 -7.0000 -11.0000 -15.00001981-Q-002 -4.0000 -8.0000 -12.0000 -16.0000

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 55/96

Concatenazione verticale non adiacentecc=[aa;bb{-1}]??? Error using ==> tsmat.extend"FillDateBeforeLastDate"

Error in ==> <a href="error:/Users/exedre/Work/Matlab/bi/toolboxes/bits2/@tsmat/extend.m,39,1">tsmat.extend at 39</a>self = extend(a, self, [ a.start_year a.start_period ], fill_date);

Error in ==> <a href="error:/Users/exedre/Work/Matlab/bi/toolboxes/bits2/@tsmat/vertcat.m,16,1">tsmat.vertcat at 16</a>res = extend(res,varargin{i},nan,true);

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 56/96

extend Unione delle serie cn il controllo della sovrapposizione>> ts1 = tsmat(1980,1,12,rand(7,1));>> ts2 = tsmat(1980,6,12,rand(4,1));>> ts=extend(ts1,ts2)>> tstab(ts,ts1,ts2)[...] T1 T1 T1

05/09/07 05/09/07 05/09/071980-M-001 0.4457 0.4457 NaN1980-M-002 0.9388 0.9388 NaN1980-M-003 0.6338 0.6338 NaN1980-M-004 0.9297 0.9297 NaN1980-M-005 0.0629 0.0629 NaN1980-M-006 0.0104 0.3069 0.01041980-M-007 0.1678 0.9498 0.16781980-M-008 0.7553 NaN 0.75531980-M-009 0.8338 NaN 0.8338

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 57/96

extend Unione delle serie cn il controllo della sovrapposizione>> ts=extend(ts1,ts2,[1980 7])>> tstab(ts,ts1,ts2)[...] T1 T1 T1

05/09/07 05/09/07 05/09/071980-M-001 0.4457 0.4457 NaN1980-M-002 0.9388 0.9388 NaN1980-M-003 0.6338 0.6338 NaN1980-M-004 0.9297 0.9297 NaN1980-M-005 0.0629 0.0629 NaN1980-M-006 0.3069 0.3069 0.01041980-M-007 0.1678 0.9498 0.16781980-M-008 0.7553 NaN 0.75531980-M-009 0.8338 NaN 0.8338

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 58/96

extend Unione di serie non contigue>> ts1=tsmat(1980,1,12,rand(5,1))>> ts2=tsmat(1980,7,12,rand(5,1));[...] T1 T11980-M-001 0.6059 NaN1980-M-002 0.5179 NaN1980-M-003 0.9429 NaN1980-M-004 0.7412 NaN1980-M-005 0.7011 NaN1980-M-006 NaN NaN1980-M-007 NaN 0.74471980-M-008 NaN 0.87381980-M-009 NaN 0.96411980-M-010 NaN 0.98191980-M-011 NaN 0.9186

>> ts=extend(ts1,ts2)??? Error using ==> tsmat.extend at 130"TimeseriesCannotBeLinked"

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 59/96

extend Unione di serie non contigue>> ts1=tsmat(1980,1,12,rand(5,1))>> ts2=tsmat(1980,7,12,rand(5,1));[...] T1 T11980-M-001 0.6059 NaN1980-M-002 0.5179 NaN1980-M-003 0.9429 NaN1980-M-004 0.7412 NaN1980-M-005 0.7011 NaN1980-M-006 NaN NaN1980-M-007 NaN 0.74471980-M-008 NaN 0.87381980-M-009 NaN 0.96411980-M-010 NaN 0.98191980-M-011 NaN 0.9186>> ts=extend(ts1,ts2)??? Error using ==> tsmat.extend at 130"TimeseriesCannotBeLinked"

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 59/96

extend Unione serie non contigue>> ts=extend(ts1,ts2,nan,true)

T1 T1 T105/09/07 05/09/07 05/09/07

1980-M-001 0.6059 0.6059 NaN1980-M-002 0.5179 0.5179 NaN1980-M-003 0.9429 0.9429 NaN1980-M-004 0.7412 0.7412 NaN1980-M-005 0.7011 0.7011 NaN1980-M-006 NaN NaN NaN1980-M-007 0.7447 NaN 0.74471980-M-008 0.8738 NaN 0.87381980-M-009 0.9641 NaN 0.96411980-M-010 0.9819 NaN 0.98191980-M-011 0.9186 NaN 0.9186

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 60/96

Operazioni tra le tsmat

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 61/96

La di!erenza tra struttura e oggetto

Struttura Oggettos = struct() o = class() Creaziones.proprieta o.proprieta Accessos.campo = valore o.campo = valore Riempimentofunzione(s) metodo(o) Funzione/Metodos.dati = m.dati + n.dati Operazioni tra strutture

s = m + n Operazioni tra oggetti

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 62/96

Operazioni sulle serie (senza esclusione dei nan)

max(A)min(A)mean(A)median(A)std(A)sum(A)var(A)abs(A)ceil(A)TODO: iqr(A)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 63/96

Operazioni sulle serie con esclusione dei nan

nanmax(A)nanmin(A)nanmean(A)nanmedian(A)nanstd(A)nansum(A)nanva(A)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 64/96

Operazioni tra le serie

tstab(A,B, A*10)tstab(A,B, A+B)tstab(A,B, A-B)tstab(A,B, A*B)tstab(A,B, A./B)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 65/96

Altre operazioni

corr(TSC)corr(TSC.matdata)corr(count(:,1),count(:,2))ans =

0.9331corr(TSC(:,1),TSC(:,2))??? Error using ==> tsmat.subsref@tsmat\subsref::TS Index exceeds matrix dimensions

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 66/96

Altre operazioni

cov(TSC)cov(TSC.matdata)ans =

1.0e+003 *0.6437 0.9802 1.65670.9802 1.7144 2.69081.6567 2.6908 4.6278

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 67/96

Altre operazioni

tstab(TSC,cumsum(TSC))tstab(cumprod(TSC))

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 68/96

Operazioni logiche

tstab(TSC==TSC)tsStart: 1980 1tsRelease: 18-Jan-2008Frequency: 365 (daily)Date

T1 T2 T318/01/08 18/01/08 18/01/08

1980-D-001 01-Jan-1980 1.0000 1.0000 1.00001980-D-002 01-Jan-1980 1.0000 1.0000 1.00001980-D-003 01-Jan-1980 1.0000 1.0000 1.0000[...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 69/96

Operazioni logiche

TSC1 = tsmat(1980,1,365,rand(24,3)*10*)tstab(TSC>TSC1) (tstab(ge(TSC,TSC1))tsStart: 1980 1tsRelease: 18-Jan-2008Frequency: 365 (daily)Date

T1 T2 T318/01/08 18/01/08 18/01/08

1980-D-001 01-Jan-1980 0.0000 0.0000 0.00001980-D-002 01-Jan-1980 0.0000 0.0000 0.00001980-D-003 01-Jan-1980 0.0000 0.0000 1.00001980-D-004 01-Jan-1980 0.0000 0.0000 0.0000[...]

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 70/96

Operazioni e Funzioni

Operazione Funzionegt(A,B) A > Bge(A,B) A " Blt(A,B) A < Ble(A,B) A # Bne(A,B) A $= B

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 71/96

Determinazione dei valori

Operazione Funzioneisnan $= NaNisfinite $=%

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 72/96

Introduciamo dei missing in testa e in coda

TSC([1:3],[1:3])=NaN; tstab(TSC)TSC([24],[1:3])=NaN; tstab(TSC)

Stringiamo l’intervallo di definizione della serie ai valori non missing

TSCn=notnanrange(TSC)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 73/96

Lag e Lead

TSC = tsmat(1980,1,365,count(:,1))tstab(TSC,tslag(TSC,1),tslag(TSC,-1))tsStart: 1979 365tsRelease: 18-Jan-2008Frequency: 365 (daily)Date

T1 T1 T118/01/08 18/01/08 18/01/08

1979-D-365 31-Dec-1979 NaN NaN 11.00001980-D-001 31-Dec-1979 11.0000 NaN 7.00001980-D-002 31-Dec-1979 7.0000 11.0000 14.0000[...]1980-D-023 31-Dec-1979 13.0000 11.0000 10.00001980-D-024 31-Dec-1979 10.0000 13.0000 NaN1980-D-025 31-Dec-1979 NaN 10.0000 NaN

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 74/96

Gestione dei Metadati

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 75/96

Metadati della collezione

ts.meta.<campo>getmeta(ts,’<campo>’)getmeta(ts,’<campo>’,<default>)

rel = getmeta(ts,’release’,today)rel =

733426

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 76/96

Metadati della collezione

A.meta.pippo=’pluto1’;pluto=A.meta.pippo;A.metaans =

release: 733426pippo: ’pluto1’

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

A=addmeta(A,’pippo’,’pluto2’);A.metaans =

release: 733426pippo: ’pluto2’

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 77/96

Un metadato notevole: release

Il metadato release indica la data in cui la serie epresa in considerazione (?!?)

Non viene utilizzata in alcun modo nei calcoli ma emolto importante nelle operazioni di vintage dei dati

La release della singola serie (ovverots.meta colsj.release), se presente, verrautilizzata per indicare il vintage dei dati, altrimenti siutilizzera la release dell’intera collezione(ts.meta.release), nel caso in cui non sia presenteneppure questo (errore) si utilizzera la data corrente.

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 78/96

EsempioA.meta.releaseans =

733426>> datestr(A.meta.release)ans =19-Jan-2008

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 79/96

Cancellare un metadatormfield(A.meta,’pippo’) % NON HA EFFETTO

A.meta = rmfield(A.meta,’pippo’) % CORRETTO

oppure

A=delmeta(A,’pippo’)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 80/96

Metadati di colonnaTSC.meta_colsans =

label: {’T1’ ’T2’ ’T3’}

Metadati di colonnaI metadati di colonna sono ospitati in una struttura i cui campi sono

cell-array.E uno strumento del linguaggio Matlab un po’ contorto a cui bisogna

fare attenzione e che provoca un bel po’ di problemi.

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 81/96

Metadati di colonnaTSC.meta_cols.release(2) # NON FUNZIONA

M = TSC.meta_cols # BISOGNA FARE COSI’label = M.labels(2)

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 82/96

Metadati di colonnagetcolmeta(TSC,2,’label’)ans =

’T2’getcolmeta(TSC,2,’release’,today)ans =

733426

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 83/96

Metadati di colonnagetcolmeta(TSC,2,’label’)ans =

’T2’getcolmeta(TSC,2,’release’,today)ans =

733426FCM = getfullcolmeta(TSC)1x3 struct array with fields:

labelFCM(1).labelans =

’T1’

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 84/96

Metadati di colonnagetcolmeta(TSC,2,’label’)ans =

’T2’getcolmeta(TSC,2,’release’,today)ans =

733426FCM = getfullcolmeta(TSC)1x3 struct array with fields:

labelFCM(1).labelans =

’T1’

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 85/96

Metadati di colonnagetcolmeta(TSC,2,’label’)ans =

’T2’getcolmeta(TSC,2,’release’,today)ans =

733426FCM = getfullcolmeta(TSC)1x3 struct array with fields:

labelFCM(1).labelans =

’T1’

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 86/96

Metadati di colonnaTSC=addmeta_cols(TSC, 3, ’release’, {today })

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 87/96

Plot

1 Le serie storiche in MATLABL’oggetto timeseriesL’oggetto fints

2 BITS - Bank of Italy Time SeriesL’oggetto tsmatCreazione della tsmatIndicizzazione per l’accessoRiorganizzazioneOperazioni tra le tsmatGestione dei MetadatiPlot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 88/96

Esempio di plot

aa=tsmat(1980,1,12,randn(120,1));bb=filter(1,[1 -0.9],aa);h=plot([aa,bb]);legend(’aa tsmat’,’filtered tsmat bb’);% Let’s add a moving average of the bb tsmatcc=mave(bb,12);plot([aa,bb,cc]);legend(’aa tsmat’,’filtered tsmat bb’,’12 terms moving average of bb’);

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 89/96

Esempio di plot

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 90/96

Grazie dell’attenzione

E. Somma (SIA-BdI) WMBB 2008 21/01/2008 91/96