Polynomial regression: Difference between revisions

 
(18 intermediate revisions by 11 users not shown)
Line 15:
{{trans|Swift}}
 
<langsyntaxhighlight lang="11l">F average(arr)
R sum(arr) / Float(arr.len)
 
Line 47:
V x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
V y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
poly_regression(x, y)</langsyntaxhighlight>
 
{{out}}
Line 69:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
 
function Fit (X, Y : Real_Vector; N : Positive) return Real_Vector is
Line 80:
end loop;
return Solve (A * Transpose (A), A * Y);
end Fit;</langsyntaxhighlight>
The function Fit implements least squares approximation of a function defined in the points as specified by the arrays ''x''<sub>''i''</sub> and ''y''<sub>''i''</sub>. The basis &phi;<sub>''j''</sub> is ''x''<sup>''j''</sup>, ''j''=0,1,..,''N''. The implementation is straightforward. First the plane matrix A is created. A<sub>ji</sub>=&phi;<sub>''j''</sub>(''x''<sub>''i''</sub>). Then the linear problem AA<sup>''T''</sup>''c''=A''y'' is solved. The result ''c''<sub>''j''</sub> are the coefficients. Constraint_Error is propagated when dimensions of X and Y differ or else when the problem is ill-defined.
===Example===
<langsyntaxhighlight lang="ada">with Fit;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
 
Line 97:
Put (C (1), Aft => 3, Exp => 0);
Put (C (2), Aft => 3, Exp => 0);
end Fitting;</langsyntaxhighlight>
{{out}}
<pre>
Line 110:
 
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386 - ELLA has no FORMATted transput}} -->
<langsyntaxhighlight lang="algol68">MODE FIELD = REAL;
 
