# How to force LinearModelFit to return Non-negative coefficients?

LinearModelFit[{m,v}] will return a coefficients list $$\beta$$ from the design matrix $$m$$ and response vector $$v$$, where $$m.\beta$$ is fitted to $$v$$. However, the parameters in $$\beta$$ could be negative. For example,

LinearModelFit[{{{1, 2, 3, 4}, {2, 3, 7, 6}}, {2, 3}}]["BestFitParameters"]


will return {0.0381679, 0.198473, -0.00763359, 0.396947}, where the third parameter is negative.

How could I force LinearModelFit[{m,v}] to fit with only positive parameters? Is there an Option I can set constrain on coefficients?

I use LinearModelFit[{m,v}] because the length of $$m$$ (the number of variable) could vary from case to case.

Seems I raise a bad example. In reality the number of unknowns is less than the number of equations, that is Length@m[]<Length@v.

• An easy yet inefficient route: NonlinearModelFit[Append @@@ Transpose[{{{1, 2, 3, 4}, {2, 3, 7, 6}}, {2, 3}}], {Array[c, 4].Array[x, 4], And @@ Thread[Array[c, 4] > 0]}, Array[c, 4], Array[x, 4]] Sep 21 '20 at 8:42
• something like Array[x, 4] could be used as four variables..OMG.... Sep 21 '20 at 9:02

Consider this picture: • This is genius....wait...suppose the formula is $C_x x+C_y y$, if now $C_y$ is setted to 0 from negative value, shouldn't $C_x$ decrease a bit to balance (given xs, ys are all positive) and achieve better fitting? Sep 21 '20 at 8:25
• NMinimize evaluates a much better solution than the projective one: NMinimize[{#.# &[{{1, 2, 3, 4}, {2, 3, 7, 6}}.{b1, b2, b3, b4} - {2, 3}], b1 > 0, b2 > 0, b3 > 0, b4 > 0}, {b1, b2, b3, b4}] (*{3.35725*10^-9, {b1 -> 0.000104177, b2 -> 0.502257, b3 -> 0.0000209469, b4 -> 0.248818}}*) Sep 21 '20 at 9:12