#!/usr/bin/perl -w

use strict;

# Short Term Rate (STR)

my $NN = int 1234*rand();
print "Number of transactions is $NN\n";

my ($n, @inte);
$n=0;
for (0..$NN-1){
    $inte[$n][0] =  sprintf ("%.05f", -1*rand()/10);     # interest
    $inte[$n][1] = int rand() * 10_000_000;            # volume of transaction
    $inte[$n][2] = int rand()*100;                     # no. of bank
    $n++;
}
$n--;

my @oint = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @inte;

my $sum;
for my $i (0..$n){
    $oint[$i][3] = $oint[$i][0] * $oint[$i][1];
#    $oint[$i][4] = ($i>0) ? ( $oint[$i][3] + $oint[$i-1][3] ) : $oint[$i][3] ;
    $sum += $oint[$i][1];
}

# erasing bottom and top 25% of volume

my @dump;
my $sdump;
my %hbank2;
my $kountingency2;
my $str;
my $vol;
my %hbank;
my $kountingency1;
for my $i (0..$n){
    $hbank2{ $oint[$i][2] } += $oint[$i][1];
    if ($i/$n <= 0.25 || $i/$n >= 0.75){                  # no strict ineq to discard more
	push @dump, $oint[$i][1];
	$sdump += $oint[$i][1];
	$oint[$i][1] = 0;
    }else{
	$str += $oint[$i][3];
	$vol += $oint[$i][1];
	$hbank{ $oint[$i][2] }++;
	if ($hbank{ $oint[$i][2] } ==1 ){ $kountingency1++; }
    }
}

warn "Rate are outlayed\n" if (($sdump/$sum > 0.51 || $sdump/$sum < 0.49) && ($vol/$sum > 0.51 || $vol/$sum < 0.49));

print "Number of operations discarded is ", scalar(@dump), "\n";

$str /= $vol;

print "The STR is = $str\n";

if ( $kountingency1 < 20 ){
    print "Contingency #1 is detected\n";
    print "• the number of reporting banks is less than 20\n";
}

my $c;
for (reverse sort values %hbank2){
    $c++;
    $kountingency2 += $_;
    last if $c >= 5;
}

if ( $kountingency2 >= 0.75 * $sum ){
    print "Contingency #2 is detected\n";
    print "• five banks account for 75% or more of total transaction volumes.\n";
}

exit 1;

__END__
  

