Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eigenvectors and Eigenvalues #1

Open
davidghopez opened this issue Oct 25, 2023 · 7 comments
Open

Eigenvectors and Eigenvalues #1

davidghopez opened this issue Oct 25, 2023 · 7 comments

Comments

@davidghopez
Copy link

First comment - this resource is great.

The order of eigenvalues in relation to their corresponding eigenvectors is important. Each eigenvalue corresponds to a specific eigenvector, and the order is such that the first eigenvalue corresponds to the first eigenvector, the second eigenvalue corresponds to the second eigenvector, and so on. This order is crucial for the meaningful interpretation and application of eigenvalue-eigenvector pairs.

To get the covariance matrix from eigenvectors and eigenvalues, we can use the following matrix notation:

Σ = QΛQ^T
where:
Σ is the covariance matrix
Q is the matrix of eigenvectors
Λ is the diagonal matrix of eigenvalues
Q^T is the transpose of the matrix of eigenvectors

By ordering the eigenvalues, this "check" can not be undertaken

@edugca
Copy link
Owner

edugca commented Oct 25, 2023

Hi, David! Thanks for the feedback. Following your observation, I made some changes in the EIGENVALUES and EIGENVECTORS functions. Now, the default value of [order] is 0, which will present results without sorting them. These changes will be available in the next release of xlMATRIX.

In the meanwhile, next are the new codes as displayed in the "Advanced formula environment":

EIGENVALUES

=LET(
nCols, COLUMNS(matrix),
numIter, IF(OR(ISOMITTED(numIter), ISBLANK(numIter)), 20, numIter),
order, IF(ISOMITTED(order), 0, order),
candIter, REDUCE(
FACTORIZE_QR(matrix),
SEQUENCE(numIter),
LAMBDA(accum, x,
FACTORIZE_QR(
MMULT(
TAKE(accum, , IF(ISEVEN(x), -1, 1) * nCols),
TAKE(accum, , -IF(ISEVEN(x), -1, 1) * nCols)
)
)
)
),
vals, DIAG(TAKE(candIter, , -nCols)),
SWITCH(
order,
0, vals,
1, SORT(vals, , 1),
-1, SORT(vals, , -1),
2, SORTBY(vals, MATCH(ABS(vals), SORT(ABS(vals)), 0)),
-2, SORTBY(vals, MATCH(ABS(vals), SORT(ABS(vals), , -1), 0))
)
)

EIGENVECTORS

=LET(
norm, ROWS(matrix) * MAX(ABS(matrix)),
numIter, IF(OR(ISOMITTED(numIter), ISBLANK(numIter)), 20, numIter),
normalized, IF(ISOMITTED(normalized), FALSE, normalized),
order, IF(ISOMITTED(order), 0, order),
eigVals, EIGENVALUES(matrix, numIter, order),
n, ROWS(matrix),
eigVecs, MAKEARRAY(
n,
n,
LAMBDA(row, col,
LET(
eigMatrix, matrix - INDEX(eigVals, col) * MUNIT(ROWS(matrix)),
eigVector, VSTACK(
1,
MMULT(MINVERSE(DROP(eigMatrix, 1, 1)), -DROP(INDEX(eigMatrix, 0, 1), 1))
),
INDEX(eigVector, row)
)
)
),
normEigVecs, MAKEARRAY(
n,
n,
LAMBDA(row, col, INDEX(eigVecs, row, col) / MNORM(INDEX(eigVecs, 0, col), 2))
),
IF(normalized, normEigVecs, eigVecs)
)

@davidghopez
Copy link
Author

davidghopez commented Oct 25, 2023 via email

@davidghopez
Copy link
Author

davidghopez commented Oct 31, 2023 via email

@edugca
Copy link
Owner

edugca commented Nov 1, 2023

Hi David,

I tested the decomposition here and it actually worked as expected. One thing you may be missing is that EIGENVECTORS must be normalized for that decomposition to work. In the EIGENVECTORS function, the parameter NORMALIZED must be equal to TRUE. Default is FALSE.

Try this example:

MATRIX in F10:H12:

2 -1 0
-1 2 -1
0 -1 2

=MMULT( MMULT(EIGENVECTORS(F10:H12, , TRUE) , DIAG(EIGENVALUES(F10:H12)) ) , TRANSPOSE(EIGENVECTORS(F10:H12, , TRUE)) )

@davidghopez
Copy link
Author

davidghopez commented Nov 1, 2023 via email

@edugca
Copy link
Owner

edugca commented Nov 1, 2023 via email

@davidghopez
Copy link
Author

davidghopez commented Nov 1, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants