Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Valuing Fixed Income Investments
//Calculate the Values of Cashflows #include <iostream> #include <cmath> #include <vector> #define nl '\n' using namespace std; typedef vector<double>CFlows; typedef CFlows::iterator cfiterator; class Value{ public: Value(){} Value(const Value& v) : i(v.i), n(v.n), p(v.p){} ~Value(){} // // N = number of periods; I = amount of interest; P = times I paid per period // // present value interest rate factor // pvif = 1/(1+I/P)^N*P // double PVIF(double N, double I, double P = 1.0){ double d = 1+I/P; double pvif = 1/pow(d, N*P); return pvif; } // // future value interest rate factor // fvif = (1+I/P)^N*P // double FVIF(double N, double I, double P = 1.0){ double d = 1 + I/P; double fvif = pow(d, N*P); return fvif; } double PresentValue(double amount = 1, double I = 0.01, double N = 1, double P=1){ double pvif = PVIF(N, I, P); return amount * pvif; } double FutureValue(double amount = 1, double I = 0.01, double N = 1, double P=1){ double fvif = FVIF(N, I, P); return amount * fvif; } // Annuities // double PVAnnuity(double amt=1, double i=.01, double n=1, double p=1){ double pva = 0; for(int j = 1; j < (n+1); j++){ double pvif = PVIF(j, i, p); pva += pvif; } return amt*pva; } double FVAnnuity(double amt=1, double i=.01, double n=1, double p=1){ double fva = 0; for(int j = 0; j < n; j++){ double fvif = FVIF(j, i, p); fva += fvif; } return amt*fva; } double BondValue(double face, double coupon, double kd, double N, double p = 2.0){ double pvif = PVIF(N, kd, p); double C = face*coupon; double a = (1-pvif)/kd; double pvf = face*pvif; double BondPrice = C*a + pvf; return BondPrice; } bool operator ==(const Value& v)const{ return i == v.i && n == v.n && p == v.p; } private: double i; double n; double p; }; int main() { Value v; double A = 1000.00; // Amount invested double I = .05; // promised interest double N = 10.00; // life of the investment double P = 1.00; // // P = number of times interest is paid per period N // If N = years then: // P = 1 means that I/P = I/1 interest is paid 10*P/10 = 10(1)/10 = 1 times per year // P = 2 means that I/P = I/2 interest is paid 10*P/10 = 10(2)/10 = 2 times per year // etc. CFlows pvcf(N); // present value cash flows container CFlows fvcf(N); // future value cash flows container CFlows pvacf(N); // present value of annuity cash flow container CFlows fvacf(N); // future value of annuity cash flow container cfiterator p, f; double j = 0.0; while(j < N){ pvcf[j] = v.PresentValue(A, I, j+1, P); fvcf[j] = v.FutureValue(A, I, j+1, P); pvacf[j] = v.PVAnnuity(A, I, j+1, P); fvacf[j] = v.FVAnnuity(A, I, j+1, P); ++j; } cout.setf(ios::showpoint) << cout.precision(8); int c = 1; cout << "Present and future values of $" << A << " at the end of " << N << " periods\n"; cout << "\nN\tPV\t\t\tFV\n"; for(p=pvcf.begin(), f=fvcf.begin(); p<pvcf.end(), f<fvcf.end(); p++, f++) cout << c++ << "\t" << *p << "\t\t" << *f << "\n"; c = 1; cout << "\nPresent and future value of an annuity of $" << A << " per period for " << N << " periods\n"; cout << "\nN\tPVA\t\t\tFVA\n"; for(p=pvacf.begin(), f=fvacf.begin(); p<pvacf.end(), f<fvacf.end(); p++, f++) cout << c++ << "\t" << *p << "\t\t" << *f << "\n"; /* double BondValue(double face, double coupon, double Kd, double N, double p = 2) */ double face = 1000; double coupon = 0.10; double kd = 0.05; double n = 1; double bond1 = v.BondValue(face, coupon, kd, n); // default to semiannual payments cout << "\nBond 1's Value:\t" << bond1 << "\n"; double bond2 = v.BondValue(10000, 0.0325, .05, 5, 2); cout << "\nBond 2\'s Value:\t" << bond2 << "\n"; }
run
|
edit
|
history
|
help
0
Vectors as Inputs to Map
Primality test Fermat primality test
OperatorOverload2
good triplet
Updated Linked Lists - 5/10/2017 V4.0
max subsequence of array
MatrixVectorConversion
string probe
aa
cache_node.cc