Skip to content

Commit 13dc82e

Browse files
committed
Fix acf and acvf default handling
Moved PMCode logic to Pars with defaults and added subtest
1 parent 6c62e2b commit 13dc82e

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

lib/PDL/Stats/TS.pd

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ pp_addhdr('
4848
);
4949

5050
pp_def('acf',
51-
Pars => 'x(t); [o]r(h)',
52-
OtherPars => 'IV lag=>h',
51+
Pars => 'x(t); [o]r(h=CALC($COMP(lag) < 0 ? $SIZE(t) : $COMP(lag) + 1))',
52+
OtherPars => 'IV lag',
53+
OtherParsDefaults => { lag => -1 },
5354
GenericTypes => $F,
5455
Code => '
5556
$GENERIC(x) s, s2, m, cov0, covh;
@@ -75,14 +76,6 @@ loop (h) %{
7576
}
7677
%}
7778
',
78-
PMCode => pp_line_numbers(__LINE__, <<'EOF'),
79-
sub PDL::acf {
80-
my ($self, $h) = @_;
81-
$h ||= $self->dim(0) - 1;
82-
PDL::_acf_int($self, my $r = PDL->null, $h+1);
83-
$r;
84-
}
85-
EOF
8679
Doc => <<'EOD',
8780
=for ref
8881

@@ -104,8 +97,9 @@ EOD
10497
);
10598

10699
pp_def('acvf',
107-
Pars => 'x(t); [o]v(h)',
108-
OtherPars => 'IV lag=>h;',
100+
Pars => 'x(t); [o]v(h=CALC($COMP(lag) < 0 ? $SIZE(t) : $COMP(lag) + 1))',
101+
OtherPars => 'IV lag',
102+
OtherParsDefaults => { lag => -1 },
109103
GenericTypes => $F,
110104
Code => '
111105
$GENERIC(x) s, s2, m, covh;
@@ -130,14 +124,6 @@ loop (h) %{
130124
}
131125
%}
132126
',
133-
PMCode => pp_line_numbers(__LINE__, <<'EOF'),
134-
sub PDL::acvf {
135-
my ($self, $h) = @_;
136-
$h ||= $self->dim(0) - 1;
137-
PDL::_acvf_int($self, my $v = PDL->null, $h+1);
138-
$v;
139-
}
140-
EOF
141127
Doc => <<'EOD',
142128
=for ref
143129

t/ts.t

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ use Test::PDL;
5858
is_pdl $ms, $ans_ms, 'season_m ms';
5959
}
6060

61+
subtest 'acf and acvf with lags' => sub {
62+
my $a = sequence 10;
63+
my $m = sequence 10,2;
64+
my $acvf = pdl '[82.5 57.75 34 12.25 -6.5 -21.25 -31 -34.75 -31.5 -20.25]';
65+
my $acf = $acvf / $acvf->at(0);
66+
67+
is_pdl $a->acvf(), $acvf, "calculate autocovariance with no arguments on $a";
68+
69+
is_pdl $a->acvf(4), $acvf->slice('0:4'), "calculate autocovariance with 4 lags on $a";
70+
is_pdl $a->acvf(0), $acvf->slice('0:0'), "calculate autocovariance with 0 lag on $a";
71+
is_pdl $a->acvf(5) / $a->acvf(0), $a->acf(5), "calculate autocorrelation from autocovariance on $a";
72+
is_pdl $a->acvf(14), $acvf->append([0,0,0,0,0]), "autocovariance called with more lags than values";
73+
74+
is_pdl $a->acf(), $acf, "calculate autocorrelation with no arguments on $a";
75+
is_pdl $a->acf(0), pdl('[1]'), 'first value of the autocorrelation function is always 1';
76+
is_pdl $a->acf(5), $acf->slice('0:5'), 'autocorrelation function with 5 lags';
77+
78+
# autocorrelation on higher order pdls is a collection of 1D arrays
79+
is_pdl $m->acvf(4), $acvf->slice('0:4')->dummy(1,2), "acvf is the same for both rows of sequence(10,2)";
80+
};
81+
6182
done_testing;

0 commit comments

Comments
 (0)