blib/lib/VolSurface/Calibration/Equities.pm

filter: Statementstmt 99.1% 243 / 245 Branchbran 89.0% 57 / 64 Conditioncond 80.9% 17 / 21 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 24 / 24 Podpod 100% 3 / 3 totaltotal 95.6% 353 / 369 SCAR 38.3
CC
46
Cov
96%
_optimize
20.0
function_to_optimize
12.0
_calculate_calibrated_vol
8.7
CRAP 46.1
↑ Summary
1 package VolSurface::Calibration::Equities;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.03';
7  
8 =head1 NAME
9  
10 VolSurface::Calibration::Equities
11  
12 =head1 DESCRIPTION
13  
14 This module implements Binary.com's volatility surface calibration algorithm which is based on SABR.
15 The SABR (Stochastic alpha, beta, rho) model is a stochastic volatility model, which is used to
16 estimate the volatility smile in the derivatives market. For a more general overview of the general
17 SABR model, please refer https://en.wikipedia.org/wiki/SABR_volatility_model.
18  
19 =head1 VERSION
20  
21 Version 0.03
22  
23 =cut
24  
25 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Math::Trig qw(tanh);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Format::Util::Numbers qw(roundnear);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head2 calibration_param_names
35  
36 Calibration parameter names.
37 It is hard-coded here because it needs to be in this sequence.
38  
39 =cut
40  
41 2 our @calibration_param_names = qw/
42 atmvolshort
43 atmvol1year
44 atmvolLong
45 atmWingL
46 atmWingR
47 skewshort
48 skew1year
49 skewlong
50 skewwingL
51 skewwingR
52 kurtosisshort
53 kurtosislong
54 kurtosisgrowth/;
55  
56 2 has surface => (
57 is => 'ro',
58 isa => 'HashRef',
59 required => 1,
60 );
61  
62 =head2 term_by_day
63  
64 Get all the terms in a surface in ascending order.
65  
66 =cut
67  
68 2 has term_by_day => (
69 is => 'ro',
70 isa => 'ArrayRef',
71 required => 1,
72 );
73  
74 =head2 parameterization
75  
76 The parameterized (and, thus, smoothed) version of this surface.
77  
78 =cut
79  
80 2 has parameterization => (
81 is => 'rw',
82 isa => 'Maybe[HashRef]',
83 );
84  
85 =head2 smile_points
86  
87 The points across a smile.
88  
89 It can be delta points, moneyness points or any other points that we might have in the future.
90  
91 =cut
92  
93 2 has smile_points => (
94 is => 'ro',
95 isa => 'ArrayRef',
96 required => 1,
97 );
98  
99 2 has _max_iteration => (
100 is => 'ro',
101 isa => 'Int',
102 default => 500,
103 );
104  
105 2 has _tolerance_level => (
106 is => 'ro',
107 isa => 'Num',
108 default => 1e-05,
109 );
110  
111 2 has _scale => (
112 is => 'ro',
113 isa => 'Num',
114 default => 10,
115 );
116  
117 =head2 function_to_optimize
118  
119 The function that we want to optimize.
120  
121 =cut
122  
123 sub function_to_optimize {
124 1835 ▶ my ($self, $params) = @_;
Subroutine function_to_optimize: called CC=12 SCAR=24.8
Pod documented
125  
126 # this number is returned when the optimization is heading the wrong path
127 1835 my $error = 1000;
128  
129 1835 my %params;
130 1835 @params{@calibration_param_names} = @$params;
131  
132 1835 my @tenors = @{$self->term_by_day};
133 my @variance =
134 1835 map { $self->_calculate_variance($_ / 365, \%params) } @tenors;
135  
136 1835 foreach my $param_name (@calibration_param_names) {
137 23503 my $param_value = $params{$param_name};
138 23503 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
if $param_name =~ /wing(R|L)/i and abs $param_value > 20 38 23465 23503
Condition100%3/3
$param_name =~ /wing(R|L)/i and abs $param_value > 20
!ll&&!rl&&r
16258720738
MC/DC100%2/2
$param_name =~ /wing(R|L)/i and abs $param_value > 20
$param_name =~ /wing(R|L)/i abs $param_value > 20
Truth table100%3/3
A and B
A= $param_name =~ /wing(R|L)/iB= abs $param_value > 20
ABresult
0X0
100
111
139 if ($param_name =~ /wing(R|L)/i && abs($param_value) > 20.0);
140 23465 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
if $param_name =~ /growth/i and abs $param_value > 5 8 23457 23465
Condition100%3/3
$param_name =~ /growth/i and abs $param_value > 5
!ll&&!rl&&r
2167917788
MC/DC100%2/2
$param_name =~ /growth/i and abs $param_value > 5
$param_name =~ /growth/i abs $param_value > 5
Truth table100%3/3
A and B
A= $param_name =~ /growth/iB= abs $param_value > 5
ABresult
0X0
100
111
141 if ($param_name =~ /growth/i && abs($param_value) > 5.0);
142 23457 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
if not $param_name =~ /wing(R|L)/i || $param_name =~ /growth/i and abs $param_value > 2 11 23446 23457
Condition100%6/6
not $param_name =~ /wing(R|L)/i || $param_name =~ /growth/i and abs $param_value > 2
!ll&&!rl&&r
89851446111
$param_name =~ /wing(R|L)/i || $param_name =~ /growth/i
l!l&&r!l&&!r
7207177814472
MC/DC100%3/3
not $param_name =~ /wing(R|L)/i || $param_name =~ /growth/i and abs $param_value > 2
$param_name =~ /wing(R|L)/i $param_name =~ /growth/i abs $param_value > 2
Truth table100%4/4
not(A || B) and C
A= $param_name =~ /wing(R|L)/iB= $param_name =~ /growth/iC= abs $param_value > 2
ABCresult
1XX0
01X0
0000
0011
143 if ($param_name !~ /wing(R|L)/i
144 && $param_name !~ /growth/i
145 && abs($param_value) > 2.0);
146 }
147  
148 1778 ▶ return $error if (min(@variance) < 0);
Branch: 1 branch
BranchTrueFalseTotal
if &min(@variance) < 0 65 1713 1778
149  
150 1713 my @atmvols = map { sqrt($_) } @variance;
151 1713 my $calibrated_surface = $self->get_calibrated_surface(\%params);
152 1713 my $actual_surface = $self->surface;
153  
154 1713 my $total = 0;
155 1713 foreach my $day (@{$self->term_by_day}) {
156 11991 my $sum = 0;
157 11991 my $tenorvega = 0;
158 11991 my $atm_vol = shift @atmvols;
159 11991 foreach my $point (@{$self->smile_points}) {
160 251811 my $atm_check = log($point / 100) / $atm_vol / sqrt($day / 365);
161 251811 my $calibrated_vol = $calibrated_surface->{$day}->{$point};
162 251811 my $actual_vol = $actual_surface->{$day}->{smile}->{$point};
163 251811 my $d1 = (log(100 / $point) + (0.5 * $actual_vol**2) * ($day / 365)) / ($actual_vol * ($day / 365)**0.5);
164 251811 my $nd1 = (1 / (2 * 3.1416)**0.5) * exp(-$d1 * $d1 / 2);
165 251811 my $vega = $nd1 * ($day / 365)**0.5;
166 251811 $sum += $vega * abs($calibrated_vol - $actual_vol);
167 251811 $tenorvega += $vega;
168 }
169 11991 $total += $sum / $tenorvega;
170 }
171  
172 1713 return $total;
173 }
174  
175 =head2 default_initial_guess
176  
177 Initial guess for parameters. We need to start with something.
178  
179 =cut
180  
181 has default_initial_guess => (
182 is => 'ro',
183 isa => 'HashRef',
184 default => sub {
185 {
186 1 ▶ atmvolshort => 0.13,
Subroutine __ANON__: called CC=1 SCAR=0.0
187 atmvol1year => 0.19,
188 atmvolLong => 0.12,
189 atmWingL => 1.27,
190 atmWingR => 1.56,
191 skewshort => -0.27,
192 skew1year => -0.08,
193 skewlong => -0.03,
194 skewwingL => 2.01,
195 skewwingR => 2.03,
196 kurtosisshort => 0.001,
197 kurtosislong => 0.001,
198 kurtosisgrowth => 0.001
199 };
200 },
201 2 );
202  
203 =head2 strike_ratio
204  
205 Bounds for moneyness (strike ratio of 1 is equivalent to 100% moneyness).
206  
207 =cut
208  
209 has strike_ratio => (
210 is => 'ro',
211 isa => 'HashRef',
212 default => sub {
213 {
214 1 ▶ lower => 0.60,
Subroutine __ANON__: called CC=1 SCAR=0.0
215 higher => 1.30,
216 };
217 },
218 2 );
219  
220 =head2 compute_parameterization
221  
222 Returns a hash reference with new parameterization and calibration_error.
223 These new parameters are calculated using the current parameterization values saved in cache if present,
224 else it would use default parameterization.
225  
226 my $new_values = $self->compute_parameterization;
227 my $new_params = $new_values->{values};
228 my $new_calibration_error = $new_values->{calibration_error};
229  
230 =cut
231  
232 sub compute_parameterization {
233 1 ▶ my $self = shift;
Subroutine compute_parameterization: called CC=4 SCAR=14.1
Pod documented
234  
235 1 my $opt_using_params_from_cache;
236 1 ▶ if ($self->parameterization) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parameterization) 1 0 1
237 1 my $params_from_cache = $self->_get_params_from($self->parameterization->{values});
238 1 ▶ $opt_using_params_from_cache = try { $self->_optimize($params_from_cache) };
Subroutine __ANON__: called CC=1 SCAR=0.0
239 }
240  
241 1 my $initial_params = $self->_get_params_from($self->default_initial_guess);
242 1 my $opt_using_initial_params = $self->_optimize($initial_params);
243  
244 1 my $new_values;
245 1 ▶ if ($opt_using_params_from_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_using_params_from_cache) { } 1 0 1
246 $new_values =
247 ($opt_using_params_from_cache->{calibration_error} < $opt_using_initial_params->{calibration_error})
248 1 ▶ ? $opt_using_params_from_cache
Branch: 1 branch
BranchTrueFalseTotal
$opt_using_params_from_cache->{'calibration_error'} < $opt_using_initial_params->{'calibration_error'} ? : 1 0 1
249 : $opt_using_initial_params;
250 } else {
251 0 $new_values = $opt_using_initial_params;
252 }
253  
254 # we expect the params to be passed in the correct order
255 1 my %calib_params;
256 1 @calib_params{@calibration_param_names} = @{$new_values->{params}};
257 my %computed_params = (
258 values => \%calib_params,
259 calibration_error => $new_values->{calibration_error},
260 1 date => Date::Utility->new->datetime_iso8601,
261 );
262  
263 1 return \%computed_params;
264 }
265  
266 =head2 get_calibrated_surface
267  
268 compute the calibrated surface with the parameters being passed.
269 my $calibrated = $calibrator->get_calibrated_surface($parameters);
270  
271 =cut
272  
273 sub get_calibrated_surface {
274 1713 ▶ my $self = shift;
Subroutine get_calibrated_surface: called CC=3 SCAR=11.0
Pod documented
275 1713 my $params = shift;
276 1713 my $surface;
277 1713 foreach my $tenor (@{$self->term_by_day}) {
278 11991 foreach my $point (@{$self->smile_points}) {
279 251811 $surface->{$tenor}->{$point} = $self->_calculate_calibrated_vol($tenor, $point, $params);
280 }
281 }
282  
283 1713 return $surface;
284 }
285  
286 =head2 _calculate_calibrated_vol
287  
288 The method that calculates calibrated vol using given parameters
289  
290 =cut
291  
292 sub _calculate_calibrated_vol {
293 251811 ▶ my ($self, $tenor, $point, $params) = @_;
Subroutine _calculate_calibrated_vol: called CC=8 SCAR=21.6
294  
295 251811 ▶ return unless defined $point;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $point 0 251811 251811
296 251811 ▶ if (not $params) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params) 0 251811 251811
297 $params = $self->parameterization->{values}
298 0 ▶ || $self->default_initial_guess;
Condition0%0/3
$self->parameterization->{'values'} || $self->default_initial_guess
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->parameterization->{'values'} || $self->default_initial_guess
$self->parameterization->{'values'} $self->default_initial_guess
Truth table0%0/3
A || B
A= $self->parameterization->{'values'}B= $self->default_initial_guess
ABresult
1X1
011
000
299 }
300  
301 251811 my $tiy = $tenor / 365;
302 251811 my $kurtosis = $self->_calculate_kurtosis($tiy, $params);
303 251811 my $variance = $self->_calculate_variance($tiy, $params);
304 251811 my $skew = $self->_calculate_skew($tiy, $params);
305  
306 251811 my $atm_vol = sqrt($variance);
307 251811 my $tmp_corr = 6 * (0.25 + $kurtosis * $atm_vol / 4 / $skew**2);
308 251811 ▶ my $corr = ($tmp_corr < 0) ? -0.99 : (-0.99 * min(1, 1 / sqrt($tmp_corr)));
Branch: 1 branch
BranchTrueFalseTotal
$tmp_corr < 0 ? : 0 251811 251811
309 251811 my $volofvol = 2 * $skew / $corr;
310  
311 251811 my $x = log($point / 100) / $atm_vol / sqrt($tiy);
312 251811 my $x_min = log($self->strike_ratio->{lower}) / $atm_vol / sqrt($tiy);
313 251811 my $x_max = log($self->strike_ratio->{higher}) / $atm_vol / sqrt($tiy);
314  
315 251811 ▶ return $atm_vol if abs($x) < 0.00000001;
Branch: 1 branch
BranchTrueFalseTotal
if abs $x < "1e-08" 11991 239820 251811
316  
317 239820 ▶ $x =
Branch: 1 branch
BranchTrueFalseTotal
$x > 0 ? : 119910 119910 239820
318 ($x > 0)
319 ? $x_max * tanh($x / $x_max)
320 : $x_min * tanh($x / $x_min);
321 239820 my $z = (-1 * $volofvol * $x);
322 239820 ▶ return $atm_vol if abs($z) < 0.00000001;
Branch: 1 branch
BranchTrueFalseTotal
if abs $z < "1e-08" 0 239820 239820
323  
324 239820 my $d = log((sqrt(1 - 2 * $corr * $z + $z**2) - $corr + $z) / (1 - $corr));
325 239820 return $atm_vol * $z / $d;
326 }
327  
328 =head2 _get_params_from
329  
330 calculation metohds which mostly do "mathematical" jobs.
331  
332 =cut
333  
334 sub _get_params_from {
335 2 ▶ my ($self, $param_hash) = @_;
Subroutine _get_params_from: called CC=1 SCAR=0.0
336  
337 my @guess =
338 2 map { roundnear(0.0001, $param_hash->{$_}) } @calibration_param_names;
339  
340 2 return \@guess;
341 }
342  
343 =head2 _calculate_skew
344  
345 The calibration approach is based upon modeling the term structure of ATM Volatility and ATM Skew using exponential functions.
346  
347 =cut
348  
349 sub _calculate_skew {
350 251811 ▶ my ($self, $tiy, $params) = @_;
Subroutine _calculate_skew: called CC=1 SCAR=0.0
351  
352 251811 my $sig_small = $params->{skewshort};
353 251811 my $sig_1y = $params->{skew1year};
354 251811 my $sig_inf = $params->{skewlong};
355 251811 my $t_small = 1.0;
356 251811 my $atm_alpha1 = $params->{skewwingL};
357 251811 my $atm_alpha2 = $params->{skewwingR};
358  
359 251811 my $skew =
360 $sig_inf +
361 ($sig_1y - $sig_inf) *
362 ((exp(-$atm_alpha2 * $tiy) - exp(-$atm_alpha1 * $tiy)) / (exp(-$atm_alpha2 * $t_small) - exp(-$atm_alpha1 * $t_small))) +
363 ($sig_small - $sig_inf) *
364 ((exp(-$atm_alpha2 * $t_small - $atm_alpha1 * $tiy) - exp(-$atm_alpha1 * $t_small - $atm_alpha2 * $tiy)) /
365 (exp(-$atm_alpha2 * $t_small) - exp(-$atm_alpha1 * $t_small)));
366  
367 251811 return $skew;
368 }
369  
370 =head2 _calculate_kurtosis
371  
372 kurtosis can be manipulated using a simple growth rate function.
373 Kurtosis provides a symmetric control over the wings of a
374 surface. Kurtosis basically shifts the wings of the curve in a symetric way.
375  
376 =cut
377  
378 sub _calculate_kurtosis {
379 251811 ▶ my ($self, $tiy, $params) = @_;
Subroutine _calculate_kurtosis: called CC=1 SCAR=0.0
380  
381 251811 my $kurt_small = $params->{kurtosisshort};
382 251811 my $kurt_alpha = $params->{kurtosisgrowth};
383 251811 my $kurt_inf = $params->{kurtosislong};
384  
385 251811 return $kurt_small + ($kurt_inf - $kurt_small) * (1 - exp(-$kurt_alpha * $tiy));
386 }
387  
388 sub _calculate_variance {
389 264656 ▶ my ($self, $tiy, $params) = @_;
Subroutine _calculate_variance: called CC=1 SCAR=0.0
390  
391 264656 my $sig_small = $params->{atmvolshort}**2;
392 264656 my $sig_1y = $params->{atmvol1year}**2;
393 264656 my $sig_inf = $params->{atmvolLong}**2;
394 264656 my $t_small = 1.0;
395 264656 my $atm_alpha1 = $params->{atmWingL};
396 264656 my $atm_alpha2 = $params->{atmWingR};
397  
398 264656 my $atm_vol =
399 $sig_inf +
400 ($sig_1y - $sig_inf) *
401 ((exp(-$atm_alpha2 * $tiy) - exp(-$atm_alpha1 * $tiy)) / (exp(-$atm_alpha2 * $t_small) - exp(-$atm_alpha1 * $t_small))) +
402 ($sig_small - $sig_inf) *
403 ((exp(-$atm_alpha2 * $t_small - $atm_alpha1 * $tiy) - exp(-$atm_alpha1 * $t_small - $atm_alpha2 * $tiy)) /
404 (exp(-$atm_alpha2 * $t_small) - exp(-$atm_alpha1 * $t_small)));
405  
406 264656 return $atm_vol;
407 }
408  
409 =head2 _optimize
410  
411 Algorithm change - now based on centroid calculations
412 A function that optimizes a set of parameters against a function.
413 This optimization method is based on Amoeba optimization. We use a form of the
414 Downhill Simplex Method or Nelder-Mead (available as the R function optim).
415 This can also be coded in other languages.
416  
417 =cut
418  
419 sub _optimize {
420 2 ▶ my ($self, $params) = @_;
Subroutine _optimize: called CC=20 SCAR=30.0
421  
422 2 my $intol = $self->_tolerance_level;
423 2 my $num_of_var = scalar(@$params);
424 2 my $num_of_points = $num_of_var;
425  
426 2 my $highest_value;
427  
428 2 my $lambda1 = ((($num_of_points + 1)**0.5) - 1 + $num_of_points) / ($num_of_points * (2**0.5)) * $self->_scale;
429 2 my $lambda2 = ((($num_of_points + 1)**0.5) - 1) / ($num_of_points * (2**0.5)) * $self->_scale;
430  
431 2 my $i;
432 my $j;
433  
434 #initialize the simplex with the initial guess
435 2 my @simplex = ($params);
436  
437 2 my $step = 0;
438 2 ▶ for ($i = 0; $i < $num_of_var; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_of_var) 26 2 28
439 26 ▶ $step = 0.1 * abs($params->[$i])
Branch: 1 branch
BranchTrueFalseTotal
if "0.1" * abs($params->[$i]) > $step 10 16 26
440 if (0.1 * abs($params->[$i]) > $step);
441 }
442  
443 2 ▶ for (my $i = 0; $i < $num_of_points; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_of_points) 26 2 28
444 26 ▶ push @simplex, [map { $simplex[0][$_] + ($i == $_ ? $step : 0) } (0 .. $num_of_var - 1)];
Branch: 1 branch
BranchTrueFalseTotal
$i == $_ ? : 26 312 338
445 }
446  
447 2 my @function_eval = map { $self->function_to_optimize($_) } @simplex;
448 2 my $starting_value = $function_eval[0];
449  
450 2 my @current_reflection;
451 my @current_expansion;
452 2 my @current_contraction;
453  
454 2 my $expansion_function_eval;
455 2 my $reflected_function_eval;
456 2 my $contraction_function_eval;
457 2 my $lowest_point_index = 0;
458  
459 2 my $calcvert = 1;
460 2 my $counter = 0;
461  
462 2 while (1) {
463 798 ▶ if ($calcvert == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calcvert == 1) 9 789 798
464 9 $calcvert = 0;
465 9 ▶ for ($j = 0; $j <= $num_of_points; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $num_of_points) 126 9 135
466 126 ▶ if ($j != $lowest_point_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j != $lowest_point_index) 117 9 126
467 117 $function_eval[$j] = $self->function_to_optimize($simplex[$j]);
468 }
469 }
470 }
471  
472 798 my @sorted_eval = sort { $a <=> $b } @function_eval;
473 798 ▶ $lowest_point_index = first_index { $_ == $sorted_eval[0] } @function_eval;
Subroutine __ANON__: called CC=1 SCAR=0.0
474 798 ▶ my $highest_point_index = first_index { $_ == $sorted_eval[-1] } @function_eval;
Subroutine __ANON__: called CC=1 SCAR=0.0
475  
476 798 my @simplex_centroid = _calculate_simplex_centroid(\@simplex, $num_of_points, $num_of_var, $highest_point_index);
477  
478 798 $highest_value = $function_eval[$highest_point_index];
479  
480 798 my $convtol = $intol * ($starting_value + $intol);
481  
482 last
483 798 ▶ if ($function_eval[$lowest_point_index] < $intol
Branch: 1 branch
BranchTrueFalseTotal
if $function_eval[$lowest_point_index] < $intol or $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtol or $counter > $self->_max_iteration 2 796 798
Condition83%5/6
$function_eval[$lowest_point_index] < $intol or $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtol or $counter > $self->_max_iteration
l!l&&r!l&&!r
11796
$function_eval[$lowest_point_index] < $intol or $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtol
l!l&&r!l&&!r
01797
MC/DC66%2/3
$function_eval[$lowest_point_index] < $intol or $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtol or $counter > $self->_max_iteration
$function_eval[$lowest_point_index] < $intol $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtol $counter > $self->_max_iteration
Truth table75%3/4
A or B or C
A= $function_eval[$lowest_point_index] < $intolB= $function_eval[$highest_point_index] < $function_eval[$lowest_point_index] + $convtolC= $counter > $self->_max_iteration
ABCresult
1XX1
01X1
0011
0000
484 or ($function_eval[$highest_point_index] < ($function_eval[$lowest_point_index] + $convtol))
485 or $counter > $self->_max_iteration);
486  
487 #imple reflection of the highest point
488 @current_reflection =
489 796 map { 2 * $simplex_centroid[$_] - $simplex[$highest_point_index][$_] } (0 .. $num_of_var - 1);
490 796 my $reflected_function_eval = $self->function_to_optimize(\@current_reflection);
491 796 ▶ if ($reflected_function_eval < $function_eval[$lowest_point_index]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reflected_function_eval < $function_eval[$lowest_point_index]) { } 134 662 796
492  
493 #Do simple expansion or in other words look up a little further in this direction
494 134 @current_expansion = map { 2 * $current_reflection[$_] - $simplex_centroid[$_] } (0 .. $num_of_var - 1);
495  
496 134 $expansion_function_eval = $self->function_to_optimize(\@current_expansion);
497 134 ▶ if ($expansion_function_eval < $reflected_function_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expansion_function_eval < $reflected_function_eval) { } 81 53 134
498  
499 #replace highest point with expansion point
500 #assing function value to highest point
501 81 @{$simplex[$highest_point_index]} = @current_expansion;
502 81 $function_eval[$highest_point_index] = $expansion_function_eval;
503 } else {
504  
505 #replace highest point with reflected point
506 #assign highest value to reflection point
507 53 @{$simplex[$highest_point_index]} = @current_reflection;
508 53 $function_eval[$highest_point_index] = $reflected_function_eval;
509 }
510  
511 } else {
512 662 ▶ if ($reflected_function_eval < $highest_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reflected_function_eval < $highest_value) 558 104 662
513  
514 #replace the simplex highest point with reflected point;
515 558 @{$simplex[$highest_point_index]} = @current_reflection;
516 558 $function_eval[$highest_point_index] = $reflected_function_eval;
517 }
518  
519 @current_contraction =
520 662 map { ($simplex_centroid[$_] + $simplex[$highest_point_index][$_]) / 2 } (0 .. $num_of_var - 1);
521 662 $contraction_function_eval = $self->function_to_optimize(\@current_contraction);
522 662 ▶ if ($contraction_function_eval < $function_eval[$highest_point_index]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($contraction_function_eval < $function_eval[$highest_point_index]) { } 379 283 662
elsif ($reflected_function_eval >= $highest_value) { } 7 276 283
523 379 @{$simplex[$highest_point_index]} = @current_contraction;
524 379 $function_eval[$highest_point_index] = $contraction_function_eval;
525 } elsif ($reflected_function_eval >= $highest_value) {
526 7 $calcvert = 1;
527 7 ▶ for ($i = 0; $i <= $num_of_points; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $num_of_points) 98 7 105
528 98 ▶ if ($i != $lowest_point_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $lowest_point_index) 91 7 98
529 91 @{$simplex[$i]} =
530 91 map { ($simplex[$lowest_point_index][$_] + $simplex[$i][$_]) / 2.0 } (0 .. $num_of_var - 1);
531 }
532 98 $function_eval[$highest_point_index] = $self->function_to_optimize($simplex[$highest_point_index]);
533 }
534 }
535 }
536 796 $counter++;
537 }
538  
539 2 my $min_counter = 0;
540 2 my $lowest_point_value = $function_eval[0];
541 2 $lowest_point_index = 0;
542  
543 2 for my $eval_item (@function_eval) {
544 28 ▶ if ($eval_item < $lowest_point_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eval_item < $lowest_point_value) 4 24 28
545 4 $lowest_point_index = $min_counter;
546 4 $lowest_point_value = $eval_item;
547 }
548  
549 28 $min_counter++;
550 }
551  
552 2 my $new_params = $simplex[$lowest_point_index];
553 2 my $new_calibration_error = $function_eval[$lowest_point_index] * 1000;
554  
555 return {
556 2 params => $new_params,
557 calibration_error => $new_calibration_error,
558 };
559 }
560  
561 sub _calculate_simplex_centroid {
562 798 ▶ my ($simplex, $num_of_points, $num_of_var, $highest_point_index) = @_;
Subroutine _calculate_simplex_centroid: called CC=4 SCAR=13.9
563  
564 798 my @simplex_sum;
565  
566 798 ▶ for (my $j = 0; $j < $num_of_var; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $num_of_var) 10374 798 11172
567 10374 my $sum = 0;
568 10374 ▶ for (my $i = 0; $i <= $num_of_points; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $num_of_points) 145236 10374 155610
569 145236 ▶ $sum += $simplex->[$i]->[$j] if ($i != $highest_point_index);
Branch: 1 branch
BranchTrueFalseTotal
if $i != $highest_point_index 134862 10374 145236
570 }
571  
572 #Centroied instead of sum for easy of calculation
573 10374 $simplex_sum[$j] = $sum / $num_of_var;
574 }
575 798 return @simplex_sum;
576 }
577  
578 2 1;
579  
580 =head1 AUTHOR
581  
582 Binary.com, C<< <support at binary.com> >>
583  
584 =head1 BUGS
585  
586 Please report any bugs or feature requests to C<bug-volsurface-calibration-equities at rt.cpan.org>, or through
587 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=VolSurface-Calibration-Equities>. I will be notified, and then you'll
588 automatically be notified of progress on your bug as I make changes.
589  
590 =head1 SUPPORT
591  
592 You can find documentation for this module with the perldoc command.
593 perldoc VolSurface::Calibration::Equities
594 You can also look for information at:
595  
596 =over 4
597  
598 =item * RT: CPAN's request tracker (report bugs here)
599 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=VolSurface-Calibration-Equities>
600  
601 =item * AnnoCPAN: Annotated CPAN documentation
602  
603 L<http://annocpan.org/dist/VolSurface-Calibration-Equities>
604  
605 =item * CPAN Ratings
606  
607 L<http://cpanratings.perl.org/d/VolSurface-Calibration-Equities>
608  
609 =item * Search CPAN
610  
611 L<http://search.cpan.org/dist/VolSurface-Calibration-SAB/>
612  
613 =back
614  
615 =head1 LICENSE AND COPYRIGHT
616  
617 Copyright 2015 Binary.com.
618 This program is free software; you can redistribute it and/or modify it
619 under the terms of the the Artistic License (2.0). You may obtain a
620 copy of the full license at:
621 L<http://www.perlfoundation.org/artistic_license_2_0>
622 Any use, modification, and distribution of the Standard or Modified
623 Versions is governed by this Artistic License. By using, modifying or
624 distributing the Package, you accept this license. Do not use, modify,
625 or distribute the Package, if you do not accept this license.
626 If your Modified Version has been derived from a Modified Version made
627 by someone other than you, you are nevertheless required to ensure that
628 your Modified Version complies with the requirements of this license.
629 This license does not grant you the right to use any trademark, service
630 mark, tradename, or logo of the Copyright Holder.
631 This license includes the non-exclusive, worldwide, free-of-charge
632 patent license to make, have made, use, offer to sell, sell, import and
633 otherwise transfer the Package with respect to any patent claims
634 licensable by the Copyright Holder that are necessarily infringed by the
635 Package. If you institute patent litigation (including a cross-claim or
636 counterclaim) against any party alleging that the Package constitutes
637 direct or contributory patent infringement, then this Artistic License
638 to you shall terminate on the date that such litigation is filed.
639 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
640 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
641 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
642 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
643 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
644 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
645 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
646 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
647  
648 =cut
↑ Summary