line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Statistics::Basic::Covariance; |
3
|
|
|
|
|
|
|
|
4
|
33
|
|
|
33
|
|
139
|
use strict; |
|
33
|
|
|
|
|
43
|
|
|
33
|
|
|
|
|
1188
|
|
5
|
33
|
|
|
33
|
|
143
|
use warnings; |
|
33
|
|
|
|
|
38
|
|
|
33
|
|
|
|
|
907
|
|
6
|
33
|
|
|
33
|
|
128
|
use Carp; |
|
33
|
|
|
|
|
39
|
|
|
33
|
|
|
|
|
2101
|
|
7
|
|
|
|
|
|
|
|
8
|
33
|
|
|
33
|
|
143
|
use base 'Statistics::Basic::_TwoVectorBase'; |
|
33
|
|
|
|
|
37
|
|
|
33
|
|
|
|
|
12982
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# new {{{ |
11
|
|
|
|
|
|
|
sub new { |
12
|
18
|
|
|
18
|
1
|
1286
|
my $class = shift; |
13
|
18
|
|
100
|
|
|
47
|
my @var1 = (shift || ()); |
14
|
18
|
|
100
|
|
|
44
|
my @var2 = (shift || ()); |
15
|
18
|
50
|
|
|
|
23
|
my $v1 = eval { Statistics::Basic::Vector->new( @var1 ) } or croak $@; |
|
18
|
|
|
|
|
172
|
|
16
|
18
|
50
|
|
|
|
27
|
my $v2 = eval { Statistics::Basic::Vector->new( @var2 ) } or croak $@; |
|
18
|
|
|
|
|
55
|
|
17
|
|
|
|
|
|
|
|
18
|
18
|
|
|
|
|
51
|
my $c = $v1->_get_linked_computer( covariance => $v2 ); |
19
|
18
|
100
|
|
|
|
41
|
return $c if $c; |
20
|
|
|
|
|
|
|
|
21
|
17
|
|
|
|
|
89
|
my $this = bless({'v1'=>$v1, 'v2'=>$v2}, $class); |
22
|
17
|
50
|
|
|
|
44
|
warn "[new " . ref($this) . " v1:$this->{v1} v2:$this->{v2}]\n" if $Statistics::Basic::DEBUG >= 2; |
23
|
|
|
|
|
|
|
|
24
|
17
|
|
|
|
|
508
|
$this->{_vectors} = [ $v1, $v2 ]; |
25
|
|
|
|
|
|
|
|
26
|
17
|
50
|
|
|
|
26
|
$this->{m1} = eval { Statistics::Basic::Mean->new($v1) } or croak $@; |
|
17
|
|
|
|
|
78
|
|
27
|
17
|
50
|
|
|
|
20
|
$this->{m2} = eval { Statistics::Basic::Mean->new($v2) } or croak $@; |
|
17
|
|
|
|
|
49
|
|
28
|
|
|
|
|
|
|
|
29
|
17
|
|
|
|
|
61
|
$v1->_set_linked_computer( covariance => $this, $v2 ); |
30
|
17
|
|
|
|
|
46
|
$v2->_set_linked_computer( covariance => $this, $v1 ); |
31
|
|
|
|
|
|
|
|
32
|
17
|
|
|
|
|
83
|
return $this; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
# }}} |
35
|
|
|
|
|
|
|
# _recalc {{{ |
36
|
|
|
|
|
|
|
sub _recalc { |
37
|
28
|
|
|
28
|
|
27
|
my $this = shift; |
38
|
28
|
|
|
|
|
34
|
my $sum = 0; |
39
|
28
|
|
|
|
|
49
|
my $v1 = $this->{v1}; |
40
|
28
|
|
|
|
|
38
|
my $v2 = $this->{v2}; |
41
|
28
|
|
|
|
|
73
|
my $c1 = $v1->query_size; |
42
|
28
|
|
|
|
|
53
|
my $c2 = $v2->query_size; |
43
|
|
|
|
|
|
|
|
44
|
28
|
50
|
|
|
|
59
|
warn "[recalc " . ref($this) . "] (\$c1, \$c2) = ($c1, $c2)\n" if $Statistics::Basic::DEBUG; |
45
|
|
|
|
|
|
|
|
46
|
28
|
50
|
|
|
|
67
|
confess "the two vectors in a " . ref($this) . " object must be the same length ($c2!=$c1)" unless $c2 == $c1; |
47
|
|
|
|
|
|
|
|
48
|
28
|
|
|
|
|
33
|
my $cardinality = $c1; |
49
|
28
|
50
|
|
|
|
65
|
$cardinality -- if $Statistics::Basic::UNBIAS; |
50
|
|
|
|
|
|
|
|
51
|
28
|
|
|
|
|
41
|
delete $this->{recalc_necessary}; |
52
|
28
|
|
|
|
|
37
|
delete $this->{_value}; |
53
|
28
|
50
|
|
|
|
59
|
return unless $cardinality > 0; |
54
|
28
|
50
|
|
|
|
77
|
return unless $v1->query_filled; |
55
|
28
|
50
|
|
|
|
58
|
return unless $v2->query_filled; |
56
|
|
|
|
|
|
|
|
57
|
28
|
|
|
|
|
70
|
$v1 = $v1->query; |
58
|
28
|
|
|
|
|
61
|
$v2 = $v2->query; |
59
|
|
|
|
|
|
|
|
60
|
28
|
|
|
|
|
94
|
my $m1 = $this->{m1}->query; |
61
|
28
|
|
|
|
|
71
|
my $m2 = $this->{m2}->query; |
62
|
|
|
|
|
|
|
|
63
|
28
|
50
|
|
|
|
65
|
if( $Statistics::Basic::DEBUG >= 2 ) { |
64
|
0
|
|
|
|
|
0
|
for my $i (0 .. $#$v1) { |
65
|
0
|
|
|
|
|
0
|
warn "[recalc " . ref($this) . "] ( $v1->[$i] - $m1 ) * ( $v2->[$i] - $m2 )\n"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
28
|
|
|
|
|
83
|
for my $i (0 .. $#$v1) { |
70
|
33
|
|
|
33
|
|
202
|
no warnings 'uninitialized'; ## no critic |
|
33
|
|
|
|
|
62
|
|
|
33
|
|
|
|
|
6822
|
|
71
|
172
|
|
|
|
|
10489
|
$sum += ( $v1->[$i] - $m1 ) * ( $v2->[$i] - $m2 ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
28
|
|
|
|
|
1179
|
$this->{_value} = ($sum / $cardinality); |
75
|
|
|
|
|
|
|
|
76
|
28
|
50
|
|
|
|
1007
|
warn "[recalc " . ref($this) . "] ($sum/$cardinality) = $this->{_value}\n" if $Statistics::Basic::DEBUG; |
77
|
|
|
|
|
|
|
|
78
|
28
|
|
|
|
|
58
|
return; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
# }}} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# query_vector1 {{{ |
83
|
|
|
|
|
|
|
sub query_vector1 { |
84
|
7
|
|
|
7
|
1
|
317
|
my $this = shift; |
85
|
|
|
|
|
|
|
|
86
|
7
|
|
|
|
|
37
|
return $this->{v1}; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
# }}} |
89
|
|
|
|
|
|
|
# query_vector2 {{{ |
90
|
|
|
|
|
|
|
sub query_vector2 { |
91
|
6
|
|
|
6
|
1
|
12
|
my $this = shift; |
92
|
|
|
|
|
|
|
|
93
|
6
|
|
|
|
|
42
|
return $this->{v2}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
# }}} |
96
|
|
|
|
|
|
|
# query_mean1 {{{ |
97
|
|
|
|
|
|
|
sub query_mean1 { |
98
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
return $this->{m1}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
# }}} |
103
|
|
|
|
|
|
|
# query_mean2 {{{ |
104
|
|
|
|
|
|
|
sub query_mean2 { |
105
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
return $this->{m2}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
# }}} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |