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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|