NAME

PDL::Stats::TS -- basic time series functions

DESCRIPTION

The terms FUNCTIONS and METHODS are arbitrarily used to refer to methods that are threadable and methods that are NOT threadable, respectively. Plots require PDL::Graphics::Simple.

***EXPERIMENTAL!*** In particular, bad value support is spotty and may be shaky. USE WITH DISCRETION!

SYNOPSIS

use PDL::LiteF;
use PDL::Stats::TS;

my $r = $data->acf(5);

FUNCTIONS

acf

Signature: (x(t); [o]r(h); IV lag=>h)
Types: (float double)
$r = acf($x, $lag);
acf($x, $r, $lag);  # all arguments given
$r = $x->acf($lag); # method call
$x->acf($r, $lag);

Autocorrelation function for up to lag h. If h is not specified it's set to t-1 by default.

acf does not process bad values.

usage:

pdl> $a = sequence 10

# lags 0 .. 5

pdl> p $a->acf(5)
[1 0.7 0.41212121 0.14848485 -0.078787879 -0.25757576]

Broadcasts over its inputs.

acf does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

acvf

Signature: (x(t); [o]v(h); IV lag=>h)
Types: (float double)
$v = acvf($x, $lag);
acvf($x, $v, $lag);  # all arguments given
$v = $x->acvf($lag); # method call
$x->acvf($v, $lag);

Autocovariance function for up to lag h. If h is not specified it's set to t-1 by default.

acvf does not process bad values.

usage:

pdl> $a = sequence 10

# lags 0 .. 5

pdl> p $a->acvf(5)
[82.5 57.75 34 12.25 -6.5 -21.25]

# autocorrelation

pdl> p $a->acvf(5) / $a->acvf(0)
[1 0.7 0.41212121 0.14848485 -0.078787879 -0.25757576]

Broadcasts over its inputs.

acvf does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

dseason

Signature: (x(t); indx d(); [o]xd(t))
Types: (float double)
$xd = dseason($x, $d);
dseason($x, $d, $xd);  # all arguments given
$xd = $x->dseason($d); # method call
$x->dseason($d, $xd);

Deseasonalize data using moving average filter the size of period d.

Broadcasts over its inputs.

dseason processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

fill_ma

Signature: (x(t); indx q(); [o]xf(t))
Types: (float double)
$xf = fill_ma($x, $q);
fill_ma($x, $q, $xf);  # all arguments given
$xf = $x->fill_ma($q); # method call
$x->fill_ma($q, $xf);

Fill missing value with moving average. xf(t) = sum(x(t-q .. t-1, t+1 .. t+q)) / 2q.

fill_ma does handle bad values. Output pdl bad flag is cleared unless the specified window size q is too small and there are still bad values.

Broadcasts over its inputs.

fill_ma processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

filter_exp

Signature: (x(t); a(); [o]xf(t))
Types: (float double)
$xf = filter_exp($x, $a);
filter_exp($x, $a, $xf);  # all arguments given
$xf = $x->filter_exp($a); # method call
$x->filter_exp($a, $xf);

Filter, exponential smoothing. xf(t) = a * x(t) + (1-a) * xf(t-1)

Broadcasts over its inputs.

filter_exp does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

filter_ma

Signature: (x(t); indx q(); [o]xf(t))
Types: (float double)
$xf = filter_ma($x, $q);
filter_ma($x, $q, $xf);  # all arguments given
$xf = $x->filter_ma($q); # method call
$x->filter_ma($q, $xf);

Filter, moving average. xf(t) = sum(x(t-q .. t+q)) / (2q + 1)

Broadcasts over its inputs.

filter_ma does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

mae

Signature: (a(n); b(n); [o]c())
Types: (float double)
$c = mae($a, $b);
mae($a, $b, $c);  # all arguments given
$c = $a->mae($b); # method call
$a->mae($b, $c);

Mean absolute error. MAE = 1/n * sum( abs(y - y_pred) )

Broadcasts over its inputs.

mae processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

mape

Signature: (a(n); b(n); [o]c())
Types: (float double)
$c = mape($a, $b);
mape($a, $b, $c);  # all arguments given
$c = $a->mape($b); # method call
$a->mape($b, $c);

