| 1 | package Statistics::KernelEstimation; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.008008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # TO DOs | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # - More unit tests | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # - bandidth from data | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # - math function | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # - optimization | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # - opt broken for epanechnikov | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # - max number of integration steps in stiffness integral | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # Ctors | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | return new_gauss( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub new_gauss { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_gauss: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | my $self = _new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | $self->{pdf} = \&_gauss_pdf; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | $self->{cdf} = \&_gauss_cdf; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | $self->{curvature} = \&_gauss_curvature; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | $self->{extension} = 3; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub new_box { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_box: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | my $self = _new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | $self->{pdf} = \&_box_pdf; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | $self->{cdf} = \&_box_cdf; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | $self->{curvature} = \&_box_curvature; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $self->{extension} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | $self->{optimizable} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub new_epanechnikov { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_epanechnikov: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $self = _new( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $self->{pdf} = \&_epanechnikov_pdf; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | $self->{cdf} = \&_epanechnikov_cdf; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | $self->{curvature} = \&_epanechnikov_curvature; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $self->{extension} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $self->{optimizable} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | sub _new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | my ( $class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | bless { data => [], | |||||||||||||||||||||||||||||||||||||||||
| 73 | sum_x => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | sum_x2 => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | sum_cnt => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | min => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | max => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | optimizable => 1 }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # Accessors | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 15831 | ▶ | sub count { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 15831 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 15831 | return $self->{sum_cnt}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 37 | ▶ | sub range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
range: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 37 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 37 | ▶ | if( wantarray ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 31 | return ( $self->{min}, $self->{max} ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | return $self->{max}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 27 | ▶ | sub extended_range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extended_range: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 27 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 27 | my ( $min, $max ) = $self->range(); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 27 | my $w = $self->{extension}*$self->default_bandwidth(); | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 27 | ▶ | if( wantarray ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 27 | return ( $min - $w, $max + $w ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | return $max + $w; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 34 | ▶ | sub default_bandwidth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_bandwidth: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 34 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 34 | ▶ | if( $self->{sum_cnt} == 0 ) { return undef; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 31 | my $x = $self->{sum_x}/$self->{sum_cnt}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 31 | my $x2 = $self->{sum_x2}/$self->{sum_cnt}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 31 | my $sigma = sqrt( $x2 - $x**2 ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # This is the optimal bandwidth if the point distribution is Gaussian. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # (Applied Smoothing Techniques for Data Analysis | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # by Adrian W, Bowman & Adelchi Azzalini (1997)) */ | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 31 | return $sigma * ( (3.0*$self->{sum_cnt}/4.0)**(-1.0/5.0) ); | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # Adding Data | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | sub add_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_data: called CC=9 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 18 | my ( $self, $x, $y, $w ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | unless( _isNumber( $x ) ) { croak "Input ,$x, is not numeric."; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 18 | ▶ | if( !defined( $y ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 12 | $y = 1; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } elsif( !_notNegative( $y ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | croak "Weight ,$y, must be non-negative number."; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 18 | ▶ | if( defined( $w ) && !_isPositive( $w ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $w and not _isPositive($w)
MC/DC0%0/2
defined $w and not _isPositive($w)
defined $w
not _isPositive($w)
Truth table33%1/3
A and not(B)
A= defined $wB= not _isPositive($w)
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | croak "Bandwidth ,$w, must be strictly positive number in add_data."; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # If no bandwidth has been specified, $w will be undef! | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 18 | push @{ $self->{data} }, { pos => $x, cnt => $y, wid => $w }; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # Update summary statistics as we go along: | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 18 | $self->{sum_x} += $y*$x; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 18 | $self->{sum_x2} += $y*$x*$x; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 18 | $self->{sum_cnt} += $y; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 18 | ▶ | if( scalar @{ $self->{data} } == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 4 | $self->{min} = $x; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | $self->{max} = $x; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 14 | ▶ | $self->{min} = $x < $self->{min} ? $x : $self->{min}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 14 | ▶ | $self->{max} = $x > $self->{max} ? $x : $self->{max}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 18 | return; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # Kernel Estimate | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 14005 | ▶ | sub pdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pdf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 14005 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 14005 | return $self->_impl( 'pdf', 'default', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub pdf_width_from_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pdf_width_from_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return $self->_impl( 'pdf', 'fromdata', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # sub pdf_optimal { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # return $self->_impl( 'pdf', 'optimal', @_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | ▶ | sub cdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cdf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | return $self->_impl( 'cdf', 'default', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub cdf_width_from_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cdf_width_from_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $self->_impl( 'cdf', 'fromdata', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # sub cdf_optimal { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # return $self->_impl( 'cdf', 'optimal', @_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 1787 | ▶ | sub _curvature { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_curvature: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1787 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 1787 | return $self->_impl( 'curvature', 'default', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 15795 | ▶ | sub _impl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_impl: called CC=13 SCAR=34.9
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 15795 | my ( $self, $mode, $bandwidth_mode, $x, $w ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 15795 | ▶ | unless( $mode eq 'pdf' || $mode eq 'cdf' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$mode eq "pdf" or $mode eq "cdf" or $mode eq "curvature"
$mode eq "pdf" or $mode eq "cdf"
MC/DC0%0/3
$mode eq "pdf" or $mode eq "cdf" or $mode eq "curvature"
$mode eq "pdf"
$mode eq "cdf"
$mode eq "curvature"
Truth table75%3/4
A or B or C
A= $mode eq "pdf"B= $mode eq "cdf"C= $mode eq "curvature"
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | || $mode eq 'curvature' ) { die "Illegal mode: ,$mode,"; } | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | unless( _isNumber( $x ) ) { croak "Position ,$x, must be numeric."; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # If no data (or only data w/ weight zero), return immediately | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 15795 | my $count = $self->count(); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | if( $count == 0 ) { return 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | # If bandwidth is from data, calculate result and return immediately | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 15795 | ▶ | if( $bandwidth_mode eq 'fromdata' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $y = 0; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | for my $p ( @{ $self->{data} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | unless( defined $p->{wid} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | carp "Undefined bandwidth in data at position " . $p->{pos} | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | . ". Using default bandwidth."; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $w = $self->default_bandwidth(); | |||||||||||||||||||||||||||||||||||||||||
| 222 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $w = $p->{wid}; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $y += $p->{cnt} * $self->{$mode}( $x, $p->{pos}, $w ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return $y/$count; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | # ... otherwise, determine bandwidth | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 15795 | ▶ | if( $bandwidth_mode eq 'default' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 15795 | ▶ | if( !defined( $w ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | $w = $self->default_bandwidth(); | |||||||||||||||||||||||||||||||||||||||||
| 235 | } elsif( !_notNegative( $w ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | croak "Bandwidth ,$w, must be strictly positive number."; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # } elsif( $bandwidth_mode eq 'optimal' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # $w = $self->optimal_bandwidth(); | ||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | # ... now use bandwidth from above to find result | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 15795 | my $y = 0; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 15795 | for my $q ( @{ $self->{data} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 246 | 45171 | $y += $q->{cnt} * $self->{$mode}( $x, $q->{pos}, $w ); | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 15795 | return $y/$count; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # Classical Histograms | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | sub histogram { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
histogram: called CC=11 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | my ( $self, $bins ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | ▶ | unless( _isPositive( $bins ) && $bins==int($bins) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_isPositive($bins) and $bins == int $bins
MC/DC0%0/2
_isPositive($bins) and $bins == int $bins
_isPositive($bins)
$bins == int $bins
Truth table33%1/3
A and B
A= _isPositive($bins)B= $bins == int $bins
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | croak "Number of bins must be strictly positive integer."; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | ▶ | if( $self->count() == 0 ) { return []; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | my ( $min, $max ) = $self->range(); | |||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | if( $bins == 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | return [ { pos => 0.5*($max-$min), cnt => $self->count() } ]; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | my $w = ($max - $min)/($bins - 1); | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | my @histo = (); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | for my $k ( 0..$bins-1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 274 | 9 | push @histo, { pos => $min + $k*$w, cnt => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | for my $p ( @{ $self->{data} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 278 | 6 | my $i = int( ($p->{pos} - ( $min - 0.5*$w ) )/$w ); | |||||||||||||||||||||||||||||||||||||||||
| 279 | 6 | my ( $lo, $hi ) = ( $min + ($i-0.5)*$w, $min + ($i+0.5)*$w ); | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 6 | my $x = $p->{pos}; | |||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if( $x < $lo ) { $i -= 1; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 6 | ▶ | elsif( $lo <= $x && $x < $hi ) { $i = $i; } | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$lo <= $x and $x < $hi
MC/DC0%0/2
$lo <= $x and $x < $hi
$lo <= $x
$x < $hi
Truth table33%1/3
A and B
A= $lo <= $xB= $x < $hi
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | elsif( $hi <= $x ) { $i += 1; } | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 6 | $histo[ $i ]->{cnt} += $p->{cnt}; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | return \@histo; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | ▶ | sub distribution_function { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distribution_function: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | my @sorted = sort { $a->{pos} <=> $b->{pos} } @{ $self->{data} }; | |||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 2 | my @dist = (); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | my $cumul = 0; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | for my $p ( @sorted ) { | |||||||||||||||||||||||||||||||||||||||||
| 301 | 6 | $cumul += $p->{cnt}; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 6 | push @dist, { pos => $p->{pos}, cnt => $cumul }; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 2 | return \@dist; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # Input validation | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | # In general: undef evaluates to invalid input! | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 31616 | ▶ | sub _isNumber { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_isNumber: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 31616 | my ( $in ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 31616 | ▶ | if( defined( $in ) && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $in and $in =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
MC/DC0%0/2
defined $in and $in =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
defined $in
$in =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
Truth table33%1/3
A and B
A= defined $inB= $in =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 31616 | $in =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ ) { return 1; } | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 2 | ▶ | sub _isPositive { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_isPositive: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | my ( $in ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | ▶ | if( _isNumber( $in ) && $in > 0 ) { return 1; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_isNumber($in) and $in > 0
MC/DC0%0/2
_isNumber($in) and $in > 0
_isNumber($in)
$in > 0
Truth table33%1/3
A and B
A= _isNumber($in)B= $in > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 15801 | ▶ | sub _notNegative { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_notNegative: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 15801 | my ( $in ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 328 | 15801 | ▶ | if( _isNumber( $in ) && $in >= 0 ) { return 1; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_isNumber($in) and $in >= 0
MC/DC0%0/2
_isNumber($in) and $in >= 0
_isNumber($in)
$in >= 0
Truth table33%1/3
A and B
A= _isNumber($in)B= $in >= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # Optimal Bandwidth | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Development history: | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # Equation solver: | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # 1) Straight iteration | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # 2) Newton's method | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # 3) Sekant method | ||||||||||||||||||||||||||||||||||||||||||
| 341 | # 4) Bisection | ||||||||||||||||||||||||||||||||||||||||||
| 342 | # | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # Integration: | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # 1) Numerical differentiation | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # 2) Numerical differentiation, with equal step width as integration | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # 3) Symbolic differentiation | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # 4) Adaptive step size integration | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # 5) Romberg integration (not implemented) | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | # This routine solves the equation encoded in _optimal_bandwidth_equation | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # using the secant method. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 2 | ▶ | sub optimal_bandwidth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
optimal_bandwidth: called CC=8 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 355 | 2 | ▶ | my $n = @_ ? shift : 25; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 2 | ▶ | my $eps = @_ ? shift : 1e-3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 2 | ▶ | unless( $self->{optimizable} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | croak "Bandwidth Optimization not available for this type of kernel."; | |||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 2 | ▶ | if( $self->{sum_cnt} == 0 ) { return undef; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | my $x0 = $self->default_bandwidth(); | |||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | my $y0 = $self->_optimal_bandwidth_equation( $x0 ); | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | my $x = 0.8*$x0; | |||||||||||||||||||||||||||||||||||||||||
| 368 | # my $x = $x0 * ( 1 - 1e-6 ); | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | my $y = $self->_optimal_bandwidth_equation( $x ); | |||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 1 | my $dx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 1 | ▶ | while( $i++ < $n ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 25 | $x -= $y*($x0-$x)/($y0-$y); | |||||||||||||||||||||||||||||||||||||||||
| 376 | 25 | $y = $self->_optimal_bandwidth_equation( $x ); | |||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if( abs($y) < $eps*$y0 ) { last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | if( wantarray ) { return ( $x, $i ); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 1 | return $x; | |||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | # This routine uses the secant method. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | sub optimal_bandwidth_safe { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
optimal_bandwidth_safe: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | my $x0 = @_ ? shift : $self->default_bandwidth() / $self->count(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | my $x1 = @_ ? shift : 2*$self->default_bandwidth(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | my $eps = @_ ? shift : 1e-3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | unless( $self->{optimizable} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | croak "Bandwidth Optimization not available for this type of kernel."; | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | if( $self->{sum_cnt} == 0 ) { return undef; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my $y0 = $self->_optimal_bandwidth_equation( $x0 ); | |||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | my $y1 = $self->_optimal_bandwidth_equation( $x1 ); | |||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | ▶ | unless( $y0 * $y1 < 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | croak "Interval [ f(x0=$x0)=$y0 : f(x1=$x1)=$y1 ] does not bracket root."; | |||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my ( $x, $y, $i ) = ( 0, 0, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | while( abs( $x0 - $x1 ) > $eps*$x1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | $i += 1; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | $x = ( $x0 + $x1 )/2; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $y = $self->_optimal_bandwidth_equation( $x ); | |||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | if( abs( $y ) < $eps*$y0 ) { last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | if( $y * $y0 < 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | ( $x1, $y1 ) = ( $x, $y ); | |||||||||||||||||||||||||||||||||||||||||
| 417 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ( $x0, $y0 ) = ( $x, $y ); | |||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | if( wantarray ) { return ( $x, $i ); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | return $x; | |||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | # This routine encodes the self-consistent equation that is fulfilled | ||||||||||||||||||||||||||||||||||||||||||
| 427 | # by the optimal bandwidth. Notation according to Bowman & Azzalini. | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 27 | ▶ | sub _optimal_bandwidth_equation { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_optimal_bandwidth_equation: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 27 | my ( $self, $w ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 27 | my $alpha = 1.0/(2.0*sqrt( 3.14159265358979323846 ) ); | |||||||||||||||||||||||||||||||||||||||||
| 433 | 27 | my $sigma = 1.0; | |||||||||||||||||||||||||||||||||||||||||
| 434 | 27 | my $n = $self->count(); | |||||||||||||||||||||||||||||||||||||||||
| 435 | 27 | my $q = $self->_stiffness_integral( $w ); | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 27 | return $w - ( ($n*$q*$sigma**4)/$alpha )**(-1.0/5.0); | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | # This routine calculates the integral over the square of the curvature | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # (it: Int (f'')**2 ) using the trapezoidal rule. The routine decreases | ||||||||||||||||||||||||||||||||||||||||||
| 442 | # the step size by half until the relative error in the last step is less | ||||||||||||||||||||||||||||||||||||||||||
| 443 | # than epsilon. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | 27 | ▶ | sub _stiffness_integral { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stiffness_integral: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 27 | my ( $self, $w ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | 27 | my $eps = 1e-4; | |||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 27 | my ( $mn, $mx ) = $self->extended_range(); | |||||||||||||||||||||||||||||||||||||||||
| 451 | 27 | my $n = 1; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 27 | my $dx = ($mx-$mn)/$n; | |||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 27 | my $yy = 0.5*($self->_curvature($mn,$w)**2+$self->_curvature($mx,$w)**2)*$dx; | |||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | # The trapezoidal rule guarantees a relative error of better than eps | ||||||||||||||||||||||||||||||||||||||||||
| 457 | # for some number of steps less than maxn. | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 27 | my $maxn = ($mx-$mn)/sqrt($eps); | |||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | # This is not ideal, but I want to cap the total computation spent here: | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 27 | ▶ | $maxn = ( $maxn > 2048 ? 2048 : $maxn ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | 27 | ▶ | for( my $n=2; $n<=$maxn; $n*=2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 162 | $dx /= 2.0; | |||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 162 | my $y = 0; | |||||||||||||||||||||||||||||||||||||||||
| 467 | 162 | ▶ | for( my $i=1; $i<=$n-1; $i+=2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 1733 | $y += $self->_curvature( $mn + $i*$dx, $w )**2; | |||||||||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 162 | $yy = 0.5*$yy + $y*$dx; | |||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | # Make at least 8 steps, then evaluate the relative change between steps | ||||||||||||||||||||||||||||||||||||||||||
| 473 | 162 | ▶ | if( $n > 8 && abs($y*$dx-0.5*$yy) < $eps*$yy ) { last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$n > 8 and abs $y * $dx - "0.5" * $yy < $eps * $yy
MC/DC100%2/2
$n > 8 and abs $y * $dx - "0.5" * $yy < $eps * $yy
$n > 8
abs $y * $dx - "0.5" * $yy < $eps * $yy
Truth table100%3/3
A and B
A= $n > 8B= abs $y * $dx - "0.5" * $yy < $eps * $yy
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | 27 | return $yy; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | # ================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 480 | # Kernels | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | 17157 | ▶ | sub _gauss_pdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gauss_pdf: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | 17157 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 484 | 17157 | my $z = ($x - $m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 485 | 17157 | return exp(-0.5*$z*$z)/( $s*sqrt( 2.0*3.14159265358979323846 ) ); | |||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | # Abramowitz & Stegun, 26.2.17 | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 4 | ▶ | sub _gauss_cdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gauss_cdf: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 4 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | 4 | my $z = abs( $x - $m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 4 | my $t = 1.0/(1.0 + 0.2316419*$z); | |||||||||||||||||||||||||||||||||||||||||
| 494 | 4 | my $y = $t*( 0.319381530 | |||||||||||||||||||||||||||||||||||||||||
| 495 | + $t*( -0.356563782 | ||||||||||||||||||||||||||||||||||||||||||
| 496 | + $t*( 1.781477937 | ||||||||||||||||||||||||||||||||||||||||||
| 497 | + $t*( -1.821255978 + $t*1.330274429 ) ) ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | 4 | ▶ | if( $x >= $m ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 4 | return 1.0 - _gauss_pdf( $x, $m, $s )*$y*$s; | |||||||||||||||||||||||||||||||||||||||||
| 501 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | return _gauss_pdf( $x, $m, $s )*$y*$s; | |||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 7148 | ▶ | sub _gauss_curvature { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_gauss_curvature: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | 7148 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 508 | 7148 | my $z = ($x - $m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 509 | 7148 | return ($z**2 - 1.0)*_gauss_pdf( $x, $m, $s )/$s**2; | |||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 10005 | ▶ | sub _box_pdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_box_pdf: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 513 | 10005 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 10005 | ▶ | if( $x < $m-0.5*$s || $x > $m+0.5*$s ) { return 0.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x < $m - "0.5" * $s or $x > $m + "0.5" * $s
MC/DC100%2/2
$x < $m - "0.5" * $s or $x > $m + "0.5" * $s
$x < $m - "0.5" * $s
$x > $m + "0.5" * $s
Truth table100%3/3
A or B
A= $x < $m - "0.5" * $sB= $x > $m + "0.5" * $s
| |||||||||||||||||||||||||||||||||||||||||||
| 515 | 500 | return 1.0/$s; | |||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | 4 | ▶ | sub _box_cdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_box_cdf: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 4 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | if( $x < $m-0.5*$s ) { return 0.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 4 | ▶ | if( $x > $m+0.5*$s ) { return 1.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | return ( $x-$m )/$s + 0.5; | |||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | sub _box_curvature { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_box_curvature: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | 18001 | ▶ | sub _epanechnikov_pdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_epanechnikov_pdf: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 18001 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 531 | 18001 | my $z = ($x-$m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 18001 | ▶ | if( abs($z) > 1 ) { return 0.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 1800 | return 0.75*(1-$z**2)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | 4 | ▶ | sub _epanechnikov_cdf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_epanechnikov_cdf: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 4 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 538 | 4 | my $z = ($x-$m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | ▶ | if( $z < -1 ) { return 0.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 4 | ▶ | if( $z > 1 ) { return 1.0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | return 0.25*(2.0 + 3.0*$z - $z**3 ); | |||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | ▶ | sub _epanechnikov_curvature { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_epanechnikov_curvature: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | my ( $x, $m, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | my $z = ($x-$m)/$s; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | ▶ | if( abs($z) > 1 ) { return 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | return -1.5/$s**3; | |||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | __END__ | ||||||||||||||||||||||||||||||||||||||||||