std
result = std(x, [on_invalid=])
Input: x, on_invalid · Output: result
What it is
Sample standard deviation (N-1, unbiased, denominator) of every element of x — a scalar, Vec, or Mat/Signal. With no other argument, flattens x to one scalar Num. On a Mat, the optional axis= keyword (0 or 1) computes the standard deviation down each column or across each row instead, returning a length-cols/rows Vec. on_invalid= ("propagate"/"ignore"/"error", see this section's intro) controls NaN/Inf handling; under "ignore" the divisor is the count of elements that survive filtering.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = std(x, [on_invalid=]) | x scalar, Vec, Mat, or SignalSample standard deviation (N-1, unbiased, denominator) of every element of x — a scalar, Vec, or Mat/Signal.on_invalid optionalon_invalid= ("propagate"/"ignore"/"error", see this section's intro) controls NaN/Inf handling. | result scalar, Num, or Vec | Returns a scalar Num with no axis=, or that Vec with one. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
function normalize(v)
m = mean(v)
s = std(v)
return (v - m) / s
end function
z = normalize(y)
print("normalized: mean {mean(z):.2e}, std {std(z):.4f}")
normalized: mean 3.37e-18, std 1.0000
from from-zero
sample() := randn()
draws = monte_carlo("sample", 10000, seed=1)
print("mean = {mean(draws)}, std = {std(draws)}")
mean = 0.001862, std = 1.008776
from numerics-dsp
print("mean = {mean(v)}, std = {std(v):.4f}, rms = {rms(v):.4f}")
G = [1, 2, 3; 4, 5, 6]
print("colsum = {sum(G, axis=0)}")
print("rowsum = {sum(G, axis=1)}")
mean = 15, std = 7.0711, rms = 15.8114 colsum = [5, 7, 9] rowsum = [6, 15]
from fundamentals
x = [1, 2, 3, 4, 5]
print(rolling_mean(x, 3)) # [1.5, 2, 3, 4, 4.5] -- ends over 2 samples, not 3
print(rolling_min(x, 3)) # [1, 1, 2, 3, 4]
print(rolling_std(x, 3)) # [0.707107, 1, 1, 1, 0.707107]
print(std([1, 2])) # 0.707107 -- the same number the edge reports
[1.5, 2, 3, 4, 4.5] [1, 1, 2, 3, 4] [0.707107, 1, 1, 1, 0.707107] 0.707107
from noise
How it works
var and median were reachable from the dispatch arm's list and absent from this one, which is the pairing that makes the _ => NAN below dangerous: a name added there and forgotten here returns a vector of NaNs rather than an error. var is std squared here for the same reason the chapter states that invariant -- one definition, not two.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.