Hi folks,

Following on from my question the other day (Stats: Testing whether data is normally (Gaussian) distributed), here's a simple normality test.

This is the Jarque-Bera test, "a goodness-of-fit measure of departure from normality, based on the sample kurtosis and skewness".

Kurtosis and skewness are calculated like this.

Comments very welcome, if I've gone wrong here please do point it out!

Input $source should be a 1-D PDL, type float (it'll probably work with other types, but be less useful!).

Output $JB is a number indicating nearness to the Normal distribution. High number = not Normal.

Warning: this can produce NaN for data where standard deviation =0, so if you plan to sort it afterwards, you'll need to weed out the NaN ones (as I discovered for myself {'sort subroutine edge cases'}).

I hope this is useful to someone.

Best wishes, andye

use PDL; my ($mean,$std_dev,$median,$min,$max,$adev,$rms) = stats($sour +ce); my $skewness = sclr(sum( ($source - $mean)**3 ) / ( (nelem($source)-1) * $std_dev**3 )); my $exs_kurtosis = sclr(sum( ($source - $mean)**4 ) / ( (nelem($source)-1) * $std_dev**4 ) -3); my $JB = ( nelem($source) / 6 ) * ( $skewness**2 + ($exs_kurtosis**2 / 4) ) ;

In reply to Statistics: Jarque-Bera normality test by andye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.