Logo

SQL Server RANDFDIST Function

Updated 2023-10-18 15:50:18.283000

Description

Use the table-valued function RANDFDIST to generate a sequence of random numbers from an F-distribution with the degree of freedom parameters @df1 and @df2.

Syntax

SELECT * FROM [westclintech].[wct].[RANDFDIST](
  <@Rows, int,>
 ,<@df1, float,>
 ,<@df2, float,>)

Arguments

@Rows

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

@df1

the first degree of freedom parameter. @df1 must be of the type float or of a type that implicitly converts to float.

@df2

the second degree of freedom parameter. @df2 must be of the type float or of a type that implicitly converts to float.

Return Type

table

colNamecolDatatypecolDesc
SeqintA monotonically increasing sequence number
XfloatThe random variable

Remarks

@df1 must be greater than zero.

@df2 must be greater than zero.

If @df1 is NULL then @df1 is set to 1.

If @df2 is NULL then @df2 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 two decimal places from an F-distribution with @df1 = 100 and @df2 = 100, COUNT the results, paste then into Excel and graph them.

SELECT X,
       COUNT(*) as [COUNT]
FROM
(
    SELECT ROUND(X, 1) as X
    FROM wct.RANDFDIST(   1000000, --@Rows
                          100,     --@df1
                          100      --@df2
                      )
) n
GROUP BY X
ORDER BY X;

This produces the following result.

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

In this example we generate 1,000,000 random numbers from an F-distribution with @df1 of 100 and a @df2 of 100. 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 @d1 as float = 100;
DECLARE @d2 as float = 100;
DECLARE @mean as float =
CASE
WHEN @d2 <= 2 THEN NULL
ELSE @d2/(@d2-2)
END;
DECLARE @var as float =
CASE
WHEN @d2 <=4 THEN NULL
ELSE (2*POWER(@d2,2)*(@d1+@d2-2))/(@d1*POWER(@d2-2,2)*(@d2-4)) END;
DECLARE @stdev as float =
CASE
WHEN @d2 <=4 THEN NULL
ELSE SQRT(@var) END;
DECLARE @skew as float =
CASE
WHEN @d2 <= 6 THEN NULL
ELSE ((2*@d1+@d2-2)*SQRT(8*(@d2-4)))/((@d2-6)*SQRT(@d1*(@d1+@d2-2)))
END;
DECLARE @kurt as float =
CASE
WHEN @d2 <= 8 THEN NULL
ELSE 12*((@d1*(5*@d2-22)*(@d1+@d2-2)+(@d2-4)*POWER(@d2-2,2))/(@d1*(@d2-6)*(@d2-8)*(@d1+@d2-2)))
END;
 
SELECT
   stat,
   [RANDFDIST],
   [EXPECTED]
FROM (
   SELECT
      x.*
   FROM (
      SELECT
         AVG(x) as mean_FDIST,
         STDEVP(x) as stdev_FDIST,
         wct.SKEWNESS_P(x) as skew_FDIST,
         wct.KURTOSIS_P(x) as kurt_FDIST
      FROM
         wct.RANDFDIST(@size,@d1,@d2)
      )n
   CROSS APPLY(
      VALUES
         ('RANDFDIST','avg', mean_FDIST),
         ('RANDFDIST','stdev', stdev_FDIST),
         ('RANDFDIST','skew', skew_FDIST),
         ('RANDFDIST','kurt', kurt_FDIST),
         ('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([RANDFDIST],[EXPECTED])) P;

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

statRANDFDISTEXPECTED
avg1.020314281695841.02040816326531
kurt0.7422237665048180.727888318896645
skew0.6250574086751610.624361945733466
stdev0.2071891846035780.207245877669285

See Also

FINV - Calculate the inverse of the F probability distribution.

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

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

RANDTDIST - Random numbers from Student's t distribution

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