# Lasso and Ridge regularization

**URL:** <https://discourse.julialang.org/t/lasso-and-ridge-regularization/101851>\
**Category:** Machine Learning\
**Tags:** sciml\
**Created:** [July 20, 2023, 7:50pm UTC](https://discourse.julialang.org/t/lasso-and-ridge-regularization/101851 "2023-07-20T19:50:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_J](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t_j/32/51019_2.png) [@T\_J](https://discourse.julialang.org/u/T_J)\
**Post date:** [July 20, 2023, 7:50pm UTC](https://discourse.julialang.org/t/lasso-and-ridge-regularization/101851/1 "2023-07-20T19:50:14Z")

</div>

Hello to all,

How can I include Lasso and Ridge Regularization in SciML?

I am using the Lotka-Volterra case as an example.

Original loss

```julia
function loss(θ)
    X̂ = predict(θ)
    mean(abs2, Xₙ .- X̂)
end

```

Is this correct?

Ridge

```julia
function loss(θ)
    X̂ = predict(θ)
    mean(abs2, Xₙ .- X̂) + lambda* sum(θ.*θ)
end

```

Lasso

```julia
function loss(θ)
    X̂ = predict(θ)
    mean(abs2, Xₙ .- X̂) + lambda* abs.(θ)
end

```

Where lambda is an empirical penalty factor.

Best Regards

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [July 20, 2023, 8:06pm UTC](https://discourse.julialang.org/t/lasso-and-ridge-regularization/101851/2 "2023-07-20T20:06:54Z")

</div>

I don’t know much about SciML, but this [notebook](https://github.com/PaulSoderlind/FinancialEconometrics/blob/master/Ch07_OLS_Lasso.ipynb) does it from scratch, using the optimization package OSQP.
