| Bytes | Lang | Time | Link |
|---|---|---|---|
| 026 | APLNARS | 250501T102230Z | Rosario |
| 115 | Tcl | 181214T200608Z | sergiol |
| 020 | CASIO BASIC CASIO fx9750GIII | 250425T184331Z | madeforl |
| 034 | Factor | 220413T083540Z | chunes |
| 019 | Charcoal | 181215T000210Z | Neil |
| 037 | Julia 0.7 | 181219T100150Z | Kirill L |
| 229 | SNOBOL4 CSNOBOL4 | 181217T152905Z | Giuseppe |
| 007 | MathGolf | 181217T145521Z | maxb |
| 019 | Pyth | 181216T112336Z | Sok |
| 009 | 05AB1E | 181217T085156Z | Kevin Cr |
| 011 | TIBasic 83 series | 181216T212554Z | Misha La |
| 068 | Haskell | 181216T062834Z | Jon Purd |
| 029 | APL Dyalog Unicode | 181214T190954Z | Quintec |
| 026 | MATLAB | 181216T153726Z | aaaaa sa |
| 030 | APL+WIN | 181214T190358Z | Graham |
| 019 | APL Dyalog Classic | 181216T104629Z | ngn |
| 023 | K oK | 181215T103359Z | Galen Iv |
| 059 | Haskell | 181216T072431Z | xnor |
| 046 | Python 3 + numpy | 181215T122942Z | ovs |
| 022 | J | 181215T094915Z | Galen Iv |
| 025 | Mathematica | 181215T002610Z | LegionMa |
| 079 | JavaScript ES7 | 181214T185935Z | Arnauld |
| 037 | R | 181214T193444Z | Robert S |
| 010 | Jelly | 181214T203147Z | lirtosia |
| 052 | R + pryr | 181214T193621Z | Sumner18 |
| 010 | MATL | 181214T193339Z | Luis Men |
| nan | 181214T184037Z | flawr | |
| 010 | Jelly | 181214T185709Z | Erik the |
APL(NARS), 26 chars
{m←÷≢⍵⋄d÷√m×+/×⍨d←⍵-m×+/⍵}
test:
f←{m←÷≢⍵⋄d÷√m×+/×⍨d←⍵-m×+/⍵}
f 1 2
¯1 1
f 1 2 3
¯1.224744871 0 1.224744871
f ¯3 1 4 1 5
¯1.642857143 ¯0.2142857143 0.8571428571 ¯0.2142857143 1.214285714
Tcl, 115 bytes
proc S L {lmap c $L {expr ($c-[set m ([join $L +])/[set n [llength $L]].])/sqrt((([join $L -$m)**2+(]-$m)**2)/$n)}}
CASIO BASIC (CASIO fx-9750GIII), 20 bytes
?→List1
1-Variable List1
(List1-x̄)÷σx
builtins
Factor, 34 bytes
[ dup demean swap 0 std-ddof v/n ]
Sadly, while Factor has the z-score word, it uses the sample standard deviation instead of the population standard deviation.
Charcoal, 25 19 bytes
≧⁻∕ΣθLθθI∕θ₂∕ΣXθ²Lθ
Try it online! Link is to verbose version of code. Explanation:
θ Input array
≧ Update each element
⁻ Subtract
Σ Sum of
θ Input array
∕ Divided by
L Length of
θ Input array
Calculate \$\mu\$ and vectorised subtract it from each \$x_i\$.
θ Updated array
∕ Vectorised divided by
₂ Square root of
Σ Sum of
θ Updated array
X Vectorised to power
² Literal 2
∕ Divided by
L Length of
θ Array
I Cast to string
Implicitly print each element on its own line.
Calculate \$\sigma\$, vectorised divide each \$x_i\$ by it, and output the result.
Edit: Saved 6 bytes thanks to @ASCII-only for a) using SquareRoot() instead of Power(0.5) b) fixing vectorised Divide() (it was doing IntDivide() instead) c) making Power() vectorise.
SNOBOL4 (CSNOBOL4), 229 bytes
DEFINE('Z(A)')
Z X =X + 1
M =M + A<X> :S(Z)
N =X - 1.
M =M / N
D X =GT(X) X - 1 :F(S)
A<X> =A<X> - M :(D)
S X =LT(X,N) X + 1 :F(Y)
S =S + A<X> ^ 2 / N :(S)
Y S =S ^ 0.5
N A<X> =A<X> / S
X =GT(X) X - 1 :S(N)
Z =A :(RETURN)
Link is to a functional version of the code which constructs an array from STDIN given its length and then its elements, then runs the function Z on that, and finally prints out the values.
Defines a function Z which returns an array.
The 1. on line 4 is necessary to do the floating point arithmetic properly.
MathGolf, 7 bytes
▓-_²▓√/
Explanation
This is literally a byte-for-byte recreation of Kevin Cruijssen's 05AB1E answer, but I save some bytes from MathGolf having 1-byters for everything needed for this challenge. Also the answer looks quite good in my opinion!
▓ get average of list
- pop a, b : push(a-b)
_ duplicate TOS
² pop a : push(a*a)
▓ get average of list
√ pop a : push(sqrt(a)), split string to list
/ pop a, b : push(a/b), split strings
Pyth, 21 19 bytes
mc-dJ.OQ@.Om^-Jk2Q2
Try it online here.
mc-dJ.OQ@.Om^-Jk2Q2Q Implicit: Q=eval(input())
Trailing Q inferred
J.OQ Take the average of Q, store the result in J
m Q Map the elements of Q, as k, using:
-Jk Difference between J and k
^ 2 Square it
.O Find the average of the result of the map
@ 2 Square root it
- this is the standard deviation of Q
m Q Map elements of Q, as d, using:
-dJ d - J
c Float division by the standard deviation
Implicit print result of map
Edit: after seeing Kevin's answer, changed to use the average builtin for the inner results. Previous answer: mc-dJ.OQ@csm^-Jk2QlQ2
05AB1E, 9 bytes
ÅA-DnÅAt/
Port of @Arnauld's JavaScript answer, so make sure to upvote him!
Try it online or verify all test cases.
Explanation:
ÅA # Calculate the mean of the (implicit) input
# i.e. [-3,1,4,1,5] → 1.6
- # Subtract it from each value in the (implicit) input
# i.e. [-3,1,4,1,5] and 1.6 → [-4.6,-0.6,2.4,-0.6,3.4]
D # Duplicate that list
n # Take the square of each
# i.e. [-4.6,-0.6,2.4,-0.6,3.4] → [21.16,0.36,5.76,0.36,11.56]
ÅA # Pop and calculate the mean of that list
# i.e. [21.16,0.36,5.76,0.36,11.56] → 7.84
t # Take the square-root of that
# i.e. 7.84 → 2.8
/ # And divide each value in the duplicated list with it (and output implicitly)
# i.e. [-4.6,-0.6,2.4,-0.6,3.4] and 2.8 → [-1.6428571428571428,
# -0.21428571428571433,0.8571428571428572,-0.21428571428571433,1.2142857142857144]
TI-Basic (83 series), 14 11 bytes
Ans-mean(Ans
Ans/√(mean(Ans²
Takes input in Ans. For example, if you type the above into prgmSTANDARD, then {1,2,3}:prgmSTANDARD will return {-1.224744871,0.0,1.224744871}.
Previously, I tried using the 1-Var Stats command, which stores the population standard deviation in σx, but it's less trouble to compute it manually.
Haskell, 80 75 68 bytes
t x=k(/sqrt(f$sum$k(^2)))where k g=g.(-f(sum x)+)<$>x;f=(/sum(1<$x))
Thanks to @flawr for the suggestions to use sum(1<$x) instead of sum[1|_<-x] and to inline the mean, @xnor for inlining the standard deviation and other reductions.
Expanded:
-- Standardize a list of values of any floating-point type.
standardize :: Floating a => [a] -> [a]
standardize input = eachLessMean (/ sqrt (overLength (sum (eachLessMean (^2)))))
where
-- Map a function over each element of the input, less the mean.
eachLessMean f = map (f . subtract (overLength (sum input))) input
-- Divide a value by the length of the input.
overLength n = n / sum (map (const 1) input)
MATLAB, 26 bytes
Trivial-ish, std(,1) for using population standard deviation
f=@(x)(x-mean(x))/std(x,1)
APL+WIN, 41,32 30 bytes
9 bytes saved thanks to Erik + 2 more thanks to ngn
x←v-(+/v)÷⍴v←⎕⋄x÷(+/x×x÷⍴v)*.5
Prompts for vector of numbers and calculates mean standard deviation and standardised elements of input vector
APL (Dyalog Classic), 21 20 19 bytes
(-÷.5*⍨⊢÷⌹×≢)+/-⊢×≢
⊢÷⌹ is sum of squares
⊢÷⌹×≢ is sum of squares divided by length
K (oK), 33 23 bytes
-10 bytes thanks to ngn!
{t%%(+/t*t:x-/x%#x)%#x}
First attempt at coding (I don't dare to name it "golfing") in K. I'm pretty sure it can be done much better (too many variable names here...)
Haskell, 59 bytes
(%)i=sum.map(^i)
f l=[(0%l*y-1%l)/sqrt(2%l*0%l-1%l^2)|y<-l]
Doesn't use libraries.
The helper function % computes the sum of ith powers of a list, which lets us get three useful values.
0%lis the length ofl(call thisn)1%lis the sum ofl(call thiss)2%lis the sum of squares ofl(call thism)
We can express the z-score of an element y as
(n*y-s)/sqrt(n*v-s^2)
(This is the expression (y-s/n)/sqrt(v/n-(s/n)^2) simplified a bit by multiplying the top and bottom by n.)
We can insert the expressions 0%l, 1%l, 2%l without parens because the % we define has higher precedence than the arithmetic operators.
(%)i=sum.map(^i) is the same length as i%l=sum.map(^i)l. Making it more point-free doesn't help. Defining it like g i=... loses bytes when we call it. Although % works for any list but we only call it with the problem input list, there's no byte loss in calling it with argument l every time because a two-argument call i%l is no longer than a one-argument one g i.
J, 22 bytes
-1 byte thanks to Cows quack!
(-%[:%:1#.-*-%#@[)+/%#
J, 31 23 bytes
(-%[:%:#@[%~1#.-*-)+/%#
+/%# - mean (sum (+/) divided (%) by the number of samples (#))
( ) - the list is a left argument here (we have a hook)
- - the difference between each sample and the mean
* - multiplied by
- - the difference between each sample and the mean
1#. - sum by base-1 conversion
%~ - divided by
#@[ - the length of the samples list
%: - square root
[: - convert to a fork (function composition)
- - subtract the mean from each sample
% - and divide it by sigma
Mathematica, 25 bytes
Mean[(a=#-Mean@#)a]^-.5a&
Pure function. Takes a list of numbers as input and returns a list of machine-precision numbers as output. Note that the built-in Standardize function uses the sample variance by default.
JavaScript (ES7), 80 79 bytes
a=>a.map(x=>(x-g(a))/g(a.map(x=>(x-m)**2))**.5,g=a=>m=eval(a.join`+`)/a.length)
Commented
a => // given the input array a[]
a.map(x => // for each value x in a[]:
(x - g(a)) / // compute (x - mean(a)) divided by
g( // the standard deviation:
a.map(x => // for each value x in a[]:
(x - m) ** 2 // compute (x - mean(a))²
) // compute the mean of this array
) ** .5, // and take the square root
g = a => // g = helper function taking an array a[],
m = eval(a.join`+`) // computing the mean
/ a.length // and storing the result in m
) // end of outer map()
Jelly, 10 bytes
_ÆmµL½÷ÆḊ×
It's not any shorter, but Jelly's determinant function ÆḊ also calculates vector norm.
_Æm x - mean(x)
µ then:
L½ Square root of the Length
÷ÆḊ divided by the norm
× Multiply by that value
R + pryr, 53 52 bytes
-1 byte using sum(x|1) instead of length(x) as seen in @Robert S.'s solution
pryr::f((x-(y<-mean(x)))/(sum((x-y)^2)/sum(x|1))^.5)
For being a language built for statisticians, I'm amazed that this doesn't have a built-in function. At least not one that I could find. Even the function mosaic::zscore doesn't yield the expected results. This is likely due to using the population standard deviation instead of sample standard deviation.
MATL, 10 bytes
tYm-t&1Zs/
Explanation
t % Implicit input
% Duplicate
Ym % Mean
- % Subtract, element-wise
t % Duplicate
&1Zs % Standard deviation using normalization by n
/ % Divide, element-wise
% Implicit display
CW for all trivial entries
Python 3 + scipy, 31 bytes
from scipy.stats import*
zscore
Octave / MATLAB, 15 bytes
@(x)zscore(x,1)