Statistics/Chi-squared distribution: Difference between revisions
Content added Content deleted
m (→{{header|Julia}}: removed duplicated function definition) |
(→{{header|Wren}}: Changed to a DOME app so we can do the stretch task.) |
||
Line 417: | Line 417: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|DOME}} |
|||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-trait}} |
|||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-plot}} |
|||
<syntaxhighlight lang="ecmascript">import " |
<syntaxhighlight lang="ecmascript">import "dome" for Window |
||
import "graphics" for Canvas, Color |
|||
import "./math2" for Math |
|||
import "./trait" for Stepped |
|||
import "./fmt" for Fmt |
import "./fmt" for Fmt |
||
import "./plot" for Axes |
|||
class Chi2 { |
class Chi2 { |
||
Line 454: | Line 461: | ||
System.print("\n Values for χ2 with 3 degrees of freedom") |
System.print("\n Values for χ2 with 3 degrees of freedom") |
||
System.print("χ2 cum |
System.print("χ2 cum pdf p-value") |
||
for (x in [1, 2, 4, 8, 16, 32]) { |
for (x in [1, 2, 4, 8, 16, 32]) { |
||
var cpdf = Chi2.cpdf(x, 3) |
var cpdf = Chi2.cpdf(x, 3) |
||
Line 472: | Line 479: | ||
Fmt.print(" diff sum : $f", dsum) |
Fmt.print(" diff sum : $f", dsum) |
||
Fmt.print(" d.o.f. : $d", dof) |
Fmt.print(" d.o.f. : $d", dof) |
||
Fmt.print(" χ2 value : $f", Chi2.pdf(dsum, |
Fmt.print(" χ2 value : $f", Chi2.pdf(dsum, dof)) |
||
Fmt.print(" p-value : $f", Chi2.cpdf(dsum, |
Fmt.print(" p-value : $f", Chi2.cpdf(dsum, dof)) |
||
// generate points for plot |
|||
var Pts = List.filled(5, null) |
|||
for (k in 0..4) { |
|||
Pts[k] = [] |
|||
var x = 0 |
|||
while (x < 10) { |
|||
Pts[k].add([x, 10 * Chi2.pdf(x, k)]) |
|||
x = x + 0.01 |
|||
} |
|||
} |
|||
class Main { |
|||
construct new() { |
|||
Window.title = "Chi-squared distribution for k in [0, 4]" |
|||
Canvas.resize(1000, 600) |
|||
Window.resize(1000, 600) |
|||
Canvas.cls(Color.white) |
|||
var axes = Axes.new(100, 500, 800, 400, -0.5..10, -0.5..5) |
|||
axes.draw(Color.black, 2) |
|||
var xMarks = 0..10 |
|||
var yMarks = 0..5 |
|||
axes.mark(xMarks, yMarks, Color.black, 2) |
|||
var xMarks2 = Stepped.new(0..10, 2) |
|||
var yMarks2 = 0..5 |
|||
axes.label(xMarks2, yMarks2, Color.black, 2, Color.black, 1, 10) |
|||
var colors = [Color.blue, Color.yellow, Color.green, Color.red, Color.purple] |
|||
for (k in 0..4) { |
|||
axes.lineGraph(Pts[k], colors[k], 2) |
|||
} |
|||
axes.rect(8, 5, 120, 110, Color.black) |
|||
for (k in 0..4) { |
|||
var y = 4.75 - k * 0.25 |
|||
axes.line(8.2, y, 9, y, colors[k], 2) |
|||
y = 385 - k * 18 |
|||
axes.print(750, y, k.toString, Color.black) |
|||
} |
|||
} |
|||
init() {} |
|||
update() {} |
|||
draw(alpha) {} |
|||
} |
|||
var Game = Main.new()</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
Terminal output: |
|||
<pre> |
<pre> |
||
Values of the χ2 probability distribution function |
Values of the χ2 probability distribution function |
||
Line 492: | Line 547: | ||
Values for χ2 with 3 degrees of freedom |
Values for χ2 with 3 degrees of freedom |
||
χ2 cum |
χ2 cum pdf p-value |
||
1 0.198748 0.801252 |
1 0.198748 0.801252 |
||
2 0.427593 0.572407 |
2 0.427593 0.572407 |
||
Line 506: | Line 561: | ||
p-value : 0.788850 |
p-value : 0.788850 |
||
</pre> |
</pre> |
||
[[File:Wren_chisquared.png|thumb|center]] |