Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions lib/PDL/Stats/TS.pd
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ pp_addhdr('
);

pp_def('acf',
Pars => 'x(t); [o]r(h)',
OtherPars => 'IV lag=>h',
Pars => 'x(t); [o]r(h=CALC($COMP(lag) < 0 ? $SIZE(t) : $COMP(lag) + 1))',
OtherPars => 'IV lag',
OtherParsDefaults => { lag => -1 },
GenericTypes => $F,
Code => '
$GENERIC(x) s, s2, m, cov0, covh;
Expand All @@ -75,14 +76,6 @@ loop (h) %{
}
%}
',
PMCode => pp_line_numbers(__LINE__, <<'EOF'),
sub PDL::acf {
my ($self, $h) = @_;
$h ||= $self->dim(0) - 1;
PDL::_acf_int($self, my $r = PDL->null, $h+1);
$r;
}
EOF
Doc => <<'EOD',
=for ref

Expand All @@ -104,8 +97,9 @@ EOD
);

pp_def('acvf',
Pars => 'x(t); [o]v(h)',
OtherPars => 'IV lag=>h;',
Pars => 'x(t); [o]v(h=CALC($COMP(lag) < 0 ? $SIZE(t) : $COMP(lag) + 1))',
OtherPars => 'IV lag',
OtherParsDefaults => { lag => -1 },
Comment thread
duffee marked this conversation as resolved.
GenericTypes => $F,
Code => '
$GENERIC(x) s, s2, m, covh;
Expand All @@ -130,14 +124,6 @@ loop (h) %{
}
%}
',
PMCode => pp_line_numbers(__LINE__, <<'EOF'),
sub PDL::acvf {
my ($self, $h) = @_;
$h ||= $self->dim(0) - 1;
PDL::_acvf_int($self, my $v = PDL->null, $h+1);
$v;
}
EOF
Doc => <<'EOD',
=for ref

Expand Down
21 changes: 21 additions & 0 deletions t/ts.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,25 @@ use Test::PDL;
is_pdl $ms, $ans_ms, 'season_m ms';
}

subtest 'acf and acvf with lags' => sub {
my $a = sequence 10;
my $m = sequence 10,2;
my $acvf = pdl '[82.5 57.75 34 12.25 -6.5 -21.25 -31 -34.75 -31.5 -20.25]';
my $acf = $acvf / $acvf->at(0);

is_pdl $a->acvf(), $acvf, "calculate autocovariance with no arguments on $a";

is_pdl $a->acvf(4), $acvf->slice('0:4'), "calculate autocovariance with 4 lags on $a";
is_pdl $a->acvf(0), $acvf->slice('0:0'), "calculate autocovariance with 0 lag on $a";
is_pdl $a->acvf(5) / $a->acvf(0), $a->acf(5), "calculate autocorrelation from autocovariance on $a";
is_pdl $a->acvf(14), $acvf->append([0,0,0,0,0]), "autocovariance called with more lags than values";

is_pdl $a->acf(), $acf, "calculate autocorrelation with no arguments on $a";
is_pdl $a->acf(0), pdl('[1]'), 'first value of the autocorrelation function is always 1';
is_pdl $a->acf(5), $acf->slice('0:5'), 'autocorrelation function with 5 lags';

# autocorrelation on higher order pdls is a collection of 1D arrays
is_pdl $m->acvf(4), $acvf->slice('0:4')->dummy(1,2), "acvf is the same for both rows of sequence(10,2)";
};

done_testing;
Loading