Skip to content

Commit 1cc9721

Browse files
Merge pull request #55 from Boiler-Quant/notion-funcs
Notion funcs
2 parents 8d50b1e + 8f1c775 commit 1cc9721

17 files changed

Lines changed: 1875 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ file(GLOB SOURCES "src/cpp/*/*.cpp")
5959
add_library(finmath_library SHARED ${SOURCES}
6060
"src/cpp/InterestAndAnnuities/simple_interest.cpp"
6161
"include/finmath/InterestAndAnnuities/simple_interest.h"
62+
"include/finmath/InterestAndAnnuities/discount_factor.h"
63+
"include/finmath/InterestAndAnnuities/present_future_value.h"
64+
"include/finmath/InterestAndAnnuities/annuity.h"
65+
"include/finmath/InterestAndAnnuities/cash_flow.h"
66+
"include/finmath/FixedIncome/bond_pricing.h"
6267
"include/finmath/OptionPricing/options_pricing.h"
6368
"include/finmath/OptionPricing/options_pricing_types.h"
6469
"include/finmath/TimeSeries/rolling_volatility.h"
@@ -104,6 +109,15 @@ endmacro()
104109
add_cpp_test_labeled(CompoundInterestTest test/InterestAndAnnuities/compound_interest_test.cpp "InterestAndAnnuities;Unit")
105110
add_cpp_test_labeled(BlackScholesTest test/OptionPricing/black_scholes_test.cpp "OptionPricing;Unit")
106111
add_cpp_test_labeled(BinomialOptionPricingTest test/OptionPricing/binomial_option_pricing_test.cpp "OptionPricing;Unit")
112+
add_cpp_test_labeled(RSITest test/TimeSeries/rsi_test.cpp "TimeSeries;Unit")
113+
add_cpp_test_labeled(RollingStdDevTest test/TimeSeries/rolling_std_dev_test.cpp "TimeSeries;Unit")
114+
add_cpp_test_labeled(BellmanArbitrageTest test/GraphAlgos/bellman_arbitrage_test.cpp "GraphAlgos;Unit")
115+
add_cpp_test_labeled(BondPricingTest test/FixedIncome/bond_pricing_test.cpp "FixedIncome;Unit")
116+
add_cpp_test_labeled(AnnuityTest test/InterestAndAnnuities/annuity_test.cpp "InterestAndAnnuities;Unit")
117+
add_cpp_test_labeled(CashFlowTest test/InterestAndAnnuities/cash_flow_test.cpp "InterestAndAnnuities;Unit")
118+
add_cpp_test_labeled(DiscountFactorTest test/InterestAndAnnuities/discount_factor_test.cpp "InterestAndAnnuities;Unit")
119+
add_cpp_test_labeled(PresentFutureValueTest test/InterestAndAnnuities/present_future_value_test.cpp "InterestAndAnnuities;Unit")
120+
107121
# add_cpp_test_labeled(RSITest test/TimeSeries/rsi_test.cpp "TimeSeries;Unit")
108122
# add_cpp_test_labeled(RollingStdDevTest test/TimeSeries/rolling_std_dev_test.cpp "TimeSeries;Unit")
109123
# add_cpp_test_labeled(BellmanArbitrageTest test/GraphAlgos/bellman_arbitrage_test.cpp "GraphAlgos;Unit")
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef BOND_PRICING_H
2+
#define BOND_PRICING_H
3+
4+
#include <cmath>
5+
#include <stdexcept>
6+
7+
/**
8+
* Bond price (coupon bond)
9+
* Calculates the theoretical price of a bond
10+
*
11+
* @param face_value Face value (par value) of the bond
12+
* @param coupon_rate Annual coupon rate (e.g., 0.05 for 5%)
13+
* @param yield_to_maturity Yield to maturity (discount rate)
14+
* @param periods Number of coupon payment periods per year
15+
* @param time_to_maturity Time to maturity in years
16+
* @return Bond price
17+
*
18+
* Formula: Price = sum(Coupon / (1+r)^i) + Face / (1+r)^n
19+
*
20+
* Example:
21+
* 10-year bond, $1000 face, 5% coupon, paid semi-annually, 4% YTM
22+
* Coupon per period = $1000 * 0.05 / 2 = $25
23+
* Number of periods = 10 * 2 = 20
24+
* Rate per period = 0.04 / 2 = 0.02
25+
*/
26+
double bond_price(
27+
double face_value,
28+
double coupon_rate,
29+
double yield_to_maturity,
30+
int periods,
31+
double time_to_maturity
32+
);
33+
34+
/**
35+
* Bond yield (simplified, iterative)
36+
* Finds YTM given bond price
37+
* Uses Newton-Raphson method
38+
*
39+
* @param face_value Face value of the bond
40+
* @param coupon_rate Annual coupon rate
41+
* @param price Current market price of the bond
42+
* @param periods Number of coupon payment periods per year
43+
* @param time_to_maturity Time to maturity in years
44+
* @return Yield to maturity
45+
*/
46+
double bond_yield(
47+
double face_value,
48+
double coupon_rate,
49+
double price,
50+
int periods,
51+
double time_to_maturity
52+
);
53+
54+
/**
55+
* Duration (Macaulay)
56+
* Measures interest rate sensitivity
57+
*
58+
* @param face_value Face value of the bond
59+
* @param coupon_rate Annual coupon rate
60+
* @param yield_to_maturity Yield to maturity
61+
* @param periods Number of coupon payment periods per year
62+
* @param time_to_maturity Time to maturity in years
63+
* @return Macaulay duration in years
64+
*
65+
* Formula: Duration = sum(t * PV(CF_t)) / Price
66+
*/
67+
double bond_duration(
68+
double face_value,
69+
double coupon_rate,
70+
double yield_to_maturity,
71+
int periods,
72+
double time_to_maturity
73+
);
74+
75+
#endif // BOND_PRICING_H
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef ANNUITY_H
2+
#define ANNUITY_H
3+
4+
#include <cmath>
5+
#include <stdexcept>
6+
7+
/**
8+
* Present value of ordinary annuity
9+
* Payments are made at the END of each period
10+
*
11+
* @param payment Payment amount per period
12+
* @param rate Interest rate per period (not annualized if periods are not annual)
13+
* @param periods Number of payment periods
14+
* @return Present value of the annuity
15+
*
16+
* Formula: PV = P * [1 - (1+r)^(-n)] / r
17+
*
18+
* Edge cases:
19+
* - If rate == 0: return payment * periods
20+
* - If periods == 0: return 0
21+
*/
22+
double annuity_present_value(double payment, double rate, int periods);
23+
24+
/**
25+
* Future value of ordinary annuity
26+
*
27+
* @param payment Payment amount per period
28+
* @param rate Interest rate per period
29+
* @param periods Number of payment periods
30+
* @return Future value of the annuity
31+
*
32+
* Formula: FV = P * [(1+r)^n - 1] / r
33+
*/
34+
double annuity_future_value(double payment, double rate, int periods);
35+
36+
/**
37+
* Present value of annuity due
38+
* Payments are made at the BEGINNING of each period
39+
*
40+
* @param payment Payment amount per period
41+
* @param rate Interest rate per period
42+
* @param periods Number of payment periods
43+
* @return Present value of annuity due
44+
*
45+
* Formula: PV = P * [1 - (1+r)^(-n)] / r * (1 + r)
46+
*/
47+
double annuity_due_present_value(double payment, double rate, int periods);
48+
49+
/**
50+
* Future value of annuity due
51+
*
52+
* @param payment Payment amount per period
53+
* @param rate Interest rate per period
54+
* @param periods Number of payment periods
55+
* @return Future value of annuity due
56+
*
57+
* Formula: FV = P * [(1+r)^n - 1] / r * (1 + r)
58+
*/
59+
double annuity_due_future_value(double payment, double rate, int periods);
60+
61+
#endif // ANNUITY_H
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef CASH_FLOW_H
2+
#define CASH_FLOW_H
3+
4+
#include <cmath>
5+
#include <stdexcept>
6+
#include <vector>
7+
8+
9+
/**
10+
* Net Present Value
11+
* Calculates the present value of all cash flows
12+
*
13+
* @param cash_flows Vector of cash flows (negative = outflow, positive = inflow)
14+
* @param rate Discount rate (e.g., 0.10 for 10%)
15+
* @param initial_investment Optional initial investment (default: 0.0)
16+
* @return Net present value
17+
*
18+
* Formula: NPV = sum(CF_i / (1+r)^i) - Initial Investment
19+
*
20+
* Example:
21+
* cash_flows = [-1000, 100, 200, 300, 400]
22+
* rate = 0.10
23+
* NPV = -1000 + 100/(1.1) + 200/(1.1)^2 + 300/(1.1)^3 + 400/(1.1)^4
24+
*/
25+
double net_present_value(
26+
const std::vector<double>& cash_flows,
27+
double rate,
28+
double initial_investment = 0.0
29+
);
30+
31+
/**
32+
* Internal Rate of Return
33+
* Finds the discount rate that makes NPV = 0
34+
* Uses Newton-Raphson iterative method
35+
*
36+
* @param cash_flows Vector of cash flows
37+
* @param initial_guess Starting guess for IRR (default: 0.1 = 10%)
38+
* @param max_iterations Maximum iterations for convergence (default: 100)
39+
* @param tolerance Convergence tolerance (default: 1e-6)
40+
* @return Internal rate of return
41+
*
42+
* Algorithm:
43+
* 1. Start with initial guess
44+
* 2. Calculate NPV and dNPV/dr at current guess
45+
* 3. Update: r_new = r_old - NPV / dNPV/dr
46+
* 4. Repeat until |NPV| < tolerance
47+
*
48+
* @throws std::runtime_error if convergence fails
49+
*/
50+
double internal_rate_of_return(
51+
const std::vector<double>& cash_flows,
52+
double initial_guess = 0.1,
53+
int max_iterations = 100,
54+
double tolerance = 1e-6
55+
);
56+
57+
/**
58+
* Payback period
59+
* Returns the number of periods until cumulative cash flows exceed initial investment
60+
*
61+
* @param cash_flows Vector of cash flows (first element is typically initial investment)
62+
* @param initial_investment Initial investment amount
63+
* @return Number of periods until payback (returns -1 if never pays back)
64+
*
65+
* Example:
66+
* cash_flows = [100, 200, 300, 400]
67+
* initial_investment = 500
68+
* Cumulative: 100, 300, 600 (payback at period 3)
69+
*/
70+
int payback_period(
71+
const std::vector<double>& cash_flows,
72+
double initial_investment
73+
);
74+
75+
#endif // CASH_FLOW_H
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef DISCOUNT_FACTOR_H
2+
#define DISCOUNT_FACTOR_H
3+
4+
#include <cmath>
5+
#include <stdexcept>
6+
7+
8+
/**
9+
* Discrete compounding discount factor
10+
* Converts a future value to present value using discrete compounding
11+
*
12+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
13+
* @param time Time in years
14+
* @return Discount factor (0 < DF <= 1)
15+
*
16+
* Formula: DF = 1 / (1 + r)^t
17+
*
18+
* @throws std::invalid_argument if rate < 0 or time < 0
19+
*/
20+
double discount_factor(double rate, double time);
21+
22+
/**
23+
* Continuous compounding discount factor
24+
* Uses exponential compounding for continuous interest
25+
*
26+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
27+
* @param time Time in years
28+
* @return Discount factor (0 < DF <= 1)
29+
*
30+
* Formula: DF = e^(-r*t)
31+
*
32+
* @throws std::invalid_argument if rate < 0 or time < 0
33+
*/
34+
double discount_factor_continuous(double rate, double time);
35+
36+
/**
37+
* Future value factor
38+
* Converts a present value to future value
39+
*
40+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
41+
* @param time Time in years
42+
* @return Future value factor (FVF >= 1)
43+
*
44+
* Formula: FVF = (1 + r)^t
45+
*
46+
* @throws std::invalid_argument if rate < 0 or time < 0
47+
*/
48+
double future_value_factor(double rate, double time);
49+
50+
#endif // DISCOUNT_FACTOR_H
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef PRESENT_FUTURE_VALUE_H
2+
#define PRESENT_FUTURE_VALUE_H
3+
4+
#include <cmath>
5+
#include <stdexcept>
6+
7+
8+
/**
9+
* Present value with discrete compounding
10+
* Calculates what a future amount is worth today
11+
*
12+
* @param future_value Amount to be received in the future
13+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
14+
* @param time Time in years until the future value is received
15+
* @return Present value of the future amount
16+
*
17+
* Formula: PV = FV / (1 + r)^t
18+
*/
19+
double present_value(double future_value, double rate, double time);
20+
21+
/**
22+
* Future value with discrete compounding
23+
* Calculates what a current amount will be worth in the future
24+
*
25+
* @param present_value Current amount
26+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
27+
* @param time Time in years
28+
* @return Future value of the present amount
29+
*
30+
* Formula: FV = PV * (1 + r)^t
31+
*/
32+
double future_value(double present_value, double rate, double time);
33+
34+
/**
35+
* Present value with continuous compounding
36+
* Uses exponential compounding
37+
*
38+
* @param future_value Amount to be received in the future
39+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
40+
* @param time Time in years
41+
* @return Present value with continuous compounding
42+
*
43+
* Formula: PV = FV * e^(-r*t)
44+
*/
45+
double present_value_continuous(double future_value, double rate, double time);
46+
47+
/**
48+
* Future value with continuous compounding
49+
*
50+
* @param present_value Current amount
51+
* @param rate Annual interest rate (e.g., 0.05 for 5%)
52+
* @param time Time in years
53+
* @return Future value with continuous compounding
54+
*
55+
* Formula: FV = PV * e^(r*t)
56+
*/
57+
double future_value_continuous(double present_value, double rate, double time);
58+
59+
#endif // PRESENT_FUTURE_VALUE_H

0 commit comments

Comments
 (0)