| 1 | package Statistics::Autocorrelation; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Statistics::Data 0.08; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use base qw(Statistics::Data); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Statistics::Lite qw(mean); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use List::AllUtils qw(mesh); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | $Statistics::Autocorrelation::VERSION = '0.06'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Statistics::Autocorrelation - Coefficients for any lag, as correlogram, with significance tests | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Version 0.06 | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Statistics::Autocorrelation 0.06; | ||||||||||||||||||||||||||||
| 23 | $acorr = Statistics::Autocorrelation->new(); | ||||||||||||||||||||||||||||
| 24 | $coeff = $acorr->coefficient(data => \@data, lag => integer (from 1 to N-1), exact => 0, unbias => 1); | ||||||||||||||||||||||||||||
| 25 | # or load one or more data, optionally update, and test each discretely: | ||||||||||||||||||||||||||||
| 26 | $acorr->load(\@data1, \@data2); | ||||||||||||||||||||||||||||
| 27 | $coeff = $acorr->coeff(index => 0, lag => 1); # default lag => 0 | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Calculates autocorrelation coefficients for a single series of numerical data, for any valid length of I<lag>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 new | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $acorr = Statistics::Autocorrelation->new(); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Return a new class object for accessing its methods. This ISA L<Statistics::Data|Statistics::Data> object, so all the methods for loading, adding, saving, dumping, etc., data in that package are available here. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 coefficient | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | $coeff = $autocorr->coefficient(data => \@data, lag => integer (from 1 to N-1), exact => 0|1, unbias => 1|0, circular => 1|0); | ||||||||||||||||||||||||||||
| 44 | $coeff = $autocorr->coefficient(lag => 1); # using loaded data, and default args (exact = 0, unbias = 1, circular = 0) | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | I<Alias>: C<coeff>, C<acf> | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Returns the autocorrelation coefficient, the ratio of the autocovariance to variance of a sequence at any particular lag, ranging from -1 to +1, as in Chatfield (1975) and Kendall (1973). Specifically, | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =for html <table cellpadding="0" cellspacing="0"><tr><td>ρ<sub><i>k</i></sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">γ<sub><i>k</i></sub></td></tr><tr><td valign="top" align="center"><i>σ</i>²<sub><i>k</i></sub></td></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | where I<k> is the lag (see below). | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Data can be previously loaded or sent directly here (see L<Statistics::Data|Statistics::Data>). There must be at least two elements in the data array. A croak will be heard if no data have been loaded or given here. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Options are: | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =over 4 | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item B<lag> | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | An integer to define how many indices ahead or behind to start correlating the data to itself, as in how many time-intervals separate one value from another. If lag is greater than or equal to number of observations, returns empty string. If the value of B<lag> is less than zero, the calculation is made with its absolute value, given that | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =for html <table><tr><td>ρ<sub><i>k</i></sub> = </td><td>ρ<sub>–<i>k</i></sub></td></tr></table> | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | for all I<k> (so that a coefficient for a lag of -I<k> is equal in magnitude I<and sign> to that for +I<k>). If a value is not given for lag, it is set to the default value of 0. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =item B<exact> | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Boolean value, default = 0. In calculating the autocorrelation coefficient, the convention -- as in corporate stats programs (e.g., SPSS/PASW), and published examples of autocorrelation (e.g., L<nist.gov|http://www.itl.nist.gov/div898/handbook/eda/section3/eda35c.htm>), and texts such as Chatfield (1975), and Box and Jenkins (1976) -- is to calculate the sum-of-squares for the autocovariance (the numerator term in the autocorrelation coefficient) from the residuals for each observation I<x> from trial I<t> = 1 (index = 0) to I<N> - I<k> (the lag) relative to the mean of the whole sequence: | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =for html <table cellpadding="0" cellspacing="0"><tr><td valign="middle" >γ<sub><i>k</i></sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">1</td></tr><tr><td valign="top" align="center"><i>N</i></td></tr></table></td><td> </td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i></sub> – <span style="text-decoration:overline;"><i>x</i></span>)(<i>x</i><sub><i>t</i>+<i>k</i></sub> – <span style="text-decoration:overline;"><i>x</i></span>)</td></tr></table> | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | rather than the means for each sub-sequence as lagged, and (2) the sum-of-squares for the variance in the denominator as that of the whole sequence: | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =for html <table cellpadding="0" cellspacing="0"><tr><td valign="middle" align="center">σ²<sub><i>k</i></sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">1</td></tr><tr><td valign="top" align="center"><i>N</i></td></tr></table></td><td> </td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i></sub> – <span style="text-decoration:overline;"><i>x</i></span>)²</td></tr></table> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | instead of using completely pairwise products. This convention assumes that the series is stationary (has no linear or curvilinear trend, no periodicity), and that the number of observations, I<N>, in the sample is "reasonably large". You get the autocorrelation coefficient with these assumptions, with the above formulations, by default; but if you specify B<exact> => 1, then you get the coefficient as calculated by Kendall (1973) Eq. 3.35, where the sums use not the overall sample mean, but the mean for the first to the I<N> - I<k> elements, and the mean from the I<k> to I<N> elements: | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =for html <table><tr><td><table cellpadding="0" cellspacing="0"><tr><td valign="middle" ><span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i></sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">1</td></tr><tr><td valign="top" align="center"><i>N</i>–<i>k</i></td></tr></table></td><td> </td><td valign="middle" align="center"><table cellpadding="0" cellspacing="0"><tr><td valign="bottom" align="center"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle"><i>x</i><sub><i>t</i></sub></td></tr></table></td></tr></table></td><td>, and </td><td><table cellpadding="0" cellspacing="0"><tr><td><span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i>´</sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">1</td></tr><tr><td valign="top" align="center"><i>N</i>–<i>k</i></td></tr></table></td><td> </td><td valign="middle" align="center"><table cellpadding="0" cellspacing="0"><tr><td valign="bottom" align="center"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle"><i>x</i><sub><i>t</i>+<i>k</i></sub></td></tr></table></td></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Taking each observation relative to these means, the autocovariance in the numerator, and variance in the denominator, are calculated as follows to give the autocorrelation coefficient: | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =for html <table cellpadding="0" cellspacing="0"><tr><td rowspan="2" valign="middle">ρ<sub><i>k</i></sub> = </td><td style="border-bottom:thin solid #000000;" valign="bottom" align="center"><table cellpadding="0" cellspacing="0"><tr><td align="center"><table><tr><td><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i></sub> – <span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i></sub>)(<i>x</i><sub><i>t</i>+<i>k</i></sub> – <span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i>´</sub>)</td></tr></table></td></tr><tr><td><table><tr><td>[</td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i></sub> – <span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i></sub>)²</td><td>]<sup>½</sup> [</td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i>+<i>k</i></sub> – <span style="text-decoration:overline;"><i>x</i></span><sub><i>k</i>´</sub>)²</td><td>]<sup>½</sup></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item B<unbias> | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Boolean, default = 1. In calculating the approximate autocovariance, it is conventional to divide the sum-product of residuals (as given above) by I<N>, but some sources divide by I<N> - I<lag> for less biased estimation, so that | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =for html <table cellpadding="0" cellspacing="0"><tr><td valign="middle">γ<sub><i>k</i></sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" valign="bottom" align="center">1</td></tr><tr><td valign="top" align="center"><i>N</i>–<i>k</i></td></tr></table></td><td> </td><td valign="middle"><table cellpadding="0" cellspacing="0"><tr><td align="center"><sub><i>N</i>–<i>k</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center"><sup><i>t</i>=1</sup></td></tr></table></td><td valign="middle">(<i>x</i><sub><i>t</i></sub> – <span style="text-decoration:overline;"><i>x</i></span>)(<i>x</i><sub><i>t</i>+<i>k</i></sub> – <span style="text-decoration:overline;"><i>x</i></span>)</td></tr></table> | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | For the latter, set B<unbias> => 0. This is only effective where B<circular> => 0 and B<exact> => 0. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item B<circular> | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Boolean value, default = 0: For circularized lagging, set B<circular> => 1. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =back | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 325 | ▶ | sub coefficient { | ||||||||||||||||||||||||||
|
Subroutine
coefficient: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 325 | my ( $self, $args, $data, $n, $k ) = _get_args(@_); | |||||||||||||||||||||||||||
| 104 | 325 | ▶ | return q{} if !$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 325 | ▶ | $args->{'unbias'} = defined $args->{'unbias'} ? $args->{'unbias'} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 325 | ▶ | $args->{'varp'} = 1 if !defined $args->{'varp'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | #croak "Can\'t autocorrelate with a lag of < $k > for only < $n > data" if $n - $k == 1 && !$args->{'circular'}; | ||||||||||||||||||||||||||||
| 109 | return $args->{'exact'} | ||||||||||||||||||||||||||||
| 110 | ? _coeff_exact( $data, $n, abs($k), $args->{'circular'}, | ||||||||||||||||||||||||||||
| 111 | $args->{'unbias'} ) | ||||||||||||||||||||||||||||
| 112 | : _coeff_approx( $data, $n, abs($k), $args->{'circular'}, | ||||||||||||||||||||||||||||
| 113 | 325 | ▶ | $args->{'unbias'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 7 | *coeff = \&coefficient; | |||||||||||||||||||||||||||
| 116 | 7 | *acf = \&coefficient; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 autocovariance | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | $covar = $autocorr->autocovariance(data => \@data, lag => integer (from 1 to N-1), exact => 0|1, unbias => 1|0, circular => 1|0); | ||||||||||||||||||||||||||||
| 121 | $covar = $autocorr->autocovariance(lag => 1); # using loaded data, and default args (exact = 0, unbias = 1, circular = 0) | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | I<Alias>: C<autocov>, C<acvf> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Returns the autocovariance; see L<coefficient|coefficient> for definition and options. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub autocovariance { | ||||||||||||||||||||||||||
|
Subroutine
autocovariance: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ( $self, $args, $data, $n, $k ) = _get_args(@_); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | return q{} if !$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my ( $circ, $m1, $m2, $sumprod, $div ) = ( $args->{'circular'} ); | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ( $args->{'exact'} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | { # use mean of the original and lagged sequences separately | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | ( $m1, $m2 ) = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | $circ | ||||||||||||||||||||||||||||
| 137 | ? _comean_circ( $data, $n, $k ) | ||||||||||||||||||||||||||||
| 138 | : _comean_uncirc( $data, $n, $k ); | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $div = $circ ? $n : ( $n - $k ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { # use mean of the whole sequence | ||||||||||||||||||||||||||||
| 142 | 0 | $m1 = mean( @{$data} ); | |||||||||||||||||||||||||||
| 143 | 0 | $m2 = $m1; | |||||||||||||||||||||||||||
| 144 | $div = | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $circ ? $n : $args->{'unbias'} ? ( $n - $k ) : $n; #variancep(@$data); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $sumprod = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | $circ | ||||||||||||||||||||||||||||
| 149 | ? _covarsum_circ( $data, $n, abs($k), $m1, $m2 ) | ||||||||||||||||||||||||||||
| 150 | : _covarsum_uncirc( $data, $n, abs($k), $m1, $m2 ); | ||||||||||||||||||||||||||||
| 151 | 0 | $sumprod /= $div; | |||||||||||||||||||||||||||
| 152 | 0 | return $sumprod; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 7 | *autocov = \&autocovariance; | |||||||||||||||||||||||||||
| 155 | 7 | *acvf = \&autocovariance; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 correlogram | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $href = $autocorr->correlogram(nlags => integer, exact => 1|0, unbias => 1|0, circular => 1|0); # assuming data are loaded | ||||||||||||||||||||||||||||
| 160 | $href = $autocorr->correlogram(nlags => integer, exact => 1|0, unbias => 1|0, circular => 1|0); # assuming data are loaded | ||||||||||||||||||||||||||||
| 161 | $href = $autocorr->correlogram(); # use defaults, with loaded data | ||||||||||||||||||||||||||||
| 162 | $href = $autocorr->correlogram(data => \@data); # same as either of above, but give data here | ||||||||||||||||||||||||||||
| 163 | ($lags, $coeffs) = $autocorr->correlogram(); # with args as for either of the above | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | I<Alias>: C<coeff_list> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Returns the autocorrelation coefficients for lags from 0 to a limit, or (by default) over all possible lags, from 0 to I<N> - 1. If called in array context, returns two references: to an array of the lags, and an array of their respsective coefficients. Otherwise, returns a hash-reference of the coefficients keyed by their respective lags. The limit is given by argument B<nlags> giving the number of lags to return, including the zero lag, as permitted by the data to be referenced. Options are B<exact>, B<unbias> and B<circular>, as defined above for L<coefficient|coefficient>. The autocorrelation function being symmetric about lag zero, the correlogram is based only on positive lags. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | ▶ | sub correlogram { | ||||||||||||||||||||||||||
|
Subroutine
correlogram: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 1 | my ( $self, $args, $data, $n ) = _get_args(@_); | |||||||||||||||||||||||||||
| 173 | 1 | ▶ | return q{} if !$self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 1 | ▶ | my $m = $args->{'nlags'} ? $args->{'nlags'} : $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1 | $m--; | |||||||||||||||||||||||||||
| 176 | 1 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 'Value given for argument \'nlags\' is not valid - should be no more than the number of data elements less 1' | ||||||||||||||||||||||||||||
| 178 | if $m > $n - 1; | ||||||||||||||||||||||||||||
| 179 | 1 | my @range = ( 0 .. $m ); | |||||||||||||||||||||||||||
| 180 | 1 | my @coeffs = (); | |||||||||||||||||||||||||||
| 181 | 1 | foreach (@range) { | |||||||||||||||||||||||||||
| 182 | push @coeffs, | ||||||||||||||||||||||||||||
| 183 | $self->coefficient( | ||||||||||||||||||||||||||||
| 184 | data => $data, | ||||||||||||||||||||||||||||
| 185 | lag => $_, | ||||||||||||||||||||||||||||
| 186 | circular => $args->{'circular'}, | ||||||||||||||||||||||||||||
| 187 | unbias => $args->{'unbias'}, | ||||||||||||||||||||||||||||
| 188 | 200 | exact => $args->{'exact'} | |||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 1 | ▶ | return wantarray ? ( \@range, \@coeffs ) : { mesh( @range, @coeffs ) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 7 | *coeff_list = \&correlogram; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 correlogram_chart | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Experimental method to print a .png file of the correlogram. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub correlogram_chart { | ||||||||||||||||||||||||||
|
Subroutine
correlogram_chart: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | 0 | my ( $lags, $coeffs ) = correlogram(@_); | |||||||||||||||||||||||||||
| 203 | 0 | require GD::Graph::mixed; | |||||||||||||||||||||||||||
| 204 | 0 | my $graph = GD::Graph::mixed->new( 400, 300 ); | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | $graph->set( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | types => [qw(points bars lines)], | ||||||||||||||||||||||||||||
| 207 | markers => [7], # 7 = filled circle | ||||||||||||||||||||||||||||
| 208 | #line_types => [3], # 1 = solid, 2 = dashed, 3 = dotted, 4 = dot-dashed | ||||||||||||||||||||||||||||
| 209 | marker_size => 2, | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # bar_width => .9, | ||||||||||||||||||||||||||||
| 212 | # bar_spacing => 2, | ||||||||||||||||||||||||||||
| 213 | x_label => 'Lags', | ||||||||||||||||||||||||||||
| 214 | x_label_position => 1 / 2, | ||||||||||||||||||||||||||||
| 215 | y_label => 'Coeffs', | ||||||||||||||||||||||||||||
| 216 | title => 'Correlogram', | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | #y_max_value => 8, | ||||||||||||||||||||||||||||
| 219 | #y_tick_number => 8, | ||||||||||||||||||||||||||||
| 220 | y_label_skip => 2, | ||||||||||||||||||||||||||||
| 221 | dclrs => [ 'white', 'white', 'white' ], | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | #bgclr => 'lgray', | ||||||||||||||||||||||||||||
| 224 | #fgclr => 'black', | ||||||||||||||||||||||||||||
| 225 | #boxclr => 'lgray', | ||||||||||||||||||||||||||||
| 226 | #labelclr => 'black', | ||||||||||||||||||||||||||||
| 227 | #axislabelclr => 'black', | ||||||||||||||||||||||||||||
| 228 | #textclr => 'black', | ||||||||||||||||||||||||||||
| 229 | ) or croak $graph->error; | ||||||||||||||||||||||||||||
| 230 | 0 | $graph->set(); | |||||||||||||||||||||||||||
| 231 | 0 | my @zeroes = map { 0 } ( 1 .. scalar @{$coeffs} ); | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | my $gd = $graph->plot( [ $lags, $coeffs, $coeffs, \@zeroes ] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | or croak $graph->error; | ||||||||||||||||||||||||||||
| 234 | 0 | ▶ | open my $IMG, '>', 'file.png' or croak 'Cannot open file'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | binmode $IMG; | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | print {$IMG} $gd->png or croak 'Cannot print image to file'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | close $IMG or warn; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | return; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head2 ctest_bartlett | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | $bool = $acorr->ctest_bartlett(lag => integer, tails => 1|2); # assuming data are loaded, or see above for alternative and extra options | ||||||||||||||||||||||||||||
| 244 | ($crit, $coeff, $bool) = $acorr->ctest_bartlett(lag => integer, tails => 1|2); | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Performs a 95% confidence test of the null hypothesis of no autocorrelation, assuming that the series was generated by a Gaussian white noise process. Following Bartlett (1946), it compares the value of a single correlation coefficient for a given B<lag> with the critical values given B<tails> => 2 (default) or 1: | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =for html <table cellpadding="0" cellspacing="0"><tr><td><i>r</i><sub><i>k</i>,.95</sub> = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" align="center" valign="bottom"><i>s</i></td></tr><tr><td align="center" valign="top"><i>N</i><sup>½</sup></td></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | where I<s> is a constant equalling 1.96 for a two-tailed, or 1.645 for a one-tailed test. If the absolute value of the sample correlation coefficient falls beyond this critical value, the null hypothesis is rejected at the 95% level. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Returns, if called in array context, a list comprising the critical value, the sample coefficient, and a boolean as to whether the null hypothesis is rejected; otherwise, just the latter boolean. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Accepts all the options as given for L<coefficient|Statistics::Autocorrelation/coefficient>. Note that the critical value is not calculated with respect to the particular value of B<lag> - see L<ctest_anderson|Statistics::Autocorrelation/ctest_anderson> for this. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub ctest_bartlett { | ||||||||||||||||||||||||||
|
Subroutine
ctest_bartlett: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 259 | 0 | my ( $self, $args, $data, $n ) = _get_args(@_); | |||||||||||||||||||||||||||
| 260 | my $coeff = $self->acf( | ||||||||||||||||||||||||||||
| 261 | data => $data, | ||||||||||||||||||||||||||||
| 262 | lag => $args->{'lag'}, | ||||||||||||||||||||||||||||
| 263 | circular => $args->{'circular'}, | ||||||||||||||||||||||||||||
| 264 | unbias => $args->{'unbias'}, | ||||||||||||||||||||||||||||
| 265 | 0 | exact => $args->{'exact'} | |||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $tails = ( defined $args->{'tails'} and $args->{'tails'} == 1 ) ? 1 : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args->{"tails"} and $args->{"tails"} == 1
MC/DC0%0/2
defined $args->{"tails"} and $args->{"tails"} == 1
defined $args->{"tails"}
$args->{"tails"} == 1
Truth table0%0/3
A and B
A= defined $args->{"tails"}B= $args->{"tails"} == 1
| |||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | #my $c = _set_criterion($tails, $n - 1, $args->{'criteria'}); | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | my $c = $tails == 2 ? 1.95996398454005 : 1.64485362695147; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | my $crit = $c / sqrt $n; | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | my $bool = abs($coeff) > $crit ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | return wantarray ? ( $crit, $coeff, $bool ) : $bool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =head2 ctest_anderson | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | $bool = $acorr->ctest_bartlett(lag => integer, tails => 1|2); # assuming data are loaded, or see above for alternative and extra options | ||||||||||||||||||||||||||||
| 279 | ($crit, $coeff, $bool) = $acorr->ctest_b(lag => integer, tails => 1|2); | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Performs a 95% confidence test of the null hypothesis of no autocorrelation, assuming that the series was generated by a Gaussian white noise process. Following Anderson (1941), it compares the value of a single correlation coefficient for a given B<lag> with the critical values given B<tails> => 2 (default) or 1: | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =for html <table cellpadding="0" cellspacing="0"><tr><td><i>r</i><sub><i>k</i>,.95</sub>(2-tailed) = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" align="center" valign="bottom">–1 ±1.96(<i>N</i> – <i>k</i> – 1)<sup>½</sup></td></tr><tr><td align="center" valign="top"><i>N</i> – <i>k</i></td></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =for html <table cellpadding="0" cellspacing="0"><tr><td><i>r</i><sub><i>k</i>,.95</sub>(1-tailed) = </td><td><table cellpadding="0" cellspacing="0"><tr><td style="border-bottom:thin solid #000000;" align="center" valign="bottom">–1 + 1.645(<i>N</i> – <i>k</i> – 1)<sup>½</sup></td></tr><tr><td align="center" valign="top"><i>N</i> – <i>k</i></td></tr></table></td></tr></table> | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | If the sample correlation coefficient falls outside these bounds, the null hypothesis is rejected at the 95% level. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Returns, if called in array context, a list comprising the critical value, the sample coefficient, and a boolean as to whether the null hypothesis is rejected; otherwise, just the latter boolean. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Accepts all the options as given for L<coefficient|Statistics::Autocorrelation/coefficient>. Note that the critical value I<is> calculated with respect to the particular value of B<lag> - unlike L<ztest_bartlett|Statistics::Autocorrelation/ztest_bartlett>. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub ctest_anderson { | ||||||||||||||||||||||||||
|
Subroutine
ctest_anderson: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 296 | 0 | my ( $self, $args, $data, $n ) = _get_args(@_); | |||||||||||||||||||||||||||
| 297 | my $coeff = $self->acf( | ||||||||||||||||||||||||||||
| 298 | data => $data, | ||||||||||||||||||||||||||||
| 299 | lag => $args->{'lag'}, | ||||||||||||||||||||||||||||
| 300 | circular => $args->{'circular'}, | ||||||||||||||||||||||||||||
| 301 | unbias => $args->{'unbias'}, | ||||||||||||||||||||||||||||
| 302 | 0 | exact => $args->{'exact'} | |||||||||||||||||||||||||||
| 303 | ); | ||||||||||||||||||||||||||||
| 304 | 0 | ▶ | my $tails = ( defined $args->{'tails'} and $args->{'tails'} == 1 ) ? 1 : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args->{"tails"} and $args->{"tails"} == 1
MC/DC0%0/2
defined $args->{"tails"} and $args->{"tails"} == 1
defined $args->{"tails"}
$args->{"tails"} == 1
Truth table0%0/3
A and B
A= defined $args->{"tails"}B= $args->{"tails"} == 1
| |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | my $c = $tails == 2 ? 1.96 : 1.645; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | my $crit = | ||||||||||||||||||||||||||||
| 307 | ( -1 + ( $c * sqrt( $n - $args->{'lag'} - 1 ) ) ) / | ||||||||||||||||||||||||||||
| 308 | 0 | ( $n - $args->{'lag'} ); | |||||||||||||||||||||||||||
| 309 | 0 | ▶ | my $bool = abs $coeff > $crit ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | return wantarray ? ( $crit, $coeff, $bool ) : $bool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head2 ztest_bartlett | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | $p_value = $acorr->ztest_bartlett(lag => integer, tails => 1|2); # assuming data are loaded, or see above for alternative and extra options | ||||||||||||||||||||||||||||
| 316 | ($z_value, $p_value) = $acorr->ztest_bartlett(lag => integer, tails => 1|2); | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Returns the 2- or 1-tailed probability, given B<tails> => 2 (default) or 1, respectively, for the deviation of the observed autocorrelation coefficient at the given B<lag> from the expected value of zero, relative to the variance 1 / I<N>, assuming that the series was generated by a Gaussian white noise process. If called in array context, returns both the actual I<Z>-value and then the I<p>-value. Other options, and methods of assigning the data to test, are as for L<coefficient|Statistics::Autocorrelation/coefficient>. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =cut | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub ztest_bartlett { | ||||||||||||||||||||||||||
|
Subroutine
ztest_bartlett: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 323 | 0 | my ( $self, $args, $data, $n ) = _get_args(@_); | |||||||||||||||||||||||||||
| 324 | my $coeff = $self->acf( | ||||||||||||||||||||||||||||
| 325 | data => $data, | ||||||||||||||||||||||||||||
| 326 | lag => $args->{'lag'}, | ||||||||||||||||||||||||||||
| 327 | circular => $args->{'circular'}, | ||||||||||||||||||||||||||||
| 328 | unbias => $args->{'unbias'}, | ||||||||||||||||||||||||||||
| 329 | 0 | exact => $args->{'exact'} | |||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||
| 331 | 0 | ▶ | my $tails = ( defined $args->{'tails'} and $args->{'tails'} == 1 ) ? 1 : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args->{"tails"} and $args->{"tails"} == 1
MC/DC0%0/2
defined $args->{"tails"} and $args->{"tails"} == 1
defined $args->{"tails"}
$args->{"tails"} == 1
Truth table0%0/3
A and B
A= defined $args->{"tails"}B= $args->{"tails"} == 1
| |||||||||||||||||||||||||||||
| 332 | 0 | require Statistics::Zed; | |||||||||||||||||||||||||||
| 333 | 0 | my $zed = Statistics::Zed->new(); | |||||||||||||||||||||||||||
| 334 | 0 | my ( $zval, $pval ) = $zed->score( | |||||||||||||||||||||||||||
| 335 | observed => $coeff, | ||||||||||||||||||||||||||||
| 336 | expected => 0, | ||||||||||||||||||||||||||||
| 337 | variance => 1 / $n, | ||||||||||||||||||||||||||||
| 338 | tails => $tails, | ||||||||||||||||||||||||||||
| 339 | ccorr => 0 | ||||||||||||||||||||||||||||
| 340 | ); | ||||||||||||||||||||||||||||
| 341 | 0 | ▶ | return wantarray ? ( $zval, $pval ) : $pval; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head2 qtest, boxpierce | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | $p_value = $acorr->qtest(nlags => integer); # assuming data are loaded, or see above for alternative and extra options | ||||||||||||||||||||||||||||
| 347 | ($q_value, $df, $p_value) = $acorr->qtest(nlags => integer); | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | Returns the I<Q> statistic for testing whether a range of autocorrelation coefficients differs from zero, and so if the series was produced by a random process (Box & Pierce, 1970). If called in array context, returns a list giving the value of I<Q>, and, assuming I<chi>-square distribtution, its degrees of freedom (= B<nlags>) and I<p>-value; returns the I<p>-value only if called in scalar context. Other options, and methods of assigning the data to test, are as for L<coefficient|Statistics::Autocorrelation/coefficient>. The range is (by default) over all possible lags from 1 to I<N> - 1. The statistic is defined as follows: | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =for html <table cellpadding="0" cellspacing="0"><tr><td valign="middle" align="center"><i>Q</i> = </td><td valign="middle"><i>N</i></td><td> </td><td><table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><sub><i>M</i></sub></td></tr><tr><td align="center">Σ</td></tr><tr><td align="center" valign="middle"><sup><i>k</i>=1</sup></td></tr></table></td><td valign="middle"><i>ρ</i><sub><i>k</i></sub>²</td></tr></table> | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | where I<M> is the largest lag-value to test (= B<nlags>). | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =cut | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub qtest { | ||||||||||||||||||||||||||
|
Subroutine
qtest: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | 0 | my ( $self, $args, $data, $n ) = _get_args(@_); | |||||||||||||||||||||||||||
| 359 | my ( $lags, $coeffs ) = $self->correlogram( | ||||||||||||||||||||||||||||
| 360 | data => $data, | ||||||||||||||||||||||||||||
| 361 | nlags => $args->{'nlags'}, | ||||||||||||||||||||||||||||
| 362 | circular => $args->{'circular'}, | ||||||||||||||||||||||||||||
| 363 | unbias => $args->{'unbias'}, | ||||||||||||||||||||||||||||
| 364 | 0 | exact => $args->{'exact'} | |||||||||||||||||||||||||||
| 365 | ); | ||||||||||||||||||||||||||||
| 366 | 0 | my $df = scalar @{$lags}; | |||||||||||||||||||||||||||
| 367 | 0 | my $sum = 0; | |||||||||||||||||||||||||||
| 368 | 0 | foreach my $i ( 1 .. $df - 1 ) { | |||||||||||||||||||||||||||
| 369 | 0 | $sum += $coeffs->[$i]**2; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | my $q = $n * $sum; | |||||||||||||||||||||||||||
| 372 | 0 | require Math::Cephes; | |||||||||||||||||||||||||||
| 373 | 0 | my $pval = Math::Cephes::igamc( $df / 2, $q / 2 ); | |||||||||||||||||||||||||||
| 374 | 0 | ▶ | return wantarray ? ( $q, $df, $pval ) : $pval; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | 7 | *boxpierce = \&qtest; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 326 | ▶ | sub _get_args { | ||||||||||||||||||||||||||
|
Subroutine
_get_args: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 379 | 326 | my $self = shift; | |||||||||||||||||||||||||||
| 380 | 326 | ▶ | my $args = ref $_[0] ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 326 | ▶ | my $data = ref $args->{'data'} ? $args->{'data'} : $self->access($args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 326 | ▶ | my $n = scalar @{$data} or croak 'No data are available'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | #croak __PACKAGE__, ': Can\'t handle dataset less than two elements' if $n < 2; | ||||||||||||||||||||||||||||
| 385 | 326 | ▶ | $args->{'lag'} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"lag"} ||= 0
MC/DC100%1/1
$args->{"lag"} ||= 0
$args->{"lag"}
Truth table100%2/2
A
A= $args->{"lag"}
| |||||||||||||||||||||||||||||
| 386 | return $n <= abs $args->{'lag'} | ||||||||||||||||||||||||||||
| 387 | ? q{} | ||||||||||||||||||||||||||||
| 388 | 326 | ▶ | : ( $self, $args, $data, $n, $args->{'lag'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | # Kendall's (1973) Eq. 3.35., p. 40 | ||||||||||||||||||||||||||||
| 392 | 50 | ▶ | sub _coeff_exact { | ||||||||||||||||||||||||||
|
Subroutine
_coeff_exact: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 393 | 50 | my ( $data, $n, $k, $circ, $unbias ) = @_; | |||||||||||||||||||||||||||
| 394 | 50 | ▶ | my ( $m1, $m2 ) = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | $circ | ||||||||||||||||||||||||||||
| 396 | ? _comean_circ( $data, $n, $k ) | ||||||||||||||||||||||||||||
| 397 | : _comean_uncirc( $data, $n, $k ) | ||||||||||||||||||||||||||||
| 398 | ; # means for original and lagged sub-sequence | ||||||||||||||||||||||||||||
| 399 | 50 | ▶ | my $covar = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | $circ | ||||||||||||||||||||||||||||
| 401 | ? _covarsum_circ( $data, $n, $k, $m1, $m2 ) | ||||||||||||||||||||||||||||
| 402 | : _covarsum_uncirc( $data, $n, $k, $m1, $m2 ) | ||||||||||||||||||||||||||||
| 403 | ; # numerator (autocovariance) | ||||||||||||||||||||||||||||
| 404 | # calc denominator (variance): | ||||||||||||||||||||||||||||
| 405 | 50 | my ( $sum_sq_ui, $sum_sq_uik ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 406 | 50 | for my $i ( 0 .. $n - $k - 1 ) { | |||||||||||||||||||||||||||
| 407 | 8775 | $sum_sq_ui += ( $data->[$i] - $m1 )**2; | |||||||||||||||||||||||||||
| 408 | 8775 | $sum_sq_uik += ( $data->[ $i + $k ] - $m2 )**2; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | 50 | my $var = sqrt($sum_sq_ui) * sqrt($sum_sq_uik); # * 2 * ($n - $k); | |||||||||||||||||||||||||||
| 411 | 50 | ▶ | return $var ? $covar / $var : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | # Kendall (1973) Eq. 3.36 | ||||||||||||||||||||||||||||
| 415 | 275 | ▶ | sub _coeff_approx { | ||||||||||||||||||||||||||
|
Subroutine
_coeff_approx: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 416 | 275 | my ( $data, $n, $k, $circ, $unbias ) = @_; | |||||||||||||||||||||||||||
| 417 | 275 | my ( $mean, $covar, $var ) = (); | |||||||||||||||||||||||||||
| 418 | 275 | $mean = mean( @{$data} ) | |||||||||||||||||||||||||||
| 419 | ; # uses mean of the whole sequence, regardless of lag, in calculating autocovariance and variance | ||||||||||||||||||||||||||||
| 420 | 275 | ▶ | $covar = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | $circ | ||||||||||||||||||||||||||||
| 422 | ? _covarsum_circ( $data, $n, $k, $mean ) | ||||||||||||||||||||||||||||
| 423 | : _covarsum_uncirc( $data, $n, $k, $mean, undef ); | ||||||||||||||||||||||||||||
| 424 | 275 | $var = _sumsq( $data, $n, $mean ); | |||||||||||||||||||||||||||
| 425 | 275 | ▶ | if ($unbias) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | 0 | $covar /= $n - $k; | |||||||||||||||||||||||||||
| 427 | 0 | $var /= $n; #variancep(@$data); | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | 275 | ▶ | return $var ? $covar / $var : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 0 | ▶ | sub _comean_circ { | ||||||||||||||||||||||||||
|
Subroutine
_comean_circ: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 433 | 0 | my ( $data, $n, $k ) = @_; | |||||||||||||||||||||||||||
| 434 | 0 | my ( $sum1, $sum2 ) = (0); | |||||||||||||||||||||||||||
| 435 | 0 | for my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||
| 436 | 0 | $sum1 += $data->[$i]; | |||||||||||||||||||||||||||
| 437 | 0 | ▶ | $sum2 += $data->[ $i + $k >= $n ? abs( $n - $i - $k ) : $i + $k ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | 0 | return ( $sum1 / $n, $sum2 / $n ); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 50 | ▶ | sub _comean_uncirc { | ||||||||||||||||||||||||||
|
Subroutine
_comean_uncirc: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 443 | 50 | my ( $data, $n, $k ) = @_; | |||||||||||||||||||||||||||
| 444 | 50 | $n -= $k; | |||||||||||||||||||||||||||
| 445 | 50 | my ( $sum1, $sum2 ) = (0); | |||||||||||||||||||||||||||
| 446 | 50 | for my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||
| 447 | 8775 | $sum1 += $data->[$i]; | |||||||||||||||||||||||||||
| 448 | 8775 | $sum2 += $data->[ $i + $k ]; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | 50 | return ( $sum1 / $n, $sum2 / $n ); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 0 | ▶ | sub _covarsum_circ { | ||||||||||||||||||||||||||
|
Subroutine
_covarsum_circ: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 454 | 0 | my ( $data, $n, $k, $m1, $m2 ) = @_; | |||||||||||||||||||||||||||
| 455 | 0 | ▶ | $m2 = $m1 if !defined $m2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | 0 | my $sum = 0; | |||||||||||||||||||||||||||
| 457 | 0 | for my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||
| 458 | 0 | ▶ | $sum += | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | ( $data->[$i] - $m1 ) * | ||||||||||||||||||||||||||||
| 460 | ( $data->[ $i + $k >= $n ? abs( $n - $i - $k ) : $i + $k ] - $m2 ); | ||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | 0 | return $sum; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 325 | ▶ | sub _covarsum_uncirc { | ||||||||||||||||||||||||||
|
Subroutine
_covarsum_uncirc: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 466 | 325 | my ( $data, $n, $k, $m1, $m2 ) = @_; | |||||||||||||||||||||||||||
| 467 | 325 | ▶ | $m2 = $m1 if !defined $m2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | 325 | my $sum = 0; | |||||||||||||||||||||||||||
| 469 | 325 | for my $i ( 0 .. $n - $k - 1 ) { | |||||||||||||||||||||||||||
| 470 | 39687 | $sum += ( $data->[$i] - $m1 ) * ( $data->[ $i + $k ] - $m2 ); | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | 325 | return $sum; | |||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 275 | ▶ | sub _sumsq { | ||||||||||||||||||||||||||
|
Subroutine
_sumsq: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 476 | 275 | my ( $data, $n, $mean ) = @_; | |||||||||||||||||||||||||||
| 477 | 275 | my $sum = 0; | |||||||||||||||||||||||||||
| 478 | 275 | for my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||
| 479 | 52192 | $sum += ( $data->[$i] - $mean )**2; | |||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | 275 | return $sum; | |||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head1 REFERENCES | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | Anderson, R.L. (1941). Distribution of the serial correlation coefficients. I<Annals of Mathematical Statistics>, I<8>, 1-13. | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | Bartlett M.S. (1946). On the theoretical specification of sampling properties of autocorrelated time series. I<Journal of the Royal Statistical Society>, I<27>. | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | Box, G.E, & Jenkins, G. (1976). I<Time series analysis: Forecasting and control>. San Francisco, US: Holden-Day. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | Box, G.E., & Pierce D. (1970). Distribution of residual autocorrelations in ARIMA time series models. I<Journal of the American Statistical Association>, I<65>, 1509-1526. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | Chatfield, C. (1975). I<The analysis of time series: Theory and practice>. London, UK: Chapman and Hall. | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | Kendall, M. G. (1973). I<Time-series>. London, UK: Griffin. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | L<Statistics::SerialCorrelation|Statistics::SerialCorrelation> (L<at cpan|http://www.cpan.org/>). Returns single autocorrelation coefficient which, with the present modules, would be given by L<coefficient|Statistics::Autocorrelation/coefficient> given B<lag> => 1, B<circular> => 1 (and the defaults B<exact> => 0, B<unbias> => 0). | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | Roderick Garton, C<< <rgarton at cpan.org> >> | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head1 DIAGNOSTICS | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =over 4 | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =item No data are available | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Croaked by most methods if they do not receive data as given in the call by an array ref, or as pre-loaded as per L<Statistics::Data|Statistics::Data>. | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =item Value given for argument 'nlags' is not valid | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | Croaked by L<correlogram|Statistics::Autocorrelation/correlogram> when the nlags is not valid: should be no more than the number of data elements less 1. | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =item file opening/printing errors | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | Croaked by L<correlogram_chart|Statistics::Autcorrelation/correlogram_chart> when it tries to print the chart. | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | =back | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | L<Statistics::Data|Statistics::Data> - used: base | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | L<Statistics::Lite|Statistics::Lite> - used: mean | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | L<List::AllUtils|List::AllUtils> - used: mesh | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | L<Statistics::Zed|Statistics::Zed> - required if calling L<ztest_bartlett|Statistics::Autocorrelation/ztest_bartlett> | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | L<Math::Cephes|Math::Cephes> - required for igamc method is calling L<qtest|Statistics::Autocorrelation/qtest> | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =head1 BUGS AND LIMITATIONS | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | Report to C<bug-statistics-autocorrelation-0.06 at rt.cpan.org> or L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Statistics-Autocorrelation-0.06>. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | To do: rho_ctest, rho_ztest | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | Find documentation for this module with the perldoc command: | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | perldoc Statistics::Autocorrelation | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | Also look for information at: | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | =over 4 | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Statistics-Autocorrelation-0.06> | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | L<http://annocpan.org/dist/Statistics-Autocorrelation-0.06> | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | L<http://cpanratings.perl.org/d/Statistics-Autocorrelation-0.06> | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | L<http://search.cpan.org/dist/Statistics-Autocorrelation-0.06/> | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | =back | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | Copyright 2011-2014 Roderick Garton. | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 575 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 576 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =cut | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 7 | 1; # End of Statistics::Autocorrelation | |||||||||||||||||||||||||||