blib/lib/Math/Vector/Real/MultiNormalMixture.pm

filter: Statementstmt 23.2% 17 / 73 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod 100% 5 / 5 totaltotal 24.7% 27 / 109 CC 13 SCAR 33.9
CC
13
Cov
19%
new
72.0
max_density_estimation
12.0
density
6.0
CRAP 29.7
↑ Summary
1 package Math::Vector::Real::MultiNormalMixture;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 my $PI = 3.14159265358979323846264338327950288419716939937510;
13  
14 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
15 0 my ($class, %opts) = @_;
16 0 ▶ my $mu = delete $opts{mu} // croak "required argument 'mu' missing";
Condition0%0/2
delete $opts{"mu"} // croak("required argument 'mu' missing")
l!l
00
MC/DC0%0/1
delete $opts{"mu"} // croak("required argument 'mu' missing")
delete $opts{"mu"}
Truth table0%0/2
A
A= delete $opts{"mu"}
Aresult
11
00
17 0 ▶ @$mu >= 1 or die "'mu' must containt one point at least";
Branch: 1 branch
BranchTrueFalseTotal
@$mu >= 1 or die "'mu' must containt one point at least" 0 0 0
18 0 my $dim = @{$mu->[0]};
19 0 my $n = @$mu;
20 0 my $alpha = delete $opts{alpha};
21 0 ▶ my @alpha = (defined $alpha ? @$alpha : ((1/$n) x $n));
Branch: 1 branch
BranchTrueFalseTotal
defined $alpha ? : 0 0 0
22 0 ▶ my $sigma = delete $opts{sigma} // 1.0;
Condition0%0/2
delete $opts{"sigma"} // 1
l!l
00
MC/DC0%0/1
delete $opts{"sigma"} // 1
delete $opts{"sigma"}
Truth table0%0/2
A
A= delete $opts{"sigma"}
Aresult
11
00
23 0 ▶ my @sigma = (ref $sigma ? @$sigma : (($sigma) x $n));
Branch: 1 branch
BranchTrueFalseTotal
ref $sigma ? : 0 0 0
24 0 ▶ croak "bad array size" unless (@alpha == $n and @sigma == $n);
Branch: 1 branch
BranchTrueFalseTotal
unless @alpha == $n and @sigma == $n 0 0 0
Condition0%0/3
@alpha == $n and @sigma == $n
!ll&&!rl&&r
000
MC/DC0%0/2
@alpha == $n and @sigma == $n
@alpha == $n @sigma == $n
Truth table0%0/3
A and B
A= @alpha == $nB= @sigma == $n
ABresult
0X0
100
111
25 0 my $c1 = (2 * $PI) ** (-0.5 * $dim);
26 0 my @c = map $alpha[$_] * $c1 * ($sigma[$_] ** (-$dim)), (0..$#alpha);
27 0 my @isigma2 = map 1.0/($_ * $_), @sigma;
28 0 my $c2 = -sqrt(2.0 / $PI);
29 0 my @second = map $c2/($_ * $_ * $_), @sigma;
30  
31 0 my $self = { mu => [map Math::Vector::Real::clone($_), @$mu],
32 alpha => \@alpha,
33 sigma => \@sigma,
34 isigma2 => \@isigma2,
35 c => \@c,
36 second => \@second,
37 dim => $dim,
38 };
39 0 bless $self, $class;
40 }
41  
42 0 ▶sub density {
Subroutine density: not called CC=2 SCAR=17.9
Pod documented
43 0 my ($self, $p) = @_;
44 0 my $c = $self->{c};
45 0 my $mu = $self->{mu};
46 0 my $isigma2 = $self->{sigma};
47 0 my $acu = 0;
48 0 for (0..$#$mu) {
49 0 $acu += $c->[$_] * exp(-$isigma2->[$_] * $mu->[$_]->dist2($p));
50 }
51 0 $acu;
52 }
53  
54 0 ▶sub density_portion {
Subroutine density_portion: not called CC=2 SCAR=17.9
Pod documented
55 0 my $self = shift;
56 0 my $p = shift;
57 0 my $c = $self->{c};
58 0 my $mu = $self->{mu};
59 0 my $isigma2 = $self->{sigma};
60 0 my $acu = 0;
61 0 for (@_) {
62 0 $acu += $c->[$_] * exp(-$isigma2->[$_] * $mu->[$_]->dist2($p));
63 }
64 0 $acu;
65 }
66  
67 0 ▶sub density_and_gradient {
Subroutine density_and_gradient: not called CC=2 SCAR=17.9
Pod documented
68 0 my ($self, $p) = @_;
69 0 my $c = $self->{c};
70 0 my $mu = $self->{mu};
71 0 my $isigma2 = $self->{sigma};
72 0 my $d = 0;
73 0 my $g = $Math::Vector::Real->zero(scalar @{$mu->[0]});
74 0 for (0..$#$mu) {
75 0 my $mu_p = $p - $mu->[$_];
76 0 my $isigma2 = $isigma2->[$_];
77 0 my $Dd = $c->[$_] * exp(-$isigma2 * $mu_p->abs2);
78 0 $d += $Dd;
79 0 $g += -2 * $isigma2 * $mu_p * $Dd;
80 }
81 0 return ($d, $g);
82 }
83  
84 0 ▶sub max_density_estimation {
Subroutine max_density_estimation: not called CC=3 SCAR=24.8
Pod documented
85 0 my $self = shift;
86 0 my $mu = $self->{mu};
87 0 my $max = $self->density($mu->[0]);
88 0 for my $ix (1..$#$mu) {
89 0 my $d = $self->density($mu->[$ix]);
90 # print "d: $d\n";
91 0 ▶ $max = $d if $d > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $d > $max 0 0 0
92 }
93 0 return $max;
94 }
95  
96 1 1;
97 __END__
↑ Summary