-
Notifications
You must be signed in to change notification settings - Fork 609
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
feat(scripts): polynomial and rational approximations #3620
Conversation
scripts/approximations/polynomial.py
Outdated
def solve(x: np.ndarray, coeffs: np.ndarray) -> np.ndarray: | ||
""" Solves the polynomial. Given a list of x coordinates and a list of coefficients, returns a list of | ||
y coordinates, one for each x coordinate. The coefficients must be in ascending order. | ||
""" | ||
o = len(coeffs) | ||
y = 0 | ||
for i in range(o): | ||
y += coeffs[i]*x**i | ||
return y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is too slow, lmk, we can easily speed this up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine and not noticeable. Out of curiosity - what would be your suggestion to speed this up? Horner's method or some kind of factorization trick?
The graph for errors is still TBD |
In the latest commit 3cf279e, I added the ability to print the max error for each approximation. The result looks something like this:
Please let me know if this is sufficient with regards to benchmarking errors |
Made the suggested change to perform apples to apples comparison by having: polynomial_num_terms = numerator_terms + denominator_terms Now, working on better error plots across various combinations of terms to determine the optimal one |
…into roman/improved-pow
… tests at max spot price (#3676) * feat(osmomath): BigDec power function with integer exponent, overflow tests at max spot price * euler's number * nolint * more tests * changelog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
LGTM, just need an issue to measure truncating the solved coefficients before plotting/computing error. Happy to merge now and track that in an issue |
This should already be achieved here: osmosis/scripts/approximations/main.py Lines 179 to 181 in e6784a3
The 3rd plot in the PR description is made from that. I'm in the process of writing Go code right now. In that PR, I will also hard code the coefficients I'm using in this script and replot the error graph |
* feat(scripts): polynomial and rational approximations * typos * md lint * osmomath things * rename solve -> evaluate * terminology corrections for actual function * correct terminology for rational matrix * Revert "osmomath things" This reverts commit 90f137e. * compute and print max error * increase num_points_plot * polynomial_num_terms = numerator_terms + denominator_terms * add ability to approximate errors * clean up plot errors * clean up * clean up * clean up * clean up * refactores equispaced with higher precision * fix chebyshev poly * refactor chebyshev rational * clean up * updates * updates * updates * plot errors on range * isolate exponent approximation choice with expected precision * add comments in main.py * README updates * update readme and requirements * requirements.txt and gitignore updates * clean ups and docs * readme * update test test_construct_matrix_3_3 * feat(osmomath): BigDec power function with integer exponent, overflow tests at max spot price (#3676) * feat(osmomath): BigDec power function with integer exponent, overflow tests at max spot price * euler's number * nolint * more tests * changelog * feat(osmomath): mutative `PowerIntegerMut` function on `osmomath.BigDec` (#3680) (cherry picked from commit 5f55d1b) # Conflicts: # .gitignore # CHANGELOG.md
#3685) Co-authored-by: Roman <roman@osmosis.team>
Progress towards: #3013
Math Operations Approximations
Context
This is a set of scripts to approximate a mathematical operation using polynomial
and rational approximations.
The
main
script is in its respective function ofmain.py
. It does the following:Computes polynomial and rational approximations of a given function (e^x by default),
returning the coefficients.
Computes (x,y) coordinates for every kind of approximation given the same x coordinates.
Plots the results for rough comparison.
Plots the results for rough comparison.
Computes the max error for every approximation given the same x coordinates.
Computes and plots max errors for every approximation with a varying number of parameters.
In other words, this script runs various approximation methods, plots their results and deltas
from actual function values. It can be configured to print the maximum error.
The exact behavior is controlled by the global variables at the top of
main.py
.The following are the resources used to create this:
In
main.py
, there is also anexponent_approximation_choice
script.This is a shorter and simpler version of
main
that isolates the 13-parameterChebyshev Rational approximation of e^x. We are planning to use it in production.
Therefore, we need to perform coefficient truncations to 36 decimal points
(the max
osmomath
supported precision). This truncation is appliedto
exponent_approximation_choice
but notmain
.Configuration
Several parameters can be changed on the needs basis at the
top of
main.py
.Some of the parameters include the function we are approximating, the [x_start, x_end] range of
the approximation, and the number of terms to be used. For the full parameter list, see
main.py
.Usage
Assuming that you are in the root of the repository and have Sympy installed:
Example Output
Plotting Functions
This script plots the results to sanity check that they look right.
On the screenshot below, "50000 terms Equispaced poly" aims to be the most accurate representation of the function$$e^x$$
Visually, the results are as expected where "Chebyshev Rational" is plotted as the most accurate, followed by "Chebyshev Poly" and, lastly, "Equispaced Poly".
Plotting Approximation Errors For Each Approximation
Plotting Chosen Approximation Errors
13 parameter Chebyshev Rational errors with coefficient truncations done to match
osmomath.BigDec
precision of 36: