Framework/CorrelationMatrix.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 106 CC 5 SCAR 16.2
CC
5
Cov
0%
correlation_for
12.0
_build_document
6.0
save
6.0
CRAP 5.1
1 package Quant::Framework::CorrelationMatrix;
2  
3 =head1 NAME
4  
5 Quant::Framework::CorrelationMatrix
6  
7 =head1 DESCRIPTION
8  
9 Correlations have an index, a currency, and duration that corresponds
10 to a correlation. An example of a correlation is SPC, AUD, 1M, with
11 a correlation of 0.42.
12  
13 The values can be updated through backoffice's Quant Market Data page.
14  
15 =cut
16  
17 use Moose;
18 extends 'Quant::Framework::Utils::MarketData';
19  
20 use namespace::autoclean;
21 use Math::Function::Interpolator;
22 use Date::Utility;
23  
24 use Data::Chronicle::Reader;
25 use Data::Chronicle::Writer;
26  
27 =head2 for_date
28  
29 The date for which we wish data
30  
31 =cut
32  
33 has chronicle_reader => (
34 is => 'ro',
35 isa => 'Data::Chronicle::Reader',
36 );
37  
38 has chronicle_writer => (
39 is => 'ro',
40 isa => 'Data::Chronicle::Writer',
41 );
42  
43 has for_date => (
44 is => 'ro',
45 isa => 'Maybe[Date::Utility]',
46 default => undef,
47 );
48  
49 has document => (
50 is => 'rw',
51 lazy_build => 1,
52 );
53  
54 sub _build_document {
55 my $self = shift;
56  
57 my $document = $self->chronicle_reader->get('correlation_matrices', $self->symbol);
58  
59 if ($self->for_date and $self->for_date->epoch < Date::Utility->new($document->{date})->epoch) {
60 $document = $self->chronicle_reader->get_for('correlation_matrices', $self->symbol, $self->for_date->epoch);
61  
62 # This works around a problem with Volatility surfaces and negative dates to expiry.
63 # We have to use the oldest available surface.. and we don't really know when it
64 # was relative to where we are now.. so just say it's from the requested day.
65 # We do not allow saving of historical surfaces, so this should be fine.
66 $document //= {};
67 $document->{date} = $self->for_date->datetime_iso8601;
68 }
69  
70 return $document;
71 }
72  
73 around _document_content => sub {
74 my $orig = shift;
75 my $self = shift;
76  
77 return {
78 # symbol is not required
79 date => $self->recorded_date->datetime_iso8601,
80 correlations => $self->correlations,
81 };
82 };
83  
84 =head3 C<< save >>
85  
86 Saves the correlation matrix into Chronicle
87  
88 =cut
89  
90 sub save {
91 my $self = shift;
92  
93 #if chronicle does not have this document, first create it because in document_content we will need it
94 if (not defined $self->chronicle_reader->get('correlation_matrices', $self->symbol)) {
95 $self->chronicle_writer->set('correlation_matrices', $self->symbol, {});
96 }
97  
98 return $self->chronicle_writer->set('correlation_matrices', $self->symbol, $self->_document_content, $self->recorded_date);
99 }
100  
101 has correlations => (
102 is => 'rw',
103 isa => 'HashRef',
104 lazy_build => 1,
105 );
106  
107 has _latest_correlations_reload => (
108 is => 'rw',
109 isa => 'Int',
110 default => sub { time },
111 );
112  
113 has reload_frequency => (
114 is => 'ro',
115 isa => 'Int',
116 default => 20,
117 );
118  
119 =head2 recorded_date
120  
121 The date (and time) that the correlation matrix was recorded, as a Date::Utility.
122  
123 =cut
124  
125 has recorded_date => (
126 is => 'ro',
127 isa => 'Date::Utility',
128 lazy_build => 1,
129 );
130  
131 sub _build_recorded_date {
132 my $self = shift;
133 return Date::Utility->new($self->document->{date});
134 }
135  
136 # Instances of this class are able to auto-reload themselves to ensure
137 # that long-lived objects keep themselves up to date with change to the
138 # underlying data.
139 #
140 # This auto-reloading only occurs when the document is the
141 # live_document however, as if it is not, the instance is essentially
142 # representing a historical correlation matrix, which will never change.
143 #
144 # The correlations attr is a copy of the data->correlations HashRef as
145 # stored on the document. We reload the correlations attr if
146 # it has not changed from the original document value. If it
147 # has, we assume the user wishes to update the matrix, so we do not
148 # update (i.e. clear) the attr.
149 #
150 # We also assume that the user of the object will retrieve correlation
151 # data via the correlation attr.
152 before correlations => sub {
153 my $self = shift;
154  
155 if ($self->_latest_correlations_reload + $self->reload_frequency < time) {
156 $self->clear_correlations;
157 $self->_latest_correlations_reload(time);
158 }
159 };
160  
161 sub _build_correlations {
162 my $self = shift;
163  
164 return $self->document->{correlations};
165 }
166  
167 =head3 C<< correlation_for >>
168  
169 return correlation coefficient for given index, time-of-year and currency
170 You have to pass an instance of Quant::Framework::ExpiryConventions module
171 because it is needed in the calculations.
172  
173 =cut
174  
175 sub correlation_for {
176 my ($self, $index, $payout_currency, $tiy, $expiry_conventions) = @_;
177  
178 # For synthetic, it will use the mapped underlying correlation
179 if ($index =~ /^SYN(\w+)/) {
180 $index = $1;
181 }
182 my $sought_expiry = $tiy * 365.0;
183 my $data_points = $self->correlations->{$index}->{$payout_currency};
184 my $mapped_data;
185  
186 foreach my $tenor (keys %{$data_points}) {
187 my $day = $expiry_conventions->vol_expiry_date({
188 from => $self->recorded_date,
189 term => $tenor,
190 })->days_between($self->recorded_date);
191  
192 $mapped_data->{$day} = $data_points->{$tenor};
193 }
194  
195 return ($sought_expiry) ? Math::Function::Interpolator->new(points => $mapped_data)->linear($sought_expiry) : 0;
196 }
197  
198 around BUILDARGS => sub {
199 my $orig = shift;
200 my $class = shift;
201  
202 my @args = (scalar @_ == 1 and not ref $_[0]) ? {symbol => $_[0]} : @_;
203  
204 return $class->$orig(@args);
205 };
206  
207 __PACKAGE__->meta->make_immutable;
208 1;