| 1 | package Statistics::RankCorrelation; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Compute the rank correlation between two vectors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.1206'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 15 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | $self->_init(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 15 | ▶ | sub _init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=12 SCAR=25.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Handle vector and named parameters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 15 | ▶ | while( my $arg = shift ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 29 | ▶ | if( ref $arg eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 28 | ▶ | if( !defined $self->x_data ) { $self->x_data( $arg ) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 14 | elsif( !defined $self->y_data ) { $self->y_data( $arg ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | elsif( !ref $arg ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | my $v = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | $self->{$arg} = defined $v ? $v : $arg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Automatically compute the ranks if given data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 15 | ▶ | if( $self->x_data && $self->y_data && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$self->x_data and $self->y_data and @{$self->x_data;} and @{$self->y_data;}
$self->x_data and $self->y_data and @{$self->x_data;}
$self->x_data and $self->y_data
MC/DC25%1/4
$self->x_data and $self->y_data and @{$self->x_data;} and @{$self->y_data;}
$self->x_data
$self->y_data
@{$self->x_data;}
@{$self->y_data;}
Truth table40%2/5
A and B and C and D
A= $self->x_dataB= $self->y_dataC= @{$self->x_data;}D= @{$self->y_data;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 14 | @{ $self->x_data } && @{ $self->y_data } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # "Co-normalize" the vectors if they are of unequal size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 14 | my( $x, $y ) = pad_vectors( $self->x_data, $self->y_data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # "Co-sort" the bivariate data set by the first one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 14 | ▶ | ( $x, $y ) = co_sort( $x, $y ) if $self->{sorted}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Set the massaged data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 14 | $self->x_data( $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 14 | $self->y_data( $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Set the size of the data vector. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 14 | $self->size( scalar @{ $self->x_data } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Set the ranks and ties of the vectors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 14 | ( $x, $y ) = rank( $self->x_data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 14 | $self->x_rank( $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 14 | $self->x_ties( $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 14 | ( $x, $y ) = rank( $self->y_data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 14 | $self->y_rank( $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 14 | $self->y_ties( $y ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 196 | ▶ | sub size { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 196 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 196 | ▶ | $self->{size} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 196 | return $self->{size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 130 | ▶ | sub x_data { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
x_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 130 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 130 | ▶ | $self->{x_data} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 130 | return $self->{x_data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 101 | ▶ | sub y_data { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
y_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 101 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 101 | ▶ | $self->{y_data} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 101 | return $self->{y_data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 18 | ▶ | sub x_rank { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
x_rank: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | $self->{x_rank} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 18 | return $self->{x_rank}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 19 | ▶ | sub y_rank { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
y_rank: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | $self->{y_rank} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | return $self->{y_rank}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 46 | ▶ | sub x_ties { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
x_ties: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 46 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 46 | ▶ | $self->{x_ties} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 46 | return $self->{x_ties}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 44 | ▶ | sub y_ties { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
y_ties: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 44 | ▶ | $self->{y_ties} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 44 | return $self->{y_ties}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 14 | ▶ | sub spearman { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spearman: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Algorithm contributed by Jon Schutz <Jon.Schutz@youramigo.com>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 14 | my($x_sum, $y_sum) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 14 | $x_sum += $_ for @{$self->{x_rank}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 14 | $y_sum += $_ for @{$self->{y_rank}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 14 | my $n = $self->size; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 14 | my $x_mean = $x_sum / $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 14 | my $y_mean = $y_sum / $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Compute the sum of the difference of the squared ranks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 14 | my($x_sum2, $y_sum2, $xy_sum) = (0, 0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 14 | for( 0 .. $self->size - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 119 | $x_sum2 += ($self->{x_rank}[$_] - $x_mean) ** 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 119 | $y_sum2 += ($self->{y_rank}[$_] - $y_mean) ** 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 119 | $xy_sum += ($self->{x_rank}[$_] - $x_mean) * ($self->{y_rank}[$_] - $y_mean); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | ▶ | return 1 if $x_sum2 == 0 || $y_sum2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x_sum2 == 0 or $y_sum2 == 0
MC/DC50%1/2
$x_sum2 == 0 or $y_sum2 == 0
$x_sum2 == 0
$y_sum2 == 0
Truth table66%2/3
A or B
A= $x_sum2 == 0B= $y_sum2 == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 13 | return $xy_sum / sqrt($x_sum2 * $y_sum2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 28 | ▶ | sub rank { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rank: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 28 | my $u = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # Make a list of tied ranks for each datum. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 28 | my %ties; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 28 | push @{ $ties{ $u->[$_] } }, $_ for 0 .. @$u - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 28 | my ($old, $cur) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # Set the averaged ranks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 28 | my @ranks; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 28 | for my $x (sort { $a <=> $b } keys %ties) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # Get the number of ties. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 197 | my $ties = @{ $ties{$x} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 197 | $cur += $ties; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 197 | ▶ | if ($ties > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # Average the tied data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 15 | my $average = $old + ($ties + 1) / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 15 | $ranks[$_] = $average for @{ $ties{$x} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # Add the single rank to the list of ranks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 182 | $ranks[ $ties{$x}[0] ] = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 197 | $old = $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Remove the non-tied ranks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 28 | delete @ties{ grep @{ $ties{$_} } <= 1, keys %ties }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # Return the ranks arrayref in a scalar context and include ties | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # if called in a list context. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 28 | ▶ | return wantarray ? (\@ranks, \%ties) : \@ranks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | sub co_sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
co_sort: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | my( $u, $v ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | return unless @$u == @$v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # Ye olde Schwartzian Transforme: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | $v = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 10 | map { $_->[1] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 23 | ▶ | sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Statistics::RankCorrelation::a->[0] <=> $Statistics::RankCorrelation::b->[0] || $Statistics::RankCorrelation::a->[1] <=> $Statistics::RankCorrelation::b->[1]
MC/DC0%0/2
$Statistics::RankCorrelation::a->[0] <=> $Statistics::RankCorrelation::b->[0] || $Statistics::RankCorrelation::a->[1] <=> $Statistics::RankCorrelation::b->[1]
$Statistics::RankCorrelation::a->[0] <=> $Statistics::RankCorrelation::b->[0]
$Statistics::RankCorrelation::a->[1] <=> $Statistics::RankCorrelation::b->[1]
Truth table66%2/3
A || B
A= $Statistics::RankCorrelation::a->[0] <=> $Statistics::RankCorrelation::b->[0]B= $Statistics::RankCorrelation::a->[1] <=> $Statistics::RankCorrelation::b->[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | map { [ $u->[$_], $v->[$_] ] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 .. @$u - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Sort the independent vector last. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | $u = [ sort { $a <=> $b } @$u ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | return $u, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 3 | ▶ | sub csim { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
csim: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # Get the pitch matrices for each vector. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 3 | my $m1 = correlation_matrix($self->{x_data}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | #warn map { "@$_\n" } @$m1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 3 | my $m2 = correlation_matrix($self->{y_data}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | #warn map { "@$_\n" } @$m2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # Compute the rank correlation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | my $k = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 3 | for my $i (0 .. @$m1 - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 16 | for my $j (0 .. @$m1 - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 96 | ▶ | $k++ if $m1->[$i][$j] == $m2->[$i][$j]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # Return the rank correlation normalized by the number of rows in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # the pitch matrices. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 3 | return $k / (@$m1 * @$m1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 14 | ▶ | sub pad_vectors { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pad_vectors: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 14 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 14 | ▶ | if (@$u > @$v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $v = [ @$v, (0) x (@$u - @$v) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | elsif (@$u < @$v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | $u = [ @$u, (0) x (@$v - @$u) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 14 | return $u, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 6 | ▶ | sub correlation_matrix { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
correlation_matrix: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | my $u = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 6 | my $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # Is a row value (i) lower than a column value (j)? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | for my $i (0 .. @$u - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 32 | for my $j (0 .. @$u - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 192 | ▶ | $c->[$i][$j] = $u->[$i] < $u->[$j] ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 6 | return $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | ▶ | sub kendall { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kendall: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # Calculate number of concordant and discordant pairs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 13 | my( $concordant, $discordant ) = ( 0, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 13 | for my $i ( 0 .. $self->size - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 115 | for my $j ( $i + 1 .. $self->size - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 574 | my $x_sign = sign( $self->{x_data}[$j] - $self->{x_data}[$i] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 574 | my $y_sign = sign( $self->{y_data}[$j] - $self->{y_data}[$i] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 574 | ▶ | if (not($x_sign and $y_sign)) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$x_sign && $y_sign
MC/DC100%2/2
$x_sign && $y_sign
$x_sign
$y_sign
Truth table100%3/3
A && B
A= $x_signB= $y_sign
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 288 | elsif ($x_sign == $y_sign) { $concordant++ } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 178 | else { $discordant++ } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # Set the indirect relationship. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 13 | my $d = $self->size * ($self->size - 1) / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 13 | ▶ | if( keys %{ $self->x_ties } || keys %{ $self->y_ties } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
keys %{$self->x_ties;} or keys %{$self->y_ties;}
MC/DC100%2/2
keys %{$self->x_ties;} or keys %{$self->y_ties;}
keys %{$self->x_ties;}
keys %{$self->y_ties;}
Truth table100%3/3
A or B
A= keys %{$self->x_ties;}B= keys %{$self->y_ties;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 5 | my $x = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 5 | $x += @$_ * (@$_ - 1) for values %{ $self->x_ties }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 5 | $x = $d - $x / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 5 | my $y = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 5 | $y += @$_ * (@$_ - 1) for values %{ $self->y_ties }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 5 | $y = $d - $y / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 5 | $d = sqrt($x * $y); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 13 | return ($concordant - $discordant) / $d; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1148 | ▶ | sub sign { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1148 | my $x = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1148 | ▶ | return 0 if $x == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1024 | ▶ | return $x > 0 ? 1 : -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||