forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Measure-StandardDeviation.ps1
43 lines (35 loc) · 1.07 KB
/
Measure-StandardDeviation.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<#
.SYNOPSIS
Calculate the standard deviation of numeric values.
.INPUTS
A collection of System.Double values
.OUTPUTS
System.Double
.LINK
Measure-Object
.EXAMPLE
Get-Item *.ps1 |% Length |Measure-StandardDeviation.ps1
3870.16158336182
.EXAMPLE
Measure-StandardDeviation.ps1 (1..20)
5.7662812973354
#>
#Requires -Version 3
[CmdletBinding()][OutputType([double])] Param(
# The numeric values to analyze.
[Parameter(Position=0,ValueFromRemainingArguments=$true,ValueFromPipeline=$true)]
[double[]] $InputObject
)
End
{
[double[]] $values = if($input) {$input} else {$InputObject}
[double] $average = $values |Measure-Object -Average |ForEach-Object Average
Write-Verbose "Average = $average"
[double[]] $deviations = $values |ForEach-Object {[math]::Pow(($_-$average),2)}
Write-Verbose "Deviations = { $deviations }"
[double] $variance = ($deviations |Measure-Object -Sum).Sum/$values.Count
Write-Verbose "Variance = $variance"
[double] $stddev = [math]::Sqrt($variance)
Write-Verbose "Standard deviation = $stddev"
return $stddev
}