blib/lib/Finance/YieldCurve.pm

filter: Statementstmt 46.1% 18 / 39 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 42.3% 25 / 59 CC 6 SCAR 22.0
CC
6
Cov
38%
find_closest_to
20.0
interpolate
6.0
day_count
6.0
CRAP 9.0
↑ Summary
1 package Finance::YieldCurve;
2 # ABSTRACT: Handles interpolation on yield curves for interest rates and dividends
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.001';
8  
9 =head1 NAME
10  
11 Finance::YieldCurve - provides methods for interpolation on interest rates or dividends
12  
13 =head1 SYNOPSIS
14  
15 use Finance::YieldCurve;
16  
17 my $rates = Finance::YieldCurve->new(
18 data => {
19 1 => 0.014,
20 7 => 0.011,
21 14 => 0.012,
22 },
23 asset => 'USD',
24 );
25 # For dividends, we return the closest value with no interpolation
26 my $dividend_rate = $rates->find_closest_to(7 * 24 * 60 * 60);
27 # For interest rates, we interpolate linearly between the points
28 my $interest_rate = $rates->interpolate(7 * 24 * 60 * 60);
29  
30 =head1 DESCRIPTION
31  
32 Handles interpolation methods for different types of yield curve.
33  
34 Instantiate with a set of data points, then use either the L</find_closest_to>
35 or L</interpolate> methods to find the appropriate value for a given time (measured
36 in years).
37  
38 =cut
39  
40 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 ▶use Math::Function::Interpolator;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 =head1 ATTRIBUTES
45  
46 =head2 data
47  
48 The data points, as a hashref of days => value.
49  
50 =cut
51  
52 1 has data => (is => 'ro');
53  
54 =head2 asset
55  
56 String representing the currency, stock or index, for example C<USD>.
57  
58 =cut
59  
60 1 has asset => (is => 'ro');
61  
62 =head1 METHODS
63  
64 =head2 interpolate
65  
66 Get the interpolated rate for this yield curve over the given time period (fractional years).
67  
68 Example:
69  
70 my $rate = $curve->interpolate(7 * 24 * 60 * 60);
71  
72 =cut
73  
74 0 ▶sub interpolate {
Subroutine interpolate: not called CC=2 SCAR=17.9
Pod documented
75 0 my ($self, $tiy) = @_;
76  
77 # timeinyears cannot be undef
78 0 ▶ $tiy ||= 0;
Condition0%0/2
$tiy ||= 0
l!l
00
MC/DC0%0/1
$tiy ||= 0
$tiy
Truth table0%0/2
A
A= $tiy
Aresult
11
00
79  
80 0 my $interp = Math::Function::Interpolator->new(points => $self->data);
81 0 return $interp->linear($tiy * $self->day_count) / 100;
82 }
83  
84 =head2 find_closest_to
85  
86 Returns the closest point to the request value.
87  
88 Example:
89  
90 my $rate = $curve->find_closest_to(7 * 24 * 60 * 60);
91  
92 =cut
93  
94 0 ▶sub find_closest_to {
Subroutine find_closest_to: not called CC=4 SCAR=30.0
Pod documented
95 0 my ($self, $tiy) = @_;
96  
97 # If we have undef or zero, bail out early
98 0 ▶ return 0 unless $tiy;
Branch: 1 branch
BranchTrueFalseTotal
unless $tiy 0 0 0
99  
100 # Handle discrete dividend
101 0 my ($nearest_yield_days_before, $nearest_yield_before) = (0, 0);
102 0 my $days_to_expiry = $tiy * $self->day_count;
103 0 my $rates = $self->data;
104 0 my @sorted_expiries = sort { $a <=> $b } keys(%$rates);
105 0 foreach my $day (@sorted_expiries) {
106 0 ▶ if ($day <= $days_to_expiry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($day <= $days_to_expiry) 0 0 0
107 0 $nearest_yield_days_before = $day;
108 0 $nearest_yield_before = $rates->{$day};
109 0 next;
110 }
111 0 last;
112 }
113  
114 # Re-annualize
115 0 my $discrete_points = $nearest_yield_before * $nearest_yield_days_before / $self->day_count;
116  
117 0 return $discrete_points * $self->day_count / ($days_to_expiry * 100);
118 }
119  
120 # Mapping from asset to number of days - everything not in this list will be 365
121 1 my %asset_daycounts = map { ; $_ => 360 } qw(AED CHF CZK EGP EUR IDR JPY MXN NOK SAR SEK USD XAG XAU TRY);
122  
123 =head2 day_count
124  
125 Returns the day count for our asset.
126  
127 This is an integer value, and will either be 365 or 360.
128  
129 =cut
130  
131 0 ▶sub day_count {
Subroutine day_count: not called CC=2 SCAR=17.9
Pod documented
132 0 my ($self) = @_;
133 0 ▶ return $asset_daycounts{$self->asset} // 365;
Condition0%0/2
$asset_daycounts{$self->asset} // 365
l!l
00
MC/DC0%0/1
$asset_daycounts{$self->asset} // 365
$asset_daycounts{$self->asset}
Truth table0%0/2
A
A= $asset_daycounts{$self->asset}
Aresult
11
00
134 }
135  
136 1 1;
137  
↑ Summary