Framework/VolSurface.pm untested

filter: Statementstmt 0.0% 0 / 360 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 64 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 611 CC 51 SCAR 31.2
CC
51
Cov
0%
_validate_structure
90.0
get_spread
56.0
_validate_identical_surface
42.0
CRAP 22.7
1 package Quant::Framework::VolSurface;
2  
3 =head1 NAME
4  
5 Quant::Framework::VolSurface
6  
7 =head1 DESCRIPTION
8  
9 Base class for all volatility surfaces.
10  
11 =cut
12  
13 use Moose;
14 use Try::Tiny;
15 use DateTime::TimeZone;
16 use List::MoreUtils qw(notall any);
17 use Scalar::Util qw( looks_like_number );
18 use List::Util qw( min max first );
19 use Number::Closest::XS qw(find_closest_numbers_around);
20 use Math::Function::Interpolator;
21 use Storable qw(dclone);
22  
23 use Quant::Framework::Utils::Types;
24 use Quant::Framework::VolSurface::Utils;
25 use Quant::Framework::Utils::Builder;
26  
27 =head2 for_date
28  
29 The date for which we want to have the volatility surface data
30  
31 =cut
32  
33 has for_date => (
34 is => 'ro',
35 isa => 'Maybe[Date::Utility]',
36 default => undef,
37 );
38  
39 has chronicle_reader => (
40 is => 'ro',
41 isa => 'Data::Chronicle::Reader',
42 );
43  
44 has chronicle_writer => (
45 is => 'ro',
46 isa => 'Data::Chronicle::Writer',
47 );
48  
49 =head2 type
50  
51 Type of the surface, delta, moneyness or flat.
52  
53 =cut
54  
55 has type => (
56 is => 'ro',
57 isa => 'qf_surface_type',
58 required => 1,
59 init_arg => undef,
60 default => undef,
61 );
62  
63 =head2 underlying_config
64  
65 Contains underlying specific information
66  
67 =cut
68  
69 has underlying_config => (
70 is => 'ro',
71 required => 1,
72 );
73  
74 has document => (
75 is => 'rw',
76 lazy_build => 1,
77 );
78  
79 sub _build_document {
80 my $self = shift;
81  
82 my $document = $self->chronicle_reader->get('volatility_surfaces', $self->symbol);
83 if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) {
84 $document = $self->chronicle_reader->get_for('volatility_surfaces', $self->symbol, $self->for_date->epoch);
85 }
86  
87 die('Document is undefined for ' . $self->symbol) unless $document;
88  
89 return $document;
90 }
91  
92 =head2 effective_date
93  
94 Surfaces roll over at 5pm NY time, so the vols of any surfaces recorded after 5pm NY but
95 before GMT midnight are effectively for the next GMT day. This attribute holds this
96 effective date.
97  
98 =cut
99  
100 has effective_date => (
101 is => 'ro',
102 isa => 'Date::Utility',
103 init_arg => undef,
104 lazy_build => 1,
105 );
106  
107 sub _build_effective_date {
108 my $self = shift;
109  
110 return Quant::Framework::VolSurface::Utils->new->effective_date_for($self->recorded_date);
111 }
112  
113 has calendar => (
114 is => 'ro',
115 isa => 'Quant::Framework::TradingCalendar',
116 lazy_build => 1,
117 );
118  
119 sub _build_calendar {
120 my $self = shift;
121  
122 return $self->builder->build_trading_calendar;
123 }
124  
125 has builder => (
126 is => 'ro',
127 isa => 'Quant::Framework::Utils::Builder',
128 lazy_build => 1,
129 );
130  
131 sub _build_builder {
132 my $self = shift;
133  
134 return Quant::Framework::Utils::Builder->new({
135 for_date => $self->for_date,
136 chronicle_reader => $self->chronicle_reader,
137 chronicle_writer => $self->chronicle_writer,
138 underlying_config => $self->underlying_config,
139 });
140 }
141  
142 =head2 surface
143  
144 Volatility surface in a hash reference.
145  
146 =cut
147  
148 has surface => (
149 is => 'ro',
150 lazy_build => 1,
151 );
152  
153 sub _build_surface {
154 my $self = shift;
155  
156 my %surface_data = %{$self->surface_data};
157 my $expiry_conventions = $self->builder->build_expiry_conventions;
158 my $effective_date = $self->effective_date;
159  
160 # If the smile's day is given as a tenor, we convert
161 # it to a day and add the tenor to the smile:
162 foreach my $maturity (keys %surface_data) {
163 # if there's no data, delete it.
164 if (
165 not( (exists $surface_data{$maturity}{smile} and %{$surface_data{$maturity}{smile}})
166 or (exists $surface_data{$maturity}{vol_spread} and %{$surface_data{$maturity}{vol_spread}})))
167 {
168 delete $surface_data{$maturity};
169 next;
170 }
171 if ($maturity =~ /^(?:ON|\d{1,2}[WMY])$/) {
172 my $vol_expiry_date = $expiry_conventions->vol_expiry_date({
173 from => $effective_date,
174 term => $maturity
175 });
176 my $day = $vol_expiry_date->days_between($effective_date);
177  
178 $surface_data{$day} = delete $surface_data{$maturity};
179 $surface_data{$day}{tenor} = $maturity;
180 } elsif ($maturity !~ /^\d+$/) {
181 # Don't die here. This surface will be invalidated later.
182 warn("Unknown tenor[$maturity] found on volatility surface for " . $self->symbol);
183 }
184 }
185  
186 return \%surface_data;
187 }
188  
189 =head1 ATTRIBUTES
190  
191 =head2 symbol
192  
193 The symbol of the underlying that this surface is for (e.g. frxUSDJPY)
194  
195 =cut
196  
197 has symbol => (
198 is => 'ro',
199 lazy_build => 1,
200 );
201  
202 sub _build_symbol {
203 my $self = shift;
204  
205 return $self->underlying_config->system_symbol;
206 }
207  
208 =head2 recorded_date
209  
210 The date (and time) that the surface was recorded, as a Date::Utility.
211  
212 =cut
213  
214 has recorded_date => (
215 is => 'ro',
216 isa => 'Date::Utility',
217 lazy_build => 1,
218 );
219  
220 sub _build_recorded_date {
221 my $self = shift;
222  
223 return Date::Utility->new($self->document->{date});
224 }
225  
226 =head2 smile_points
227  
228 The points across a smile.
229  
230 It can be delta points, moneyness points or any other points that we might have in the future.
231  
232 =cut
233  
234 has smile_points => (
235 is => 'ro',
236 isa => 'ArrayRef',
237 lazy_build => 1,
238 );
239  
240 sub _build_smile_points {
241 my $self = shift;
242  
243 # Default to the point found in the first day we find
244 # in $self->surface that has a smile. As long as each smile
245 # has the same points, this works. If each smile has different
246 # points, the Validator is going to give you trouble!
247 my $surface = $self->surface;
248 my $suitable_day = first { exists $surface->{$_}->{smile} } @{$self->term_by_day};
249  
250 return [sort { $a <=> $b } keys %{$surface->{$suitable_day}->{smile}}] if $suitable_day;
251 return [];
252 }
253  
254 =head2 spread_points
255  
256 This will give an array-reference containing volatility spreads for first tenor which has a volatility spread (or ATM if none).
257 =cut
258  
259 has spread_points => (
260 is => 'ro',
261 isa => 'ArrayRef',
262 lazy_build => 1,
263 );
264  
265 sub _build_spread_points {
266 my $self = shift;
267  
268 # Default to the point found in the first day we find
269 # in $self->surface that has a volspread. As long as each volspread
270 # has the same points, this works. If each smile has different
271 # points, the Validator is going to give you trouble!
272 my $surface = $self->surface;
273 my $suitable_day = first { exists $surface->{$_}->{vol_spread} } keys %{$surface};
274  
275 return [sort { $a <=> $b } keys %{$surface->{$suitable_day}{vol_spread}}] if $suitable_day;
276 return [];
277 }
278  
279 =head2 term_by_day
280  
281 Get all the terms in a surface in ascending order.
282  
283 =cut
284  
285 has term_by_day => (
286 is => 'ro',
287 isa => 'ArrayRef',
288 init_arg => undef,
289 lazy_build => 1,
290 );
291  
292 sub _build_term_by_day {
293 my $self = shift;
294  
295 return [sort { $a <=> $b } keys %{$self->surface}];
296 }
297  
298 =head2 original_term
299  
300 The terms we were originally supplied for this surface.
301  
302 =cut
303  
304 has original_term => (
305 is => 'ro',
306 isa => 'HashRef',
307 lazy_build => 1,
308 );
309  
310 sub _build_original_term {
311 my $self = shift;
312  
313 # this is from original surface data.
314 my $surface = $self->surface;
315 my @days = map { /^(?:ON|\d{1,2}[WMY])$/ ? $self->get_day_for_tenor($_) : $_ } keys %{$self->surface_data};
316 my @vol_spreads = grep { exists $surface->{$_}{vol_spread} } @days;
317 my @smiles = grep { exists $surface->{$_}{smile} } @days;
318  
319 # We must have at least 2 atm_spreads if this is a valid surface with multiple smiles.
320 # Set the end points to the default if they are unset to make up any difference.
321 if (scalar @vol_spreads < min(2, scalar @smiles)) {
322 foreach my $end_index (0, -1) {
323 my $day = $days[$end_index];
324 next if exists $surface->{$day}{vol_spread};
325 $surface->{$day}{vol_spread} = {$self->atm_spread_point => 0.1};
326 push @vol_spreads, $day;
327 }
328 }
329  
330 return +{
331 vol_spread => \@vol_spreads,
332 smile => \@smiles,
333 };
334  
335 }
336  
337 =head2 original_term_for_smile
338  
339 The original term structure we have for smiles on a surface
340  
341 =cut
342  
343 has original_term_for_smile => (
344 is => 'ro',
345 isa => 'ArrayRef',
346 lazy_build => 1,
347 );
348  
349 sub _build_original_term_for_smile {
350 my $self = shift;
351  
352 return [sort { $a <=> $b } @{$self->original_term->{smile}}];
353 }
354  
355 =head2 original_term_for_spread
356  
357 The original term structure we have for spreads on a surface
358  
359 =cut
360  
361 has original_term_for_spread => (
362 is => 'ro',
363 isa => 'ArrayRef',
364 lazy_build => 1,
365 );
366  
367 sub _build_original_term_for_spread {
368 my $self = shift;
369  
370 return [sort { $a <=> $b } @{$self->original_term->{vol_spread}}];
371 }
372  
373 # PRIVATE ATTRIBUTES:
374  
375 # will probably remove this in the future. I don't see a difference between this and expiry convention.
376 has _ON_day => (
377 is => 'ro',
378 isa => 'Int',
379 lazy_build => 1,
380 );
381  
382 #Returns the day for over-night tenor of this surface
383 #ON = over-night
384 sub _build__ON_day {
385 my $self = shift;
386  
387 return $self->calendar->calendar_days_to_trade_date_after($self->effective_date);
388 }
389  
390 around BUILDARGS => sub {
391 my $orig = shift;
392 my $class = shift;
393 my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
394  
395 if ($args{surface} or $args{recorded_date} or $args{surface_data}) {
396 $args{surface_data} = $args{surface} if $args{surface} and not $args{surface_data};
397 delete $args{surface}; # surface will always be built from surface_data.
398 die('Must pass both "surface_data" and "recorded_date" if passing either.') if (not($args{surface_data} and $args{recorded_date}));
399 }
400  
401 # symbol will be built from underlying_config
402 delete $args{symbol};
403  
404 return $class->$orig(%args);
405 };
406  
407 =head2 get_spread
408  
409 Spread is ask-bid difference which can be stored for different tenor and atm/max.
410 This is value could be adjusted for pricing under certain conditions.
411  
412 USAGE:
413  
414 my $spread = $volsurface->get_spread({sought_point=> 'atm', day=> 7});
415 will return the atm spread for the day.
416  
417 my $spread = $volsurface->get_spread({sought_point=> 'max', day=> 7});
418 will return the max spread for the day.
419  
420 =cut
421  
422 sub get_spread {
423 my ($self, $args) = @_;
424  
425 my $sought_point = $args->{sought_point};
426 my $day = $args->{day};
427 $day = $self->get_day_for_tenor($day)
428 if ($day =~ /^(?:ON|\d[WMY])$/); # if day looks like tenor
429  
430 my $surface = $self->surface;
431 # prevent autovivification
432 my $smile_spread = (exists $surface->{$day} and exists $surface->{$day}{vol_spread}) ? $surface->{$day}{vol_spread} : +{};
433 $smile_spread = $self->get_smile_spread($day) unless %$smile_spread;
434  
435 my $spread;
436 if ($sought_point eq 'atm') {
437 $spread = $smile_spread->{$self->atm_spread_point};
438 } elsif ($sought_point eq 'max') {
439 $spread = max(values %$smile_spread);
440 } else {
441 die 'Unrecognized spread type ' . $sought_point;
442 }
443  
444 return $spread + $self->min_vol_spread if ($self->can('min_vol_spread') and $day < 30 and $spread < $self->min_vol_spread);
445 return $spread;
446 }
447  
448 =head2 get_smile_spread
449  
450 Returns the ask/bid spread for smile of this volatility surface
451 This is the raw spread.
452  
453 =cut
454  
455 sub get_smile_spread {
456 my ($self, $day) = @_;
457  
458 my $surface = $self->surface;
459  
460 # autovivification
461 return $surface->{$day}{vol_spread} if (exists $surface->{$day} and exists $surface->{$day}{vol_spread});
462 return $self->_compute_and_set_smile_spread($day);
463 }
464  
465 sub _compute_and_set_smile_spread {
466 my ($self, $day) = @_;
467  
468 my $surface = $self->surface;
469 my $spread_terms = $self->original_term_for_spread;
470 my @points = $self->_get_points_to_interpolate($day, $spread_terms);
471  
472 if (not $self->_is_between($day, \@points)) {
473 my $index = $day > $spread_terms->[-1] ? $spread_terms->[-1] : $spread_terms->[0];
474 return $surface->{$index}{vol_spread};
475 }
476  
477 my %smile_spread = map {
478 $_ => Math::Function::Interpolator->new(
479 points => {
480 $points[0] => $surface->{$points[0]}->{vol_spread}->{$_},
481 $points[1] => $surface->{$points[1]}->{vol_spread}->{$_},
482 }
483 )->linear($day)
484 } @{$self->spread_points};
485  
486 $self->surface->{$day}{vol_spread} = \%smile_spread;
487  
488 return \%smile_spread;
489 }
490  
491 sub _get_points_to_interpolate {
492 my ($self, $seek, $available_points) = @_;
493 die('Need 2 or more term structures to interpolate.')
494 if scalar @$available_points <= 1;
495  
496 return @{find_closest_numbers_around($seek, $available_points, 2)};
497 }
498  
499 sub _is_between {
500 my ($self, $seek, $points) = @_;
501  
502 my @points = @$points;
503  
504 die('some of the points are not defined')
505 if (notall { defined $_ } @points);
506 die('less than two available points')
507 if (scalar @points < 2);
508  
509 return if $seek > max(@points) or $seek < min(@points);
510 return 1;
511 }
512  
513 =head2 get_day_for_tenor
514  
515 USAGE:
516  
517 my $day = $volsurface->get_day_for_tenor('1W');
518  
519 Get the corresponding day for a given tenor, if one exists.
520  
521 =cut
522  
523 sub get_day_for_tenor {
524 my ($self, $tenor) = @_;
525  
526 my $surface_data = $self->surface;
527 my $day = first {
528 $surface_data->{$_}->{tenor} && $surface_data->{$_}->{tenor} eq $tenor;
529 }
530 @{$self->term_by_day};
531  
532 # If tenor doesn't already exist on surface, get $days via the vol expiry date.
533 if (not $day) {
534 $day = $self->builder->build_expiry_conventions->vol_expiry_date({
535 from => $self->effective_date,
536 term => $tenor,
537 })->days_between($self->effective_date);
538 }
539  
540 return $day;
541 }
542  
543 =head2 get_rr_bf_for_smile
544  
545 Return the rr and bf values for a given smile
546 For more info see: https://en.wikipedia.org/wiki/Risk_reversal and https://en.wikipedia.org/wiki/Butterfly_(options)
547  
548 =cut
549  
550 sub get_rr_bf_for_smile {
551 my ($self, $market_smile) = @_;
552  
553 my $result = {
554 ATM => $market_smile->{50},
555 RR_25 => $market_smile->{25} - $market_smile->{75},
556 BF_25 => ($market_smile->{25} + $market_smile->{75}) / 2 - $market_smile->{50},
557 };
558 if (exists $market_smile->{10}) {
559 $result->{RR_10} = $market_smile->{10} - $market_smile->{90};
560 $result->{BF_10} = ($market_smile->{10} + $market_smile->{90}) / 2 - $market_smile->{50};
561 }
562  
563 return $result;
564 }
565  
566 =head2 is_valid
567  
568 Does this volatility surface pass our validation.
569  
570 =cut
571  
572 sub is_valid {
573 my $self = shift;
574  
575 my @validation_methods = $self->_validation_methods;
576  
577 try {
578 $self->$_ for ($self->_validation_methods);
579 }
580 catch {
581 $self->validation_error($_);
582 };
583  
584 return !$self->validation_error;
585 }
586  
587 sub _validate_age {
588 my $self = shift;
589  
590 if (time - $self->recorded_date->epoch > 4 * 3600) {
591 die('Volatility surface from provider for ' . $self->symbol . ' is more than 4 hours old.');
592 }
593  
594 return;
595 }
596  
597 sub _validate_structure {
598 my $self = shift;
599  
600 my $surface_hashref = $self->surface;
601 my $system_symbol = $self->symbol;
602  
603 # Somehow I do not know why there is a limit of term on delta surface, but
604 # for moneyness we might need at least up to 2 years to get the spread.
605 my $type = $self->type;
606  
607 my $extra_allowed = $self->underlying_config->extra_vol_diff_by_delta || 0;
608 my $max_vol_change_by_delta = 0.4 + $extra_allowed;
609  
610 my @days = sort { $a <=> $b } keys %{$surface_hashref};
611  
612 if (@days < 2) {
613 die('Must be at least two maturities on vol surface for ' . $self->symbol . '.');
614 }
615  
616 if ($days[-1] > $self->_max_allowed_term) {
617 die("Day[$days[-1]] in volsurface for underlying[$system_symbol] greater than allowed[" . $self->_max_allowed_term . "].");
618 }
619  
620 if ($self->underlying_config->market_name eq 'forex' and $days[0] > 7) {
621 die("ON term is missing in volsurface for underlying $system_symbol, the minimum term is $days[0].");
622 }
623  
624 foreach my $day (@days) {
625 if ($day !~ /^\d+$/) {
626 die("Invalid day[$day] in volsurface for underlying[$system_symbol]. Not a positive integer.");
627 }
628 }
629  
630 foreach my $day (grep { exists $surface_hashref->{$_}->{smile} } @days) {
631 my $smile = $surface_hashref->{$day}->{smile};
632 my @volatility_level = sort { $a <=> $b } keys %$smile;
633  
634 for (my $i = 0; $i < $#volatility_level; $i++) {
635 my $level = $volatility_level[$i];
636  
637 if ($level !~ /^\d+\.?\d+$/) {
638 die("Invalid vol_point[$level] for underlying[$system_symbol].");
639 }
640  
641 my $next_level = $volatility_level[$i + 1];
642 if (abs($level - $next_level) > $self->_max_difference_between_smile_points) {
643 die("Difference between point $level and $next_level is too great for days $day.");
644 }
645  
646 if (not $self->_is_valid_volatility_smile($smile)) {
647 die("Invalid smile volatility on $day for $system_symbol");
648 }
649  
650 if (abs($smile->{$level} - $smile->{$next_level}) > $max_vol_change_by_delta * $smile->{$level}) {
651 die( "Invalid volatility points: too big jump from "
652 . "$level:$smile->{$level} to $next_level:$smile->{$next_level}"
653 . "for maturity[$day], underlying[$system_symbol]");
654 }
655 }
656 }
657  
658 return;
659 }
660  
661 sub _is_valid_volatility_smile {
662 my ($self, $smile) = @_;
663  
664 foreach my $vol (values %$smile) {
665 # sanity check on volatility. Cannot be more than 500% and must be a number.
666 return if ($vol !~ /^\d?\.?\d*$/ or $vol > 5);
667 }
668  
669 return 1;
670 }
671  
672 sub _validate_identical_surface {
673 my $self = shift;
674  
675 my $existing = $self->new({
676 underlying_config => $self->underlying_config,
677 chronicle_reader => $self->chronicle_reader,
678 chronicle_writer => $self->chronicle_writer,
679 });
680  
681 my $existing_surface_data = $existing->surface;
682 my $new_surface_data = $self->surface;
683  
684 my @existing_terms = sort { $a <=> $b } grep { exists $existing_surface_data->{$_}{smile} } keys %$existing_surface_data;
685 my @new_terms = sort { $a <=> $b } grep { exists $new_surface_data->{$_}{smile} } keys %$new_surface_data;
686  
687 return if @existing_terms != @new_terms;
688 return if any { $existing_terms[$_] != $new_terms[$_] } (0 .. $#existing_terms);
689  
690 foreach my $term (@existing_terms) {
691 my $existing_smile = $existing_surface_data->{$term}->{smile};
692 my $new_smile = $new_surface_data->{$term}->{smile};
693 return if (scalar(keys %$existing_smile) != scalar(keys %$new_smile));
694 return if (any { $existing_smile->{$_} != $new_smile->{$_} } keys %$existing_smile);
695 }
696  
697 my $existing_surface_epoch = Date::Utility->new($self->document->{date})->epoch;
698  
699 if (time - $existing_surface_epoch > 15000 and not $self->underlying_config->quanto_only) {
700 die('Surface data has not changed since last update [' . $existing_surface_epoch . '] for ' . $self->symbol . '.');
701 }
702  
703 return;
704 }
705  
706 sub _validate_volatility_jumps {
707 my $self = shift;
708  
709 my $existing = $self->new({
710 underlying_config => $self->underlying_config,
711 chronicle_reader => $self->chronicle_reader,
712 chronicle_writer => $self->chronicle_writer,
713 });
714  
715 my @terms = @{$self->original_term_for_smile};
716 my @new_expiry = @{$self->get_smile_expiries};
717 my @existing_expiry = @{$existing->get_smile_expiries};
718  
719 my @points = @{$self->smile_points};
720 my $type = $self->type;
721  
722 for (my $i = 1; $i <= $#new_expiry; $i++) {
723 for (my $j = 0; $j <= $#points; $j++) {
724 my $sought_point = $points[$j];
725 my $new_vol = $self->get_volatility({
726 $type => $sought_point,
727 from => $self->recorded_date,
728 to => $new_expiry[$i],
729 });
730 my $existing_vol = $existing->get_volatility({
731 $type => $sought_point,
732 from => $existing->recorded_date,
733 to => $existing_expiry[$i],
734 });
735 my $diff = abs($new_vol - $existing_vol);
736 if ($diff > 0.03 and $diff > $existing_vol) {
737 die('Big difference found on term[' . $terms[$i - 1] . '] for point [' . $sought_point . '] with absolute diff [' . $diff . '].');
738 }
739 }
740 }
741  
742 return;
743 }
744  
745 has validation_error => (
746 is => 'rw',
747 default => '',
748 );
749  
750 =head2 save
751 Saves current surface using given chronicle writer.
752 =cut
753  
754 sub save {
755 my $self = shift;
756  
757 return $self->chronicle_writer->set('volatility_surfaces', $self->symbol, $self->_document_content, $self->recorded_date);
758 }
759  
760 =head2 get_surface_smile
761  
762 Returns the smile on the surface.
763 Returns an empty hash reference if not present.
764  
765 =cut
766  
767 sub get_surface_smile {
768 my ($self, $days) = @_;
769  
770 return $self->surface->{$days}->{smile} // {};
771 }
772  
773 =head2 get_surface_volatility
774  
775 Returns the volatility point based on the raw volatility surface data.
776 Returns undef if requested smile is not present.
777 Returns the interpolated/raw volatility point across smile if requested smile is present.
778  
779 =cut
780  
781 sub get_surface_volatility {
782 my ($self, $days, $smile_point) = @_;
783  
784 die('[days] and [smile_point] are required.') if (not($days and $smile_point));
785  
786 my $smile = $self->get_surface_smile($days);
787  
788 return if not keys %$smile;
789 return $smile->{$smile_point} if $smile->{$smile_point};
790  
791 return $self->interpolate({
792 smile => $smile,
793 sought_point => $smile_point,
794 });
795 }
796  
797 =head2 clone
798  
799 USAGE:
800  
801 my $clone = $s->clone({
802 surface_data => $surface_data,
803 });
804  
805 Returns a new Quant::Framework::VolSurface instance. You can pass overrides to override an attribute value as it is on the original surface.
806  
807 =cut
808  
809 sub clone {
810 my ($self, $args) = @_;
811  
812 my %clone_args;
813 %clone_args = %$args if $args;
814  
815 # moneyness has spot_reference.
816 if ($self->can('spot_reference') and not exists $clone_args{spot_reference}) {
817 $clone_args{spot_reference} = $self->spot_reference;
818 }
819  
820 foreach my $att (qw(underlying_config recorded_date chronicle_reader chronicle_writer)) {
821 if (not exists $clone_args{$att}) {
822 $clone_args{$att} = $self->$att;
823 }
824 }
825  
826 if (not exists $clone_args{surface_data}) {
827 $clone_args{surface_data} = dclone($self->surface_data);
828 }
829  
830 return $self->new(\%clone_args);
831 }
832  
833 sub _clean {
834 my ($self, $surface) = @_;
835  
836 # For backward compatibility in volatility spread,
837 # we will convert the legacy structure to the new structure here.
838 foreach my $tenor (keys %$surface) {
839 if (exists $surface->{$tenor}{atm_spread}) {
840 my $spread = delete $surface->{$tenor}{atm_spread};
841 $surface->{$tenor}{vol_spread} = {$self->atm_spread_point => $spread};
842 }
843 }
844  
845 return $surface;
846 }
847  
848 sub _document_content {
849 my $self = shift;
850  
851 my %structure = (
852 surface => $self->surface,
853 date => $self->recorded_date->datetime_iso8601,
854 symbol => $self->symbol,
855 type => $self->type,
856 );
857  
858 $structure{spot_reference} = $self->spot_reference if ($self->can('spot_reference'));
859  
860 return \%structure;
861 }
862  
863 no Moose;
864 __PACKAGE__->meta->make_immutable;
865 1;