I'm having trouble getting the array formula below to return the values I'm expecting. It's an implementation of the normal equation for solving linear regression problems using linear algebra.
The expected output is an array with the same number of elements as there are columns in X (the features), with different coefficients for the slope associated with each feature. This works within the VBA editor - I can see the correct values are stored in the array theta() when debugging - but when I do a Ctrl-Shift-Enter it just returns an array repeating the number stored in theta(1,1).
What am I missing?
The expected output is an array with the same number of elements as there are columns in X (the features), with different coefficients for the slope associated with each feature. This works within the VBA editor - I can see the correct values are stored in the array theta() when debugging - but when I do a Ctrl-Shift-Enter it just returns an array repeating the number stored in theta(1,1).
What am I missing?
Code:
Public Function NormalEq(X As Range, y As Range) As Variant()
' theta() = pinv(X' * X) * X' * y
Dim theta() As Variant
Dim XT As Variant
Dim thetaSize As Integer
thetaSize = Range("X").Columns.Count
ReDim theta(1 To thetaSize)
XT = WorksheetFunction.Transpose(X)
theta = WorksheetFunction.MMult(WorksheetFunction.MMult(WorksheetFunction.MInverse(WorksheetFunction.MMult(XT, X)), XT), y)
NormalEq = theta()
End Function