Last active
August 29, 2015 14:16
-
-
Save preaction/3df4d95b357ce9810023 to your computer and use it in GitHub Desktop.
Basic time series outlier analysis using PDL and TAlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use feature qw( :5.16 ); | |
use PDL; | |
use PDL::Finance::TA; | |
my $threshold = 4; | |
my $limit = 100; | |
my $period = 30; | |
my $p = pdl [ map { rand() * 60 + 10 } 1..100000 ]; | |
#say $p->slice('1:100'); | |
my $roc = ta_roc( $p, 1 ); | |
#say $roc->slice( '1:100' ); | |
my $avg = ta_sma( $p, $period ); | |
my $stddev = ta_stddev( $p, $period, 1 )->abs * $threshold; | |
#say $stddev->slice( '1:100' ); | |
my $stddev_roc = ta_stddev( $roc, $period, 1 )->abs * $threshold; | |
#say $stddev_roc->slice( '1:100' ); | |
my %alerts = ( | |
shock => which( $roc > $stddev_roc ), | |
stddev => which( abs( $p - $avg ) > $stddev ), | |
flat => which( ta_stddev( $p, 5, 1 ) == 0 ), | |
); | |
for my $alert_name ( keys %alerts ) { | |
my $alert = $alerts{ $alert_name }; | |
my $num_alerts = $alert->getdim(0); | |
say $num_alerts . " alerts ($alert_name)"; | |
next unless $num_alerts; | |
my $show = $num_alerts <= $limit ? $num_alerts - 1 : $limit; | |
my $slice = '0:' . $show; | |
say $alert->slice( $slice ); | |
say $p->slice( $alert->slice( $slice ) ); | |
say $stddev->slice( $alert->slice( $slice ) ); | |
say $roc->slice( $alert->slice( $slice ) ); | |
say $stddev_roc->slice( $alert->slice( $slice ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment