# Inverse and backslash \\ division

**URL:** https://discourse.julialang.org/t/inverse-and-backslash-division/44680
**Category:** Statistics
**Tags:** question, optimization
**Created:** [August 10, 2020, 1:19pm UTC](https://discourse.julialang.org/t/inverse-and-backslash-division/44680 "2020-08-10T13:19:31Z")
**Posts on this page:** 1
**Showing post:** 4

<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: [August 10, 2020, 2:28pm UTC](https://discourse.julialang.org/t/inverse-and-backslash-division/44680/4 "2020-08-10T14:28:28Z")

</div>

> [@luboshanus](#):
>
> Thanks for an answer, but in my case it is more about the results, which, for data I have, lead to exploding results in OLS because of definiteness of `inv(X^T X)` matrix etc.

It sounds like you are solving a least-squares problem by the “normal-equations” approach, i.e. you are computing z = (X^T X)^{-1} X^T y to minimize the least-square error \Vert X z - y \Vert\_2. If you are getting “exploding” results, it is presumably because X^T X is badly conditioned (nearly singular), in which case you have to be careful about how you solve the problem — your answer is very sensitive to roundoff errors and other errors (e.g. noise in the data).

If X^T X is badly conditioned, then `inv(X'X) * (X'y)` could indeed give very different answers than `(X'X) \ (X'y)` because of differences in roundoff errors — but the correct interpretation is that _both_ approaches are giving you an inaccurate result.

A better approach would be to use `X \ y`, which is equivalent to `qr(X, Val(true)) \ y` — this uses a pivoted QR factorization to solve the least-squares problem, which is much more accurate than the normal-equations approach for badly conditioned X.

If you need even more control over regularization for a badly conditioned problem, you could use a pseudo-inverse (`pinv`) approach. See also my explanation in another thread: [Efficient way of doing linear regression - #33 by stevengj](https://discourse.julialang.org/t/efficient-way-of-doing-linear-regression/31232/33)

---

_[View the full topic](https://discourse.julialang.org/t/inverse-and-backslash-division/44680)._
