Multiple regression: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Perl 6}}: comment on speed)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 467:
-143.162
61.9603</pre>
 
 
=={{header|Common Lisp}}==
Line 533 ⟶ 532:
"35.2014388489 X1 - 3.95683453237 X2 - 42.7410071942"
</pre>
 
 
 
=={{header|ERRE}}==
Line 908 ⟶ 905:
gels_jlapack_ X;y
128.813 _143.162 61.9603</lang>
 
=={{header|Julia}}==
{{trans|MATLAB}}
 
As in Matlab, the backslash or slash operator (depending on the matrix ordering) can be used for solving this problem, for example:
 
<lang julia>x = [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83]
y = [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46]
X = [x.^0 x.^1 x.^2];
b = X \ y</lang>
{{out}}
<pre>
3-element Array{Float64,1}:
128.813
-143.162
61.9603
</pre>
 
=={{header|JavaScript}}==
Line 986 ⟶ 966:
-143.1620228653037
61.960325442985436</pre>
 
=={{header|Julia}}==
{{trans|MATLAB}}
 
As in Matlab, the backslash or slash operator (depending on the matrix ordering) can be used for solving this problem, for example:
 
<lang julia>x = [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83]
y = [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46]
X = [x.^0 x.^1 x.^2];
b = X \ y</lang>
{{out}}
<pre>
3-element Array{Float64,1}:
128.813
-143.162
61.9603
</pre>
 
=={{header|Kotlin}}==
Line 1,192 ⟶ 1,189:
X -143.162
X**2 61.960</pre>
 
=={{header|Perl 6}}==
We're going to solve the example on the Wikipedia article using [https://github.com/grondilu/clifford Clifford], a [https://en.wikipedia.org/wiki/Geometric_algebra geometric algebra] module. Optimization for large vector space does not quite work yet, so it's going to take (a lof of) time and a fair amount of memory, but it should work.
 
Let's create four vectors containing our input data:
 
<math>\begin{align}
\mathbf{w} & = w^k\mathbf{e}_k\\
\mathbf{h_0} & = (h^k)^0\mathbf{e}_k\\
\mathbf{h_1} & = (h^k)^1\mathbf{e}_k\\
\mathbf{h_2} & = (h^k)^2\mathbf{e}_k
\end{align}</math>
 
Then what we're looking for are three scalars <math>\alpha</math>, <math>\beta</math> and <math>\gamma</math> such that:
 
<math>\alpha\mathbf{h0} + \beta\mathbf{h1} + \gamma\mathbf{h2} = \mathbf{w}</math>
 
To get for instance <math>\alpha</math> we can first make the <math>\beta</math> and <math>\gamma</math> terms disappear:
 
<math>\alpha\mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2} = \mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>
 
Noting <math>I = \mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>, we then get:
 
<math>\alpha = (\mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2})\cdot\tilde{I}/I\cdot\tilde{I}</math>
 
'''Note:''' a number of the formulae above are invisible to the majority of browsers, including Chrome, IE/Edge, Safari and Opera. They may (subject to the installation of necessary fronts) be visible to Firefox.
 
 
<lang perl6>use Clifford;
my @height = <1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70 1.73 1.75 1.78 1.80 1.83>;
my @weight = <52.21 53.12 54.48 55.84 57.20 58.57 59.93 61.29 63.11 64.47 66.28 68.10 69.92 72.19 74.46>;
 
my $w = [+] @weight Z* @e;
 
my $h0 = [+] @e[^@weight];
my $h1 = [+] @height Z* @e;
my $h2 = [+] (@height X** 2) Z* @e;
 
my $I = $h0∧$h1∧$h2;
my $I2 = ($I·$I.reversion).Real;
 
say "α = ", ($w∧$h1∧$h2)·$I.reversion/$I2;
say "β = ", ($w∧$h2∧$h0)·$I.reversion/$I2;
say "γ = ", ($w∧$h0∧$h1)·$I.reversion/$I2;</lang>
{{out}}
<pre>α = 128.81280357844
β = -143.1620228648
γ = 61.960325442</pre>
 
April 2016: This computation took over an hour with MoarVM, running in a VirtualBox linux system guest hosted by a windows laptop with a i7 intel processor.<br>
March 2020: With various improvements to the language, 6.d releases of Perl6/Raku now run the code 2x to 3x as fast, depending on the hardware used, but even so it is generous to describe it as 'slow'.
 
=={{header|Phix}}==
Line 1,490 ⟶ 1,436:
(array #[#[9 1/3] #[-3 1/3]])
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
We're going to solve the example on the Wikipedia article using [https://github.com/grondilu/clifford Clifford], a [https://en.wikipedia.org/wiki/Geometric_algebra geometric algebra] module. Optimization for large vector space does not quite work yet, so it's going to take (a lof of) time and a fair amount of memory, but it should work.
 
Let's create four vectors containing our input data:
 
<math>\begin{align}
\mathbf{w} & = w^k\mathbf{e}_k\\
\mathbf{h_0} & = (h^k)^0\mathbf{e}_k\\
\mathbf{h_1} & = (h^k)^1\mathbf{e}_k\\
\mathbf{h_2} & = (h^k)^2\mathbf{e}_k
\end{align}</math>
 
Then what we're looking for are three scalars <math>\alpha</math>, <math>\beta</math> and <math>\gamma</math> such that:
 
<math>\alpha\mathbf{h0} + \beta\mathbf{h1} + \gamma\mathbf{h2} = \mathbf{w}</math>
 
To get for instance <math>\alpha</math> we can first make the <math>\beta</math> and <math>\gamma</math> terms disappear:
 
<math>\alpha\mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2} = \mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>
 
Noting <math>I = \mathbf{h0}\wedge\mathbf{h1}\wedge\mathbf{h2}</math>, we then get:
 
<math>\alpha = (\mathbf{w}\wedge\mathbf{h1}\wedge\mathbf{h2})\cdot\tilde{I}/I\cdot\tilde{I}</math>
 
'''Note:''' a number of the formulae above are invisible to the majority of browsers, including Chrome, IE/Edge, Safari and Opera. They may (subject to the installation of necessary fronts) be visible to Firefox.
 
 
<lang perl6>use Clifford;
my @height = <1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70 1.73 1.75 1.78 1.80 1.83>;
my @weight = <52.21 53.12 54.48 55.84 57.20 58.57 59.93 61.29 63.11 64.47 66.28 68.10 69.92 72.19 74.46>;
 
my $w = [+] @weight Z* @e;
 
my $h0 = [+] @e[^@weight];
my $h1 = [+] @height Z* @e;
my $h2 = [+] (@height X** 2) Z* @e;
 
my $I = $h0∧$h1∧$h2;
my $I2 = ($I·$I.reversion).Real;
 
say "α = ", ($w∧$h1∧$h2)·$I.reversion/$I2;
say "β = ", ($w∧$h2∧$h0)·$I.reversion/$I2;
say "γ = ", ($w∧$h0∧$h1)·$I.reversion/$I2;</lang>
{{out}}
<pre>α = 128.81280357844
β = -143.1620228648
γ = 61.960325442</pre>
 
April 2016: This computation took over an hour with MoarVM, running in a VirtualBox linux system guest hosted by a windows laptop with a i7 intel processor.<br>
March 2020: With various improvements to the language, 6.d releases of Perl6/Raku now run the code 2x to 3x as fast, depending on the hardware used, but even so it is generous to describe it as 'slow'.
 
=={{header|Ruby}}==
10,327

edits