forked from paulgriffiths/financial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbond.cpp
45 lines (38 loc) · 1.31 KB
/
bond.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*!
* \file bond.cpp
* \brief Bond classes and functions implementation.
* \details Bond classes and functions implementation.
* \author Paul Griffiths
* \copyright Copyright 2013 Paul Griffiths. Distributed under the terms
* of the GNU General Public License. <http://www.gnu.org/licenses/>
*/
#include <cmath>
#include "common_financial_types.h"
#include "basic_dcf.h"
#include "bond.h"
using namespace financial;
double SimpleBond::value(const double discount_rate) const {
double ret_value = 0;
if ( m_coupon_frequency ) {
std::vector<TimedCashFlow> tcf;
const double cpymt = m_principal * m_coupon / m_coupon_frequency;
const double periods = num_payments();
for ( int cp = 1; cp <= periods; ++cp ) {
tcf.push_back(TimedCashFlow(cpymt, cp));
}
tcf.push_back(TimedCashFlow(m_principal, periods));
const double pdr = std::pow(1 + discount_rate,
1.0 / m_coupon_frequency) - 1;
ret_value = pv_stream(tcf, pdr);
} else {
ret_value = pv(m_principal, discount_rate, m_maturity);
}
return ret_value;
}
double SimpleBond::num_payments() const {
if ( m_coupon_frequency ) {
return m_maturity * m_coupon_frequency;
} else {
return 1;
}
}