MODE
Line 223:
);
print polynomial(d)
END # fitting #</langsyntaxhighlight>
{{out}}
<pre>
Line 232:
=={{header|AutoHotkey}}==
{{trans|Lua}}
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>
regression(xa,ya){
n := xa.Count()
Line 273:
eval(a,b,c,x){
return a + (b + c*x) * x
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">xa := [0, 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10]
ya := [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
MsgBox % result := regression(xa, ya)
return</langsyntaxhighlight>
{{out}}
<pre>y = 3.000000x^2 + 2.000000x + 1.000000
Line 294:
9, 262 262.000000
10, 321 321.000000</pre>
 
=={{header|AWK}}==
{{trans|Lua}}
<syntaxhighlight lang="awk">
BEGIN{
i = 0;
xa[i] = 0; i++;
xa[i] = 1; i++;
xa[i] = 2; i++;
xa[i] = 3; i++;
xa[i] = 4; i++;
xa[i] = 5; i++;
xa[i] = 6; i++;
xa[i] = 7; i++;
xa[i] = 8; i++;
xa[i] = 9; i++;
xa[i] = 10; i++;
i = 0;
ya[i] = 1; i++;
ya[i] = 6; i++;
ya[i] = 17; i++;
ya[i] = 34; i++;
ya[i] = 57; i++;
ya[i] = 86; i++;
ya[i] =121; i++;
ya[i] =162; i++;
ya[i] =209; i++;
ya[i] =262; i++;
ya[i] =321; i++;
exit;
}
{
# (nothing to do)
}
END{
a = 0; b = 0; c = 0; # globals - will change by regression()
regression(xa,ya);
 
printf("y = %6.2f x^2 + %6.2f x + %6.2f\n",c,b,a);
printf("%-13s %-8s\n","Input","Approximation");
printf("%-6s %-6s %-8s\n","x","y","y^")
for (i=0;i<length(xa);i++) {
printf("%6.1f %6.1f %8.3f\n",xa[i],ya[i],eval(a,b,c,xa[i]));
}
}
 
function eval(a,b,c,x) {
return a+b*x+c*x*x;
}
# locals
function regression(x,y, n,xm,ym,x2m,x3m,x4m,xym,x2ym,sxx,sxy,sxx2,sx2x2,sx2y) {
n = 0
xm = 0.0;
ym = 0.0;
x2m = 0.0;
x3m = 0.0;
x4m = 0.0;
xym = 0.0;
x2ym = 0.0;
 
for (i in x) {
xm += x[i];
ym += y[i];
x2m += x[i] * x[i];
x3m += x[i] * x[i] * x[i];
x4m += x[i] * x[i] * x[i] * x[i];
xym += x[i] * y[i];
x2ym += x[i] * x[i] * y[i];
n++;
}
xm = xm / n;
ym = ym / n;
x2m = x2m / n;
x3m = x3m / n;
x4m = x4m / n;
xym = xym / n;
x2ym = x2ym / n;
 
sxx = x2m - xm * xm;
sxy = xym - xm * ym;
sxx2 = x3m - xm * x2m;
sx2x2 = x4m - x2m * x2m;
sx2y = x2ym - x2m * ym;
 
b = (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2);
c = (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2);
a = ym - b * xm - c * x2m;
}
</syntaxhighlight>
{{out}}
<pre>
y = 3.00 x^2 + 2.00 x + 1.00
Input Approximation
x y y^
0.0 1.0 1.000
1.0 6.0 6.000
2.0 17.0 17.000
3.0 34.0 34.000
4.0 57.0 57.000
5.0 86.0 86.000
6.0 121.0 121.000
7.0 162.0 162.000
8.0 209.0 209.000
9.0 262.0 262.000
10.0 321.0 321.000
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Line 299 ⟶ 406:
and fits an order-5 polynomial, so the test data for this task
is hardly challenging!
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"ARRAYLIB"
Max% = 10000
Line 347 ⟶ 454:
FOR term% = 5 TO 0 STEP -1
PRINT ;vector(term%) " * x^" STR$(term%)
NEXT</langsyntaxhighlight>
{{out}}
<pre>
Line 363 ⟶ 470:
 
'''Include''' file (to make the code reusable easily) named <tt>polifitgsl.h</tt>
<langsyntaxhighlight lang="c">#ifndef _POLIFITGSL_H
#define _POLIFITGSL_H
#include <gsl/gsl_multifit.h>
Line 370 ⟶ 477:
bool polynomialfit(int obs, int degree,
double *dx, double *dy, double *store); /* n, p */
#endif</langsyntaxhighlight>
'''Implementation''' (the examples [http://www.gnu.org/software/gsl/manual/html_node/Fitting-Examples.html here] helped alot to code this quickly):
<langsyntaxhighlight lang="c">#include "polifitgsl.h"
 
bool polynomialfit(int obs, int degree,
Line 412 ⟶ 519:
return true; /* we do not "analyse" the result (cov matrix mainly)
to know if the fit is "good" */
}</langsyntaxhighlight>
'''Testing''':
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#include "polifitgsl.h"
Line 434 ⟶ 541:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1.000000
Line 442 ⟶ 549:
=={{header|C sharp|C#}}==
{{libheader|Math.Net}}
<langsyntaxhighlight lang="csharp"> public static double[] Polyfit(double[] x, double[] y, int degree)
{
// Vandermonde matrix
Line 456 ⟶ 563:
var p = r.Inverse().Multiply(q.TransposeThisAndMultiply(yv));
return p.Column(0).ToArray();
}</langsyntaxhighlight>
Example:
<langsyntaxhighlight Clang="c sharp"> static void Main(string[] args)
{
const int degree = 2;
Line 469 ⟶ 576:
Console.WriteLine("{0} => {1} diff {2}", x[i], Polynomial.Evaluate(x[i], p), y[i] - Polynomial.Evaluate(x[i], p));
Console.ReadKey(true);
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <numeric>
Line 534 ⟶ 641:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>y = 1 + 2x + 3x^2
Line 554 ⟶ 661:
Uses the routine (lsqr A b) from [[Multiple regression]] and (mtp A) from [[Matrix transposition]].
 
<langsyntaxhighlight lang="lisp">;; Least square fit of a polynomial of order n the x-y-curve.
(defun polyfit (x y n)
(let* ((m (cadr (array-dimensions x)))
Line 562 ⟶ 669:
(setf (aref A i j)
(expt (aref x 0 i) j))))
(lsqr A (mtp y))))</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight lang="lisp">(let ((x (make-array '(1 11) :initial-contents '((0 1 2 3 4 5 6 7 8 9 10))))
(y (make-array '(1 11) :initial-contents '((1 6 17 34 57 86 121 162 209 262 321)))))
(polyfit x y 2))
 
#2A((0.9999999999999759d0) (2.000000000000005d0) (3.0d0))</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.range;
import std.stdio;
Line 620 ⟶ 727:
auto y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321];
polyRegression(x, y);
}</langsyntaxhighlight>
{{out}}
<pre>y = 1 + 2x + 3x^2
Line 636 ⟶ 743:
9 262 262.0
10 321 321.0</pre>
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight lang=easylang>
func eval a b c x .
return a + (b + c * x) * x
.
proc regression xa[] ya[] . .
n = len xa[]
for i = 1 to n
xm = xm + xa[i]
ym = ym + ya[i]
x2m = x2m + xa[i] * xa[i]
x3m = x3m + xa[i] * xa[i] * xa[i]
x4m = x4m + xa[i] * xa[i] * xa[i] * xa[i]
xym = xym + xa[i] * ya[i]
x2ym = x2ym + xa[i] * xa[i] * ya[i]
.
xm = xm / n
ym = ym / n
x2m = x2m / n
x3m = x3m / n
x4m = x4m / n
xym = xym / n
x2ym = x2ym / n
#
sxx = x2m - xm * xm
sxy = xym - xm * ym
sxx2 = x3m - xm * x2m
sx2x2 = x4m - x2m * x2m
sx2y = x2ym - x2m * ym
#
b = (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
c = (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
a = ym - b * xm - c * x2m
print "y = " & a & " + " & b & "x + " & c & "x^2"
numfmt 0 3
for i = 1 to n
print xa[i] & " " & ya[i] & " " & eval a b c xa[i]
.
.
xa[] = [ 0 1 2 3 4 5 6 7 8 9 10 ]
ya[] = [ 1 6 17 34 57 86 121 162 209 262 321 ]
regression xa[] ya[]
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
 
{{libheader|Calc}}
Simple solution by Emacs Lisp and built-in Emacs Calc.
<syntaxhighlight lang="lisp">(let ((x '(0 1 2 3 4 5 6 7 8 9 10))
 
(y '(1 6 17 34 57 86 121 162 209 262 321)))
<lang emacs-lisp>
(calc-eval "fit(a*x^2+b*x+c,[x],[a,b,c],[$1 $2])" nil (cons 'vec x) (cons 'vec y)))</syntaxhighlight>
(setq x '[0 1 2 3 4 5 6 7 8 9 10])
(setq y '[1 6 17 34 57 86 121 162 209 262 321])
(calc-eval
(format "fit(a*x^2+b*x+c,[x],[a,b,c],[%s %s])" x y))
</lang>
 
{{out}}
 
<pre>
"3. x^2 + 1.99999999996 x + 1.00000000006"
</pre>
 
=={{header|Fortran}}==
{{libheader|LAPACK}}
<langsyntaxhighlight lang="fortran">module fitting
contains
 
Line 718 ⟶ 865:
end function
end module</langsyntaxhighlight>
 
===Example===
<langsyntaxhighlight lang="fortran">program PolynomalFitting
use fitting
implicit none
Line 739 ⟶ 886:
write (*, '(F9.4)') a
 
end program</langsyntaxhighlight>
 
{{out}} (lower powers first, so this seems the opposite of the Python output):
Line 750 ⟶ 897:
=={{header|FreeBASIC}}==
General regressions for different polynomials, here it is for degree 2, (3 terms).
<langsyntaxhighlight FreeBASIClang="freebasic">#Include "crt.bi" 'for rounding only
 
Type vector
Line 937 ⟶ 1,084:
 
print show(ans())
sleep</langsyntaxhighlight>
{{out}}
<pre>+1 +2*x +3*x^2</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">PolynomialRegression := function(x, y, n)
local a;
a := List([0 .. n], i -> List(x, s -> s^i));
Line 953 ⟶ 1,100:
# Return coefficients in ascending degree order
PolynomialRegression(x, y, 2);
# [ 1, 2, 3 ]</langsyntaxhighlight>
 
=={{header|gnuplot}}==
 
<langsyntaxhighlight lang="gnuplot"># The polynomial approximation
f(x) = a*x**2 + b*x + c
 
Line 980 ⟶ 1,127:
e
 
print sprintf("\n --- \n Polynomial fit: %.4f x^2 + %.4f x + %.4f\n", a, b, c)</langsyntaxhighlight>
 
=={{header|Go}}==
===Library gonum/matrix===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,024 ⟶ 1,171:
}
return x
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,034 ⟶ 1,181:
===Library go.matrix===
Least squares solution using QR decomposition and package [http://github.com/skelterjohn/go.matrix go.matrix].
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,074 ⟶ 1,221:
}
fmt.Println(c)
}</langsyntaxhighlight>
{{out}} (lowest order coefficient first)
<pre>
Line 1,082 ⟶ 1,229:
=={{header|Haskell}}==
Uses module Matrix.LU from [http://hackage.haskell.org/package/dsp hackageDB DSP]
<langsyntaxhighlight lang="haskell">import Data.List
import Data.Array
import Control.Monad
Line 1,092 ⟶ 1,239:
polyfit d ry = elems $ solve mat vec where
mat = listArray ((1,1), (d,d)) $ liftM2 concatMap ppoly id [0..fromIntegral $ pred d]
vec = listArray (1,d) $ take d ry</langsyntaxhighlight>
{{out}} in GHCi:
<langsyntaxhighlight lang="haskell">*Main> polyfit 3 [1,6,17,34,57,86,121,162,209,262,321]
[1.0,2.0,3.0]</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">REAL :: n=10, x(n), y(n), m=3, p(m)
 
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Line 1,112 ⟶ 1,259:
! called by the solver of the SOLVE function. All variables are global
Theory = p(1)*x(nr)^2 + p(2)*x(nr) + p(3)
END</langsyntaxhighlight>
{{out}}
<pre>SOLVE performs a (nonlinear) least-square fit (Levenberg-Marquardt):
Line 1,118 ⟶ 1,265:
 
=={{header|Hy}}==
<langsyntaxhighlight lang="lisp">(import [numpy [polyfit]])
 
(setv x (range 11))
(setv y [1 6 17 34 57 86 121 162 209 262 321])
 
(print (polyfit x y 2))</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight lang="j"> Y=:1 6 17 34 57 86 121 162 209 262 321
(%. ^/~@x:@i.@#) Y
1 2 3 0 0 0 0 0 0 0 0</langsyntaxhighlight>
 
Note that this implementation does not use floating point numbers,
Line 1,136 ⟶ 1,283:
but for small problems like this it is inconsequential.
 
The above solution fits a polynomial of order 11 (or, more specifically, a polynomial whose order matches the length of its argument sequence).
If the order of the polynomial is known to be 3
(as is implied in the task description)
then the following solution is probably preferable:
<langsyntaxhighlight lang="j"> Y %. (i.3) ^/~ i.#Y
1 2 3</langsyntaxhighlight>
(note that this time we used floating point numbers, so that result is approximate rather than exact - it only looks exact because of how J displays floating point numbers (by default, J assumes six digits of accuracy) - changing (i.3) to (x:i.3) would give us an exact result, if that mattered.)
 
Line 1,147 ⟶ 1,294:
{{trans|D}}
{{works with|Java|8}}
<langsyntaxhighlight Javalang="java">import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
import java.util.stream.IntStream;
Line 1,154 ⟶ 1,301:
private static void polyRegression(int[] x, int[] y) {
int n = x.length;
int[] r = IntStream.range(0, n).toArray();
double xm = Arrays.stream(x).average().orElse(Double.NaN);
double ym = Arrays.stream(y).average().orElse(Double.NaN);
double x2m = Arrays.stream(rx).map(a -> a * a).average().orElse(Double.NaN);
double x3m = Arrays.stream(rx).map(a -> a * a * a).average().orElse(Double.NaN);
double x4m = Arrays.stream(rx).map(a -> a * a * a * a).average().orElse(Double.NaN);
double xym = 0.0;
for (int i = 0; i < x.length && i < y.length; ++i) {
Line 1,196 ⟶ 1,342:
polyRegression(x, y);
}
}</langsyntaxhighlight>
{{out}}
<pre>y = 1.0 + 2.0x + 3.0x^2
Line 1,212 ⟶ 1,358:
9 262 262.0
10 321 321.0</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
<syntaxhighlight lang="jq">
def mean: add/length;
 
def inner_product($y):
. as $x
| reduce range(0; length) as $i (0; . + ($x[$i] * $y[$i]));
 
# $x and $y should be arrays of the same length
# Emit { a, b, c, z}
# Attempt to avoid overflow
def polynomialRegression($x; $y):
($x | length) as $length
| ($length * $length) as $l2
| ($x | map(./$length)) as $xs
| ($xs | add) as $xm
| ($y | mean) as $ym
| ($xs | map(. * .) | add * $length) as $x2m
| ($x | map( (./$length) * . * .) | add) as $x3m
| ($xs | map(. * . | (.*.) ) | add * $l2 * $length) as $x4m
| ($xs | inner_product($y)) as $xym
| ($xs | map(. * .) | inner_product($y) * $length) as $x2ym
| ($x2m - $xm * $xm) as $sxx
| ($xym - $xm * $ym) as $sxy
| ($x3m - $xm * $x2m) as $sxx2
| ($x4m - $x2m * $x2m) as $sx2x2
| ($x2ym - $x2m * $ym) as $sx2y
| {z: ([$x,$y] | transpose) }
| .b = ($sxy * $sx2x2 - $sx2y * $sxx2) / ($sxx * $sx2x2 - $sxx2 * $sxx2)
| .c = ($sx2y * $sxx - $sxy * $sxx2) / ($sxx * $sx2x2 - $sxx2 * $sxx2)
| .a = $ym - .b * $xm - .c * $x2m ;
 
# Input: {a,b,c,z}
def report:
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
def abc($x): .a + .b * $x + .c * $x * $x;
def print($p): "\($p[0] | lpad(3)) \($p[1] | lpad(4)) \(abc($p[0]) | lpad(5))";
"y = \(.a) + \(.b)x + \(.c)x^2\n",
" Input Approximation",
" x y y\u0302",
print(.z[]) ;
def x: [range(0;11)];
def y: [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321];
 
polynomialRegression(x; y)
| report
</syntaxhighlight>
{{output}}
<pre>
y = 1 + 2x + 3x^2
 
Input Approximation
x y ŷ
0 1 1
1 6 6
2 17 17
3 34 34
4 57 57
5 86 86
6 121 121
7 162 162
8 209 209
9 262 262
10 321 321
</pre>
 
=={{header|Julia}}==
Line 1,217 ⟶ 1,439:
The least-squares fit problem for a degree <i>n</i>
can be solved with the built-in backslash operator (coefficients in increasing order of degree):
<langsyntaxhighlight lang="julia">polyfit(x::Vector, y::Vector, deg::Int) = collect(v ^ p for v in x, p in 0:deg) \ y
 
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
@show polyfit(x, y, 2)</langsyntaxhighlight>
 
{{out}}
Line 1,228 ⟶ 1,450:
=={{header|Kotlin}}==
{{trans|REXX}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun polyRegression(x: IntArray, y: IntArray) {
Line 1,263 ⟶ 1,485:
val y = intArrayOf(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
polyRegression(x, y)
}</langsyntaxhighlight>
 
{{out}}
Line 1,286 ⟶ 1,508:
=={{header|Lua}}==
{{trans|Modula-2}}
<langsyntaxhighlight lang="lua">function eval(a,b,c,x)
return a + (b + c * x) * x
end
Line 1,337 ⟶ 1,559:
local xa = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local ya = {1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}
regression(xa, ya)</langsyntaxhighlight>
{{out}}
<pre>y = 1 + 2x + 3x^2
Line 1,353 ⟶ 1,575:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">with(CurveFitting);
PolynomialInterpolation([[0, 1], [1, 6], [2, 17], [3, 34], [4, 57], [5, 86], [6, 121], [7, 162], [8, 209], [9, 262], [10, 321]], 'x');
</syntaxhighlight>
</lang>
Result:
<pre>3*x^2+2*x+1</pre>
Line 1,361 ⟶ 1,583:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Using the built-in "Fit" function.
<langsyntaxhighlight Mathematicalang="mathematica">data = Transpose@{Range[0, 10], {1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}};
Fit[data, {1, x, x^2}, x]</langsyntaxhighlight>
Second version: using built-in "InterpolatingPolynomial" function.
<langsyntaxhighlight Mathematicalang="mathematica">Simplify@InterpolatingPolynomial[{{0, 1}, {1, 6}, {2, 17}, {3, 34}, {4, 57}, {5, 86}, {6, 121}, {7, 162}, {8, 209}, {9, 262}, {10, 321}}, x]</langsyntaxhighlight>
WolframAlpha version:
<syntaxhighlight lang="mathematica">curve fit (0,1), (1,6), (2,17), (3,34), (4,57), (5,86), (6,121), (7,162), (8,209), (9,262), (10,321)</syntaxhighlight>
Result:
<pre>1 + 2x + 3x^2</pre>
Line 1,373 ⟶ 1,597:
The output of this function is the coefficients of the polynomial which best fit these x,y value pairs.
 
<langsyntaxhighlight MATLABlang="matlab">>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
>> y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321];
>> polyfit(x,y,2)
Line 1,379 ⟶ 1,603:
ans =
 
2.999999999999998 2.000000000000019 0.999999999999956</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
Part 1:
<syntaxhighlight lang="text">ПC С/П ПD ИП9 + П9 ИПC ИП5 + П5
ИПC x^2 П2 ИП6 + П6 ИП2 ИПC * ИП7
+ П7 ИП2 x^2 ИП8 + П8 ИПC ИПD *
ИПA + ПA ИП2 ИПD * ИПB + ПB ИПD
КИП4 С/П БП 00</langsyntaxhighlight>
 
''Input'': В/О x<sub>1</sub> С/П y<sub>1</sub> С/П x<sub>2</sub> С/П y<sub>2</sub> С/П ...
 
Part 2:
<syntaxhighlight lang="text">ИП5 ПC ИП6 ПD П2 ИП7 П3 ИП4 ИПD *
ИПC ИП5 * - ПD ИП4 ИП7 * ИПC ИП6
* - П7 ИП4 ИПA * ИПC ИП9 * -
Line 1,400 ⟶ 1,624:
ИПD ИП8 * ИП7 ИП3 * - / ПB ИПA
ИПB ИП7 * - ИПD / ПA ИП9 ИПB ИП6
* - ИПA ИП5 * - ИП4 / П9 С/П</langsyntaxhighlight>
 
''Result'': Р9 = a<sub>0</sub>, РA = a<sub>1</sub>, РB = a<sub>2</sub>.
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE PolynomialRegression;
FROM FormatString IMPORT FormatString;
FROM RealStr IMPORT RealToStr;
Line 1,491 ⟶ 1,715:
 
ReadChar;
END PolynomialRegression.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import lenientops, sequtils, stats, strformat
 
proc polyRegression(x, y: openArray[int]) =
Line 1,528 ⟶ 1,752:
let x = toSeq(0..10)
let y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
polyRegression(x, y)</langsyntaxhighlight>
 
{{out}}
Line 1,550 ⟶ 1,774:
{{trans|Kotlin}}
{{libheader|Base}}
<langsyntaxhighlight OCamllang="ocaml">open Base
open Stdio
 
Line 1,590 ⟶ 1,814:
let x = Array.init 11 ~f:Float.of_int in
let y = [| 1.; 6.; 17.; 34.; 57.; 86.; 121.; 162.; 209.; 262.; 321. |] in
regression x y</langsyntaxhighlight>
 
{{out}}
Line 1,613 ⟶ 1,837:
=={{header|Octave}}==
 
<langsyntaxhighlight lang="octave">x = [0:10];
y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321];
coeffs = polyfit(x, y, 2)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Lagrange interpolating polynomial:
<langsyntaxhighlight lang="parigp">polinterpolate([0,1,2,3,4,5,6,7,8,9,10],[1,6,17,34,57,86,121,162,209,262,321])</langsyntaxhighlight>
In newer versions, this can be abbreviated:
<langsyntaxhighlight lang="parigp">polinterpolate([0..10],[1,6,17,34,57,86,121,162,209,262,321])</langsyntaxhighlight>
{{out}}
<pre>3*x^2 + 2*x + 1</pre>
 
Least-squares fit:
<langsyntaxhighlight lang="parigp">V=[1,6,17,34,57,86,121,162,209,262,321]~;
M=matrix(#V,3,i,j,(i-1)^(j-1));Polrev(matsolve(M~*M,M~*V))</langsyntaxhighlight>
<small>Code thanks to [http://pari.math.u-bordeaux.fr/archives/pari-users-1105/msg00006.html Bill Allombert]</small>
{{out}}
Line 1,633 ⟶ 1,857:
 
Least-squares polynomial fit in its own function:
<langsyntaxhighlight lang="parigp">lsf(X,Y,n)=my(M=matrix(#X,n+1,i,j,X[i]^(j-1))); Polrev(matsolve(M~*M,M~*Y~))
lsf([0..10], [1,6,17,34,57,86,121,162,209,262,321], 2)</langsyntaxhighlight>
 
=={{header|Perl}}==
This code identical to that of [[Multiple regression]] task.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Statistics::Regression;
Line 1,651 ⟶ 1,875:
my @coeff = $reg->theta();
 
printf "%-6s %8.3f\n", $model[$_], $coeff[$_] for 0..@model-1;</langsyntaxhighlight>
{{output}}
<pre>const 1.000
Line 1,658 ⟶ 1,882:
 
PDL Alternative:
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
use strict;
 
Line 1,683 ⟶ 1,907:
print "\n" unless($_)
}
</syntaxhighlight>
</lang>
{{output}}
<pre> 3.00000037788248 * $x**2 + 1.99999750988868 * $x + 1.00000180493936</pre>
Line 1,692 ⟶ 1,916:
{{libheader|Phix/pGUI}}
You can run this online [http://phix.x10.mx/p2js/Polynomial_regression.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Polynomial_regression.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- DEV MINSIZE not honoured</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57</span><span style="color: #0000FF;">,</span><span style="color: #000000;">86</span><span style="color: #0000FF;">,</span><span style="color: #000000;">121</span><span style="color: #0000FF;">,</span><span style="color: #000000;">162</span><span style="color: #0000FF;">,</span><span style="color: #000000;">209</span><span style="color: #0000FF;">,</span><span style="color: #000000;">262</span><span style="color: #0000FF;">,</span><span style="color: #000000;">321</span><span style="color: #0000FF;">},</span>
Line 1,765 ⟶ 1,988:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
(plus a simple graphical plot, as per [[Polynomial_regression#Racket|Racket]])
Line 1,786 ⟶ 2,009:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function qr([double[][]]$A) {
$m,$n = $A.count, $A[0].count
Line 1,867 ⟶ 2,090:
"X^2 X constant"
"$(polyfit $x $y 2)"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,878 ⟶ 2,101:
 
{{libheader|NumPy}}
<langsyntaxhighlight lang="python">>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
>>> coeffs = numpy.polyfit(x,y,deg=2)
>>> coeffs
array([ 3., 2., 1.])</langsyntaxhighlight>
Substitute back received coefficients.
<langsyntaxhighlight lang="python">>>> yf = numpy.polyval(numpy.poly1d(coeffs), x)
>>> yf
array([ 1., 6., 17., 34., 57., 86., 121., 162., 209., 262., 321.])</langsyntaxhighlight>
Find max absolute error:
<langsyntaxhighlight lang="python">>>> '%.1g' % max(y-yf)
'1e-013'</langsyntaxhighlight>
 
===Example===
For input arrays `x' and `y':
<langsyntaxhighlight lang="python">>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]</langsyntaxhighlight>
 
<langsyntaxhighlight lang="python">>>> p = numpy.poly1d(numpy.polyfit(x, y, deg=2), variable='N')
>>> print p
2
1.085 N + 10.36 N - 0.6164</langsyntaxhighlight>
Thus we confirm once more that for already sorted sequences
the considered quick sort implementation has
Line 1,911 ⟶ 2,134:
which will find the least squares solution via a QR decomposition:
 
<syntaxhighlight lang="r">
<lang R>
x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
coef(lm(y ~ x + I(x^2)))</langsyntaxhighlight>
 
{{out}}
Line 1,924 ⟶ 2,147:
Alternately, use poly:
 
<langsyntaxhighlight Rlang="r">coef(lm(y ~ poly(x, 2, raw=T)))</langsyntaxhighlight>{{out}}
<pre> (Intercept) poly(x, 2, raw = T)1 poly(x, 2, raw = T)2
1 2 3</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require math plot)
Line 1,948 ⟶ 2,171:
(plot (list (points (map vector xs ys))
(function (poly (fit xs ys 2)))))
</syntaxhighlight>
</lang>
{{out}}
[[File:polyreg-racket.png]]
Line 1,954 ⟶ 2,177:
=={{header|Raku}}==
(formerly Perl 6)
We'll use a Clifford algebra library. Very slow.
 
Rationale (in French for some reason):
<lang perl6>use Clifford;
 
Le système d'équations peut s'écrire :
<math>\left(a + b x_i + cx_i^2 = y_i\right)_{i=1\ldots N}</math>, où on cherche <math>(a,b,c)\in\mathbb{R}^3</math>. On considère <math>\mathbb{R}^N</math> et on répartit chaque équation sur chaque dimension:
 
<math> (a + b x_i + cx_i^2)\mathbf{e}_i = y_i\mathbf{e}_i</math>
 
Posons alors :
 
<math>
\mathbf{x}_0 = \sum_{i=1}^N \mathbf{e}_i,\,
\mathbf{x}_1 = \sum_{i=1}^N x_i\mathbf{e}_i,\,
\mathbf{x}_2 = \sum_{i=1}^N x_i^2\mathbf{e}_i,\,
\mathbf{y} = \sum_{i=1}^N y_i\mathbf{e}_i
</math>
 
Le système d'équations devient : <math>a\mathbf{x}_0+b\mathbf{x}_1+c\mathbf{x}_2 = \mathbf{y}</math>.
 
D'où :
<math>\begin{align}
a = \mathbf{y}\and\mathbf{x}_1\and\mathbf{x}_2/(\mathbf{x}_0\and\mathbf{x_1}\and\mathbf{x_2})\\
b = \mathbf{y}\and\mathbf{x}_2\and\mathbf{x}_0/(\mathbf{x}_1\and\mathbf{x_2}\and\mathbf{x_0})\\
c = \mathbf{y}\and\mathbf{x}_0\and\mathbf{x}_1/(\mathbf{x}_2\and\mathbf{x_0}\and\mathbf{x_1})\\
\end{align}</math>
 
<syntaxhighlight lang="raku" line>use MultiVector;
 
constant @x1 = <0 1 2 3 4 5 6 7 8 9 10>;
Line 1,966 ⟶ 2,214:
 
constant $y = [+] @y Z* @e;
 
my $J = $x1 ∧ $x2;
my $I = $x0 ∧ $J;
 
my $I2 = ($I·$I.reversion).Real;
 
.say for
$y∧$x1∧$x2/($x0∧$x1∧$x2),
(($y ∧ $J)·$I.reversion)/$I2,
$y∧$x2∧$x0/($x1∧$x2∧$x0),
(($y ∧ ($x2 ∧ $x0))·$I.reversion)/$I2,
$y∧$x0∧$x1/($x2∧$x0∧$x1);
(($y ∧ ($x0 ∧ $x1))·$I.reversion)/$I2;</lang>
</syntaxhighlight>
{{out}}
<pre>1
Line 1,983 ⟶ 2,227:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* Implementation of http://keisan.casio.com/exec/system/14059932254941
*--------------------------------------------------------------------*/
Line 2,033 ⟶ 2,277:
fun:
Parse Arg x
Return a+b*x+c*x**2 </langsyntaxhighlight>
{{out}}
<pre>y=1+2*x+3*x**2
Line 2,049 ⟶ 2,293:
9 262 262.000
10 321 321.000</pre>
 
=={{header|RPL}}==
{{trans|Ada}}
≪ 1 + → x y n
≪ { } n + x SIZE + 0 CON
1 x SIZE '''FOR''' j
1 n '''FOR''' k
{ } k + j + x j GET k 1 - ^ PUT
'''NEXT NEXT'''
DUP y * SWAP DUP TRN * /
<span style="color:grey">@ the following lines convert the resulting vector into a polynomial equation</span>
DUP 'x' STO 1 GET
2 x SIZE '''FOR''' j 'X' * x j GET + '''NEXT'''
EXPAN COLCT
≫ ≫ '<span style="color:blue">FIT</span>' STO
 
[1 2 3 4 5 6 7 8 9 10] [1 6 17 34 57 86 121 162 209 262 321] 2 <span style="color:blue">FIT</span>
{{out}}
<pre>
1: '3+2*X+1*X^2'
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'matrix'
 
def regress x, y, degree
Line 2,060 ⟶ 2,325:
 
((mx.t * mx).inv * mx.t * my).transpose.to_a[0].map(&:to_f)
end</langsyntaxhighlight>
'''Testing:'''
<langsyntaxhighlight lang="ruby">p regress([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321],
2)</langsyntaxhighlight>
{{out}}
<pre>[1.0, 2.0, 3.0]</pre>
Line 2,073 ⟶ 2,338:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">object PolynomialRegression extends App {
private def xy = Seq(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321).zipWithIndex.map(_.swap)
 
Line 2,106 ⟶ 2,371:
polyRegression(xy)
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func regress(x, y, degree) {
var A = Matrix.build(x.len, degree+1, {|i,j|
x[i]**j
Line 2,129 ⟶ 2,394:
)
 
say coeff</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3]</pre>
Line 2,135 ⟶ 2,400:
=={{header|Stata}}==
See '''[http://www.stata.com/help.cgi?fvvarlist Factor variables]''' in Stata help for explanations on the ''c.x##c.x'' syntax.
<langsyntaxhighlight lang="stata">. clear
. input x y
0 1
Line 2,167 ⟶ 2,432:
|
_cons | 1 . . . . .
------------------------------------------------------------------------------</langsyntaxhighlight>
 
=={{header|Swift}}==
{{trans|Kotlin}}
<syntaxhighlight lang="swift">
<lang Swift>
 
let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Line 2,214 ⟶ 2,479:
 
polyRegression(x: x, y: y)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,239 ⟶ 2,504:
<!-- This implementation from Emiliano Gavilan;
posted here with his explicit permission -->
<langsyntaxhighlight lang="tcl">package require math::linearalgebra
 
proc build.matrix {xvec degree} {
Line 2,287 ⟶ 2,552:
set coeffs [math::linearalgebra::solveGauss $A $b]
# show results
puts $coeffs</langsyntaxhighlight>
This will print:
1.0000000000000207 1.9999999999999958 3.0
Line 2,293 ⟶ 2,558:
 
=={{header|TI-83 BASIC}}==
<langsyntaxhighlight lang="ti83b">DelVar X
seq(X,X,0,10) → L1
{1,6,17,34,57,86,121,162,209,262,321} → L2
QuadReg L1,L2</langsyntaxhighlight>
 
{{out}}
Line 2,307 ⟶ 2,572:
 
=={{header|TI-89 BASIC}}==
<langsyntaxhighlight lang="ti89b">DelVar x
seq(x,x,0,10) → xs
{1,6,17,34,57,86,121,162,209,262,321} → ys
QuadReg xs,ys
Disp regeq(x)</langsyntaxhighlight>
 
<code>seq(''expr'',''var'',''low'',''high'')</code> evaluates ''expr'' with ''var'' bound to integers from ''low'' to ''high'' and returns a list of the results. <code> →</code> is the assignment operator.
Line 2,329 ⟶ 2,594:
whereby the data can be passed as lists rather than arrays,
and all memory management is handled automatically.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
#import flo
 
(fit "n") ("x","y") = ..dgelsd\"y" (gang \/*pow float*x iota successor "n")* "x"</langsyntaxhighlight>
test program:
<langsyntaxhighlight Ursalalang="ursala">x = <0.,1.,2.,3.,4.,5.,6.,7.,8.,9.,10.>
y = <1.,6.,17.,34.,57.,86.,121.,162.,209.,262.,321.>
 
#cast %eL
 
example = fit2(x,y)</langsyntaxhighlight>
{{out}}
<pre><3.000000e+00,2.000000e+00,1.000000e+00></pre>
Line 2,346 ⟶ 2,611:
=={{header|VBA}}==
Excel VBA has built in capability for line estimation.
<langsyntaxhighlight lang="vb">Option Base 1
Private Function polynomial_regression(y As Variant, x As Variant, degree As Integer) As Variant
Dim a() As Double
Line 2,375 ⟶ 2,640:
Debug.Print "Degrees of freedom:"; result(4, 2)
Debug.Print "Standard error of y estimate:"; result(3, 2)
End Sub</langsyntaxhighlight>{{out}}
<pre>coefficients : 1, 2, 3,
standard errors: 0, 0, 0,
Line 2,389 ⟶ 2,654:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
import "./seq" for Lst
import "./fmt" for Fmt
 
var polynomialRegression = Fn.new { |x, y|
Line 2,424 ⟶ 2,689:
for (i in 1..10) x[i] = i
var y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
polynomialRegression.call(x, y)</langsyntaxhighlight>
 
{{out}}
Line 2,447 ⟶ 2,712:
=={{header|zkl}}==
Using the GNU Scientific Library
<langsyntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
xs:=GSL.VectorFromData(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
ys:=GSL.VectorFromData(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321);
Line 2,453 ⟶ 2,718:
v.format().println();
GSL.Helpers.polyString(v).println();
GSL.Helpers.polyEval(v,xs).format().println();</langsyntaxhighlight>
{{out}}
<pre>
Line 2,465 ⟶ 2,730:
 
Example:
<langsyntaxhighlight lang="zkl">polyfit(T(T(0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0)),
T(T(1.0,6.0,17.0,34.0,57.0,86.0,121.0,162.0,209.0,262.0,321.0)), 2)
.flatten().println();</langsyntaxhighlight>
{{out}}<pre>L(1,2,3)</pre>
2,442

edits