Logo

SQL Server RANDCHISQ Function

Updated 2023-10-18 15:37:58.560000

Description

Use the table-valued function RANDCHISQ to generate a sequence of random numbers from a chi-squared distribution with @df degrees of freedom.

Syntax

SELECT * FROM [westclintech].[wct].[RANDCHISQ](
  <@Rows, int,>
 ,<@df, 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.

@df

the degrees of freedom. @df 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

@df must be greater than zero.

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

If @MaxIterations 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 chi-squared distribution with @df = 1, COUNT the results, paste them into Excel and graph them.

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

This produces the following result.

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

In this example we generate 1,000,000 random numbers from a chi-squared distribution with @df of 9. 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 @df as float = 9;
DECLARE @mean as float = @df;
DECLARE @var as float = 2*@df;
DECLARE @stdev as float = SQRT(@var);
DECLARE @skew as float = SQRT(8e+00/@df);
DECLARE @kurt as float = 12e+00/@df;
 
SELECT
   stat,
   [RANDCHISQ],
   [EXPECTED]
FROM (
   SELECT
      x.*
   FROM (
      SELECT
         AVG(x) as mean_CHISQ,
         STDEVP(x) as stdev_CHISQ,
         wct.SKEWNESS_P(x) as skew_CHISQ,
         wct.KURTOSIS_P(x) as kurt_CHISQ
      FROM
         wct.RANDCHISQ(@size,@df)
      )n
   CROSS APPLY(
      VALUES
         ('RANDCHISQ','avg', mean_CHISQ),
         ('RANDCHISQ','stdev', stdev_CHISQ),
         ('RANDCHISQ','skew', skew_CHISQ),
         ('RANDCHISQ','kurt', kurt_CHISQ),
         ('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([RANDCHISQ],[EXPECTED])) P;

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

statRANDCHISQEXPECTED
avg9.005406866145319
kurt1.360286542608511.33333333333333
skew0.9473201266759910.942809041582063
stdev4.251338626500764.24264068711928

See Also

CHIINV - Calculate the inverse of the one-tailed probability of the chi-square distribution.

RANDBETA - Random numbers from a beta distribution

RANDBINOM - Random numbers from a binomial distribution

RANDCAUCHY - Random numbers from a Cauchy 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

RANDTDIST - Random numbers from Student's t distribution

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