# Best way for linear regression problem on product features

**URL:** <https://discourse.julialang.org/t/best-way-for-linear-regression-problem-on-product-features/127786>\
**Category:** General Usage\
**Tags:** performance, regression, qr\
**Created:** [April 7, 2025, 7:52am UTC](https://discourse.julialang.org/t/best-way-for-linear-regression-problem-on-product-features/127786 "2025-04-07T07:52:46Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2025, 1:34pm UTC](https://discourse.julialang.org/t/best-way-for-linear-regression-problem-on-product-features/127786/8 "2025-04-07T13:34:17Z")

</div>

> [@JADekker](#):
>
> I can transform this into a least-squares problem manually by first creating a feature matrix W by taking all products of the columns in X and Z, but that seems a bit burdensome with many features.

For reference, here is an implementation of this strategy:

```julia
Wᵀ = reshape([conj(X[i,k] * Z[i,ℓ]) for ℓ in 1:m, k in 1:n, i in 1:N], m*n, N)
reshape(Wᵀ' \ Y, m, n)

```

I’ve verified that it gives the same answer (to 12 digits) as the iterative `lsqr` example above, which gives me some confidence that my code is correct.

The complexity of the direct OLS approach is O(N(mn)^2). In contrast, each iteration of the `lsqr` approach with implicit (matrix-free) operators is O(Nmn). So, if the iteration converges quickly enough, and mn is large enough, the latter should be faster

---

_[View the full topic](https://discourse.julialang.org/t/best-way-for-linear-regression-problem-on-product-features/127786)._
