Welch's t-test: Difference between revisions
Content added Content deleted
m (→Using GSL: Rm blank lines) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 69: | Line 69: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F betain(x, p, q) |
||
I p <= 0 | q <= 0 | x < 0 | x > 1 |
I p <= 0 | q <= 0 | x < 0 | x > 1 |
||
X ValueError(0) |
X ValueError(0) |
||
Line 139: | Line 139: | ||
V a1 = [Float(3), 4, 1, 2.1] |
V a1 = [Float(3), 4, 1, 2.1] |
||
V a2 = [Float(490.2), 340, 433.9] |
V a2 = [Float(490.2), 340, 433.9] |
||
print(welch_ttest(a1, a2))</ |
print(welch_ttest(a1, a2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 161: | Line 161: | ||
This shows how pvalue can be calculated from any two arrays, using Welch's 2-sided t-test, which doesn't assume equal variance. |
This shows how pvalue can be calculated from any two arrays, using Welch's 2-sided t-test, which doesn't assume equal variance. |
||
This is the equivalent of R's<code>t.test(vector1,vector2, alternative="two.sided", var.equal=FALSE)</code> and as such, it is compared against R's pvalues with the same vectors/arrays to show that the differences are very small (here 10^-14). |
This is the equivalent of R's<code>t.test(vector1,vector2, alternative="two.sided", var.equal=FALSE)</code> and as such, it is compared against R's pvalues with the same vectors/arrays to show that the differences are very small (here 10^-14). |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 428: | Line 428: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 443: | Line 443: | ||
'''If''' your computer does not have <code>lgammal</code>, add the following function before <code>main</code> and replace <code>lgammal</code> with <code>lngammal</code> in the <code>calculate_Pvalue</code> function: |
'''If''' your computer does not have <code>lgammal</code>, add the following function before <code>main</code> and replace <code>lgammal</code> with <code>lngammal</code> in the <code>calculate_Pvalue</code> function: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 463: | Line 463: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 470: | Line 470: | ||
Alternatively, the program shows the p-value computed using the IMSL '''BETAI''' function. |
Alternatively, the program shows the p-value computed using the IMSL '''BETAI''' function. |
||
< |
<syntaxhighlight lang="fortran">subroutine welch_ttest(n1, x1, n2, x2, t, df, p) |
||
use tdf_int |
use tdf_int |
||
implicit none |
implicit none |
||
Line 495: | Line 495: | ||
print *, t, df, p |
print *, t, df, p |
||
print *, betai(df / (t**2 + df), 0.5d0 * df, 0.5d0) |
print *, betai(df / (t**2 + df), 0.5d0 * df, 0.5d0) |
||
end program</ |
end program</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 505: | Line 505: | ||
With Absoft Pro Fortran, compile with <code>af90 -m64 pvalue.f90 %SLATEC_LINK%</code>. |
With Absoft Pro Fortran, compile with <code>af90 -m64 pvalue.f90 %SLATEC_LINK%</code>. |
||
< |
<syntaxhighlight lang="fortran">subroutine welch_ttest(n1, x1, n2, x2, t, df, p) |
||
implicit none |
implicit none |
||
integer :: n1, n2 |
integer :: n1, n2 |
||
Line 530: | Line 530: | ||
call welch_ttest(4, x, 3, y, t, df, p) |
call welch_ttest(4, x, 3, y, t, df, p) |
||
print *, t, df, p |
print *, t, df, p |
||
end program</ |
end program</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 542: | Line 542: | ||
Instead of implementing the t-distribution by ourselves, we bind to GNU Scientific Library: |
Instead of implementing the t-distribution by ourselves, we bind to GNU Scientific Library: |
||
< |
<syntaxhighlight lang="fortran">module t_test_m |
||
use, intrinsic :: iso_c_binding |
use, intrinsic :: iso_c_binding |
||
Line 596: | Line 596: | ||
print *, t, df, p |
print *, t, df, p |
||
end program main</ |
end program main</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 603: | Line 603: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 694: | Line 694: | ||
func(r float64) float64 { return math.Pow(r, ν/2-1) / math.Sqrt(1-r) }) / |
func(r float64) float64 { return math.Pow(r, ν/2-1) / math.Sqrt(1-r) }) / |
||
math.Exp(g1+g2-g3) |
math.Exp(g1+g2-g3) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 708: | Line 708: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">integrate=: adverb define |
||
'a b steps'=. 3{.y,128 |
'a b steps'=. 3{.y,128 |
||
size=. (b - a)%steps |
size=. (b - a)%steps |
||
Line 731: | Line 731: | ||
hi=. v%(t^2)+v |
hi=. v%(t^2)+v |
||
(F f. simpson integrate lo,hi) % 0.5 B v%2 |
(F f. simpson integrate lo,hi) % 0.5 B v%2 |
||
)</ |
)</syntaxhighlight> |
||
<code>integrate</code> and <code>simpson</code> are from the [[Numerical_integration#J|Numerical integration]] task. |
<code>integrate</code> and <code>simpson</code> are from the [[Numerical_integration#J|Numerical integration]] task. |
||
Line 748: | Line 748: | ||
Data for task examples: |
Data for task examples: |
||
< |
<syntaxhighlight lang="j">d1=: 27.5 21 19 23.6 17 17.9 16.9 20.1 21.9 22.6 23.1 19.6 19 21.7 21.4 |
||
d2=: 27.1 22 20.8 23.4 23.4 23.5 25.8 22 24.8 20.2 21.9 22.1 22.9 20.5 24.4 |
d2=: 27.1 22 20.8 23.4 23.4 23.5 25.8 22 24.8 20.2 21.9 22.1 22.9 20.5 24.4 |
||
d3=: 17.2 20.9 22.6 18.1 21.7 21.4 23.5 24.2 14.7 21.8 |
d3=: 17.2 20.9 22.6 18.1 21.7 21.4 23.5 24.2 14.7 21.8 |
||
Line 757: | Line 757: | ||
d8=: 29.89 29.93 29.72 29.98 30.02 29.98 |
d8=: 29.89 29.93 29.72 29.98 30.02 29.98 |
||
d9=: 3 4 1 2.1 |
d9=: 3 4 1 2.1 |
||
da=: 490.2 340 433.9</ |
da=: 490.2 340 433.9</syntaxhighlight> |
||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="j"> d1 p2_tail d2 |
||
0.021378 |
0.021378 |
||
d3 p2_tail d4 |
d3 p2_tail d4 |
||
Line 769: | Line 769: | ||
0.0907733 |
0.0907733 |
||
d9 p2_tail da |
d9 p2_tail da |
||
0.0107377</ |
0.0107377</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Using the '''[http://commons.apache.org/proper/commons-math/ Apache Commons Mathematics Library]'''. |
Using the '''[http://commons.apache.org/proper/commons-math/ Apache Commons Mathematics Library]'''. |
||
< |
<syntaxhighlight lang="java">import org.apache.commons.math3.distribution.TDistribution; |
||
public class WelchTTest { |
public class WelchTTest { |
||
Line 822: | Line 822: | ||
System.out.println("p = " + res[2]); |
System.out.println("p = " + res[2]); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
'''Result''' |
'''Result''' |
||
Line 840: | Line 840: | ||
jq's `lgamma` returns the natural logarithm of the absolute value of the gamma function of x. |
jq's `lgamma` returns the natural logarithm of the absolute value of the gamma function of x. |
||
< |
<syntaxhighlight lang="jq">def mean: add / length; |
||
# Sample variance using division by (length-1) |
# Sample variance using division by (length-1) |
||
Line 900: | Line 900: | ||
pValue(d5; d6), |
pValue(d5; d6), |
||
pValue(d7; d8), |
pValue(d7; d8), |
||
pValue(x; y)</ |
pValue(x; y)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 914: | Line 914: | ||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">using HypothesisTests |
||
d1 = [27.5, 21.0, 19.0, 23.6, 17.0, 17.9, 16.9, 20.1, 21.9, 22.6, 23.1, 19.6, 19.0, 21.7, 21.4] |
d1 = [27.5, 21.0, 19.0, 23.6, 17.0, 17.9, 16.9, 20.1, 21.9, 22.6, 23.1, 19.6, 19.0, 21.7, 21.4] |
||
Line 934: | Line 934: | ||
ttest = UnequalVarianceTTest(y1, y2) |
ttest = UnequalVarianceTTest(y1, y2) |
||
println("\nData:\n y1 = $y1\n y2 = $y2\nP-value for unequal variance TTest: ", round(pvalue(ttest), 4)) |
println("\nData:\n y1 = $y1\n y2 = $y2\nP-value for unequal variance TTest: ", round(pvalue(ttest), 4)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 966: | Line 966: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
This program brings in code from other tasks for gamma functions and integration by Simpson's rule as Kotlin doesn't have these built-in: |
This program brings in code from other tasks for gamma functions and integration by Simpson's rule as Kotlin doesn't have these built-in: |
||
< |
<syntaxhighlight lang="scala">// version 1.1.4-3 |
||
typealias Func = (Double) -> Double |
typealias Func = (Double) -> Double |
||
Line 1,071: | Line 1,071: | ||
println(f.format(p2Tail(da7, da8))) |
println(f.format(p2Tail(da7, da8))) |
||
println(f.format(p2Tail(x, y))) |
println(f.format(p2Tail(x, y))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,084: | Line 1,084: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import math, stats, strutils, sugar |
||
func sqr(f: float): float = f * f |
func sqr(f: float): float = f * f |
||
Line 1,141: | Line 1,141: | ||
echo p2Tail(Da5, Da6).formatFloat(ffDecimal, 6) |
echo p2Tail(Da5, Da6).formatFloat(ffDecimal, 6) |
||
echo p2Tail(Da7, Da8).formatFloat(ffDecimal, 6) |
echo p2Tail(Da7, Da8).formatFloat(ffDecimal, 6) |
||
echo p2Tail(X, Y).formatFloat(ffDecimal, 6)</ |
echo p2Tail(X, Y).formatFloat(ffDecimal, 6)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,152: | Line 1,152: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">WelschTTest:=proc(x::list(numeric),y::list(numeric)) |
||
uses Statistics; |
uses Statistics; |
||
local n1:=nops(x),n2:=nops(y), |
local n1:=nops(x),n2:=nops(y), |
||
Line 1,167: | Line 1,167: | ||
y:=[490.2,340,433.9]: |
y:=[490.2,340,433.9]: |
||
WelschTTest(x,y); |
WelschTTest(x,y); |
||
# -9.55949772193266, 2.00085234885628, 0.0107515611497845</ |
# -9.55949772193266, 2.00085234885628, 0.0107515611497845</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
{{trans|Stata}} |
{{trans|Stata}} |
||
< |
<syntaxhighlight lang="octave">x = [3.0,4.0,1.0,2.1]; |
||
y = [490.2,340.0,433.9]; |
y = [490.2,340.0,433.9]; |
||
n1 = length(x); |
n1 = length(x); |
||
Line 1,184: | Line 1,184: | ||
ans = |
ans = |
||
-9.559498 2.000852 0.010752</ |
-9.559498 2.000852 0.010752</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">B2(x,y)=exp(lngamma(x)+lngamma(y)-lngamma(x+y)) |
||
B3(x,a,b)=a--;b--;intnum(r=0,x,r^a*(1-r)^b) |
B3(x,a,b)=a--;b--;intnum(r=0,x,r^a*(1-r)^b) |
||
Welch2(u,v)=my(m1=vecsum(u)/#u, m2=vecsum(v)/#v, v1=var(u,m1), v2=var(v,m2), s=v1/#u+v2/#v, t=(m1-m2)/sqrt(s), nu=s^2/(v1^2/#u^2/(#u-1)+v2^2/#v^2/(#v-1))); B3(nu/(t^2+nu),nu/2,1/2)/B2(nu/2,1/2); |
Welch2(u,v)=my(m1=vecsum(u)/#u, m2=vecsum(v)/#v, v1=var(u,m1), v2=var(v,m2), s=v1/#u+v2/#v, t=(m1-m2)/sqrt(s), nu=s^2/(v1^2/#u^2/(#u-1)+v2^2/#v^2/(#v-1))); B3(nu/(t^2+nu),nu/2,1/2)/B2(nu/2,1/2); |
||
Welch2([3,4,1,2.1], [490.2,340,433.9])</ |
Welch2([3,4,1,2.1], [490.2,340,433.9])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = 0.010751561149784496723954539777213062928</pre> |
<pre>%1 = 0.010751561149784496723954539777213062928</pre> |
||
Line 1,198: | Line 1,198: | ||
Uses Math::AnyNum for gamma and pi. It is possible to use some other modules (e.g. Math::Cephes) if Math::AnyNum has problematic dependencies. |
Uses Math::AnyNum for gamma and pi. It is possible to use some other modules (e.g. Math::Cephes) if Math::AnyNum has problematic dependencies. |
||
{{trans|Sidef}} |
{{trans|Sidef}} |
||
< |
<syntaxhighlight lang="perl">use utf8; |
||
use List::Util qw(sum); |
use List::Util qw(sum); |
||
use Math::AnyNum qw(gamma pi); |
use Math::AnyNum qw(gamma pi); |
||
Line 1,258: | Line 1,258: | ||
my ($left, $right) = splice(@tests, 0, 2); |
my ($left, $right) = splice(@tests, 0, 2); |
||
print p_value($left, $right), "\n"; |
print p_value($left, $right), "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,271: | Line 1,271: | ||
We use a slightly more accurate lgamma than the C code. Note that Perl can be compiled with different underlying floating point representations -- double, long double, or quad double. |
We use a slightly more accurate lgamma than the C code. Note that Perl can be compiled with different underlying floating point representations -- double, long double, or quad double. |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use List::Util 'sum'; |
use List::Util 'sum'; |
||
Line 1,398: | Line 1,398: | ||
printf("p-value = %.14g\n",$pvalue); |
printf("p-value = %.14g\n",$pvalue); |
||
} |
} |
||
printf("cumulative error is %g\n", $error);</ |
printf("cumulative error is %g\n", $error);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>p-value = 0.021378001462867 |
<pre>p-value = 0.021378001462867 |
||
Line 1,413: | Line 1,413: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> |
||
Line 1,521: | Line 1,521: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">pValue</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">pValue</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,533: | Line 1,533: | ||
The above was a bit off on the fifth test, so I also tried this.<br> |
The above was a bit off on the fifth test, so I also tried this.<br> |
||
using gamma() from [[Gamma_function#Phix]] (the one from above is probably also fine, but I didn't test that) |
using gamma() from [[Gamma_function#Phix]] (the one from above is probably also fine, but I didn't test that) |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #000080;font-style:italic;">--<copy of gamma from Gamma_function#Phix></span> |
<span style="color: #000080;font-style:italic;">--<copy of gamma from Gamma_function#Phix></span> |
||
Line 1,650: | Line 1,650: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"cumulative error"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cerr</span><span style="color: #0000FF;">}</span> |
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"cumulative error"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cerr</span><span style="color: #0000FF;">}</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,668: | Line 1,668: | ||
=== Using NumPy & SciPy === |
=== Using NumPy & SciPy === |
||
< |
<syntaxhighlight lang="python">import numpy as np |
||
import scipy as sp |
import scipy as sp |
||
import scipy.stats |
import scipy.stats |
||
Line 1,685: | Line 1,685: | ||
welch_ttest(np.array([3.0, 4.0, 1.0, 2.1]), np.array([490.2, 340.0, 433.9])) |
welch_ttest(np.array([3.0, 4.0, 1.0, 2.1]), np.array([490.2, 340.0, 433.9])) |
||
(-9.559497721932658, 2.0008523488562844, 0.01075156114978449)</ |
(-9.559497721932658, 2.0008523488562844, 0.01075156114978449)</syntaxhighlight> |
||
=== Using betain from AS 63 === |
=== Using betain from AS 63 === |
||
First, the implementation of betain (translated from the Stata program in the discussion page). The original Fortran code is under copyrighted by the Royal Statistical Society. The C translation is under GPL, written by John Burkardt. The exact statement of the RSS license is unclear. |
First, the implementation of betain (translated from the Stata program in the discussion page). The original Fortran code is under copyrighted by the Royal Statistical Society. The C translation is under GPL, written by John Burkardt. The exact statement of the RSS license is unclear. |
||
< |
<syntaxhighlight lang="python">import math |
||
def betain(x, p, q): |
def betain(x, p, q): |
||
Line 1,739: | Line 1,739: | ||
else: |
else: |
||
temp = psq |
temp = psq |
||
psq += 1</ |
psq += 1</syntaxhighlight> |
||
The Python code is then straightforward: |
The Python code is then straightforward: |
||
< |
<syntaxhighlight lang="python">import math |
||
def welch_ttest(a1, a2): |
def welch_ttest(a1, a2): |
||
Line 1,761: | Line 1,761: | ||
p = betain(df / (t**2 + df), df / 2, 1 / 2) |
p = betain(df / (t**2 + df), df / 2, 1 / 2) |
||
return t, df, p</ |
return t, df, p</syntaxhighlight> |
||
'''Example''' |
'''Example''' |
||
< |
<syntaxhighlight lang="python">a1 = [3, 4, 1, 2.1] |
||
a2 = [490.2, 340, 433.9] |
a2 = [490.2, 340, 433.9] |
||
print(welch_ttest(a1, a2))</ |
print(welch_ttest(a1, a2))</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 1,773: | Line 1,773: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">#!/usr/bin/R |
||
printf <- function(...) cat(sprintf(...)) |
printf <- function(...) cat(sprintf(...)) |
||
Line 1,810: | Line 1,810: | ||
results <- t.test(z1,z2, alternative="two.sided", var.equal=FALSE) |
results <- t.test(z1,z2, alternative="two.sided", var.equal=FALSE) |
||
printf("%.15g\n", results$p.value); |
printf("%.15g\n", results$p.value); |
||
</syntaxhighlight> |
|||
</lang> |
|||
The output here is used to compare against C's output above. |
The output here is used to compare against C's output above. |
||
Line 1,827: | Line 1,827: | ||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require math/statistics math/special-functions) |
(require math/statistics math/special-functions) |
||
Line 1,876: | Line 1,876: | ||
(p-value (list 3.0 4.0 1.0 2.1) |
(p-value (list 3.0 4.0 1.0 2.1) |
||
(list 490.2 340.0 433.9))))</ |
(list 490.2 340.0 433.9))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,890: | Line 1,890: | ||
Perhaps "inspired by C example" may be more accurate. Gamma subroutine from [[Gamma_function#Raku|Gamma function task]]. |
Perhaps "inspired by C example" may be more accurate. Gamma subroutine from [[Gamma_function#Raku|Gamma function task]]. |
||
<lang |
<syntaxhighlight lang="raku" line>sub Γ(\z) { |
||
constant g = 9; |
constant g = 9; |
||
z < .5 ?? π / sin(π × z) / Γ(1 - z) !! |
z < .5 ?? π / sin(π × z) / Γ(1 - z) !! |
||
Line 1,955: | Line 1,955: | ||
[<3.0 4.0 1.0 2.1>], |
[<3.0 4.0 1.0 2.1>], |
||
[<490.2 340.0 433.9>] |
[<490.2 340.0 433.9>] |
||
) -> @left, @right { say p-value @left, @right }</ |
) -> @left, @right { say p-value @left, @right }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0.0213780014628669 |
<pre>0.0213780014628669 |
||
Line 1,971: | Line 1,971: | ||
This uses the Soper reduction formula to evaluate the integral, which converges much more quickly than Simpson's formula. |
This uses the Soper reduction formula to evaluate the integral, which converges much more quickly than Simpson's formula. |
||
<lang |
<syntaxhighlight lang="raku" line>sub lgamma ( Num(Real) \n --> Num ){ |
||
use NativeCall; |
use NativeCall; |
||
sub lgamma (num64 --> num64) is native {} |
sub lgamma (num64 --> num64) is native {} |
||
Line 2,075: | Line 2,075: | ||
$error += abs($p-value - shift @answers); |
$error += abs($p-value - shift @answers); |
||
} |
} |
||
printf("cumulative error is %g\n", $error);</ |
printf("cumulative error is %g\n", $error);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>p-value = 0.021378001462867 |
<pre>p-value = 0.021378001462867 |
||
Line 2,089: | Line 2,089: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">def calculate_p_value(array1, array2) |
||
return 1.0 if array1.size <= 1 |
return 1.0 if array1.size <= 1 |
||
return 1.0 if array2.size <= 1 |
return 1.0 if array2.size <= 1 |
||
Line 2,214: | Line 2,214: | ||
printf("the cumulative error is %g\n", error) |
printf("the cumulative error is %g\n", error) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,230: | Line 2,230: | ||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
{{trans|Stata}} |
{{trans|Stata}} |
||
<lang>data tbl; |
<syntaxhighlight lang="text">data tbl; |
||
input value group @@; |
input value group @@; |
||
cards; |
cards; |
||
Line 2,240: | Line 2,240: | ||
class group; |
class group; |
||
var value; |
var value; |
||
run;</ |
run;</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,401: | Line 2,401: | ||
Implementation in IML: |
Implementation in IML: |
||
< |
<syntaxhighlight lang="sas">proc iml; |
||
use tbl; |
use tbl; |
||
read all var {value} into x where(group=1); |
read all var {value} into x where(group=1); |
||
Line 2,414: | Line 2,414: | ||
p = 2*probt(-abs(t), df); |
p = 2*probt(-abs(t), df); |
||
print t df p; |
print t df p; |
||
quit;</ |
quit;</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,421: | Line 2,421: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">import org.apache.commons.math3.distribution.TDistribution |
||
object WelchTTest extends App { |
object WelchTTest extends App { |
||
Line 2,454: | Line 2,454: | ||
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]") |
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
Line 2,461: | Line 2,461: | ||
Scilab will print a warning because the number of degrees of freedom is not an integer. However, the underlying implementation makes use of the [http://www.netlib.org/random/ dcdflib] Fortran library, which happily accepts a noninteger df. |
Scilab will print a warning because the number of degrees of freedom is not an integer. However, the underlying implementation makes use of the [http://www.netlib.org/random/ dcdflib] Fortran library, which happily accepts a noninteger df. |
||
<lang>x = [3.0,4.0,1.0,2.1]; |
<syntaxhighlight lang="text">x = [3.0,4.0,1.0,2.1]; |
||
y = [490.2,340.0,433.9]; |
y = [490.2,340.0,433.9]; |
||
n1 = length(x); |
n1 = length(x); |
||
Line 2,470: | Line 2,470: | ||
df = (v1/n1+v2/n2)^2/(v1^2/(n1^2*(n1-1))+v2^2/(n2^2*(n2-1))); |
df = (v1/n1+v2/n2)^2/(v1^2/(n1^2*(n1-1))+v2^2/(n2^2*(n2-1))); |
||
[p, q] = cdft("PQ", -abs(t), df); |
[p, q] = cdft("PQ", -abs(t), df); |
||
[t df 2*p]</ |
[t df 2*p]</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,480: | Line 2,480: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func p_value (A, B) { |
||
[A.len, B.len].all { _ > 1 } || return 1 |
[A.len, B.len].all { _ > 1 } || return 1 |
||
Line 2,532: | Line 2,532: | ||
tests.each_slice(2, {|left, right| |
tests.each_slice(2, {|left, right| |
||
say p_value(left, right) |
say p_value(left, right) |
||
})</ |
})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,547: | Line 2,547: | ||
Notice that here we use the option '''unequal''' of the '''ttest''' command, and not '''welch''', so that Stata uses the Welch-Satterthwaite approximation. |
Notice that here we use the option '''unequal''' of the '''ttest''' command, and not '''welch''', so that Stata uses the Welch-Satterthwaite approximation. |
||
< |
<syntaxhighlight lang="stata">mat a=(3,4,1,2.1,490.2,340,433.9\1,1,1,1,2,2,2)' |
||
clear |
clear |
||
svmat double a |
svmat double a |
||
Line 2,574: | Line 2,574: | ||
di r(p) |
di r(p) |
||
.01075156</ |
.01075156</syntaxhighlight> |
||
The computation can easily be implemented in Mata. Here is how to compute the t statistic (t), the approximate degrees of freedom (df) and the p-value (p). |
The computation can easily be implemented in Mata. Here is how to compute the t statistic (t), the approximate degrees of freedom (df) and the p-value (p). |
||
< |
<syntaxhighlight lang="stata">st_view(a=., ., .) |
||
x = select(a[., 1], a[., 2] :== 1) |
x = select(a[., 1], a[., 2] :== 1) |
||
y = select(a[., 1], a[., 2] :== 2) |
y = select(a[., 1], a[., 2] :== 2) |
||
Line 2,592: | Line 2,592: | ||
+----------------------------------------------+ |
+----------------------------------------------+ |
||
1 | -9.559497722 2.000852349 .0107515611 | |
1 | -9.559497722 2.000852349 .0107515611 | |
||
+----------------------------------------------+</ |
+----------------------------------------------+</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 2,602: | Line 2,602: | ||
This is not particularly idiomatic Tcl, but perhaps illustrates some of the language's relationship with the Lisp family. |
This is not particularly idiomatic Tcl, but perhaps illustrates some of the language's relationship with the Lisp family. |
||
< |
<syntaxhighlight lang="tcl">#!/usr/bin/tclsh |
||
package require math::statistics |
package require math::statistics |
||
Line 2,677: | Line 2,677: | ||
puts [pValue $left $right] |
puts [pValue $left $right] |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,691: | Line 2,691: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Math, Nums |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 2,748: | Line 2,748: | ||
Fmt.print("$0.6f", pValue.call(d5, d6)) |
Fmt.print("$0.6f", pValue.call(d5, d6)) |
||
Fmt.print("$0.6f", pValue.call(d7, d8)) |
Fmt.print("$0.6f", pValue.call(d7, d8)) |
||
Fmt.print("$0.6f", pValue.call(x, y))</ |
Fmt.print("$0.6f", pValue.call(x, y))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,761: | Line 2,761: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="zkl">fcn calculate_Pvalue(array1,array2){ |
||
if (array1.len()<=1 or array2.len()<=1) return(1.0); |
if (array1.len()<=1 or array2.len()<=1) return(1.0); |
||
Line 2,811: | Line 2,811: | ||
foreach x in (cof){ ser+=(x/(y+=1)); } |
foreach x in (cof){ ser+=(x/(y+=1)); } |
||
return((2.5066282746310005 * ser / x).log() - tmp); |
return((2.5066282746310005 * ser / x).log() - tmp); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">testSets:=T( |
||
T(T(27.5,21.0,19.0,23.6,17.0,17.9,16.9,20.1,21.9,22.6,23.1,19.6,19.0,21.7,21.4), |
T(T(27.5,21.0,19.0,23.6,17.0,17.9,16.9,20.1,21.9,22.6,23.1,19.6,19.0,21.7,21.4), |
||
T(27.1,22.0,20.8,23.4,23.4,23.5,25.8,22.0,24.8,20.2,21.9,22.1,22.9,20.5,24.4)), |
T(27.1,22.0,20.8,23.4,23.4,23.5,25.8,22.0,24.8,20.2,21.9,22.1,22.9,20.5,24.4)), |
||
Line 2,824: | Line 2,824: | ||
foreach x,y in (testSets) |
foreach x,y in (testSets) |
||
{ println("Test set 1 p-value = %f".fmt(calculate_Pvalue(x,y))); }</ |
{ println("Test set 1 p-value = %f".fmt(calculate_Pvalue(x,y))); }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |