Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Testing Random Number Generators


June 1996/Testing Random Number Generators/Listing 5

Listing 5: The serial correlation test

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
/* Repeat Step 1 through Step 4 N times, then proceed to Step 5:    */
/*                                                                  */
/*   Step 1: Generate T variates, Xk, 0 <= Xk <= 32767, 1 <= k <= T.*/
/*                                                                  */
/*   Step 2: Calculate NumCorr correlation coefficients as follows: */
/*                                                                  */
/*           err = bjcorr(X, T, Corr, NumCorr);                     */
/*                                                                  */
/*           where                                                  */
/*                                                                  */
/*               Corr      receiving area for correlation           */
/*             coefficients                                         */
/*               NumCorr   User-specified number <= T - 3           */
/*                                                                  */
/*   Step 3: Calculate a test statistic, Ut:                        */
/*                                                                  */
/*           Ut = T * (T + 2) * Sum{Corr[j]^2 / (T-j), j = 1, J}    */
/*                                                                  */
/*           where                                                  */
/*               J = NumCorr                                        */
/*                                                                  */
/*                                                                  */
/*   Step 4: Calculate a chi-square probability (Pt) associated     */
/*      with Ut, with J degrees of freedom ; save it in an          */
/*      array of size N,                                            */
/*                                                                  */
/*           When N probabilities, Pt, have been computed,          */
/*           go  to  Step  5.                                       */
/*                                                                  */
/*   Step 5: Run a KS analysis on Pt(j), 1 <= j <= N.               */
/*                                                                  */
/*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mconf.h>
#include <srcrdefs.h>
#include <miscdefs.h>

#define LOW_PROB        1.0e-8

int     Variates[32768u];
double  CorCoefs[32768u];
/* ============================================================== */
/* CalcSerCorChiSq - Gets Chi-square p-value for NumCorr          */
/* Coefficients                                                   */
/* ============================================================== */
void
CalcSerCorChiSq(SERCOR_DATA_STRU * SerCorData)
{
    UINT    k;
    UINT    NumCoefs = SerCorData->NumCoefs,
            NumVar   = SerCorData->NumVarPerPass;

    double  ChiSqProb, ChiSqStat;
    double  CoefSos = 0, CoefSum = 0, RmsCoefs, Sum = 0;
    double  LoLimit, HiLimit;

    /* ----------------------------------------- */
    /* Step 1: Generate Array of Random Deviates */
    /* ----------------------------------------- */
    for (k = 0; k < NumVar; ++k)
    {
        Variates[k] = SerCorData->RandFun();
    }

    /* ------------------------------------------ */
    /* Step 2: Calculate Correlation Coefficients */
    /* ------------------------------------------ */
    SerCorData->CallStatus =
        BJCoef(Variates, NumVar, CorCoefs, NumCoefs);

    if (SerCorData->CallStatus == OK)
    {
        /* ---------------------------------------------------- */
        /* Calculate Limits on Acceptable Chi-Square Statistics */
        /* ---------------------------------------------------- */
        ChiSqDist(LOW_PROB, NumCoefs, &LoLimit, &HiLimit);

        /* -------------------------------- */
        /* Step 3: Calculate Test Statistic */
        /* -------------------------------- */
        for (k = 0; k < NumCoefs; ++k)
        {
            double  SqrCoef = SQR(CorCoefs[k]);

            /* ------------------- */
            /* Sum of Coefficients */
            /* ------------------- */
            CoefSum += CorCoefs[k];

            /* ------------------------------ */
            /* Sum of Squares of Coefficients */
            /* ------------------------------ */
            CoefSos += SqrCoef;

            /* ------------------------- */
            /* Accumulate Test Statistic */
            /* ------------------------- */
            Sum += SqrCoef / (double) (NumVar - (k+1));
        }

        /* ----------------------------- */
        /* Finish Step 3: Test Statistic */
        /* ----------------------------- */
        ChiSqStat = Sum * (double)NumVar * (double)(NumVar + 2);

        /* ---------------------------------------- */
        /* Step 4: Calculate Chi-Square Probability */
        /* ---------------------------------------- */
        if (ChiSqStat < LoLimit)
        {
              ChiSqProb = LOW_PROB;
        }       else if (ChiSqStat > HiLimit)
        {
              ChiSqProb = 1.0 - LOW_PROB;
        }
        else
        {
             ChiSqProb = chdtr(NumCoefs, ChiSqStat);
        }
        SerCorData->ChiSqProb = ChiSqProb;

        /* --------------------------------------------------- */
        /* Maintain Counts of Numbers of Variates and Outliers */
        /* --------------------------------------------------- */
        SerCorData->TotCoefs += NumCoefs;
        SerCorData->TotSos   += CoefSos;
        SerCorData->TotSum   += CoefSum;

        RmsCoefs = sqrt(CoefSos / (double)NumCoefs);

        for (k = 0; k < NumCoefs; ++k)
        {
            if (fabs(CorCoefs[k]) > 2*RmsCoefs)
            {
                ++SerCorData->NumOutliers;
            }
        }
    }
                                  /* End of if */
}
/* End of File */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.