Commits

Kang-min Liu committed a58717e

Add a method for Statistics::Descriptive::Full to calculate "median absolute deviation".

Comments (0)

Files changed (3)

Statistics-Descriptive/MANIFEST

 t/descr.t
 t/descr_smooth_methods.t
 t/freq_distribution-1-rt-34999.t
+t/median_absolute_deviation.t
 t/mode.t
 t/outliers.t
 t/pod-coverage.t

Statistics-Descriptive/lib/Statistics/Descriptive.pm

   return @{ $self->_least_squares_fit() };
 }
 
+sub median_absolute_deviation {
+    my ($self) = @_;
+    my $stat = $self->new;
+    $stat->add_data(map { abs($_ - $self->median) } $self->get_data);
+    return $stat->median;
+}
+
+
 1;
 
 package Statistics::Descriptive;

Statistics-Descriptive/t/median_absolute_deviation.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+use Statistics::Descriptive;
+
+{
+    my $stat = Statistics::Descriptive::Full->new();
+
+    $stat->add_data( 1, 1, 1, 2, 2, 2, 2, 4, 7 );
+
+    my $mad = $stat->median_absolute_deviation();
+
+    # TEST
+    ok (defined($mad),
+        "median_absolute_deviation is not undefined"
+    );
+
+    # TEST
+    is($mad, 1,
+        "median_absolute_deviation is correct"
+    );
+}