Logo

RANDTDIST

Updated 2023-10-18 19:36:36.863000

Syntax

SELECT * FROM [westclintech].[wct].[RANDTDIST](
  <@Rows, int,>
 ,<@df, float,>)

Description

Use the table-valued function RANDTDIST to generate a sequence of random numbers from Student's t distribution with parameter @df.

Arguments

@df

the degrees of freedom. @df must be of the type float or of a type that implicitly converts to float.

@Rows

the number of rows to generate. @Rows must be of the type int or of a type that implicitly converts to int.

Return Type

table

{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "25a80935-ae01-439f-84df-7056159672d4", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "ea72d5e8-bd1f-436d-80ba-7f5e91ab0f35", "colName": "X", "colDatatype": "float", "colDesc": "The random variable"}]}

Remarks

@df must be greater than zero.

If @df is NULL then @Shape is set to 1.

If @Rows is less than 1 then no rows are returned.

Examples

In this example we create a sequence 1,000,000 random numbers rounded to one decimal place from a Student's t distribution with @df = 10, COUNT the results, paste the results into Excel and graph them.

SELECT X,
       COUNT(*) as [COUNT]
FROM
(
    SELECT ROUND(X, 1) as X
    FROM wct.RANDTDIST(   1000000, --@Rows
                          10       --@df
                      )
) n
GROUP BY X
ORDER BY X;

This produces the following result.

http://westclintech.com/Portals/0/images/doc_math_RANDTDIST_img1.jpg

In this example we generate 1,000,000 random numbers from a t distribution with @lambda of 6. We calculate the mean, standard deviation, skewness, and excess kurtosis from the resultant table and compare those values to the expected values for the distribution.

DECLARE @size as int = 1000000;
DECLARE @lambda as float = 6;
DECLARE @mean as float = 0;
DECLARE @var as float = CASE
                            WHEN @lambda <= 2 THEN
                                NULL
                            ELSE
                                @lambda / (@lambda - 2)
                        END;
DECLARE @stdev as float = SQRT(@var);
DECLARE @skew as float = 0;
DECLARE @kurt as float = CASE
                             WHEN @lambda <= 4 THEN
                                 NULL
                             ELSE
                                 6 / (@lambda - 4)
                         END;
SELECT stat,
       [RANDTDIST],
       [EXPECTED]
FROM
(
    SELECT x.*
    FROM
    (
        SELECT AVG(x) as mean_TDIST,
               STDEVP(x) as stdev_TDIST,
               wct.SKEWNESS_P(x) as skew_TDIST,
               wct.KURTOSIS_P(x) as kurt_TDIST
        FROM wct.RANDTDIST(@size, @lambda)
    ) n
        CROSS APPLY
    (
        VALUES
            ('RANDTDIST', 'avg', mean_TDIST),
            ('RANDTDIST', 'stdev', stdev_TDIST),
            ('RANDTDIST', 'skew', skew_TDIST),
            ('RANDTDIST', 'kurt', kurt_TDIST),
            ('EXPECTED', 'avg', @mean),
            ('EXPECTED', 'stdev', @stdev),
            ('EXPECTED', 'skew', @skew),
            ('EXPECTED', 'kurt', @kurt)
    ) x (fn_name, stat, val_stat)
) d
PIVOT
(
    sum(val_stat)
    FOR fn_name in ([RANDTDIST], [EXPECTED])
) P;

This produces the following result (your result will be different).

{"columns":[{"field":"stat"},{"field":"RANDTDIST","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"EXPECTED","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"stat":"avg","RANDTDIST":"0.000255903904467406","EXPECTED":"0"},{"stat":"kurt","RANDTDIST":"3.00998030754108","EXPECTED":"3"},{"stat":"skew","RANDTDIST":"0.0148237899631972","EXPECTED":"0"},{"stat":"stdev","RANDTDIST":"1.22461089322925","EXPECTED":"1.22474487139159"}]}

See Also

TINV - The inverse t distribution function

RANDBETA - Random numbers from a beta distribution

RANDBINOM - Random numbers from a binomial distribution

RANDCAUCHY - Random numbers from a Cauchy distribution

RANDCHISQ - Random numbers from a chi-squared distribution

RANDEXP - Random numbers from an exponential distribution

RANDFDIST - Random numbers from an F-distribution

RANDGAMMA - Random numbers from a gamma distribution

RANDLAPLACE - Random numbers from a LaPlace distribution

RANDLOGISTIC - Random numbers from a logistic distribution

RANDNORMAL - Random numbers from the normal distribution

RANDPOISSON - Random numbers from a Poisson distribution

RANDSNORMAL - Random numbers from the standard normal distribution

RANDWEIBULL - Generate a sequence of random numbers from w Weibull distribution with parameters shape (?) and scale (?).