Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jbirley
Last active March 5, 2024 14:16
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbirley/f4c7775007aabbcf6b67b9160276b198 to your computer and use it in GitHub Desktop.
Save jbirley/f4c7775007aabbcf6b67b9160276b198 to your computer and use it in GitHub Desktop.
A percentile function for PowerShell
# MIT License
# Copyright (c) 2022 Jim Birley
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
function Get-Percentile {
<#
.SYNOPSIS
Returns the specified percentile value for a given set of numbers.
.DESCRIPTION
This function expects a set of numbers passed as an array to the 'Sequence' parameter. For a given percentile, passed as the 'Percentile' argument,
it returns the calculated percentile value for the set of numbers.
.PARAMETER Sequence
A array of integer and/or decimal values the function uses as the data set.
.PARAMETER Percentile
The target percentile to be used by the function's algorithm.
.EXAMPLE
$values = 98.2,96.5,92.0,97.8,100,95.6,93.3
Get-Percentile -Sequence $values -Percentile 0.95
.NOTES
Author: Jim Birley
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[Double[]]$Sequence
,
[Parameter(Mandatory)]
[Double]$Percentile
)
$Sequence = $Sequence | Sort-Object
[int]$N = $Sequence.Length
Write-Verbose "N is $N"
[Double]$Num = ($N - 1) * $Percentile + 1
Write-Verbose "Num is $Num"
if ($num -eq 1) {
return $Sequence[0]
} elseif ($num -eq $N) {
return $Sequence[$N-1]
} else {
$k = [Math]::Floor($Num)
Write-Verbose "k is $k"
[Double]$d = $num - $k
Write-Verbose "d is $d"
return $Sequence[$k - 1] + $d * ($Sequence[$k] - $Sequence[$k - 1])
}
}
@bburgin
Copy link

bburgin commented Sep 2, 2021

Thanks for this function. What is the license for it? Can you mark it with one? Thanks.

@jbirley
Copy link
Author

jbirley commented Oct 20, 2022

I have added an MIT license to this Gist. Thanks for inquiring.

@KurtKroeker
Copy link

This is awesome! Thanks, @jbirley. Used it to generate some metrics to measure percentiles for client upload file sizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment