; Method according to the Lindsay Smith tutorial. ; http://csnet.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf ; Create the data. x = [2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1] y = [2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9] ; Subtract the means from the dta. xmean = x - Mean(x, /DOUBLE) ymean = y - Mean(y, /DOUBLE) ; Plot of the original data with the means subtracted. Window, XSIZE=500, YSIZE=350, TITLE='Plot of Original Data (Means Subtracted)', 0 Plot, xmean, ymean, BACKGROUND=FSC_Color('ivory'), COLOR=FSC_Color('navy'), $ /NODATA, POSITION=Aspect(1.0), XRANGE=[-2,2], YRANGE=[-2,2] OPlot, xmean, ymean, PSYM=7, COLOR=FSC_Color('indian red'), THICK=2, SYMSIZE=1.5 OPlot, [-2, 2], [0,0], LineStyle=2, COLOR=FSC_Color('gray') OPlot, [0,0], [-2, 2], LineStyle=2, COLOR=FSC_Color('gray') ; Create data in a column array. dataAdjust = Transpose([ [xmean], [ymean] ]) ; Create the covariance matrix. covMatrix = Correlate(dataAdjust, /COVARIANCE, /DOUBLE) ; Calculate the eigenvalues and eigenvectors. eigenvalues = EIGENQL(covMatrix, EIGENVECTORS=eigenvectors, /DOUBLE) ; Print the values. Print, 'IDL EIGENVALUES: ', eigenvalues Print, 'IDL EIGENVECTORS' Print, eigenvectors Print, "" ; A plot of the original data, showing the eigenvectors. Window, XSIZE=500, YSIZE=350, TITLE='Plot of Original Data with Calculated Eigenvalues', 1 Plot, xmean, ymean, BACKGROUND=FSC_Color('ivory'), COLOR=FSC_Color('navy'), $ /NODATA, POSITION=Aspect(1.0) OPlot, xmean, ymean, PSYM=7, COLOR=FSC_Color('indian red'), THICK=2, SYMSIZE=1.5 xx = Scale_Vector(Findgen(11), -2, 2) OPlot, eigenvectors[0,0]*xx, eigenvectors[0,1]*xx, Color=FSC_Color('Charcoal'), Thick=2 OPlot, eigenvectors[1,0]*xx, eigenvectors[1,1]*xx, Color=FSC_Color('Charcoal'), Thick=2 OPlot, [-2, 2], [0,0], LineStyle=2, COLOR=FSC_Color('gray') OPlot, [0,0], [-2, 2], LineStyle=2, COLOR=FSC_Color('gray') ; The feature vector contains all the eigenvectors. Transform the data. featureVector = eigenvectors finalData = Transpose(featureVector) ## Transpose(dataAdjust) ; Plot of the data rotated by the eigenvectors. Window, XSIZE=500, YSIZE=350, TITLE='Plot of Original Data Rotated by Eigenvalues', 2 Plot, finalData[*,0], finalData[*,1], BACKGROUND=FSC_Color('ivory'), COLOR=FSC_Color('navy'), $ /NODATA, POSITION=Aspect(1.0), XRANGE=[-2,2], YRANGE=[-2,2] OPlot, finalData[*,0], finalData[*,1], PSYM=7, COLOR=FSC_Color('indian red'), THICK=2, SYMSIZE=1.5 OPlot, [-2, 2], [0,0], LineStyle=2, COLOR=FSC_Color('gray') OPlot, [0,0], [-2, 2], LineStyle=2, COLOR=FSC_Color('gray') ; Use the first principal component to rotate the data. featureVector = eigenvectors[0,*] ; The first COLUMN!! singleVector = Transpose(featureVector) ## Transpose(dataAdjust) ; Plot of the data transformed by the first principal component. Window, XSIZE=500, YSIZE=350, TITLE='Data Transformed with First Principle Component', 3 Plot, singleVector, singleVector, BACKGROUND=FSC_Color('ivory'), $ COLOR=FSC_Color('navy'), /NODATA, POSITION=Aspect(1.0) OPlot, finaldata, finaldata, PSYM=7, COLOR=FSC_Color('indian red'), $ THICK=2, SYMSIZE=1.5 OPlot, [-2, 2], [0,0], LineStyle=2, COLOR=FSC_Color('gray') OPlot, [0,0], [-2, 2], LineStyle=2, COLOR=FSC_Color('gray') ; Original data, transformed by eigenvalues. rotatedOrigData = finalData + Rebin([[Mean(x)], [Mean(y)]], 10, 2) Window, XSIZE=500, YSIZE=350, TITLE='Original Data, Rotated by Eigenvectors', 4 Plot, rotatedOrigData [*,0], rotatedOrigData [*,1], /NODATA, $ BACKGROUND=FSC_Color('ivory'), COLOR=FSC_Color('navy'), POSITION=Aspect(1.0) OPlot, rotatedOrigData [*,0], rotatedOrigData [*,1], THICK=2, $ COLOR=FSC_Color('indian red'), SYMSIZE=1.5, PSYM=7 ; Method using PCOMP in IDL library. x = [2.5, 0.5, 2.2, 1.9, 3.1, 2.3, 2.0, 1.0, 1.5, 1.1] y = [2.4, 0.7, 2.9, 2.2, 3.0, 2.7, 1.6, 1.1, 1.6, 0.9] xmean = x - Mean(x, /DOUBLE) ymean = y - Mean(y, /DOUBLE) dataAdjust = Transpose([ [xmean], [ymean] ]) fd = PCOMP(dataAdjust, /COVARIANCE, EIGENVALUES=evalues, COEFFICIENTS=evectors, /DOUBLE) ; Comparison of PCOMP vs. the regular IDL way. Print, 'PCOMP EIGENVALUES: ' Print, Transpose(evalues) Print, '' Print, 'PCOMP EIGENVECTORS: ' Print, evectors / Rebin(SQRT(evalues), 2, 2) Print, "" Print, 'Data From Previous Analysis: ' Print, Transpose(rotatedOrigData) Print, '' Print, 'Data From PCOMP Analysis: ' pcompRotatedOrigData = fd / Rebin([SQRT(evalues[0]), SQRT(evalues[1])], 2, 10) pcompRotatedOrigData = pcompRotatedOrigData + Rebin([Mean(x), Mean(y)], 2, 10) Print, pcompRotatedOrigData END