Logo

RANDCHISQ

Updated 2023-10-18 15:37:58.560000

Syntax

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

Description

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

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": "c77a2631-40cf-48b3-b3e0-37abd0eee4a2", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "3df49f71-7a67-4572-b661-b49fa4601fc3", "colName": "X", "colDatatype": "float", "colDesc": "The 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).

{"columns":[{"field":"stat"},{"field":"RANDCHISQ","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","RANDCHISQ":"9.00540686614531","EXPECTED":"9"},{"stat":"kurt","RANDCHISQ":"1.36028654260851","EXPECTED":"1.33333333333333"},{"stat":"skew","RANDCHISQ":"0.947320126675991","EXPECTED":"0.942809041582063"},{"stat":"stdev","RANDCHISQ":"4.25133862650076","EXPECTED":"4.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 (?).