8
use base 'Statistics::Basic::_TwoVectorBase';
12
my ($alpha,$beta) = map{$Statistics::Basic::fmt->format_number($_, $ENV{IPRES})} $_[0]->query;
13
"alpha: $alpha, beta: $beta";
12
my ($alpha,$beta) = map{$Statistics::Basic::fmt->format_number($_, $Statistics::Basic::IPRES)} $_[0]->query;
13
"LSF( alpha: $alpha, beta: $beta )";
15
15
'0+' => sub { croak "the result of LSF may not be used as a number" },
16
16
fallback => 1; # tries to do what it would have done if this wasn't present.
23
my $v1 = eval { Statistics::Basic::Vector->new( shift ) }; croak $@ if $@;
24
my $v2 = eval { Statistics::Basic::Vector->new( shift ) }; croak $@ if $@;
21
my @var1 = (shift || ());
22
my @var2 = (shift || ());
23
my $v1 = eval { Statistics::Basic::Vector->new( @var1 ) } or croak $@;
24
my $v2 = eval { Statistics::Basic::Vector->new( @var2 ) } or croak $@;
26
26
$this = bless {}, $this;
28
my $c = $v1->get_linked_computer( LSF => $v2 );
28
my $c = $v1->_get_linked_computer( LSF => $v2 );
31
$this->{vrx} = new Statistics::Basic::Variance($v1);
32
$this->{vry} = new Statistics::Basic::Variance($v2);
33
$this->{mnx} = new Statistics::Basic::Mean($v1);
34
$this->{mny} = new Statistics::Basic::Mean($v2);
35
$this->{cov} = new Statistics::Basic::Covariance($v1, $v2);
37
$v1->set_linked_computer( LSF => $this, $v2 );
38
$v2->set_linked_computer( LSF => $this, $v1 );
31
$this->{_vectors} = [ $v1, $v2 ];
33
$this->{vrx} = eval { Statistics::Basic::Variance->new($v1) } or croak $@;
34
$this->{mnx} = eval { Statistics::Basic::Mean->new($v1) } or croak $@;
35
$this->{mny} = eval { Statistics::Basic::Mean->new($v2) } or croak $@;
36
$this->{cov} = eval { Statistics::Basic::Covariance->new($v1, $v2) } or croak $@;
38
$v1->_set_linked_computer( LSF => $this, $v2 );
39
$v2->_set_linked_computer( LSF => $this, $v1 );
47
48
delete $this->{recalc_needed};
48
49
delete $this->{alpha};
49
50
delete $this->{beta};
51
unless( $this->{vrx}->query ) {
52
unless( defined $this->{vrx}->query ) {
53
warn "[recalc LSF] undef variance...\n" if $ENV{DEBUG};
56
warn "[recalc LSF] narrowly avoided division by zero. Something is probably wrong.\n" if $ENV{DEBUG};
62
$this->{beta} = ($this->{cov}->query / $this->{vrx}->query);
63
$this->{alpha} = ($this->{mny}->query - ($this->{beta} * $this->{mnx}->query));
65
warn "[recalc LSF] (alpha: $this->{alpha}, beta: $this->{beta})\n" if $ENV{DEBUG};
71
$this->{recalc_needed} = 1;
73
warn "[recalc_needed LSF]\n" if $ENV{DEBUG};
52
my $vrx = $this->{vrx}->query; return unless defined $vrx; return unless $vrx > 0;
53
my $mnx = $this->{mnx}->query; return unless defined $mnx; return unless $mnx > 0;
54
my $mny = $this->{mny}->query; return unless defined $mny;
55
my $cov = $this->{cov}->query; return unless defined $cov;
57
$this->{beta} = ($cov / $vrx);
58
$this->{alpha} = ($mny - ($this->{beta} * $mnx));
60
warn "[recalc " . ref($this) . "] (alpha: $this->{alpha}, beta: $this->{beta})\n" if $Statistics::Basic::DEBUG;
80
$this->recalc if $this->{recalc_needed};
69
$this->_recalc if $this->{recalc_needed};
82
warn "[query LSF ($this->{alpha}, $this->{beta})]\n" if $ENV{DEBUG};
71
warn "[query " . ref($this) . " ($this->{alpha}, $this->{beta})]\n" if $Statistics::Basic::DEBUG;
84
73
return (wantarray ? ($this->{alpha}, $this->{beta}) : [$this->{alpha}, $this->{beta}] );
87
77
# query_vector1 {{{
88
78
sub query_vector1 {
149
125
my ($alpha, $beta) = $this->query;
152
my $x = eval { ( ($y-$alpha)/$beta ) }; croak $@ if $@;
128
defined( my $x = eval { ( ($y-$alpha)/$beta ) }) or croak $@;
161
return $this->{cov}->size;
169
eval { $this->{vrx}->set_size( $size );
170
$this->{vry}->set_size( $size ); }; croak $@ if $@;
177
warn "[insert LSF]\n" if $ENV{DEBUG};
179
croak "this insert() takes precisely two arguments. They can be arrayrefs if you like." unless 2 == int @_;
181
$this->{vrx}->insert( $_[0] );
182
$this->{vry}->insert( $_[1] );
189
warn "[ginsert LSF]\n" if $ENV{DEBUG};
191
croak "this ginsert() takes precisely two arguments. They can be arrayrefs if you like."
194
$this->{vrx}->ginsert( $_[0] );
195
$this->{vry}->ginsert( $_[1] );
197
croak "The two vectors in a LeastSquareFit object must be the same length."
198
unless $this->{vrx}->size == $this->{vry}->size;
205
warn "[set_vector LSF]\n" if $ENV{DEBUG};
207
croak "this set_vector() takes precisely two arguments. They can be arrayrefs if you like."
210
$this->{vrx}->set_vector( $_[0] );
211
$this->{vry}->set_vector( $_[1] );
213
croak "The two vectors in a LeastSquareFit object must be the same length."
214
unless $this->{vrx}->size == $this->{vry}->size;