Polynomial regression: Difference between revisions

Content added Content deleted
(PowerShell added)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 11: Line 11:


This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].



=={{header|Ada}}==
=={{header|Ada}}==
Line 320: Line 319:
2.000000
2.000000
3.000000</pre>
3.000000</pre>

=={{header|C sharp|C#}}==
{{libheader|Math.Net}}
<lang csharp> public static double[] Polyfit(double[] x, double[] y, int degree)
{
// Vandermonde matrix
var v = new DenseMatrix(x.Length, degree + 1);
for (int i = 0; i < v.RowCount; i++)
for (int j = 0; j <= degree; j++) v[i, j] = Math.Pow(x[i], j);
var yv = new DenseVector(y).ToColumnMatrix();
QR qr = v.QR();
// Math.Net doesn't have an "economy" QR, so:
// cut R short to square upper triangle, then recompute Q
var r = qr.R.SubMatrix(0, degree + 1, 0, degree + 1);
var q = v.Multiply(r.Inverse());
var p = r.Inverse().Multiply(q.TransposeThisAndMultiply(yv));
return p.Column(0).ToArray();
}</lang>
Example:
<lang C sharp> static void Main(string[] args)
{
const int degree = 2;
var x = new[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
var y = new[] {1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0};
var p = Polyfit(x, y, degree);
foreach (var d in p) Console.Write("{0} ",d);
Console.WriteLine();
for (int i = 0; i < x.Length; i++ )
Console.WriteLine("{0} => {1} diff {2}", x[i], Polyval(p,x[i]), y[i] - Polyval(p,x[i]));
Console.ReadKey(true);
}</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 400: Line 430:
9 262 262.0
9 262 262.0
10 321 321.0</pre>
10 321 321.0</pre>

=={{header|C#|C sharp}}==
{{libheader|Math.Net}}
<lang csharp> public static double[] Polyfit(double[] x, double[] y, int degree)
{
// Vandermonde matrix
var v = new DenseMatrix(x.Length, degree + 1);
for (int i = 0; i < v.RowCount; i++)
for (int j = 0; j <= degree; j++) v[i, j] = Math.Pow(x[i], j);
var yv = new DenseVector(y).ToColumnMatrix();
QR qr = v.QR();
// Math.Net doesn't have an "economy" QR, so:
// cut R short to square upper triangle, then recompute Q
var r = qr.R.SubMatrix(0, degree + 1, 0, degree + 1);
var q = v.Multiply(r.Inverse());
var p = r.Inverse().Multiply(q.TransposeThisAndMultiply(yv));
return p.Column(0).ToArray();
}</lang>
Example:
<lang C sharp> static void Main(string[] args)
{
const int degree = 2;
var x = new[] {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
var y = new[] {1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0};
var p = Polyfit(x, y, degree);
foreach (var d in p) Console.Write("{0} ",d);
Console.WriteLine();
for (int i = 0; i < x.Length; i++ )
Console.WriteLine("{0} => {1} diff {2}", x[i], Polyval(p,x[i]), y[i] - Polyval(p,x[i]));
Console.ReadKey(true);
}</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 1,436: Line 1,435:
1 6 17 34 57 86 121 162 209 262 321
1 6 17 34 57 86 121 162 209 262 321
An approximating polynomial for your dataset is 3x^2 + 2x + 1.</pre>
An approximating polynomial for your dataset is 3x^2 + 2x + 1.</pre>

=={{header|Perl 6}}==
We'll use a Clifford algebra library.

<lang perl6>use Clifford;

constant @x1 = <0 1 2 3 4 5 6 7 8 9 10>;
constant @y = <1 6 17 34 57 86 121 162 209 262 321>;

constant $x0 = [+] @e[^@x1];
constant $x1 = [+] @x1 Z* @e;
constant $x2 = [+] @x1 »**» 2 Z* @e;

constant $y = [+] @y Z* @e;

my $J = $x1 ∧ $x2;
my $I = $x0 ∧ $J;

my $I2 = ($I·$I.reversion).Real;

.say for
(($y ∧ $J)·$I.reversion)/$I2,
(($y ∧ ($x2 ∧ $x0))·$I.reversion)/$I2,
(($y ∧ ($x0 ∧ $x1))·$I.reversion)/$I2;</lang>
{{out}}
<pre>1
2
3
</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,555: Line 1,525:
IupMainLoop()
IupMainLoop()
IupClose()</lang>
IupClose()</lang>

=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang PowerShell>
<lang PowerShell>
Line 1,644: Line 1,615:
3 1.99999999999998 1.00000000000005
3 1.99999999999998 1.00000000000005
</pre>
</pre>



=={{header|Python}}==
=={{header|Python}}==
Line 1,722: Line 1,692:
{{out}}
{{out}}
[[File:polyreg-racket.png]]
[[File:polyreg-racket.png]]

=={{header|Raku}}==
(formerly Perl 6)
We'll use a Clifford algebra library.

<lang perl6>use Clifford;

constant @x1 = <0 1 2 3 4 5 6 7 8 9 10>;
constant @y = <1 6 17 34 57 86 121 162 209 262 321>;

constant $x0 = [+] @e[^@x1];
constant $x1 = [+] @x1 Z* @e;
constant $x2 = [+] @x1 »**» 2 Z* @e;

constant $y = [+] @y Z* @e;

my $J = $x1 ∧ $x2;
my $I = $x0 ∧ $J;

my $I2 = ($I·$I.reversion).Real;

.say for
(($y ∧ $J)·$I.reversion)/$I2,
(($y ∧ ($x2 ∧ $x0))·$I.reversion)/$I2,
(($y ∧ ($x0 ∧ $x1))·$I.reversion)/$I2;</lang>
{{out}}
<pre>1
2
3
</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 1,848: Line 1,848:


}</lang>
}</lang>

=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
Line 2,043: Line 2,044:
Degrees of freedom: 8
Degrees of freedom: 8
Standard error of y estimate: 2,79482284961344E-14 </pre>
Standard error of y estimate: 2,79482284961344E-14 </pre>

=={{header|zkl}}==
=={{header|zkl}}==
Using the GNU Scientific Library
Using the GNU Scientific Library