Polynomial Regression in Julia - GeeksforGeeks

01 Nov, 2020
In statistics, polynomial regression is a form of regression analysis in which the relationship between the independent variable x and the dependent variable y is modeled as an nth degree polynomial in x. Polynomial regression fits a nonlinear relationship between the value of x and the corresponding conditional mean of y, denoted E(y |x). Polynomial regression is one example of regression analysis using basis functions to model a functional relationship between two quantities.
Polynomial provides the best approximation of the relationship between the dependent and independent variables.
A broad range of functions can be fit under it.
Polynomial basically fits a wide range of curvature.
Polynomial Regression in Julia can be implemented by using Polynomials.jl package. Polynomials.jl is a Julia package that provides basic arithmetic, integration, differentiation, evaluation, and root finding over dense univariate polynomials.
To install the package, run
pkg> add Polynomials
Once the package installed, you can load the package using
using Polynomials
Polynomial: Standard basis polynomials, a(x) = a0 + a1 x + a2 x2 + ... + an xn, n ∈ N
ImmutablePolynomial: Standard basis polynomials backed by a Tuple type for faster evaluation of values
SparsePolynomial:⁠ Standard basis polynomial backed by a dictionary to hold sparse high-degree polynomials
LaurentPolynomial:⁠ Laurent polynomials, a(x) = am xm + ... + an xn m ≤ n, m,n ∈ Z backed by an offset array; for example, if m<0 and n>0, a(x) = am xm + ... + a−1 x−1 + a0 + a1 x + ... + an xn
ChebyshevT:⁠ Chebyshev polynomials of the first kind
Syntax:
julia> using Polynomials
Now, let’s see how can we use Polynomial.jl to achieve arithmetic, integration, differentiation, evaluation, and root finding over dense univariate polynomials.
Construct a polynomial from an array of its coefficients, lowest order first.
Construct a polynomial from its roots by entering the roots as the parameters of the function of function fromroots() in the form of an array
Evaluate the polynomial p at x by entering the coefficients as the parameters of function polynomial() in the form of an array
The usual arithmetic operators are overloaded to work on polynomials and combinations of polynomials and scalars.
The following arithmetic operators are supported on all primitive numeric types:
Integrate the polynomial p term by term, optionally adding a constant term k. The degree of the resulting polynomial is one higher than the degree of p (for a nonzero polynomial).
Differentiate the polynomial p term by term. The degree of the resulting polynomial is one lower than the degree of p.
Return the roots (zeros) of p, with multiplicity. The number of roots returned is equal to the degree of p. By design, this is not type-stable, the returned roots may be real or complex.
Fit a polynomial (of degree deg or less) to x and y using a least-squares approximation.
Julia
using Plots, Polynomialsxs = range(0, 10, length = 10)ys = @.exp(-xs)f = fit(xs, ys) # degree = length(xs) - 1f2 = fit(xs, ys, 2) # degree = 2  scatter(xs, ys, markerstrokewidth = 0, label = "Data")plot!(f, extrema(xs)..., label = "Fit")plot!(f2, extrema(xs)..., label = "Quadratic Fit")
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Get array dimensions and size of a dimension in Julia - size() Method
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Searching in Array for a given element in Julia
Reverse array elements in Julia - reverse(), reverse!() and reverseind() Methods
Exception handling in Julia
Get number of elements of array in Julia - length() Method
Find maximum element along with its index in Julia - findmax() Method
Working with Date and Time in Julia
Getting last element of an array in Julia - last() Method
Getting the maximum value from a list in Julia - max() Method