line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Statistics::Basic::LeastSquareFit; |
3
|
|
|
|
|
|
|
|
4
|
33
|
|
|
33
|
|
156
|
use strict; |
|
33
|
|
|
|
|
81
|
|
|
33
|
|
|
|
|
2665
|
|
5
|
33
|
|
|
33
|
|
157
|
use warnings; |
|
33
|
|
|
|
|
778
|
|
|
33
|
|
|
|
|
992
|
|
6
|
33
|
|
|
33
|
|
899
|
use Carp; |
|
33
|
|
|
|
|
37
|
|
|
33
|
|
|
|
|
3840
|
|
7
|
|
|
|
|
|
|
|
8
|
33
|
|
|
33
|
|
179
|
use base 'Statistics::Basic::_TwoVectorBase'; |
|
33
|
|
|
|
|
845
|
|
|
33
|
|
|
|
|
6227
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use overload |
11
|
|
|
|
|
|
|
'""' => sub { |
12
|
1
|
|
|
1
|
|
8
|
my ($alpha,$beta) = map{$Statistics::Basic::fmt->format_number($_, $Statistics::Basic::IPRES)} $_[0]->query; |
|
2
|
|
|
|
|
181
|
|
13
|
1
|
|
|
|
|
148
|
"LSF( alpha: $alpha, beta: $beta )"; |
14
|
|
|
|
|
|
|
}, |
15
|
1
|
|
|
1
|
|
217
|
'0+' => sub { croak "the result of LSF may not be used as a number" }, |
16
|
33
|
|
|
33
|
|
56226
|
fallback => 1; # tries to do what it would have done if this wasn't present. |
|
33
|
|
|
|
|
38331
|
|
|
33
|
|
|
|
|
530
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# new {{{ |
19
|
|
|
|
|
|
|
sub new { |
20
|
4
|
|
|
4
|
1
|
1157
|
my $this = shift; |
21
|
4
|
|
100
|
|
|
25
|
my @var1 = (shift || ()); |
22
|
4
|
|
100
|
|
|
18
|
my @var2 = (shift || ()); |
23
|
4
|
50
|
|
|
|
5
|
my $v1 = eval { Statistics::Basic::Vector->new( @var1 ) } or croak $@; |
|
4
|
|
|
|
|
111
|
|
24
|
4
|
50
|
|
|
|
6
|
my $v2 = eval { Statistics::Basic::Vector->new( @var2 ) } or croak $@; |
|
4
|
|
|
|
|
10
|
|
25
|
|
|
|
|
|
|
|
26
|
4
|
|
|
|
|
8
|
$this = bless {}, $this; |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
11
|
my $c = $v1->_get_linked_computer( LSF => $v2 ); |
29
|
4
|
50
|
|
|
|
9
|
return $c if $c; |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
|
|
|
111
|
$this->{_vectors} = [ $v1, $v2 ]; |
32
|
|
|
|
|
|
|
|
33
|
4
|
50
|
|
|
|
6
|
$this->{vrx} = eval { Statistics::Basic::Variance->new($v1) } or croak $@; |
|
4
|
|
|
|
|
16
|
|
34
|
4
|
50
|
|
|
|
7
|
$this->{mnx} = eval { Statistics::Basic::Mean->new($v1) } or croak $@; |
|
4
|
|
|
|
|
14
|
|
35
|
4
|
50
|
|
|
|
6
|
$this->{mny} = eval { Statistics::Basic::Mean->new($v2) } or croak $@; |
|
4
|
|
|
|
|
10
|
|
36
|
4
|
50
|
|
|
|
5
|
$this->{cov} = eval { Statistics::Basic::Covariance->new($v1, $v2) } or croak $@; |
|
4
|
|
|
|
|
18
|
|
37
|
|
|
|
|
|
|
|
38
|
4
|
|
|
|
|
10
|
$v1->_set_linked_computer( LSF => $this, $v2 ); |
39
|
4
|
|
|
|
|
9
|
$v2->_set_linked_computer( LSF => $this, $v1 ); |
40
|
|
|
|
|
|
|
|
41
|
4
|
|
|
|
|
15
|
return $this; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
# }}} |
44
|
|
|
|
|
|
|
# _recalc {{{ |
45
|
|
|
|
|
|
|
sub _recalc { |
46
|
4
|
|
|
4
|
|
5
|
my $this = shift; |
47
|
|
|
|
|
|
|
|
48
|
4
|
|
|
|
|
9
|
delete $this->{recalc_needed}; |
49
|
4
|
|
|
|
|
5
|
delete $this->{alpha}; |
50
|
4
|
|
|
|
|
5
|
delete $this->{beta}; |
51
|
|
|
|
|
|
|
|
52
|
4
|
50
|
|
|
|
19
|
my $vrx = $this->{vrx}->query; return unless defined $vrx; return unless $vrx > 0; |
|
4
|
50
|
|
|
|
9
|
|
|
4
|
|
|
|
|
9
|
|
53
|
4
|
50
|
|
|
|
10
|
my $mnx = $this->{mnx}->query; return unless defined $mnx; |
|
4
|
|
|
|
|
78
|
|
54
|
4
|
50
|
|
|
|
11
|
my $mny = $this->{mny}->query; return unless defined $mny; |
|
4
|
|
|
|
|
12
|
|
55
|
4
|
50
|
|
|
|
16
|
my $cov = $this->{cov}->query; return unless defined $cov; |
|
4
|
|
|
|
|
7
|
|
56
|
|
|
|
|
|
|
|
57
|
4
|
|
|
|
|
8
|
$this->{beta} = ($cov / $vrx); |
58
|
4
|
|
|
|
|
8
|
$this->{alpha} = ($mny - ($this->{beta} * $mnx)); |
59
|
|
|
|
|
|
|
|
60
|
4
|
50
|
|
|
|
7
|
warn "[recalc " . ref($this) . "] (alpha: $this->{alpha}, beta: $this->{beta})\n" if $Statistics::Basic::DEBUG; |
61
|
|
|
|
|
|
|
|
62
|
4
|
|
|
|
|
5
|
return; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
# }}} |
65
|
|
|
|
|
|
|
# query {{{ |
66
|
|
|
|
|
|
|
sub query { |
67
|
10
|
|
|
10
|
1
|
24
|
my $this = shift; |
68
|
|
|
|
|
|
|
|
69
|
10
|
100
|
|
|
|
31
|
$this->_recalc if $this->{recalc_needed}; |
70
|
|
|
|
|
|
|
|
71
|
10
|
50
|
|
|
|
16
|
warn "[query " . ref($this) . " ($this->{alpha}, $this->{beta})]\n" if $Statistics::Basic::DEBUG; |
72
|
|
|
|
|
|
|
|
73
|
10
|
100
|
|
|
|
49
|
return (wantarray ? ($this->{alpha}, $this->{beta}) : [$this->{alpha}, $this->{beta}] ); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
# }}} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# query_vector1 {{{ |
78
|
|
|
|
|
|
|
sub query_vector1 { |
79
|
2
|
|
|
2
|
1
|
52
|
my $this = shift; |
80
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
13
|
return $this->{cov}->query_vector1; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
# }}} |
84
|
|
|
|
|
|
|
# query_vector2 {{{ |
85
|
|
|
|
|
|
|
sub query_vector2 { |
86
|
2
|
|
|
2
|
1
|
5
|
my $this = shift; |
87
|
|
|
|
|
|
|
|
88
|
2
|
|
|
|
|
6
|
return $this->{cov}->query_vector2; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
# }}} |
91
|
|
|
|
|
|
|
# query_mean1 {{{ |
92
|
|
|
|
|
|
|
sub query_mean1 { |
93
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return $this->{mnx}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
# }}} |
98
|
|
|
|
|
|
|
# query_variance1 {{{ |
99
|
|
|
|
|
|
|
sub query_variance1 { |
100
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
return $this->{vrx}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
# }}} |
105
|
|
|
|
|
|
|
# query_covariance {{{ |
106
|
|
|
|
|
|
|
sub query_covariance { |
107
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
return $this->{cov}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
# }}} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# y_given_x {{{ |
114
|
|
|
|
|
|
|
sub y_given_x { |
115
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
116
|
0
|
|
|
|
|
|
my ($alpha, $beta) = $this->query; |
117
|
0
|
|
|
|
|
|
my $x = shift; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
return ($beta*$x + $alpha); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
# }}} |
122
|
|
|
|
|
|
|
# x_given_y {{{ |
123
|
|
|
|
|
|
|
sub x_given_y { |
124
|
0
|
|
|
0
|
1
|
|
my $this = shift; |
125
|
0
|
|
|
|
|
|
my ($alpha, $beta) = $this->query; |
126
|
0
|
|
|
|
|
|
my $y = shift; |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
defined( my $x = eval { ( ($y-$alpha)/$beta ) }) or croak $@; |
|
0
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
return $x; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
# }}} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |