in reply to A lesson in statistics

I don't know much statistics, but it seemed like the average of the last x samples would do what you want. This code behaves how I interpreted what you want, changing samples will change how much data it holds to average over, that would be a matter of preference on your part. I would think if you don't restrict the samples you'll just end up with jibberhish, assuming you are sampling some source for this data on a regular basis. If that is the case than this will tell you if at any point the last x samples averaged over 15:1.

use strict; use warnings; use Data::Dumper; my @que = (); my $sample = 5; sub average_ratio { my @data = @_; my $ratio = 0; for (@data) { $ratio = $ratio + ($_->[1] != 0 ? ($_->[0] / $_->[1]) : 0); } return $ratio / scalar @data; } while(my $line = <DATA>) { chomp $line; my ($po, $fr) = split (m/\s/, $line); push @que, [$po, $fr]; shift @que if @que > $sample; my $avg = average_ratio(@que); print "Adding [$po,\t $fr]\t makes the avg_ratio: $avg\n"; print "DANGER\n" if $avg > 15; } __DATA__ 0 0 0 0 150 10 0 0 200 40 210 40 220 40 220 30 0 0 0 0 0 0 220 20 220 10 220 05 220 01 220 100 220 100 2200 100 2200 2 2200 2 0 0 2200 1 0 0 0 0 0 0 0 0 0 0 0 0

___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: A lesson in statistics
by 0xbeef (Hermit) on Mar 20, 2007 at 22:13 UTC
    Hi Eric,

    Thanks for your nice example, it matches what I currently deem the best solution (with input from others here) - calculating the mean of the ratio.

    I'm not sure if there is a way to eliminate more false positives... but using this method a single spike will at least not cause an exception.

    Niel