Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Statistics: Jarque-Bera normality test

by andye (Curate)
on Apr 30, 2007 at 14:16 UTC ( #612779=snippet: print w/ replies, xml ) Need Help??

Description: 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)
                    )
                  ;

Comment on Statistics: Jarque-Bera normality test
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://612779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-12-28 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls