Basic/LeastSquareFit.pm

filter: Statementstmt 80.7% 67 / 83 Branchbran 52.9% 18 / 34 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 70.5% 12 / 17 Podpod 100% 9 / 9 totaltotal 75.8% 116 / 153 CC 20 SCAR 17.2
CC
20
Cov
74%
new
10.5
_recalc
7.4
x_given_y
6.0
CRAP 5.6
« Basic/Correlation.pm ↑ Summary Basic/Variance.pm »
1  
2 package Statistics::Basic::LeastSquareFit;
3  
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 33 ▶use base 'Statistics::Basic::_TwoVectorBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use overload
11 33 ▶ '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 my ($alpha,$beta) = map{$Statistics::Basic::fmt->format_number($_, $Statistics::Basic::IPRES)} $_[0]->query;
13 1 "LSF( alpha: $alpha, beta: $beta )";
14 },
15 1 ▶ '0+' => sub { croak "the result of LSF may not be used as a number" },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 33 fallback => 1; # tries to do what it would have done if this wasn't present.
17  
18 # new {{{
19 4 ▶sub new {
Subroutine new: called CC=10 SCAR=23.5
Pod documented
20 4 my $this = shift;
21 4 ▶ my @var1 = (shift || ());
Condition100%3/3
shift() || ()
l!l&&r!l&&!r
112
MC/DC100%2/2
shift() || ()
shift() ()
Truth table100%3/3
A || B
A= shift()B= ()
ABresult
1X1
011
000
22 4 ▶ my @var2 = (shift || ());
Condition100%3/3
shift() || ()
l!l&&r!l&&!r
112
MC/DC100%2/2
shift() || ()
shift() ()
Truth table100%3/3
A || B
A= shift()B= ()
ABresult
1X1
011
000
23 4 ▶ my $v1 = eval { Statistics::Basic::Vector->new( @var1 ) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
my $v1 = eval { do { "Statistics::Basic::Vector"->new(@var1) } } or croak($@) 4 0 4
24 4 ▶ my $v2 = eval { Statistics::Basic::Vector->new( @var2 ) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
my $v2 = eval { do { "Statistics::Basic::Vector"->new(@var2) } } or croak($@) 4 0 4
25  
26 4 $this = bless {}, $this;
27  
28 4 my $c = $v1->_get_linked_computer( LSF => $v2 );
29 4 ▶ return $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 4 4
30  
31 4 $this->{_vectors} = [ $v1, $v2 ];
32  
33 4 ▶ $this->{vrx} = eval { Statistics::Basic::Variance->new($v1) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"vrx"} = eval { do { "Statistics::Basic::Variance"->new($v1) } } or croak($@) 4 0 4
34 4 ▶ $this->{mnx} = eval { Statistics::Basic::Mean->new($v1) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"mnx"} = eval { do { "Statistics::Basic::Mean"->new($v1) } } or croak($@) 4 0 4
35 4 ▶ $this->{mny} = eval { Statistics::Basic::Mean->new($v2) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"mny"} = eval { do { "Statistics::Basic::Mean"->new($v2) } } or croak($@) 4 0 4
36 4 ▶ $this->{cov} = eval { Statistics::Basic::Covariance->new($v1, $v2) } or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"cov"} = eval { do { "Statistics::Basic::Covariance"->new($v1, $v2) } } or croak($@) 4 0 4
37  
38 4 $v1->_set_linked_computer( LSF => $this, $v2 );
39 4 $v2->_set_linked_computer( LSF => $this, $v1 );
40  
41 4 return $this;
42 }
43 # }}}
44 # _recalc {{{
45 4 ▶sub _recalc {
Subroutine _recalc: called CC=7 SCAR=20.1
46 4 my $this = shift;
47  
48 4 delete $this->{recalc_needed};
49 4 delete $this->{alpha};
50 4 delete $this->{beta};
51  
52 4 ▶ my $vrx = $this->{vrx}->query; return unless defined $vrx; return unless $vrx > 0;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $vrx 0 4 4
unless $vrx > 0 0 4 4
53 4 ▶ my $mnx = $this->{mnx}->query; return unless defined $mnx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mnx 0 4 4
54 4 ▶ my $mny = $this->{mny}->query; return unless defined $mny;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mny 0 4 4
55 4 ▶ my $cov = $this->{cov}->query; return unless defined $cov;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cov 0 4 4
56  
57 4 $this->{beta} = ($cov / $vrx);
58 4 $this->{alpha} = ($mny - ($this->{beta} * $mnx));
59  
60 4 ▶ warn "[recalc " . ref($this) . "] (alpha: $this->{alpha}, beta: $this->{beta})\n" if $Statistics::Basic::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG 0 4 4
61  
62 4 return;
63 }
64 # }}}
65 # query {{{
66 10 ▶sub query {
Subroutine query: called CC=4 SCAR=13.9
Pod documented
67 10 my $this = shift;
68  
69 10 ▶ $this->_recalc if $this->{recalc_needed};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"recalc_needed"} 4 6 10
70  
71 10 ▶ warn "[query " . ref($this) . " ($this->{alpha}, $this->{beta})]\n" if $Statistics::Basic::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG 0 10 10
72  
73 10 ▶ return (wantarray ? ($this->{alpha}, $this->{beta}) : [$this->{alpha}, $this->{beta}] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 8 10
74 }
75 # }}}
76  
77 # query_vector1 {{{
78 2 ▶sub query_vector1 {
Subroutine query_vector1: called CC=1 SCAR=0.0
Pod documented
79 2 my $this = shift;
80  
81 2 return $this->{cov}->query_vector1;
82 }
83 # }}}
84 # query_vector2 {{{
85 2 ▶sub query_vector2 {
Subroutine query_vector2: called CC=1 SCAR=0.0
Pod documented
86 2 my $this = shift;
87  
88 2 return $this->{cov}->query_vector2;
89 }
90 # }}}
91 # query_mean1 {{{
92 0 ▶sub query_mean1 {
Subroutine query_mean1: not called CC=1 SCAR=6.9
Pod documented
93 0 my $this = shift;
94  
95 0 return $this->{mnx};
96 }
97 # }}}
98 # query_variance1 {{{
99 0 ▶sub query_variance1 {
Subroutine query_variance1: not called CC=1 SCAR=6.9
Pod documented
100 0 my $this = shift;
101  
102 0 return $this->{vrx};
103 }
104 # }}}
105 # query_covariance {{{
106 0 ▶sub query_covariance {
Subroutine query_covariance: not called CC=1 SCAR=6.9
Pod documented
107 0 my $this = shift;
108  
109 0 return $this->{cov};
110 }
111 # }}}
112  
113 # y_given_x {{{
114 0 ▶sub y_given_x {
Subroutine y_given_x: not called CC=1 SCAR=6.9
Pod documented
115 0 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 0 ▶sub x_given_y {
Subroutine x_given_y: not called CC=2 SCAR=17.9
Pod documented
124 0 my $this = shift;
125 0 my ($alpha, $beta) = $this->query;
126 0 my $y = shift;
127  
128 0 ▶ defined( my $x = eval { ( ($y-$alpha)/$beta ) }) or croak $@;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $x = eval { do { ($y - $alpha) / $beta } }) or croak($@) 0 0 0
129 0 return $x;
130 }
131 # }}}
132  
133 33 1;
« Basic/Correlation.pm ↑ Summary Basic/Variance.pm »