yugao1986的专栏

SAS and R 学习记录

相关系数与协方差间的转换

首先,相关系数和协方差间的数学关系为:

其次,上述数学表达式在sas/iml中的算法表达如下,R为相关系数矩阵,S为对角阵, COV协方差矩阵

其中:

 

最后,上代码:

***** Program 4.10 PROC IML ***********;
***** Converting a correlation matrix to covariance matrix, and vice versa;
***** Example data from Table 4.4 ;
****** Part I: Converting correlation matrix to covariance matrix;
PROC IML;

*** define the correlation matrix;
R={1.00 0.70 0.20,
   0.70 1.00 0.40,
   0.20 0.40 1.00};

*** define the diagonal matrix with standard deviations on the diagonal;
S={15 0 0,
    0 10 0,
    0 0 1};

*** obtain the covariance matrix;	
COV=S*R*S;  

*** print the covariance matrix;
PRINT COV; 
RUN;

***** Part II: Converting covariance matrix to correlation matrix;
PROC IML;

*** define the covariance matrix;
COV={225 105 3,
     105 100 4,
       3   4 1};
	   
*** obtain the matrix with standard deviations on the diagonal;
S=SQRT(DIAG(COV));

*** the inverse of S matrix;
S_INV=INV(S); 

*** obtain correlation matrix;
R=S_INV*COV*S_INV; 

** print out the three matrices;
PRINT COV S R;
RUN;


FROM 《SAS for Monte Carlo Studies》,P96-97

阅读更多
文章标签: matrix table 算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