Array formula UDF only returning first item from array

oCoCarbon

New Member
Joined
Oct 11, 2010
Messages
29
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?

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
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top