Statistical programming, matrix languages, and more

Matrix in proc fcmp

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Matrix in proc fcmp

Hey to all,

I would like to know how to delete in array and matrix using sas proc fcmp. And also, I wanted to know if can call the same subroutine

such I did at line 49.

Here is the code I made.

Regards

 

proc fcmp outlib=work.func.matrix;
  subroutine GSPfunc(mat[*,*],GSP[*,*]);
    outargs GSP;

epsilon= 10 ** -10;
array vectors{1,1} _temporary_;
array t_vectors{1,1} _temporary_;
call dynamic_array(vectors, dim(mat,1),dim(mat,1));
call dynamic_array(t_vectors, dim(mat,1),dim(mat,1));

array values{dim(mat,1)} _temporary_;
array mat2{1,1} _temporary_;
call dynamic_array(mat2, dim(mat,1),dim(mat,1));

/*Calculation of Eigenvecteurs and Eigenvalues by suroutine Jacobi*/
call jacobi(mat,values,vectors,nrot);

do i= dim to 1 by -1;
	if values[i]<= epsilon then  do;
		if  values[i]< -epsilon then IsNegative= 1 do; 
			delete /* value[i] ?*/;
delete /*vectors[i,.] ?*/; /*HELP ME HERE: I want to delete the value[i] and the corresponding vectors[i,.]*/ end;
end; else do; do j= dim to 1 by -1; vectors[j,i]= vectors[j,i] * sqrt(values[i]); end; end; end; if IsNegative= 1 then do; /*In this case, I'm bulding a new matrix: mat2, that is set with mat without vectors[i,.]*/ call transpose(vectors, t_vectors); call mult(vectors, t_vectors, mat2); do i= 1 to dim(mat2,1); mat2[i, i] = Sqrt(mat2[i, i]); end; do i= 1 to dim(mat2,1)-1; do j= i+1 to dim(mat2,1); mat2[i,j]= mat2[i,j] / (mat2[j,j] * mat2[i, i]); mat2[j,i]= mat2[i,j]; end; end; do i= 1 to dim(mat2,1); mat2[i,j]= 1; end; /*repete all the process on mat2*/ do i= 1 to dim(vectors,1); do j= 1 to dim(vectors,2); GSP[i,j] = GSPfunc(mat2,GSP]; end; end; end; else if IsNegative ne 1 then do; do i= 1 to dim(vectors,1); do j= 1 to dim(vectors,2); GSP[i,j] = vectors[i,j]; end; end; end; endsub; run;

 


Accepted Solutions
Solution
‎08-21-2017 08:40 AM
SAS Super FREQ
Posts: 3,559

Re: Matrix in proc fcmp

1. What are you trying to accomplish?

2. Are your input matrices always symmetric?

3. Do you have SAS/IML software?

 

It looks like maybe you are trying to detect whether a matrix is positive definite by checking if it has negative eigenvalues. It looks like you want to delete the eigenvectors that correspond to the negative eigenvalues.  Am I close?

 

 

 

View solution in original post


All Replies
Solution
‎08-21-2017 08:40 AM
SAS Super FREQ
Posts: 3,559

Re: Matrix in proc fcmp

1. What are you trying to accomplish?

2. Are your input matrices always symmetric?

3. Do you have SAS/IML software?

 

It looks like maybe you are trying to detect whether a matrix is positive definite by checking if it has negative eigenvalues. It looks like you want to delete the eigenvectors that correspond to the negative eigenvalues.  Am I close?

 

 

 

Contributor
Posts: 62

Re: Matrix in proc fcmp

 

 

Trying to answer to you, I accidentally click on "accep as solution". Could you re-edit my post please ?

 

I don't have proc iml.

 

Yes absolutly, my input matrix is alway symmetric. It's a symetric correlation matrix and I calculate its

EigenVectors and EigenValues.

 

When one EigenValue is negative, I delete it and the corresponding EigenVectors. And  I re-bult a new symetric correlation matrix, that is mat2.

 

Regards

SAS Super FREQ
Posts: 3,559

Re: Matrix in proc fcmp

I don't have the authority to "un-accept" the solution. 

 

If the input matrices are correlation matrices, then they are always positive semidefinite. That is, they will never have negative eigenvalues. There might be one or more zero eigenvalues if the correlation matrix is rank-deficient.

 

If you don't have PROC IML, then the next best solution is to use PROC PRINCOMP to compute the eigenvectors and eigenvalues. Because the eigenvectors are ordered by the magnitude of the eigenvalues, you can use the "Eigenvalues" table to figure out how many eigenvalues/eigenvectors you want to keep.

 

The part of the code that you are asking about is for handling the eigenvectors that are associated with negative eigenvalues. As I've said, negative eigenvalues will never occur for a valid correlation matrix. If you are getting an invalid correlation matrix from PROC CORR, it might be because you are not using the NOMISS option, as explained in the article "When is a correlation matrix not a correlation matrix."  Add the NOMISS option and the correlation matrix will always be positive semidefinite.

 

 

Contributor
Posts: 62

Re: Matrix in proc fcmp

I' ll try with proc corr and this option and check. Thank you.

 

Regards

Contributor
Posts: 62

Re: Matrix in proc fcmp

Using proc corr nomiss option and after that proc princomp gives different eigenvalues or eigenvectors in the case where

one would get a non semi defite positive correl matrix with nomiss.

SAS Super FREQ
Posts: 3,559

Re: Matrix in proc fcmp

Different from what?  If you'd like assistance, please post the code you are running, the results you are seeing, and specify what part of the results seem unusual to you.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 217 views
  • 0 likes
  • 2 in conversation