| 1 | package PDLDM::Rank; | ||
| 2 | |||
| 3 | use 5.0; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | require Exporter; | ||
| 7 | |||
| 8 | use vars qw($VERSION); | ||
| 9 | $VERSION = 2015.070902; | ||
| 10 | |||
| 11 | require Exporter; | ||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | our @EXPORT_OK = qw(TiedRank EstimateTiedRank); | ||
| 14 | |||
| 15 | use PDL; | ||
| 16 | |||
| 17 | sub EstimateTiedRank{#Estimate the tied rank of an unseen instance | ||
| 18 | my $insts = $_[0]; #Unseen instances | ||
| 19 | my $mypdl = $_[1]; #Original Dataset of which Tied Ranks are already calculated | ||
| 20 | my $ranked_pdl = $_[2]; # Tied Ranked dataset of $mypdl | ||
| 21 | |||
| 22 | my $rows = $mypdl->getdim(0); | ||
| 23 | my $cols = $mypdl->getdim(1); | ||
| 24 | |||
| 25 | my $insts_rows = $insts->getdim(0); | ||
| 26 | |||
| 27 | my $inst_ranked = PDL->zeros($insts_rows,$cols) -1; | ||
| 28 | my $inst_uniqueness = PDL->ones($insts_rows,$cols); | ||
| 29 | |||
| 30 | |||
| 31 | for (my $this_col=0; $this_col < $cols; $this_col++){ | ||
| 32 | my $original_values = $mypdl->dice_axis(1,$this_col)->flat; | ||
| 33 | my $sorted_indx = qsorti($original_values); | ||
| 34 | my $sorted_values = $original_values->dice($sorted_indx); | ||
| 35 | |||
| 36 | for (my $this_inst_raw=0; $this_inst_raw < $insts_rows; $this_inst_raw++){ | ||
| 37 | my $this_value = $insts->dice($this_inst_raw,$this_col)->flat; | ||
| 38 | |||
| 39 | for (my $i = 0; $i < $rows; $i++){ | ||
| 40 | my $sorted_val = $sorted_values->dice($i)->flat; | ||
| 41 | if ($this_value == $sorted_val) { | ||
| 42 | SetValue($inst_ranked->dice($this_inst_raw,$this_col), | ||
| 43 | $ranked_pdl->dice($sorted_indx->dice($i),$this_col)); | ||
| 44 | SetValue($inst_uniqueness->dice($this_inst_raw,$this_col),0,0); | ||
| 45 | last; | ||
| 46 | }elsif ($this_value < $sorted_val) { | ||
| 47 | SetValue($inst_ranked->dice($this_inst_raw,$this_col),$i); | ||
| 48 | last; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | if ($inst_ranked->dice($this_inst_raw,$this_col) == -1) { | ||
| 52 | SetValue($inst_ranked->dice($this_inst_raw,$this_col),$rows); | ||
| 53 | } | ||
| 54 | |||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | return ($inst_ranked,$inst_uniqueness); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub TiedRank{ #Calculate Tied Ranks | ||
| 62 | my $mypdl = $_[0]; | ||
| 63 | my $rows = $mypdl->getdim(0); | ||
| 64 | my $cols = $mypdl->getdim(1); | ||
| 65 | my $ranked_pdl = PDL->zeros($rows,$cols); | ||
| 66 | my $duplicates_pdl = PDL->zeros($rows,$cols); | ||
| 67 | |||
| 68 | for (my $this_col=0; $this_col < $cols; $this_col++){ | ||
| 69 | my $sorted_indx = qsorti($mypdl->dice_axis(1,$this_col)->flat); | ||
| 70 | my $last_ranked_indx = -1; | ||
| 71 | my $last_rank = 0; | ||
| 72 | my $last_value = $mypdl->dice($sorted_indx->dice(0),$this_col); | ||
| 73 | my $duplicate_count = 1; | ||
| 74 | |||
| 75 | for (my $i=1; $i <= $rows; $i++){ | ||
| 76 | my $this_value = 0; | ||
| 77 | if ($i < $rows) { | ||
| 78 | $this_value =$mypdl->dice($sorted_indx->dice($i),$this_col); | ||
| 79 | } | ||
| 80 | if (($this_value > $last_value) || ($i == $rows)) { | ||
| 81 | my $average_rank = ((2 * $last_rank) + $duplicate_count + 1) / 2; | ||
| 82 | #my $ranked_slice = PDL->ones($duplicate_count,1) * $average_rank; | ||
| 83 | my $rank_start_indx = $last_ranked_indx + 1; | ||
| 84 | $last_ranked_indx = $last_ranked_indx + $duplicate_count; | ||
| 85 | |||
| 86 | for (my $tmp_rank_indx=$rank_start_indx;$tmp_rank_indx <= $last_ranked_indx; $tmp_rank_indx++){ | ||
| 87 | my $tmp_correct_indx = sclr $sorted_indx->dice($tmp_rank_indx); | ||
| 88 | SetValue($ranked_pdl->slice("($tmp_correct_indx),($this_col)"),$average_rank); | ||
| 89 | SetValue($duplicates_pdl->slice("($tmp_correct_indx),($this_col)"),$duplicate_count); | ||
| 90 | } | ||
| 91 | |||
| 92 | $duplicate_count = 1; | ||
| 93 | $last_value = $this_value; | ||
| 94 | $last_rank = $i; | ||
| 95 | }else{ | ||
| 96 | $duplicate_count++; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | return ($ranked_pdl,$duplicates_pdl); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub SetValue{ | ||
| 104 | my $this_slice = $_[0]; | ||
| 105 | my $new_values = $_[1]; | ||
| 106 | my $ops = $_[2]; #0 = Substitute, 1 = add, 2= substract | ||
| 107 | |||
| 108 | if (!(defined($ops))) { | ||
| 109 | $ops = 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | if ($ops == 0) { | ||
| 113 | $this_slice .= $new_values; | ||
| 114 | }elsif($ops == 1){ | ||
| 115 | $this_slice += $new_values; | ||
| 116 | }elsif($ops == 2){ | ||
| 117 | $this_slice -= $new_values; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | |||
| 123 | __END__ |