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

Least Square Approximation and Logarithm evaluation #639

Merged
merged 4 commits into from
Dec 31, 2023

Conversation

mihaubuhai
Copy link
Contributor

@mihaubuhai mihaubuhai commented Dec 29, 2023

Numerical Analysis Algorithms

Description

Current pull request adds 2 new algorithms.

Least Square Approximation

This algorithm is a response to the problem risen by analysis of some data, projected in grafing the data to points (in 2 dimension)
and figuring out a pattern that the data follow or fiding a median for those data.

Least Square Approximation creates a polynomial of any degree with the given set of points, starting from the following idea:
    --> Fiding something in common for those points means, mathematically, to evaluate a distance; since we want to observe, relative to a polynomial, the behaviour of the data, we define a function ( called E ) that describes this idea:

                                     E(a0, a1, ... , aD) = $\sum_{i=0..n}$ (yi - P(xi))2, where:

      P(..) -> polynomial we want to find; D -> degree of polynomial
      n -> number of points (data)
      yi -> ordinate of some data
      xi -> abscissa of some data
      a0, a1, ... , aD -> coefficients of terms of the polynomial

Now, for the idea to really produce a result, the function E shall be as small as possible, fact supported by smaller the sum, closer the values added, meaning yi and P(xi) are very close to each other. This now becomes a Calculus 2 problem, more specifically minimising the function E. This is possible by taking the partial derivative of the function "E" with respect to each coefficient ai and evaluate it to zero, as follows:

                                      $\frac{∂}{∂a_k}$ E(a0, a1, ..., aD) = 0 , for k = 0 .. D

Choosing a degree for P and replacing it's form into the formula for E above, we will have a system of equations, generated by the derivatives above, looking something like:

                                      $\sum_{i=0..D}$ (ai * $\sum_{j=1..n}$ xj j+k) = $\sum_{i=0..n}$ yi* xi k , for k = 0 .. D

Decomposing the bigger sum from the left side and exposing all the D + 1 equations, we can observe that the left side can be rewritten as follows:
$\to$ A matrix A which holds, as elements, $\sum_{j=1..n}$ xj j+k ( For each line, the elements will be the sums for the respective derivative (k representing it's index) )
$\to$ A column vector x which holds the coefficients $a_0, a_1, ..., a_D$, in this order

Also, the right side can be expressed as a column vector b, which holds the sums $\sum_{i=0..n}$ yi* xi k

Finally, the whole algorithm is based on solving the equation: A * x = b for the column vector x; in this implementation, the method used is QR decomposition and ultimately solving it.

Logarithm evaluation

This function calculates the logbasex, for any base > 0, x > 0

For the case base = e and x between 0 and 1, the function uses the MacLaurin Series for ln: ln(x) = $\sum_{n>=1} {(-1)^n * \frac{x^n}{n}}$

@codecov-commenter
Copy link

codecov-commenter commented Dec 29, 2023

Codecov Report

Attention: 13 lines in your changes are missing coverage. Please review.

Comparison is base (f4e696e) 94.55% compared to head (94835bf) 94.52%.
Report is 1 commits behind head on master.

Files Patch % Lines
src/math/logarithm.rs 73.91% 12 Missing ⚠️
src/math/least_square_approx.rs 98.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #639      +/-   ##
==========================================
- Coverage   94.55%   94.52%   -0.03%     
==========================================
  Files         280      282       +2     
  Lines       22482    22606     +124     
==========================================
+ Hits        21258    21369     +111     
- Misses       1224     1237      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak merged commit c522e60 into TheAlgorithms:master Dec 31, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants