Difference between revisions of "Recipe/Theory/Non-negative least squares"
Line 66: | Line 66: | ||
| Forced NNLS || 0.394 || 0.175 || 0.175 || 0.099 || 0.099 || 0.076 || 0.017 || 0.001 || 0 | | Forced NNLS || 0.394 || 0.175 || 0.175 || 0.099 || 0.099 || 0.076 || 0.017 || 0.001 || 0 | ||
|- | |- | ||
− | | Forced NNLS || Voorbeeld || Voorbeeld | + | | Forced NNLS left out || Voorbeeld || Voorbeeld |
|} | |} |
Revision as of 14:38, 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;
Multiple equations
Formula (1) can often be solved directly. For instance for 7 ingredients and 7 nutrients, it will become 7 equations with 7 unknown. If there are less than 7 ingredients, there will be multiple solutions. If there are more than 7 ingredients, we should make a fit, either a least squares fit or a maximum likelihood fit. (or is it the other way around?)
There are however some constraints that could be applied:
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) - β3(x1j + x2j) + β3(x1j + x2j) + .... + βNxNj
(2e) pj = (β1 - β2)x1j + (β2 - β3)(x1j + x2j) + β3(x1j + x2j) + .... + βNxNj
(2f) etc.
Formula 2f can be solved using a NNLS-approach, after which the original βi's can be calculated. Using this approach the results are very (too?) good.
This approach forces the ingredients which were first not participating, to contribute a bit to the recipe and the nutritional values. Although an ingredient i now participates, it is only pushed up and will be the same as the next ingredient i+1. In NNLS terms these are called bound ingredients (variables). And thus the fractions found for these ingredients are only a lower limit to the ingredient.
Bound ingredients
Do these bound ingredients have an upper limit? And can we determine these? Can we leave out these bound ingredients?
Comparing approaches
The various approaches above have been tested with the Vegan Nutella.
Solver | Sugar | Palm oil | Hazelnuts | Chickpeas | Cocoa Powder | Rice Syrup | Soy Lecithins | Salt | Deviance |
---|---|---|---|---|---|---|---|---|---|
LS | Voorbeeld | Voorbeeld | |||||||
NNLS | Voorbeeld | Voorbeeld | |||||||
Forced NNLS | 0.394 | 0.175 | 0.175 | 0.099 | 0.099 | 0.076 | 0.017 | 0.001 | 0 |
Forced NNLS left out | Voorbeeld | Voorbeeld |