API reference · Finance

StDev

Computes the population standard deviation of numeric first-column values using an online variance update.

FunctionFinance→DoubleSource L125

Signature#

VBA
Function StDev(ByVal vArr As Variant) As Double

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredInput 1D array or 2D array. Only the column at LBound(vArr, 2) contributes for 2D input.

Returns#

Double. Sqr(M2 / n), where n is the number of accepted numeric values and M2 is their accumulated squared deviation. Returns 0 for no observations, a single observation, or rejected input shapes.

Example#

SEE THE TRANSFORMATIONPopulation standard deviation
Before
values1D · (8,)

Indices 0..7 · one dimension

01234567
24445579
After
Return valueValues / state

No array dimensions

ItemValue
StDev2

The numeric count is 8 and the mean is 5. The squared deviations total 32, so √(32 ÷ 8) = 2.

Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.

VBA
Option Explicit

Public Sub ExampleFinanceStDev()
    Dim f As New Finance
    Dim values As Variant
    values = VBA.Array(2, 4, 4, 4, 5, 5, 7, 9)
    Debug.Print f.StDev(values) '2 (population standard deviation)
End Sub

Behavior and edge cases#

Performance#

O(n) time for n rows in the first column, with O(1) accumulator state. The 1D-to-2D normalization allocates O(n) elements; passing a Variant array ByVal can also require an array copy.

View implementation
VBA
Function StDev(ByVal vArr As Variant) As Double
    'Returnds stdof a variant

    Dim lRow                    As Long
    Dim lCol                    As Long
    Dim n                       As Long
    Dim vValue                  As Variant
    Dim dblMean                 As Double
    Dim dblValue                As Double
    Dim dblDelta                As Double
    Dim dblM2                   As Double

    If Not IsArray(vArr) Then Exit Function
    If Arr.DimCounter(vArr) = 1 Then vArr = Arr.DimExpand(vArr)
    If Arr.DimCounter(vArr) <> 2 Then Exit Function

    lCol = LBound(vArr, 2)

    For lRow = LBound(vArr, 1) To UBound(vArr, 1)
        vValue = vArr(lRow, lCol)
        If Not IsError(vValue) Then
            If Not IsNull(vValue) Then
                If Not IsEmpty(vValue) Then
                    If IsNumeric(vValue) Then
                        'welford algoritlhm
                        dblValue = CDbl(vValue)
                        n = n + 1
                        dblDelta = dblValue - dblMean
                        dblMean = dblMean + dblDelta / n
                        dblM2 = dblM2 + dblDelta * (dblValue - dblMean)
                    End If
                End If
            End If
        End If
    Next lRow

    If n = 0 Then Exit Function
    If dblM2 < 0 Then dblM2 = 0

    StDev = Sqr(dblM2 / n)
End Function