I recently needed a good way of picking from a list of volumes, free space and directory count where factors. I would almost expect that computing standard deviation and variance for a set of numbers would be built in to System.Math but in their absence, this is what I've come up with:
function Compute-StandardDeviation {
param (
$set,
[switch]$sampleSet
)
$variance = Compute-Variance $set -sampleSet:$sampleSet
[Math]::Sqrt($variance)
}
function Compute-Variance {
param (
$set,
[switch]$sampleSet
)
[double]$sum = 0
[double]$div = 0
$set | ? { $_ -match '^[\d.]+$' } | % { [double]$i = [double]$; $sum += $i; $div++ }
if ($sampleSet) { $div-- }
$mean = ($sum)/$div
($set | % { [Math]::Pow($($ - $mean),2) } | measure -Average).Average
}
I have found some implementations that have proven to be buggy. This may not be the most optimized code but it works.