#!/usr/bin/perl

use 5.040.3;

# generic discrete probability

# simular pares de inputs 
my (@x, @f);

for (0 .. 1000){
    push @x, 100*rand();
    push @f, 100*rand();
}

################

my $report;
$report = discrete(\@x, \@f);

say $report, "\n";

exit 3;


sub discrete {
    my ($i, $j) = @_;
    my @x = @$i;
    my @f = @$j;
    my @idx = 0 .. scalar(@x)-1;
    @idx = sort { $x[$a] <=> $x[$b] } @idx;   # ascendente, por defecto, pero podría no ser así
    my %h;
    my $sum = 0;
    my %histogram;
    for my $i ( @idx ){
	$h { $x[$i] } = $f[$i];
	$sum += $f[$i];
	$histogram { $x[$i] } += $sum;
    }
    for my $i ( @idx ){
	$histogram { $x[$i] } /= $sum;
    }
    my $string;
    $string = "\nListing report of ascendent probability by ocurrence\n\n";
    for my $k ( sort keys %histogram ) {
	$string .= "$k -> $histogram{ $k }\n";
    }
    return $string;
}
    
