⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pca.m

📁 计量工具箱
💻 M
字号:
function [weights, principalcomponets, eigenvalues,explainedvariance, cumR2]=pca(data,type);
% PURPOSE:
%      To perform Centered(demeaned) Principal Componet Analysis for use in the O_MVGARCH proceedure
% 
% 
% USAGE:
%      [weights, principalcomponets, eigenvalues,explainedvariance, cumR2]=pca(data,type);
% 
% 
% INPUTS:
%      data    - a t by k matrix to be decomposed using PCA
%      type    - (optional) a string, wither 'cov' or 'corr' that denotees the way
%                you wish to use PCA, on either the Covariance Matrix or Correlation MAtrix
%                both are useful and the cov is similar to TMW princomp
% 
% OUTPUTS:
%      weights            - a k by k matrix of componet weights
%      principalcomponets - a t by k matrix of principal componets
%      eigenvalues        - The eeigenvaluse associated with each PC
%      explainedvariance  - The percent of the viariance explained by each PC
%      cumR2              - The cumulative R2 of including the ith PC
% 
% 
% COMMENTS:
% 
% 
% Author: Kevin Sheppard
% kksheppard@ucsd.edu
% Revision: 2    Date: 12/31/2001
% 


[t,k]=size(data);
data=data-repmat(mean(data),t,1);
if nargin==1 | strcmp(type,'Corr')
    newdata=data./repmat(std(data),t,1);
    CorrMat=corrcoef(newdata);
else
    newdata=data;
    CorrMat=cov(data);
end

[EigenVects,EigenVals]=eig(CorrMat);
EigenVals=fliplr(flipud(EigenVals));
EigenVects=fliplr(flipud(EigenVects));
tempdata=fliplr(newdata);
weights=EigenVects;

principalcomponets=tempdata*EigenVects;
weights=flipud(weights);
eigenvalues=diag(EigenVals);
explainedvariance=eigenvalues/sum(eigenvalues);
cumR2=cumsum(explainedvariance);
if strcmp(type,'Cov')
    principalcomponets=principalcomponets;%.*repmat(std(data),length(data),1)
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -