Logo

STIRLING2

Updated 2024-03-14 20:29:45.457000

Syntax

SELECT [westclintech].[wct].[STIRLING2] (
   <@n, int,>
  ,<@k, int,>)

Description

Use the scalar function STIRLING2 to calculate the Stirling numbers of the second kind. The Stirling numbers of the second kind count the number of ways to partition n elements into k nonempty sets:

\left\{ {n \atop k}\right\} = \frac{1}{k!}\sum_{i=0}^k (-1)^{k-i} \binom{k}{i} i^n = \sum_{i=0}^k \frac{(-1)^{k-i} i^n}{(k-i)!i!}

Where

\binom{k}{i}

is the binomial coefficient (see BICO). Stirling numbers of the second kind exhibit the following recursive relationship.

\left\{{n+1\atop k}\right\} = k \left\{{ n \atop k }\right\} + \left\{{n\atop k-1}\right\} \quad \mbox{for} \; 0<k<n

Arguments

@n

Number of items.

@k

Number chosen.

Return Type

float

Remarks

If n <= 0 then NULL is returned.

If k < 0 then NULL is returned.

If k > n then NULL is returned.

S(0,0) = 1.

S(n,0) = 0.

Examples

Example #1

Let’s say that we have a fair die and we roll it 10 times. We can use the Stirling numbers of the second kind to predict the probability that the result set will contain 1, 2, 3, 4, 5 or 6 distinct values.

SELECT k.SeriesValue as [N],

       wct.BICO(6, k.seriesvalue) * wct.STIRLING2(10, k.seriesvalue) * wct.FACT(

                 k.SeriesValue) * wct.POWER(6, -10) as p

FROM wct.Seriesint(1, 6, NULL, NULL, NULL) k;

This produces the following result.

{"columns":[{"field":"N","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"p","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"N":"1","p":"9.92290301275212E-08"},{"N":"2","p":"0.000253530171975817"},{"N":"3","p":"0.0185161370217955"},{"N":"4","p":"0.203052364349947"},{"N":"5","p":"0.506365740740741"},{"N":"6","p":"0.271812128486511"}]}

[Example #2](./Example #2) A lower triangular representation of the first few Stirling numbers of the second kind.

SELECT [1],

       [2],

       [3],

       [4],

       [5],

       [6],

       [7],

       [8],

       [9],

       [10]

FROM

(

    SELECT n.SeriesValue as n,

           k.seriesValue as k,

           cast(wct.STIRLING2(n.SeriesValue, k.SeriesValue) as int) as S1

    FROM wct.SeriesInt(1, 10, NULL, NULL, NULL) n

        CROSS APPLY wct.SeriesInt(1, n.seriesvalue, NULL, NULL, NULL) k

) d

PIVOT

(

    MAX(S1)

    FOR k IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10])

) pvt

ORDER BY n;

This produces the following result.

{"columns":[{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2"},{"field":"3"},{"field":"4"},{"field":"5"},{"field":"6"},{"field":"7"},{"field":"8"},{"field":"9"},{"field":"10"}],"rows":[{"1":"1","2":"NULL","3":"NULL","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"1","3":"NULL","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"3","3":"1","4":"NULL","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"7","3":"6","4":"1","5":"NULL","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"15","3":"25","4":"10","5":"1","6":"NULL","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"31","3":"90","4":"65","5":"15","6":"1","7":"NULL","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"63","3":"301","4":"350","5":"140","6":"21","7":"1","8":"NULL","9":"NULL","10":"NULL"},{"1":"1","2":"127","3":"966","4":"1701","5":"1050","6":"266","7":"28","8":"1","9":"NULL","10":"NULL"},{"1":"1","2":"255","3":"3025","4":"7770","5":"6951","6":"2646","7":"462","8":"36","9":"1","10":"NULL"},{"1":"1","2":"511","3":"9330","4":"34105","5":"42525","6":"22827","7":"5880","8":"750","9":"45","10":"1"}]}

See Also

FACTORIALPOWER - the step-h factorial power x(n,h)

LCHOOSE - Natural logarithm of the binomial coefficient

LPERMUTA - calculate the natural logarithm of the PERMUTATIONA function.

PERMUT - calculate the number of permutations for a given number objects that can be selected from a number of objects

PERMUTATIONA - number of permutations with repetition

STIRLING1 - Stirling numbers of the first kind