Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for monomial computation (local evaluation) #72

Merged
merged 20 commits into from
Nov 21, 2024
Merged

Conversation

moralapablo
Copy link
Member

This PR closes #71, providing an option in eval_poly() and nn2poly.predict() to compute the local evaluation of each monomial instead of the final polynomial, using an optional boolean parameter monomials. Defaults to FALSE.

The new functionality has been finally included by changing the inner behavior of eval_poly() to store all the monomials in a 3D array as suggested in #71, and then at the end the sum of all monomial values is performed if monomials=FALSEm, thus obtaining the final polynomial prediction. This may require more memory usage than before if there are too many polynomials or too many terms in them, but alternative implementations that were tested would require duplicating code or including too many if statements. This shouldn't be a problem in the usual applications, but if scaling becomes a problem we may need to revisit this implementation.

Corresponding tests have been added and the documentation has been updated. Now the nn2poly.predict() output has several conditions that change its format and therefore a "long" explanation appears in the docs, but it seems to be as compact as possible. Vignette 01 has been also updated to include an extra paragraph and code block showing how to obtain the monomials with a given polynomial.

Finally, a small discussion about nn2poly.output() might be needed: ignoring the case with several internal layers, the output is generally a matrix when monomials=FALSE and a 3D array when monomials=TRUE. However, when we have a single polynomial being evaluated, the output when monomials=FALSE is reduced to a vector, as the matrix would have a single column. In the case of monomials=TRUE and a single polynomial, I initially thought of reducing the dimension from a 3D array to a matrix because the last dimension would be 1 too. However, this lead to some problems as setting the output to be output[ , , 1] creates a matrix if both remaining dimensions are greater than 1, or a vector if one of them is 1, which creates problems when trying to interact with the output in a general manner. Therefore, in the monomials case I decided to leave it as a 3D array always. This leaves two options:

  • Leave everything as it is right now, with reduction from matrix to vector in the full polynomials case, but not from 3D array to matrix in the monomials case.
  • Make the case with monomials=FALSE be always a matrix and not make it a vector when there is only 1 polynomial. This was done like this to match other predict methods that provide a vector as output.

…otation in eval_poly (as it was changed sinc the original creation of eval_local)
…f rowProds to multipli the values of the variables selected by label. Also moved that part to an aux function.
… n_sample value to be provided in order to have a more general code that can receive both vectors and matrices.
…f monomials==TRUE and otherwise sum them to obtain the final poly prediction.
…d references to monomials=TRUE output being reduce to a matrix if n_poly = 1, as we will always keep the 3D array output when we consider monomials.
@moralapablo moralapablo added the enhancement New feature or request label Nov 20, 2024
@moralapablo moralapablo requested a review from Enchufa2 November 20, 2024 19:15
@moralapablo moralapablo self-assigned this Nov 20, 2024
Copy link
Member

@Enchufa2 Enchufa2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@moralapablo moralapablo merged commit ebf15db into master Nov 21, 2024
9 of 13 checks passed
@moralapablo moralapablo deleted the eval_local branch November 21, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for monomial evaluation
2 participants