# Package for fast OLS using StaticArrays

**URL:** <https://discourse.julialang.org/t/package-for-fast-ols-using-staticarrays/96943>\
**Category:** Optimization (Mathematical)\
**Created:** [April 1, 2023, 11:20am UTC](https://discourse.julialang.org/t/package-for-fast-ols-using-staticarrays/96943 "2023-04-01T11:20:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [April 1, 2023, 11:20am UTC](https://discourse.julialang.org/t/package-for-fast-ols-using-staticarrays/96943/1 "2023-04-01T11:20:39Z")

</div>

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:

```julia
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…
