Workshop Matlab BITS Avanzato
-
Upload
emmanuele-somma -
Category
Technology
-
view
1.316 -
download
0
description
Transcript of Workshop Matlab BITS Avanzato
WMBA 2008BITS: Serie Storiche in Matlab II
Emmanuele [email protected]
Supporto Informatico per l’Area RicercheBanca d’Italia
21 Gennaio 2008
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 1/43
Piano della presentazione
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 2/43
Preprocessing
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 3/43
Lettura dei dati
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 4/43
Vettore e time-series di datiload count.datc3 = count(:,3) # VETTOREt3 = tsmat(2007,1,4,c3) # TIME SERIESc3NaNCount = sum(isnan(c3))ans =
0t3NanCount = sum(isnan(t3))ans =
0
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 5/43
plot(c3); plot(t3)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 6/43
Missing e outliers
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 7/43
Outliersbin_counts = hist(c3) bin_counts = hist(t3.matdata)
# Massimo elementoN = max(bin_counts);
# Mediamu3 = mean(c3); mu3 = mean(t3);
# Deviazione Stdsigma3 = std(c3); sigma3 = std(c3);
hist(c3); hist(t3.matdata)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 8/43
hold on; X = repmat(mu3+(1:2)*sigma3,2,1);Y = repmat([0;N],1,2);plot([mu3 mu3],[0 N],’r’,’LineWidth’,2);plot(X,Y,’g’,’LineWidth’,2)legend(’Data’,’Mean’,’Stds’); hold off
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 9/43
outliers = (c3 - mu3) > 2*sigma3;c3m = c3;c3m(outliers) = NaN;
outliers = (t3 - mu3) > 2*sigma3;t3m = t3;t3m.matdata(outliers.matdata) = NaN;
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 10/43
bin_counts = hist(t3m.matdata); N = max(bin_counts);hist(t3m.matdata); mu3 = nanmean(t3m); sigma3 = nanstd(t3m)hold on; X = repmat(mu3+(1:2)*sigma3,2,1);Y = repmat([0;N],1,2);plot([mu3 mu3],[0 N],’r’,’LineWidth’,2);plot(X,Y,’g’,’LineWidth’,2)legend(’Data’,’Mean’,’Stds’); hold off
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 11/43
Aggiustare e filtrare
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 12/43
Smoothing
plot(t3m,’o-’)hold on
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 13/43
Smoothing
span = 3; % Ampiezza della finestrawindow = ones(span,1)/span; smoothed_t3m = t3msmoothed_t3m.matdata = convn(t3m.matdata,window,’same’);h = plot(smoothed_t3m,’ro-’);
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 14/43
Filtraggiosmoothed2_t3m = t3m;smoothed2_t3m.matdata = filter(window,1,t3m.matdata);plot(smoothed2_t3m,’go-’);legend(’Data’,’Smoothed Data’,’Filtered Data’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 15/43
Misurazione
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 16/43
Misurazione dei dati% Posizione % Scalax1 = mean(t) ; dx1 = max(t)-min(t)x2 = median(t) ; dx2 = std(t)x3 = mode(t) ; dx3 = var(t)
% Formafigure ; hist(t.matdata)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 17/43
Forma dei dati/1
I modelli parametrici danno una rappresentazione analitica della formadella distribuzione
t1 = tsmat(2007,1,4,count(:,1));[bin_counts,bin_locations] = hist(t1.matdata);bin_width = bin_locations(2) - bin_locations(1);hist_area = (bin_width)*(sum(bin_counts));figurehist(t1.matdata)hold onmu1 = mean(t1);exp_pdf = @(t)(1/mu1)*exp(-t/mu1);t = 0:150;y = exp_pdf(t);plot(t,(hist_area)*y,’r’,’LineWidth’,2)legend(’Distribution’,’Exponential Fit’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 18/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 19/43
Rappresentazione
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 20/43
Grafici 2D
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 21/43
Forma dei dati/2
t1 = tsmat(2007,1,4,count(:,1)); % Serie 1t2 = tsmat(2007,1,4,count(:,2)); % Serie 2figurescatter(t1.matdata,t2.matdata,’filled’)xlabel(’Intersection 1’)ylabel(’Intersection 2’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 22/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 23/43
Forma dei dati/2
C12 = cov([t1 t2]) % CovarianzaR12 = corrcoef([t1 t2])r12 = R12(1,2) % Coefficiente di correlazioner12sq = r12^2 % Coefficiente di determinazione
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 24/43
Grafici 3D
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 25/43
Forma dei dati/2
figurescatter3(t1.matdata,t2.matdata,t3.matdata,’filled’)xlabel(’Intersection 1’)ylabel(’Intersection 2’)zlabel(’Intersection 3’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 26/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 27/43
La forza della relazione lineare tra le variabili e misurata calcolando gliautovalori della matrice di covarianza:
>> vars = eig(cov([t1 t2 t3]))vars =
1.0e+003 *0.04420.11186.8300
>> explained = max(vars)/sum(vars)explained =
0.9777
Gli autovalori sono le varianze lungo le componenti principali dei dati. Lavariabile explained misura la proporzione della variazione spiegata dallaprima componente principale lungo l’asse dei dati.
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 28/43
Matrice di Grafici Scatter
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 29/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 30/43
t = tsmat(2007,1,4,count)figureplotmatrix(t.matdata)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 31/43
Modellazione
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 32/43
Regressione Polinomiale
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 33/43
t3 = tsmat(2007,1,4,count(:,3)); % Serie 3tdata = [1:size(t3.dates,1)]’;p_coeffs = polyfit(tdata,t3.matdata,6);figureplot(t3,’o-’)hold onn = ( max(tdata) / 6 ) / 365;tfit = (1:n:24)’;yfit = tsmat(2007,1,365,polyval(p_coeffs,tfit));plot(yfit,’r-’)legend(’Data’,’Polynomial Fit’,’Location’,’NW’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 34/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 35/43
Regressione Lineare Generalizzata
1 PreprocessingLettura dei datiMissing e outliersAggiustare e filtrare
2 Misurazione
3 RappresentazioneGrafici 2DGrafici 3DMatrice di Grafici Scatter
4 ModellazioneRegressione PolinomialeRegressione Lineare Generalizzata
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 36/43
y = a + b cos((2!/12)(t! 7))
t3 = tsmat(2007,1,4,count(:,3)); % Serie 3tdata = [1:size(t3.dates,1)]’;X = [ones(size(tdata)) cos((2*pi/12)*(tdata-7))];s_coeffs = X\t3.matdata;figureplot(t3,’o-’)hold onn = ( max(tdata) / 6 ) / 365;tfit = (1:n:24)’;yfit = [ones(size(tfit)) cos((2*pi/12)*(tfit-7))]*s_coeffs;yt = tsmat(2007,1,365,yfit);plot(yt,’r-’)legend(’Data’,’Sinusoidal Fit’,’Location’,’NW’)
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 37/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 38/43
>> [s_coeffs,stdx,mse] = lscov(X,c3)s_coeffs =
65.583373.2819
stdx =8.918512.6127
mse =1.9090e+003
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 39/43
Fs = 1; % Frequenza di campionamenton = length(t3.matdata); % Lunghezza della finestraY = fft(t3.matdata);% DFTf = (0:n-1)*(Fs/n); % Ampiezza della frequenzaP = Y.*conj(Y)/n; % Potenza della DFTfigureplot(f,P)xlabel(’Frequency’)ylabel(’Power’)predicted_f = 1/12predicted_f =
0.0833
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 40/43
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 41/43
Grazie dell’attenzione
E. Somma (SIA-BdI) WMBA 2008 21/01/2008 42/43