11
'""' => sub { my $v = $_[0]->query; $Statistics::Basic::fmt->format_number("$v", $ENV{IPRES}) },
12
'0+' => sub { $_[0]->query },
13
( exists($ENV{TOLER}) ? ('==' => sub { abs($_[0]-$_[1])<=$ENV{TOLER} }) : () ),
14
'eq' => sub { "$_[0]" eq "$_[1]" },
15
fallback => 1; # tries to do what it would have done if this wasn't present.
8
use base 'Statistics::Basic::_TwoVectorBase';
22
my $v1 = eval { Statistics::Basic::Vector->new( $_[0] ) }; croak $@ if $@;
23
my $v2 = eval { Statistics::Basic::Vector->new( $_[1] ) }; croak $@ if $@;
13
my @var1 = (shift || ());
14
my @var2 = (shift || ());
15
my $v1 = eval { Statistics::Basic::Vector->new( @var1 ) } or croak $@;
16
my $v2 = eval { Statistics::Basic::Vector->new( @var2 ) } or croak $@;
25
if( my $c = $v1->get_linked_computer( covariance => $v2 ) ) {
26
warn "[linked covariance v1:$v1 v2:$v2]\n" if $ENV{DEBUG} >= 2;
18
my $c = $v1->_get_linked_computer( covariance => $v2 );
30
21
my $this = bless {v1=>$v1, v2=>$v2}, $class;
31
warn "[new covariance v1:$this->{v1} v2:$this->{v2}]\n" if $ENV{DEBUG} >= 2;
33
$this->{m1} = eval { Statistics::Basic::Mean->new($v1) }; croak $@ if $@;
34
$this->{m2} = eval { Statistics::Basic::Mean->new($v2) }; croak $@ if $@;
36
$v1->set_linked_computer( covariance => $this, $v2 );
37
$v2->set_linked_computer( covariance => $this, $v1 );
22
warn "[new " . ref($this) . " v1:$this->{v1} v2:$this->{v2}]\n" if $Statistics::Basic::DEBUG >= 2;
24
$this->{_vectors} = [ $v1, $v2 ];
26
$this->{m1} = eval { Statistics::Basic::Mean->new($v1) } or croak $@;
27
$this->{m2} = eval { Statistics::Basic::Mean->new($v2) } or croak $@;
29
$v1->_set_linked_computer( covariance => $this, $v2 );
30
$v2->_set_linked_computer( covariance => $this, $v1 );
46
my $c1 = $this->{v1}->size;
47
my $c2 = $this->{v2}->size;
49
warn "[recalc covariance] (\$c1, \$c2) = ($c1, $c2)\n" if $ENV{DEBUG};
51
confess "the two vectors in a Covariance object must be the same length ($c2!=$c1)" unless $c2 == $c1;
41
my $c1 = $v1->query_size;
42
my $c2 = $v2->query_size;
44
warn "[recalc " . ref($this) . "] (\$c1, \$c2) = ($c1, $c2)\n" if $Statistics::Basic::DEBUG;
46
confess "the two vectors in a " . ref($this) . " object must be the same length ($c2!=$c1)" unless $c2 == $c1;
53
48
my $cardinality = $c1;
54
$cardinality -- if $ENV{UNBIAS};
49
$cardinality -- if $Statistics::Basic::UNBIAS;
56
51
delete $this->{recalc_necessary};
57
delete $this->{covariance};
52
delete $this->{_value};
58
53
return unless $cardinality > 0;
54
return unless $v1->query_filled;
55
return unless $v2->query_filled;
60
my $v1 = $this->{v1}->query;
61
my $v2 = $this->{v2}->query;
63
60
my $m1 = $this->{m1}->query;
64
61
my $m2 = $this->{m2}->query;
66
if( $ENV{DEBUG} >= 2 ) {
63
if( $Statistics::Basic::DEBUG >= 2 ) {
67
64
for my $i (0 .. $#$v1) {
68
warn "[recalc covariance] ( $v1->[$i] - $m1 ) * ( $v2->[$i] - $m2 )\n";
65
warn "[recalc " . ref($this) . "] ( $v1->[$i] - $m1 ) * ( $v2->[$i] - $m2 )\n";
72
69
for my $i (0 .. $#$v1) {
70
no warnings 'uninitialized'; ## no critic
73
71
$sum += ( $v1->[$i] - $m1 ) * ( $v2->[$i] - $m2 );
76
$this->{covariance} = ($sum / $cardinality);
78
warn "[recalc covariance] ($sum/$cardinality) = $this->{covariance}\n" if $ENV{DEBUG};
84
$this->{recalc_needed} = 1;
86
warn "[recalc_needed covariance]\n" if $ENV{DEBUG};
93
$this->recalc if $this->{recalc_needed};
95
warn "[query covariance $this->{covariance}]\n" if $ENV{DEBUG};
97
return $this->{covariance};
74
$this->{_value} = ($sum / $cardinality);
76
warn "[recalc " . ref($this) . "] ($sum/$cardinality) = $this->{_value}\n" if $Statistics::Basic::DEBUG;
100
82
# query_vector1 {{{
101
83
sub query_vector1 {
133
return ($this->{v2}->size, $this->{v1}->size); # only v1->size is returned in scalar context
141
eval { $this->{v1}->set_size( $size );
142
$this->{v2}->set_size( $size ); }; croak $@ if $@;
149
warn "[insert covariance]\n" if $ENV{DEBUG};
151
croak "this insert() takes precisely two arguments. They can be arrayrefs if you like." unless 2 == int @_;
153
$this->{v1}->insert( $_[0] );
154
$this->{v2}->insert( $_[1] );
161
warn "[ginsert covariance]\n" if $ENV{DEBUG};
163
croak "this ginsert() takes precisely two arguments. They can be arrayrefs if you like."
166
my ($v1, $v2) = (@$this{'v1', 'v2'});
168
$v1->ginsert( $_[0] );
169
$v2->ginsert( $_[1] );
175
croak "The two vectors in a Covariance object must be the same length ($c1!=$c2)."
184
warn "[set_vector covariance]\n" if $ENV{DEBUG};
186
croak "this set_vector() takes precisely two arguments. They can be arrayrefs if you like."
189
my ($v1, $v2) = (@$this{'v1', 'v2'});
191
$v1->set_vector( $_[0] );
192
$v2->set_vector( $_[1] );
197
confess "The two vectors in a Covariance object must be the same length ($c1!=$c2)."