Difference between revisions of "Recipe/Theory/Non-negative least squares"
Line 28: | Line 28: | ||
With forced we mean that the first ingredients is more present than the second ingredient, the second more than the third, etc. This implies that β<sub>i</sub> > β<sub>i+1</sub>. Or β<sub>i</sub>-β<sub>i+1</sub> > 0. By rewriting formula 1, we get another NNLS problem: | With forced we mean that the first ingredients is more present than the second ingredient, the second more than the third, etc. This implies that β<sub>i</sub> > β<sub>i+1</sub>. Or β<sub>i</sub>-β<sub>i+1</sub> > 0. By rewriting formula 1, we get another NNLS problem: | ||
− | (2a) p<sub>j</sub> = β<sub>1</sub>x<sub>1j</sub> - β<sub>2</sub>x<sub>1j</sub> + β<sub>2</sub>x<sub>1j</sub>+ .... + β<sub>N</sub>x<sub>Nj</sub> | + | (2a) p<sub>j</sub> = β<sub>1</sub>x<sub>1j</sub> - β<sub>2</sub>x<sub>1j</sub> + β<sub>2</sub>x<sub>1j</sub> + .... + β<sub>N</sub>x<sub>Nj</sub> |
− | (2b) p<sub>j</sub> = (β<sub>1</sub> - β<sub>2</sub>)x<sub>1j</sub> + β<sub>2</sub>x<sub>1j</sub>+ .... + β<sub>N</sub>x<sub>Nj</sub> | + | (2b) p<sub>j</sub> = (β<sub>1</sub> - β<sub>2</sub>)x<sub>1j</sub> + β<sub>2</sub>x<sub>1j</sub> + .... + β<sub>N</sub>x<sub>Nj</sub> |
+ | |||
+ | (2c) p<sub>j</sub> = (β<sub>1</sub> - β<sub>2</sub>)x<sub>1j</sub> + β<sub>2</sub>x<sub>1j</sub> + β<sub>2</sub>x<sub>2j</sub> + .... + β<sub>N</sub>x<sub>Nj</sub> | ||
+ | |||
+ | (2d) p<sub>j</sub> = (β<sub>1</sub> - β<sub>2</sub>)x<sub>1j</sub> + β<sub>2</sub> (x<sub>1j</sub> + x<sub>2j</sub>) + .... + β<sub>N</sub>x<sub>Nj</sub> | ||
− | |||
== Comparing approaches == | == Comparing approaches == |
Revision as of 13:24, 21 February 2025
Theoretical solution
The best mathematical solution to the recipe estimation problem is offer by the non-negative least squares approach. There are multiple algorithms to solve approach. In this approach the general equation is:
(1) pj = β0 + β1x1j + .... + βNxNj
In this is
- pj - the nutritional value of the product for nutrient j;
- β0 - the base value without any ingredients;
- β1 - the fraction of ingredient 1 in the recipe;
- x1j - the nutritional value of ingredient 1 for nutrient j;
- N - the total number of ingredients;
- M - the total number of nutrients used;
It is clear that β0 must be zero and should be set as such in some solvers.
The values for βi will always be larger than 0. We are yet to see ingredients that will subtract nutrients.
Thus a solver that forces the βi larger than 0 is required
Non-Negative Least Squares
Solvers that only support solutions with βi > 0 are called Non-Negative Least Squares.
There seem to be many implementations for this. For instance nnls in the Rstudio package. Or nnls in the SciPy-package.
Unfortunately this approach does not always give the results we are looking for. The results favour ingredients that dominate the nutrients, like oil, sugar or salt. In most case the other ingredients are not needed and set to zero. In fact most products can be explained through these dominating ingredients. So another solution is needed that forces the other ingredients to participate.
Non-Negative Least Squares with forced order
With forced we mean that the first ingredients is more present than the second ingredient, the second more than the third, etc. This implies that βi > βi+1. Or βi-βi+1 > 0. By rewriting formula 1, we get another NNLS problem:
(2a) pj = β1x1j - β2x1j + β2x1j + .... + βNxNj
(2b) pj = (β1 - β2)x1j + β2x1j + .... + βNxNj
(2c) pj = (β1 - β2)x1j + β2x1j + β2x2j + .... + βNxNj
(2d) pj = (β1 - β2)x1j + β2 (x1j + x2j) + .... + βNxNj