Mean absolute percent error. MAPE = 1/n * sum(abs((y - y_pred) / y))

Broadcasts over its inputs.

mape processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

wmape

Signature: (a(n); b(n); [o]c())
Types: (float double)
$c = wmape($a, $b);
wmape($a, $b, $c);  # all arguments given
$c = $a->wmape($b); # method call
$a->wmape($b, $c);

Weighted mean absolute percent error. avg(abs(error)) / avg(abs(data)). Much more robust compared to mape with division by zero error (cf. Schütz, W., & Kolassa, 2006).

Broadcasts over its inputs.

wmape processes bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

portmanteau

Signature: (r(h); longlong t(); [o]Q())
Types: (float double)
$Q = portmanteau($r, $t);
portmanteau($r, $t, $Q);  # all arguments given
$Q = $r->portmanteau($t); # method call
$r->portmanteau($t, $Q);

Portmanteau significance test (Ljung-Box) for autocorrelations.

Usage:

  pdl> $a = sequence 10

  # acf for lags 0-5
  # lag 0 excluded from portmanteau

  pdl> p $chisq = $a->acf(5)->portmanteau( $a->nelem )
  11.1753902662994

  # get p-value from chisq distr

  pdl> use PDL::GSL::CDF
  pdl> p 1 - gsl_cdf_chisq_P( $chisq, 5 )
  0.0480112934306748

Broadcasts over its inputs.

portmanteau does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

pred_ar

Signature: (x(p); b(p); [o]pred(t); IV end=>t)
Types: (float double)
$pred = pred_ar($x, $b, $end);
pred_ar($x, $b, $pred, $end);  # all arguments given
$pred = $x->pred_ar($b, $end); # method call
$x->pred_ar($b, $pred, $end);

Calculates predicted values up to period t (extend current series up to period t) for autoregressive series, with or without constant. If there is constant, it is the last element in b, as would be returned by ols or ols_t.

pred_ar does not process bad values.

CONST  => 1,

Usage:

pdl> $x = sequence 2

  # last element is constant
pdl> $b = pdl(.8, -.2, .3)

pdl> p $x->pred_ar($b, 7)
[0       1     1.1    0.74   0.492  0.3656 0.31408]

  # no constant
pdl> p $x->pred_ar($b(0:1), 7, {const=>0})
[0       1     0.8    0.44   0.192  0.0656 0.01408]

Broadcasts over its inputs.

pred_ar does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.

season_m

Given length of season, returns seasonal mean and variance for each period (returns seasonal mean only in scalar context).

Default options (case insensitive):

START_POSITION => 0,     # series starts at this position in season
MISSING        => -999,  # internal mark for missing points in season
PLOT  => 0,              # boolean
 # see PDL::Graphics::Simple for next options
WIN   => undef,          # pass pgswin object for more plotting control
COLOR => 1,
my ($m, $ms) = $data->season_m( 24, { START_POSITION=>2 } );

plot_dseason

Plots deseasonalized data and original data points. Opens and closes default window for plotting unless a WIN object is passed in options. Returns deseasonalized data.

Default options (case insensitive):

WIN   => undef,
COLOR => 1,        # data point color

METHODS

plot_acf

Plots and returns autocorrelations for a time series.

Default options (case insensitive):

SIG  => 0.05,      # can specify .10, .05, .01, or .001
WIN  => undef,

Usage:

pdl> $a = sequence 10

pdl> p $r = $a->plot_acf(5)
[1 0.7 0.41212121 0.14848485 -0.078787879 -0.25757576]

REFERENCES

Brockwell, P.J., & Davis, R.A. (2002). Introduction to Time Series and Forecasting (2nd ed.). New York, NY: Springer.

Schütz, W., & Kolassa, S. (2006). Foresight: advantages of the MAD/Mean ratio over the MAPE. Retrieved Jan 28, 2010, from http://www.saf-ag.com/226+M5965d28cd19.html

AUTHOR

Copyright (C) 2009 Maggie J. Xiong <maggiexyz users.sourceforge.net>

All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation as described in the file COPYING in the PDL distribution.