edugca / xlMATRIX

Handle vectors and matrices in Excel with Lambda functions. Conduct statistical and econometric analysis with ease. No VBA!
26 stars 1 forks source link

Eigenvectors and Eigenvalues #1

Open davidghopez opened 10 months ago

davidghopez commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

Hi Eduardo,

Our emails crossed in flight - thanks so much - you are a star, :-)

Regards David

On Thu, 26 Oct 2023 at 00:13, Eduardo G. C. Amaral @.***> wrote:

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) )

— Reply to this email directly, view it on GitHub https://github.com/edugca/xlMATRIX/issues/1#issuecomment-1780182399, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDQ6CS33FFGSUAKFSC4IXVLYBGMI7AVCNFSM6AAAAAA6QDMMFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBQGE4DEMZZHE . You are receiving this because you authored the thread.Message ID: @.***>

davidghopez commented 10 months ago

Hi Eduardo,

To prove the Lambdas you sent work , I can't get the reverse operation to work, so starting with Σ and deriving Q and Λ.

I want to prove they work correctly by recreating the Σ matrix.

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

The Eigenvalue Lamda works because I checked that the Eigenvalues summed to the same as the diagonal sum of the original covariance matrix, but I don't think the Eigenvectors Lamda works, or am I missing something ?

Regards

David

---------- Forwarded message --------- From: David Hope @.> Date: Thu, 26 Oct 2023 at 00:27 Subject: Re: [edugca/xlMATRIX] Eigenvectors and Eigenvalues (Issue #1) To: edugca/xlMATRIX < @.>

Hi Eduardo,

Our emails crossed in flight - thanks so much - you are a star, :-)

Regards David

On Thu, 26 Oct 2023 at 00:13, Eduardo G. C. Amaral @.***> wrote:

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

LAMBDA(matrix,[numIter],[order], 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

LAMBDA(matrix,[numIter],[order],

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) )

Reply to this email directly, view it on GitHub https://github.com/edugca/xlMATRIX/issues/1#issuecomment-1780182399, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDQ6CS33FFGSUAKFSC4IXVLYBGMI7AVCNFSM6AAAAAA6QDMMFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBQGE4DEMZZHE . You are receiving this because you authored the thread.Message ID: @.***>

edugca commented 10 months ago

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 commented 10 months ago

Hi, It's late over here, so on my way to bed, so will try this out later this week.

Are these Lamda's the updated ones you sent, Or Are these the original Lambda's that are on GitHub

Regards David

On Wed, 1 Nov 2023 at 00:53, Eduardo G. C. Amaral @.***> wrote:

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)) )

— Reply to this email directly, view it on GitHub https://github.com/edugca/xlMATRIX/issues/1#issuecomment-1788234079, or unsubscribe https://github.com/notifications/unsubscribe-auth/BDQ6CS6UHZ3E52Q4PT46ERDYCGMP5AVCNFSM6AAAAAA6QDMMFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBYGIZTIMBXHE . You are receiving this because you authored the thread.Message ID: @.***>

edugca commented 10 months ago

The updated ones.

davidghopez commented 10 months ago

Thanks so much 👍Sent from my iPhoneOn 1 Nov 2023, at 01:12, Eduardo G. C. Amaral @.***> wrote: The updated ones.

Em ter., 31 de out. de 2023 às 22:10, davidghopez @.***>

escreveu:

Hi,

It's late over here, so on my way to bed, so will try this out later this

week.

Are these Lamda's the updated ones you sent,

Or

Are these the original Lambda's that are on GitHub

Regards

David

On Wed, 1 Nov 2023 at 00:53, Eduardo G. C. Amaral @.***>

wrote:

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)) )

Reply to this email directly, view it on GitHub

https://github.com/edugca/xlMATRIX/issues/1#issuecomment-1788234079,

or

unsubscribe

<

https://github.com/notifications/unsubscribe-auth/BDQ6CS6UHZ3E52Q4PT46ERDYCGMP5AVCNFSM6AAAAAA6QDMMFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBYGIZTIMBXHE>

.

You are receiving this because you authored the thread.Message ID:

@.***>

Reply to this email directly, view it on GitHub

https://github.com/edugca/xlMATRIX/issues/1#issuecomment-1788246841, or

unsubscribe

https://github.com/notifications/unsubscribe-auth/AG2QCHDF7L6S2HVDTOYODPTYCGOR5AVCNFSM6AAAAAA6QDMMFGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBYGI2DMOBUGE

.

You are receiving this because you commented.Message ID:

@.***>

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>