11/28/2006

11.11.2 多項式迴歸polyfit & polyval

前文已介紹ployfit多項式適配之用法,現在再介紹其詳細的應用。polyfit指令係以多項式函數尋找配合X資料之曲線,主要在求其適配之多項式係數:


y(x)=p1xn+p2xn-1+...+pnx+pn+1


求係數與求預測值之語法分別如下:

[p,S] = polyfit(x,y,n)
Y = polyval(p,X)
[Y,DELTA] = polyval(p,X,S)
[Y,DELTA] = polyconf(p,X,S)
[Y,DELTA] = polyconf(p,X,S,alpha)

其中X為觀測值,n為模擬之階數。輸出參數p則為多項式之係數,S為特性矩陣,可用於polyval指令函數以產生預測值。若y資料為獨立常態性且定值變方,則利用polyval或polyconf所產生之誤差區間,或Y DELTA至少包含50%之預測點。polyconf之輸入參數有alpha項,此為顯著水準,其預設值為0.05,此為設定信任水準求得相關對應值之指令。

例如:要適配一組隨機之變數樣本,利用normrnd來完成。normrnd之函數指令,其功能與randn指令相同,只是normrnd(m,s,M,N)之參數中,m為設定之均值,s為設定之標準差。

[p,S]=polyfit(1:20,[1:20]+normrnd(0,1,1,20),2)
p =
-0.0083 1.1649 -0.7796
S =
R: [3x3 double]
df: 17
normr: 4.0758

得到的方程式為

y(x)=-0.0083x²+1.1649x - 0.7796

使用上述得到之參數,可代入polyval進行預測:

X=2:0.5:15;
[Y, D]=polyval(p,X,S);
plot(X,Y,'r-',X,Y+D,'b:',X,Y-D,'b:'),
xlabel('X');ylabel('Y');grid



例二:汽車之資料包括車重,選樣測定其里程數,其中有部份不合格者,試求不合格之比例:


% A set of car weights
weight = [2100 2300 2500 2700 2900 3100 3300 3500 3700 3900 4100 4300]';
% The number of cars tested at each weight
tested = [48 42 31 34 31 21 23 23 21 16 17 21]';
% The number of cars failing the test at each weight
failed = [1 2 0 3 8 8 14 17 19 15 17 21]';
% The proportion of cars failing for each weight
proportion = failed ./ tested;
plot(weight,proportion,'s')
xlabel('Weight'); ylabel('Proportion');
% We can try fitting a straight line to these data.
linearCoef = polyfit(weight,proportion,3);
linearFit = polyval(linearCoef,weight);
plot(weight,proportion,'s', weight,linearFit,'r-', ...
[2000 4500],[0 0],'k:', [2000 4500],[1 1],'k:')
xlabel('Weight'); ylabel('Proportion');