header image

Calculating Standard Deviation – the function

Posted by: | December 20, 2016 Comments Off on Calculating Standard Deviation – the function |

Last time I showed how to calculate the standard deviation of a set of numbers and said the code could easily be turned into a function

function Measure-StandardDeviation {
[CmdletBinding()]
param (
[double[]]$numbers
)
$mean = $numbers | Measure-Object -Average | select -ExpandProperty Average
$sqdiffs = $numbers | foreach {[math]::Pow(($psitem - $mean), 2)}


$sigma = [math]::Sqrt( ($sqdiffs | Measure-Object -Average | select -ExpandProperty Average) )
[math]::Round($sigma, 3)
}

 

I’ve called the function Measure-StandardDeviation based on Measure-Object producing the mean.

The parameter is set to accept an array of doubles – floating point numbers – integers will be automatically converted to doubles.

To use the function

$numbers = 1..10
Measure-StandardDeviation -numbers $numbers

 

or create the input array directly

Measure-StandardDeviation -numbers (1..10)

under: PowerShell Basics

Comments are closed.

Categories