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[[1]]<Length@v.

  • $\begingroup$ 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]] $\endgroup$
    – J. M.'s torpor
    Sep 21 '20 at 8:42
  • $\begingroup$ something like Array[x, 4] could be used as four variables..OMG.... $\endgroup$
    – Harry
    Sep 21 '20 at 9:02

Consider this picture: enter image description here

What would the best approximation to "vec" be, when the condition: "y component of vec is zero" is imposed? The best approx. under this condition is setting the y component to zero.

Back to your question. You simply need to set the negative component to zero.

  • $\begingroup$ 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? $\endgroup$
    – Harry
    Sep 21 '20 at 8:25
  • $\begingroup$ The picture is illuminating....but is somewhere a proof make sure "set the negative component to zero" gives the best approx? seems it makes a vector closest to the solution with no constrain....emmm.... $\endgroup$
    – Harry
    Sep 21 '20 at 8:32
  • $\begingroup$ Think about it this way: you are searching a vector vec1 so that the distance vec1-vec is minimal. In the above example this vector is the projection of vec on the x axis.In the general case, it is the projection to the subspace with y=0 $\endgroup$ Sep 21 '20 at 8:53
  • $\begingroup$ 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}}*) $\endgroup$ Sep 21 '20 at 9:12
  • $\begingroup$ You are partially right. The main problem is that we have 4 unknowns and only 2 equations. Therefore, there are an infinity of solutions. the simplest one would be: {0,1,0,0}. Now, what I did is to approximate the vector: {0.0381679, 0.198473, -0.00763359, 0.396947} as well as possible with positive coefficients. Now, the question arise why neither LinearModelFit nor NMinimize gives a correct result. I can only guess that numerical errors with real numbers are the reason. $\endgroup$ Sep 21 '20 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.