Hi all,
I was just wondering if there is a package commonly used for performing OLS when the number of explanatory variables is known (and small), that leverages the benefits of StaticArrays.jl?
For example:
using StaticArrays, BenchmarkTools
function ols1(y::Vector{Float64}, x1::Vector{Float64})
x1sum = sum(x1)
xpxinv = inv(SMatrix{2,2}([Float64(length(x1)) x1sum ; x1sum sum(abs2, x1)]))
b = xpxinv * [sum(y), dot(x1,y)]
return b
end
y = randn(100);
x1 = randn(100);
xmat = hcat(ones(Float64, length(x1)), x1);
@btime ols1($y, $x1)
@btime ($xmat \ $y)
has ols1
running over 10x faster than the usual left-division operator. That’s a pretty drastic improvement, and I imagine that similar gains can be made when we only need to invert a 3x3 or 4x4 matrix. Are there any obvious problems with doing things this way, and if not, has anyone already built a package around this approach?
Cheers,
Colin
EDIT: As a bonus, ols1
also computes the inverse explicitly, which means it could also return the estimated covariance matrix of b
with only a few trivial additional computations…