forked from let-me-illustrate/lmi
/
loads_test.cpp
257 lines (222 loc) · 14 KB
/
loads_test.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Loads and expense charges--unit test.
//
// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Gregory W. Chicares.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
// https://savannah.nongnu.org/projects/lmi
// email: <gchicares@sbcglobal.net>
// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
#include "pchfile.hpp"
#include "loads.hpp"
#include "loads_impl.hpp"
#include "materially_equal.hpp"
#include "oecumenic_enumerations.hpp"
#include "round_to.hpp"
#include "ssize_lmi.hpp"
#include "test_tools.hpp"
#include "timer.hpp"
#include <functional> // bind()
namespace
{
int const length = 50;
std::vector<double> const dummy_vector(length);
} // Unnamed namespace.
// TODO ?? Consider factoring out these stubs...or abandoning the
// present experiment and finding a better way of making classes such
// as this one independent of class BasicValues.
#include "basic_values.hpp"
std::vector<double> BasicValues::GetCurrSpecAmtLoadTable() const {return dummy_vector;}
std::vector<double> BasicValues::GetGuarSpecAmtLoadTable() const {return dummy_vector;}
#include "database.hpp"
int product_database::length() const {return length_;}
void product_database::query_into(e_database_key, std::vector<double>& v) const {v.resize(length_);}
void product_database::query_into(e_database_key, std::vector<currency>& v) const {v.resize(length_);}
double product_database::query(e_database_key, database_index const&) const {return 0.0;}
#include "premium_tax.hpp"
double premium_tax::levy_rate () const {return 0.0;}
double premium_tax::load_rate () const {return 0.0;}
double premium_tax::maximum_load_rate() const {return 0.0;}
double premium_tax::minimum_load_rate() const {return 0.0;}
struct LoadsTest
{
LoadsTest(load_details const& details)
:details_ {details}
,database_ (details.length_)
,loads_ {}
{}
void Allocate () {loads_.Allocate(details_.length_);}
void Initialize() {loads_.Initialize(database_, details_);}
void Calculate () {loads_.Calculate(details_);}
void Reinitialize();
void Recalculate () {Reinitialize(); Calculate();}
void TestVectorLengths(char const* file, int line);
void TestCalculations (char const* file, int line);
load_details const& details_;
product_database database_;
Loads loads_;
};
void LoadsTest::Reinitialize()
{
loads_.refundable_sales_load_proportion_ = std::vector<double> (length, 0.50000);
loads_.dac_tax_load_ = std::vector<double> (length, 0.00500);
loads_.monthly_policy_fee_ [mce_gen_guar] = std::vector<currency>(length, 800_cents);
loads_.annual_policy_fee_ [mce_gen_guar] = std::vector<currency>(length, 200_cents);
loads_.specified_amount_load_[mce_gen_guar] = std::vector<double> (length, 0.00003);
loads_.separate_account_load_[mce_gen_guar] = std::vector<double> (length, 0.00130);
loads_.target_premium_load_ [mce_gen_guar] = std::vector<double> (length, 0.04000);
loads_.excess_premium_load_ [mce_gen_guar] = std::vector<double> (length, 0.03000);
loads_.target_sales_load_ [mce_gen_guar] = std::vector<double> (length, 0.30000);
loads_.excess_sales_load_ [mce_gen_guar] = std::vector<double> (length, 0.15000);
loads_.monthly_policy_fee_ [mce_gen_curr] = std::vector<currency>(length, 525_cents);
loads_.annual_policy_fee_ [mce_gen_curr] = std::vector<currency>(length, 100_cents);
loads_.specified_amount_load_[mce_gen_curr] = std::vector<double> (length, 0.00002);
loads_.separate_account_load_[mce_gen_curr] = std::vector<double> (length, 0.00110);
loads_.target_premium_load_ [mce_gen_curr] = std::vector<double> (length, 0.02000);
loads_.excess_premium_load_ [mce_gen_curr] = std::vector<double> (length, 0.01000);
loads_.target_sales_load_ [mce_gen_curr] = std::vector<double> (length, 0.10000);
loads_.excess_sales_load_ [mce_gen_curr] = std::vector<double> (length, 0.05000);
}
void LoadsTest::TestVectorLengths(char const* file, int line)
{
int const z = details_.length_;
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.refundable_sales_load_proportion ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.premium_tax_load ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.amortized_premium_tax_load ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.dac_tax_load ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load_excluding_premium_tax()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load_excluding_premium_tax()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load_maximum_premium_tax ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load_maximum_premium_tax ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load_minimum_premium_tax ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load_minimum_premium_tax ()), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.monthly_policy_fee (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.annual_policy_fee (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.specified_amount_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.separate_account_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_sales_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_sales_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_total_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_total_load (mce_gen_curr)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.monthly_policy_fee (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.annual_policy_fee (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.specified_amount_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.separate_account_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_sales_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_sales_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_total_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_total_load (mce_gen_guar)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.monthly_policy_fee (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.annual_policy_fee (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.specified_amount_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.separate_account_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_premium_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_premium_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_sales_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_sales_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.target_total_load (mce_gen_mdpt)), file, line);
INVOKE_LMI_TEST_EQUAL(z, lmi::ssize(loads_.excess_total_load (mce_gen_mdpt)), file, line);
}
void LoadsTest::TestCalculations(char const* file, int line)
{
INVOKE_LMI_TEST(materially_equal(0.500000, loads_.refundable_sales_load_proportion()[0]), file, line);
// (8.00 + 5.25 + 0.50) / 2 = 13.75 / 2 = 6.875, rounded to cents
INVOKE_LMI_TEST(688_cents == loads_.monthly_policy_fee (mce_gen_mdpt)[0] , file, line);
INVOKE_LMI_TEST(150_cents == loads_.annual_policy_fee (mce_gen_mdpt)[0] , file, line);
INVOKE_LMI_TEST(materially_equal(0.000625, loads_.specified_amount_load (mce_gen_mdpt)[0]), file, line);
// 12 bp and 19 bp, both converted to monthly, then added together.
INVOKE_LMI_TEST(materially_equal(0.0002581402795930, loads_.separate_account_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.030000, loads_.target_premium_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.020000, loads_.excess_premium_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.217000, loads_.target_sales_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.117000, loads_.excess_sales_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.383000, loads_.target_total_load (mce_gen_guar)[0]), file, line); // 0.30 + 0.04 + 0.021 + 0.005 + 0.017
INVOKE_LMI_TEST(materially_equal(0.223000, loads_.excess_total_load (mce_gen_guar)[0]), file, line); // 0.15 + 0.03 + 0.021 + 0.005 + 0.017
INVOKE_LMI_TEST(materially_equal(0.163000, loads_.target_total_load (mce_gen_curr)[0]), file, line); // 0.10 + 0.02 + 0.021 + 0.005 + 0.017
INVOKE_LMI_TEST(materially_equal(0.103000, loads_.excess_total_load (mce_gen_curr)[0]), file, line); // 0.05 + 0.01 + 0.021 + 0.005 + 0.017
INVOKE_LMI_TEST(materially_equal(0.273000, loads_.target_total_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.163000, loads_.excess_total_load (mce_gen_mdpt)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.021000, loads_.premium_tax_load ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.000000, loads_.amortized_premium_tax_load ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.005000, loads_.dac_tax_load ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.163000, loads_.target_total_load (mce_gen_curr)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.103000, loads_.excess_total_load (mce_gen_curr)[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.142000, loads_.target_premium_load_excluding_premium_tax()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.082000, loads_.excess_premium_load_excluding_premium_tax()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.164000, loads_.target_premium_load_maximum_premium_tax ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.104000, loads_.excess_premium_load_maximum_premium_tax ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.162000, loads_.target_premium_load_minimum_premium_tax ()[0]), file, line);
INVOKE_LMI_TEST(materially_equal(0.102000, loads_.excess_premium_load_minimum_premium_tax ()[0]), file, line);
}
int test_main(int, char*[])
{
round_to<double> round_interest_rate(0, r_not_at_all);
round_to<double> round_minutiae (2, r_to_nearest);
std::vector<double> extra_comp_load (length, 0.0170);
std::vector<double> extra_asset_comp (length, 0.0019);
std::vector<currency> extra_policy_fee (length, 50_cents);
std::vector<double> guar_specamt_load(length, 0.0007);
std::vector<double> curr_specamt_load(length, 0.0005);
load_details details
(length // length_
,false // AmortizePremLoad_
,0.021 // premium_tax_load_
,0.022 // maximum_premium_tax_load_rate_
,0.02 // minimum_premium_tax_load_rate_
,999.999 // premium_tax_rate_ [unused]
,999.999 // premium_tax_amortization_rate_ [unused]
,999 // premium_tax_amortization_period_ [unused]
,oe_asset_charge_load // asset_charge_type_
,true // NeedMidpointRates_
,round_interest_rate // round_interest_rate_
,round_minutiae // round_minutiae_
,extra_comp_load // VectorExtraCompLoad_
,extra_asset_comp // VectorExtraAssetComp_
,extra_policy_fee // VectorExtraPolFee_
,guar_specamt_load // TabularGuarSpecAmtLoad_
,curr_specamt_load // TabularCurrSpecAmtLoad_
);
LoadsTest t(details);
t.Allocate();
t.Initialize();
t.Reinitialize();
t.TestVectorLengths(__FILE__, __LINE__);
t.Calculate();
t.TestCalculations (__FILE__, __LINE__);
std::cout
<< " Allocate: "
<< TimeAnAliquot(std::bind(&LoadsTest::Allocate , &t))
<< '\n'
;
std::cout
<< " Initialize: "
<< TimeAnAliquot(std::bind(&LoadsTest::Initialize , &t))
<< '\n'
;
std::cout
<< " Reinitialize: "
<< TimeAnAliquot(std::bind(&LoadsTest::Reinitialize , &t))
<< '\n'
;
std::cout
<< " Recalculate: "
<< TimeAnAliquot(std::bind(&LoadsTest::Recalculate, &t))
<< '\n'
;
return 0;
}