CYCLUS
Loading...
Searching...
No Matches
economic_entity.h
Go to the documentation of this file.
1#ifndef ECONOMIC_ENTITY_H
2#define ECONOMIC_ENTITY_H
3
4#include <stdexcept>
5#include <string>
6#include <unordered_map>
7
9 public:
10 /// @brief Fetches the value corresponding to "key" from financial_data_
11 /// @param key the name of the parameter to fetch. Must be an exact match
12 /// @return The value associated with "key" in financial_data_
13 virtual double GetEconParameter(const std::string& key) const {
14 if (financial_data_.count(key) > 0) {
15 return financial_data_.find(key)->second;
16 } else {
17 throw std::runtime_error("Key '" + key +
18 "' not found in financial_data_");
19 }
20 }
21
22 /// @brief Add a new EconParameter to financial_data_
23 /// @param key The key of the EconParameter
24 /// @param value The value of the EconParameter
25 virtual void SetEconParameter(const std::string& key, double value) {
26 financial_data_[key] = value;
27 }
28
29 // Given default implementation so as not to break backwards compatability
30 virtual std::unordered_map<std::string, double> GenerateParamList() const {
31 return {};
32 }
33
34 /// @brief Initialize the list of EconParameters from the ParamList
36 std::unordered_map<std::string, double> econ_params = GenerateParamList();
37 for (const auto& parameter : econ_params) {
38 SetEconParameter(parameter.first, parameter.second);
39 }
40 }
41
42 /// @brief calculates the present value of a series of payments, using
43 /// discount rate i per time period, for future payment F that occurs n time
44 /// periods in the future plus regular payments A that occurs each time
45 /// period for n periods
46 /// @param n Number of periods
47 /// @param i Discout rate
48 /// @param F Future payment
49 /// @param A Regular payments at each period
50 /// @return Present value
51 virtual double PV(int n, double i, double F, double A) const {
52 double pv_F = F / std::pow((1 + i), n);
53 double pv_A;
54
55 // Since we're allowing certain terms to be zero
56 if (i != 0.0) {
57 pv_A = A * (1 - std::pow((1 + i), -n)) / i;
58 } else {
59 pv_A = A * n;
60 }
61
62 return pv_F + pv_A;
63 }
64
65 /// @brief calculates the future value of a series of payments, using
66 /// compound rate i per time period, for present payment P plus regular
67 /// payments A that occurs every period for n periods.
68 /// @param n Number of periods
69 /// @param i Compound Rate
70 /// @param P Present payment
71 /// @param A Regular payment at each period.
72 /// @return Future value
73 virtual double FV(int n, double i, double P, double A) const {
74 double fv_P = P * std::pow((1 + i), n);
75 double fv_A;
76
77 // Since we're allowing certain terms to be zero
78 if (i != 0.0) {
79 fv_A = A * (std::pow((1 + i), n) - 1) / i;
80 } else {
81 fv_A = A * n;
82 }
83
84 return fv_P + fv_A;
85 }
86
87 /// @brief computes the regular payment for n time periods that is equivalent
88 /// to the combination of a single payment of P immediately and a single
89 /// payment F after n time periods, using a discount rate of i per time
90 /// periods
91 /// @param n Number of payments
92 /// @param i Discount rate
93 /// @param P Immediate payment
94 /// @param F Future payment
95 /// @return the regular payment (PMT) which is equivalent to P and F
96 virtual double PMT(int n, double i, double P, double F) const {
97 if (n <= 0) {
98 throw std::invalid_argument("n must be greater than or equal to zero!");
99 }
100
101 double p_term = P;
102 double f_term = F / std::pow((1 + i), n);
103
104 // Since we're allowing certain terms to be zero
105 if (i != 0) {
106 return (p_term + f_term) * i / (1 - std::pow((1 + i), -n));
107 } else {
108 return (p_term + f_term) / n;
109 }
110 }
111
112 /// @brief calculates the present value of a series of payments described by
113 /// the vector [A], occurring over a time period defined by the length of [A],
114 /// with a discount rate of i per time periods
115 /// @param i Discount rate
116 /// @param A Vector of payments which also defines how many time periods are
117 /// summed over (by the length of A)
118 /// @return Present value
119 virtual double PV(double i, const std::vector<double>& A) const {
120 double pv = 0.0;
121
122 for (int t = 0; t < A.size(); ++t) {
123 pv += A[t] / std::pow((1 + i), t + 1);
124 }
125
126 return pv;
127 }
128
129 protected:
130 static constexpr int kDefaultUnitCost = 1;
131
132 private:
133 std::unordered_map<std::string, double> financial_data_;
134};
135
136#endif // ECONOMIC_ENTITY_H
virtual double PMT(int n, double i, double P, double F) const
computes the regular payment for n time periods that is equivalent to the combination of a single pay...
virtual double FV(int n, double i, double P, double A) const
calculates the future value of a series of payments, using compound rate i per time period,...
virtual double PV(double i, const std::vector< double > &A) const
calculates the present value of a series of payments described by the vector [A], occurring over a ti...
static constexpr int kDefaultUnitCost
void InitEconParameters()
Initialize the list of EconParameters from the ParamList.
virtual std::unordered_map< std::string, double > GenerateParamList() const
virtual void SetEconParameter(const std::string &key, double value)
Add a new EconParameter to financial_data_.
virtual double PV(int n, double i, double F, double A) const
calculates the present value of a series of payments, using discount rate i per time period,...
virtual double GetEconParameter(const std::string &key) const
Fetches the value corresponding to "key" from financial_data_.