# Three million linear regressions

**URL:** https://discourse.julialang.org/t/three-million-linear-regressions/81439
**Category:** Performance
**Created:** [May 21, 2022, 10:03pm UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439 "2022-05-21T22:03:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Baba\_Yara\_Fahiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baba_yara_fahiz/32/36478_2.png) [@Baba\_Yara\_Fahiz](https://discourse.julialang.org/u/Baba_Yara_Fahiz)
#### Post date: [May 21, 2022, 10:03pm UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439/1 "2022-05-21T22:03:25Z")

</div>

I am running three million regressions of the form below and I feel like I am leaving a lot of performance on the table.

Can I get some tips to help speed things up?

```julia
function test_1(data_0)
EignVectors = rand(100, 12)
Factors = zeros(size(data_0, 1), 12)

for i in 1:size(Factors, 1)
    Indexer = Not(ismissing.(data_0[i, :])) # real data contains some missing values
  Factors[i, :] = EignVectors[Indexer, :]\data_0[i, Indexer] # all I need are the slope coefficients
end
return Factors
end 
data_1 = rand(3000000, 100)
@time test_1( data_1 );

```

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [May 21, 2022, 10:46pm UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439/2 "2022-05-21T22:46:56Z")

</div>

`@views` is your friend.

---

<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: [May 21, 2022, 10:49pm UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439/3 "2022-05-21T22:49:46Z")

</div>

Did you [read the performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/?)

Try [using views](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-views) to avoid making copies with slices. Replace the `Not` with `(!).` since you are allocating an array anyway with `isimissing.`, and perhaps pre-allocate the `Indexer` array. Try changing the order of (transposing) your `data` array so that you [access the data in memory order](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major).

Unfortunately, having irregular missing values makes things a _lot_ worse — if it weren’t for that, you could (after transposing the data array) replace the entire loop with a single `Factors = EignVectors \ data_0` call, which would probably be much faster. Maybe consider sorting your data into chunks that have identical `ismissing` patterns, so that you can do the `\` in chunks.

---

<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: [May 21, 2022, 10:59pm UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439/4 "2022-05-21T22:59:33Z")

</div>

> [@stevengj](#):
>
> you could (after transposing the data array) replace the entire loop with a single `Factors = EignVectors \ data_0` call, which would probably be much faster.

In particular, this is about 50x faster on my machine.

---

<div class="post-metadata">

### Author: ![Baba\_Yara\_Fahiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baba_yara_fahiz/32/36478_2.png) [@Baba\_Yara\_Fahiz](https://discourse.julialang.org/u/Baba_Yara_Fahiz)
#### Post date: [May 23, 2022, 12:52am UTC](https://discourse.julialang.org/t/three-million-linear-regressions/81439/5 "2022-05-23T00:52:39Z")

</div>

Thanks for all the comments.  
I managed to implement the suggestions and saw a significant increase in speed.
