Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ file(GLOB SOURCES "src/cpp/*/*.cpp")
add_library(finmath_library SHARED ${SOURCES}
"src/cpp/InterestAndAnnuities/simple_interest.cpp"
"include/finmath/InterestAndAnnuities/simple_interest.h"
"include/finmath/InterestAndAnnuities/discount_factor.h"
"include/finmath/InterestAndAnnuities/present_future_value.h"
"include/finmath/InterestAndAnnuities/annuity.h"
"include/finmath/InterestAndAnnuities/cash_flow.h"
"include/finmath/FixedIncome/bond_pricing.h"
"include/finmath/OptionPricing/options_pricing.h"
"include/finmath/OptionPricing/options_pricing_types.h"
"include/finmath/TimeSeries/rolling_volatility.h"
Expand Down Expand Up @@ -104,6 +109,15 @@ endmacro()
add_cpp_test_labeled(CompoundInterestTest test/InterestAndAnnuities/compound_interest_test.cpp "InterestAndAnnuities;Unit")
add_cpp_test_labeled(BlackScholesTest test/OptionPricing/black_scholes_test.cpp "OptionPricing;Unit")
add_cpp_test_labeled(BinomialOptionPricingTest test/OptionPricing/binomial_option_pricing_test.cpp "OptionPricing;Unit")
add_cpp_test_labeled(RSITest test/TimeSeries/rsi_test.cpp "TimeSeries;Unit")
add_cpp_test_labeled(RollingStdDevTest test/TimeSeries/rolling_std_dev_test.cpp "TimeSeries;Unit")
add_cpp_test_labeled(BellmanArbitrageTest test/GraphAlgos/bellman_arbitrage_test.cpp "GraphAlgos;Unit")
add_cpp_test_labeled(BondPricingTest test/FixedIncome/bond_pricing_test.cpp "FixedIncome;Unit")
add_cpp_test_labeled(AnnuityTest test/InterestAndAnnuities/annuity_test.cpp "InterestAndAnnuities;Unit")
add_cpp_test_labeled(CashFlowTest test/InterestAndAnnuities/cash_flow_test.cpp "InterestAndAnnuities;Unit")
add_cpp_test_labeled(DiscountFactorTest test/InterestAndAnnuities/discount_factor_test.cpp "InterestAndAnnuities;Unit")
add_cpp_test_labeled(PresentFutureValueTest test/InterestAndAnnuities/present_future_value_test.cpp "InterestAndAnnuities;Unit")

# add_cpp_test_labeled(RSITest test/TimeSeries/rsi_test.cpp "TimeSeries;Unit")
# add_cpp_test_labeled(RollingStdDevTest test/TimeSeries/rolling_std_dev_test.cpp "TimeSeries;Unit")
# add_cpp_test_labeled(BellmanArbitrageTest test/GraphAlgos/bellman_arbitrage_test.cpp "GraphAlgos;Unit")
Expand Down
75 changes: 75 additions & 0 deletions include/finmath/FixedIncome/bond_pricing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef BOND_PRICING_H
#define BOND_PRICING_H

#include <cmath>
#include <stdexcept>

/**
* Bond price (coupon bond)
* Calculates the theoretical price of a bond
*
* @param face_value Face value (par value) of the bond
* @param coupon_rate Annual coupon rate (e.g., 0.05 for 5%)
* @param yield_to_maturity Yield to maturity (discount rate)
* @param periods Number of coupon payment periods per year
* @param time_to_maturity Time to maturity in years
* @return Bond price
*
* Formula: Price = sum(Coupon / (1+r)^i) + Face / (1+r)^n
*
* Example:
* 10-year bond, $1000 face, 5% coupon, paid semi-annually, 4% YTM
* Coupon per period = $1000 * 0.05 / 2 = $25
* Number of periods = 10 * 2 = 20
* Rate per period = 0.04 / 2 = 0.02
*/
double bond_price(
double face_value,
double coupon_rate,
double yield_to_maturity,
int periods,
double time_to_maturity
);

/**
* Bond yield (simplified, iterative)
* Finds YTM given bond price
* Uses Newton-Raphson method
*
* @param face_value Face value of the bond
* @param coupon_rate Annual coupon rate
* @param price Current market price of the bond
* @param periods Number of coupon payment periods per year
* @param time_to_maturity Time to maturity in years
* @return Yield to maturity
*/
double bond_yield(
double face_value,
double coupon_rate,
double price,
int periods,
double time_to_maturity
);

/**
* Duration (Macaulay)
* Measures interest rate sensitivity
*
* @param face_value Face value of the bond
* @param coupon_rate Annual coupon rate
* @param yield_to_maturity Yield to maturity
* @param periods Number of coupon payment periods per year
* @param time_to_maturity Time to maturity in years
* @return Macaulay duration in years
*
* Formula: Duration = sum(t * PV(CF_t)) / Price
*/
double bond_duration(
double face_value,
double coupon_rate,
double yield_to_maturity,
int periods,
double time_to_maturity
);

#endif // BOND_PRICING_H
61 changes: 61 additions & 0 deletions include/finmath/InterestAndAnnuities/annuity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef ANNUITY_H
#define ANNUITY_H

#include <cmath>
#include <stdexcept>

/**
* Present value of ordinary annuity
* Payments are made at the END of each period
*
* @param payment Payment amount per period
* @param rate Interest rate per period (not annualized if periods are not annual)
* @param periods Number of payment periods
* @return Present value of the annuity
*
* Formula: PV = P * [1 - (1+r)^(-n)] / r
*
* Edge cases:
* - If rate == 0: return payment * periods
* - If periods == 0: return 0
*/
double annuity_present_value(double payment, double rate, int periods);

/**
* Future value of ordinary annuity
*
* @param payment Payment amount per period
* @param rate Interest rate per period
* @param periods Number of payment periods
* @return Future value of the annuity
*
* Formula: FV = P * [(1+r)^n - 1] / r
*/
double annuity_future_value(double payment, double rate, int periods);

/**
* Present value of annuity due
* Payments are made at the BEGINNING of each period
*
* @param payment Payment amount per period
* @param rate Interest rate per period
* @param periods Number of payment periods
* @return Present value of annuity due
*
* Formula: PV = P * [1 - (1+r)^(-n)] / r * (1 + r)
*/
double annuity_due_present_value(double payment, double rate, int periods);

/**
* Future value of annuity due
*
* @param payment Payment amount per period
* @param rate Interest rate per period
* @param periods Number of payment periods
* @return Future value of annuity due
*
* Formula: FV = P * [(1+r)^n - 1] / r * (1 + r)
*/
double annuity_due_future_value(double payment, double rate, int periods);

#endif // ANNUITY_H
75 changes: 75 additions & 0 deletions include/finmath/InterestAndAnnuities/cash_flow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef CASH_FLOW_H
#define CASH_FLOW_H

#include <cmath>
#include <stdexcept>
#include <vector>


/**
* Net Present Value
* Calculates the present value of all cash flows
*
* @param cash_flows Vector of cash flows (negative = outflow, positive = inflow)
* @param rate Discount rate (e.g., 0.10 for 10%)
* @param initial_investment Optional initial investment (default: 0.0)
* @return Net present value
*
* Formula: NPV = sum(CF_i / (1+r)^i) - Initial Investment
*
* Example:
* cash_flows = [-1000, 100, 200, 300, 400]
* rate = 0.10
* NPV = -1000 + 100/(1.1) + 200/(1.1)^2 + 300/(1.1)^3 + 400/(1.1)^4
*/
double net_present_value(
const std::vector<double>& cash_flows,
double rate,
double initial_investment = 0.0
);

/**
* Internal Rate of Return
* Finds the discount rate that makes NPV = 0
* Uses Newton-Raphson iterative method
*
* @param cash_flows Vector of cash flows
* @param initial_guess Starting guess for IRR (default: 0.1 = 10%)
* @param max_iterations Maximum iterations for convergence (default: 100)
* @param tolerance Convergence tolerance (default: 1e-6)
* @return Internal rate of return
*
* Algorithm:
* 1. Start with initial guess
* 2. Calculate NPV and dNPV/dr at current guess
* 3. Update: r_new = r_old - NPV / dNPV/dr
* 4. Repeat until |NPV| < tolerance
*
* @throws std::runtime_error if convergence fails
*/
double internal_rate_of_return(
const std::vector<double>& cash_flows,
double initial_guess = 0.1,
int max_iterations = 100,
double tolerance = 1e-6
);

/**
* Payback period
* Returns the number of periods until cumulative cash flows exceed initial investment
*
* @param cash_flows Vector of cash flows (first element is typically initial investment)
* @param initial_investment Initial investment amount
* @return Number of periods until payback (returns -1 if never pays back)
*
* Example:
* cash_flows = [100, 200, 300, 400]
* initial_investment = 500
* Cumulative: 100, 300, 600 (payback at period 3)
*/
int payback_period(
const std::vector<double>& cash_flows,
double initial_investment
);

#endif // CASH_FLOW_H
50 changes: 50 additions & 0 deletions include/finmath/InterestAndAnnuities/discount_factor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef DISCOUNT_FACTOR_H
#define DISCOUNT_FACTOR_H

#include <cmath>
#include <stdexcept>


/**
* Discrete compounding discount factor
* Converts a future value to present value using discrete compounding
*
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Discount factor (0 < DF <= 1)
*
* Formula: DF = 1 / (1 + r)^t
*
* @throws std::invalid_argument if rate < 0 or time < 0
*/
double discount_factor(double rate, double time);

/**
* Continuous compounding discount factor
* Uses exponential compounding for continuous interest
*
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Discount factor (0 < DF <= 1)
*
* Formula: DF = e^(-r*t)
*
* @throws std::invalid_argument if rate < 0 or time < 0
*/
double discount_factor_continuous(double rate, double time);

/**
* Future value factor
* Converts a present value to future value
*
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Future value factor (FVF >= 1)
*
* Formula: FVF = (1 + r)^t
*
* @throws std::invalid_argument if rate < 0 or time < 0
*/
double future_value_factor(double rate, double time);

#endif // DISCOUNT_FACTOR_H
59 changes: 59 additions & 0 deletions include/finmath/InterestAndAnnuities/present_future_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef PRESENT_FUTURE_VALUE_H
#define PRESENT_FUTURE_VALUE_H

#include <cmath>
#include <stdexcept>


/**
* Present value with discrete compounding
* Calculates what a future amount is worth today
*
* @param future_value Amount to be received in the future
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years until the future value is received
* @return Present value of the future amount
*
* Formula: PV = FV / (1 + r)^t
*/
double present_value(double future_value, double rate, double time);

/**
* Future value with discrete compounding
* Calculates what a current amount will be worth in the future
*
* @param present_value Current amount
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Future value of the present amount
*
* Formula: FV = PV * (1 + r)^t
*/
double future_value(double present_value, double rate, double time);

/**
* Present value with continuous compounding
* Uses exponential compounding
*
* @param future_value Amount to be received in the future
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Present value with continuous compounding
*
* Formula: PV = FV * e^(-r*t)
*/
double present_value_continuous(double future_value, double rate, double time);

/**
* Future value with continuous compounding
*
* @param present_value Current amount
* @param rate Annual interest rate (e.g., 0.05 for 5%)
* @param time Time in years
* @return Future value with continuous compounding
*
* Formula: FV = PV * e^(r*t)
*/
double future_value_continuous(double present_value, double rate, double time);

#endif // PRESENT_FUTURE_VALUE_H
Loading
Loading