Commits

barbasa  committed dc83d35

Passing statistics object in outlier filter function

Having the object in the function might be useful for complex filters
where statistics property are needed

  • Participants
  • Parent commits 4760049

Comments (0)

Files changed (2)

File Statistics-Descriptive/lib/Statistics/Descriptive.pm

 
     my $outlier_candidate_index = $self->_outlier_candidate_index;
     my $possible_outlier = ($self->_data())->[$outlier_candidate_index];
-    my $is_outlier = $self->{_outlier_filter}->($possible_outlier);
+    my $is_outlier = $self->{_outlier_filter}->($self, $possible_outlier);
 
     return $self->get_data unless $is_outlier;
     # Removing the outlier from the dataset
 
 Example #2: Valid code reference
 
-    sub outlier_filter { return $_[0] > 1; }
+    sub outlier_filter { return $_[1] > 1; }
 
     my $stat = Statistics::Descriptive::Full->new();
     $stat->add_data( 1, 1, 1, 100, 1, );
 For more complex series the outlier filter function might be more complex
 (see Grubbs' test for outliers).
 
+The outlier filter function will receive as first parameter the Statistics::Descriptive::Full object,
+as second the value of the candidate outlier. Having the object in the function
+might be useful for complex filters where statistics property are needed (again see Grubbs' test for outlier).
+
 =back
 
 =item $stat->set_smoother({ method => 'exponential', coeff => 0, });

File Statistics-Descriptive/t/outliers.t

 use strict;
 use warnings;
 
-use Test::More tests => 9;
+use Test::More tests => 11;
 
 use Statistics::Descriptive;
 
     $stat->add_data( 1, 2, 3, 4, 100, 6, 7, 8 );
 
     # We force the filter function to always detect outliers for this data set
-    $stat->set_outlier_filter( sub {$_[0] > 0} );
+    $stat->set_outlier_filter( sub {$_[1] > 0} );
     my @results = $stat->get_data_without_outliers();
 
     # Note that 100 has been filtered out from the data set
 
 }
 
+my ($first_val, $second_val);
+sub check_params { ($first_val, $second_val) = @_; }
+
+{
+    # testing params passed to outlier filter
+    my $stat = Statistics::Descriptive::Full->new();
+
+    # 100 is definitively the candidate to be an outlier in this series
+    $stat->add_data( 1, 2, 3, 4, 100, 6, 7, 8 );
+
+    $stat->set_outlier_filter( \&check_params );
+    my @results = $stat->get_data_without_outliers();
+
+    # TEST
+    isa_ok ($first_val, 'Statistics::Descriptive::Full', 'first param of outlier filter ok');
+    # TEST
+    is ($second_val, 100, 'second param of outlier filter ok');
+
+}
+
 {
     # testing _outlier_candidate_index
     my $stat = Statistics::Descriptive::Full->new();