Polynomial regression: Difference between revisions
Content added Content deleted
(→{{header|Sidef}}: simplified the code, as the Matrix class is now built-in) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: POD syntax updated) |
||
Line 1,236: | Line 1,236: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
This script depends on the Math::MatrixReal CPAN module to compute matrix determinants. |
This script depends on the <tt>Math::MatrixReal</tt> CPAN module to compute matrix determinants. |
||
<lang Perl> |
<lang Perl>use strict; |
||
#!bin/usr/perl |
|||
use strict; |
|||
use warnings; |
use warnings; |
||
use 5.020; |
|||
#This is a script to calculate an equation for a given set of coordinates. |
#This is a script to calculate an equation for a given set of coordinates. |
||
Line 1,249: | Line 1,246: | ||
=pod |
=pod |
||
Step 1: Get each x coordinate all at once (delimited by " ") and each for y at once |
Step 1: Get each x coordinate all at once (delimited by " ") and each for y at once |
||
on the next prompt in the same format (delimited by " "). |
on the next prompt in the same format (delimited by " "). |
||
=cut |
=cut |
||
sub getPairs() { |
sub getPairs() { |
||
my $buffer = <STDIN>; |
my $buffer = <STDIN>; |
||
Line 1,263: | Line 1,262: | ||
#This whole thing depends on the number of x's being the same as the number of y's |
#This whole thing depends on the number of x's being the same as the number of y's |
||
my $pairs = scalar(@x); |
my $pairs = scalar(@x); |
||
=pod |
=pod |
||
Step 2: Devise the base equation of our polynomial using the following idea |
Step 2: Devise the base equation of our polynomial using the following idea |
||
There is some polynomial of degree n (n == number of pairs - 1) such that |
There is some polynomial of degree n (n == number of pairs - 1) such that |
||
f(x)=ax^n + bx^(n-1) + ... yx + z |
f(x)=ax^n + bx^(n-1) + ... yx + z |
||
=cut |
=cut |
||
#Create an array of coefficients and their degrees with the format ("coefficent degree") |
#Create an array of coefficients and their degrees with the format ("coefficent degree") |
||
my @alphabet; |
my @alphabet; |
||
Line 1,280: | Line 1,280: | ||
=pod |
=pod |
||
Step 3: Using the array of coeffs and their degrees, set up individual equations solving for |
Step 3: Using the array of coeffs and their degrees, set up individual equations solving for |
||
each coordinate pair. Why put it in this format? It interfaces witht he Math::MatrixReal package better this way. |
each coordinate pair. Why put it in this format? It interfaces witht he Math::MatrixReal package better this way. |
||
=cut |
=cut |
||
my @coeffs; |
my @coeffs; |
||
for(my $count = 0; $count < $pairs; $count++) { |
for(my $count = 0; $count < $pairs; $count++) { |
||
Line 1,295: | Line 1,297: | ||
$row .= ("$_\n"); |
$row .= ("$_\n"); |
||
} |
} |
||
=pod |
=pod |
||
Step 4: We now have rows of x's raised to powers. With this in mind, we create a coefficient matrix. |
Step 4: We now have rows of x's raised to powers. With this in mind, we create a coefficient matrix. |
||
=cut |
=cut |
||
my $matrix = Math::MatrixReal->new_from_string($row); |
my $matrix = Math::MatrixReal->new_from_string($row); |
||
my $buffMatrix = $matrix->new_from_string($row); |
my $buffMatrix = $matrix->new_from_string($row); |
||
=pod |
=pod |
||
Step 5: Now that we've gotten the matrix to do what we want it to do, we need to calculate the various determinants of the matrices |
Step 5: Now that we've gotten the matrix to do what we want it to do, we need to calculate the various determinants of the matrices |
||
=cut |
=cut |
||
my $coeffDet = $matrix->det(); |
my $coeffDet = $matrix->det(); |
||
=pod |
=pod |
||
Step 6: Now that we have the determinant of the coefficient matrix, we need to find the determinants of the coefficient matrix with each column (1 at a time) replaced with the y values. |
Step 6: Now that we have the determinant of the coefficient matrix, we need to find the determinants of the coefficient matrix with each column (1 at a time) replaced with the y values. |
||
=cut |
=cut |
||
#NOTE: Unlike in Perl, matrix indices start at 1, not 0. |
#NOTE: Unlike in Perl, matrix indices start at 1, not 0. |
||
for(my $rows = my $column = 1; $column <= $pairs; $column++) { |
for(my $rows = my $column = 1; $column <= $pairs; $column++) { |
||
Line 1,332: | Line 1,337: | ||
=pod |
=pod |
||
Step 7: Now that we've found the values of a, b, ... y, z of the original polynomial, it's time to form our polynomial! |
Step 7: Now that we've found the values of a, b, ... y, z of the original polynomial, it's time to form our polynomial! |
||
=cut |
=cut |
||
my $polynomial; |
my $polynomial; |
||
for(my $i = 0; $i < $pairs-1; $i++) { |
for(my $i = 0; $i < $pairs-1; $i++) { |
||
Line 1,355: | Line 1,362: | ||
</lang> |
</lang> |
||
{{output}} |
{{output}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
0 1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 5 6 7 8 9 10 |
||
Please enter the values for the y coordinates, each delimited by a space. (Ex: 0 1 2 3) |
Please enter the values for the y coordinates, each delimited by a space. (Ex: 0 1 2 3) |
||
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. |
An approximating polynomial for your dataset is 3x^2 + 2x + 1.</pre> |
||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |