sourcestd/Stats.xtl
1⍝# Stats: a small statistics library (a standard library, built into
2⍝# xetal). Import it with an alias of your choice: "s:" u_se< "Stats".
3⍝# It is a demo library, loaded only by that import: a small subset of
4⍝# X_eTaL-libraries' Statistics, which has much more (a program may use
5⍝# either).
6⍝# Names with l: are exported; names with h: are private to this file.
7
8⍝## Center
9
10⍝# The arithmetic mean, of Int or Float numbers.
11⍝# >> "s:" u_se< "Stats"
12⍝# >> s:m_ean 2 4 4 4 5 5 7 9
13⍝# 5.0
14ˡm̲ean ← [[f̲loat '+ r̲/] ÷ [f̲loat t̲ally]]
15
16⍝# Each number times itself.
17ʰs̲quare ← { ⍵ × ⍵ } ⍝ private: not visible outside
18⍝# How far each number is from the mean, as Floats.
19ʰd̲eviations ← [f̲loat − ˡm̲ean]
20
21⍝## Spread
22
23⍝# The variance: the mean squared deviation from the mean.
24⍝# >> "s:" u_se< "Stats"
25⍝# >> s:v_ariance 2 4 4 4 5 5 7 9
26⍝# 4.0
27ˡv̲ariance ← [ˡm̲ean [ʰs̲quare ʰd̲eviations]]
28
29⍝# The standard deviation: the square root of the variance.
30⍝# >> "s:" u_se< "Stats"
31⍝# >> s:s_d 2 4 4 4 5 5 7 9
32⍝# 2.0
33ˡs̲d ← { (ˡv̲ariance ⍵) ^ 0.5 }
34
35⍝# The range: the largest minus the smallest.
36⍝# >> "s:" u_se< "Stats"
37⍝# >> s:r_ange 2 4 4 4 5 5 7 9
38⍝# 7
39ˡr̲ange ← ['m̲ax r̲/ − 'm̲in r̲/]