Skip to content

Commit

Permalink
Merge reinsurance code into fmcalc
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatharu committed Jan 25, 2018
1 parent 01dbb9c commit a24b168
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 145 deletions.
1 change: 1 addition & 0 deletions reinsurance_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
4 changes: 2 additions & 2 deletions reinsurance_tests/ri_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def apply_fm(self, loss_percentage_of_tiv=1.0, net=False):
net_flag = ""
if net:
net_flag = "-n"
command = "gultobin -S 1 < guls.csv | xfmcalc -p direct {} | tee ils.bin | fmtocsv > ils.csv".format(
command = "gultobin -S 1 < guls.csv | fmcalc -p direct {} | tee ils.bin | fmtocsv > ils.csv".format(
net_flag)
proc = subprocess.Popen(command, shell=True)
proc.wait()
Expand Down Expand Up @@ -624,7 +624,7 @@ def write_oasis_files(self):

def apply_fm(self, input):
command = \
"xfmcalc -p {0} -n < {1}.bin | tee {0}.bin | fmtocsv > {0}.csv".format(
"fmcalc -p {0} -n < {1}.bin | tee {0}.bin | fmtocsv > {0}.csv".format(
self.name, input)

proc = subprocess.Popen(command, shell=True)
Expand Down
2 changes: 0 additions & 2 deletions reinsurance_tests/test1/fm_policytc_2.csv

This file was deleted.

4 changes: 0 additions & 4 deletions reinsurance_tests/test1/fm_profile_2.csv

This file was deleted.

13 changes: 0 additions & 13 deletions reinsurance_tests/test1/fm_programme_2.csv

This file was deleted.

3 changes: 0 additions & 3 deletions reinsurance_tests/test1/ri1/fm_policytc_2.csv

This file was deleted.

4 changes: 0 additions & 4 deletions reinsurance_tests/test1/ri1/fm_profile_2.csv

This file was deleted.

13 changes: 0 additions & 13 deletions reinsurance_tests/test1/ri1/fm_programme_2.csv

This file was deleted.

15 changes: 0 additions & 15 deletions reinsurance_tests/test1/ri2/fm_policytc.csv

This file was deleted.

5 changes: 0 additions & 5 deletions reinsurance_tests/test1/ri2/fm_profile.csv

This file was deleted.

13 changes: 0 additions & 13 deletions reinsurance_tests/test1/ri2/fm_programme.csv

This file was deleted.

25 changes: 0 additions & 25 deletions reinsurance_tests/test1/ri2/fm_xref.csv

This file was deleted.

113 changes: 94 additions & 19 deletions src/fmcalc/fmcalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ inline void fmcalc::dofmcalc_r(std::vector<std::vector<int>> &aggid_to_vectorlo
for (LossRec &x : agg_vec) {
if (x.allocrule_id == -1 || x.allocrule_id == 0 ) { // no back allocation
if (x.agg_id > 0) { // agg id cannot be zero
rec.loss = x.loss;
if (netvalue_) { // get net gul value
rec.loss = x.retained_loss;
}else {
rec.loss = x.loss;
}
if (rec.loss > 0.0 || rec.sidx < 0) {
fmxref_key k;
k.layer_id = layer;
Expand All @@ -376,8 +380,12 @@ inline void fmcalc::dofmcalc_r(std::vector<std::vector<int>> &aggid_to_vectorlo
for (int idx : avx[layer][vec_idx].item_idx) {
OASIS_FLOAT prop = 0;
if (gultotal > 0) prop = guls[idx] / gultotal;
//fmhdr.output_id = items[idx];
rec.loss = x.loss * prop;
//fmhdr.output_id = items[idx];
if (netvalue_) { // get net gul value
rec.loss = x.retained_loss * prop;
}else {
rec.loss = x.loss * prop;
}
if (rec.loss > 0.0 || rec.sidx < 0) {
fmxref_key k;
k.layer_id = layer;
Expand All @@ -402,7 +410,12 @@ inline void fmcalc::dofmcalc_r(std::vector<std::vector<int>> &aggid_to_vectorlo
for (int idx : avx[layer][vec_idx].item_idx) {
OASIS_FLOAT prop = 0;
if (prev_gul_total > 0) prop = prev_agg_vec[idx].loss / prev_gul_total;
rec.loss = x.loss * prop;
if (netvalue_) { // get net gul value
rec.loss = x.retained_loss * prop;
}
else {
rec.loss = x.loss * prop;
}
if (rec.loss > 0.0 || rec.sidx < 0) {
fmxref_key k;
k.layer_id = layer;
Expand Down Expand Up @@ -570,6 +583,10 @@ void fmcalc::dofm(int event_id, const std::vector<int> &items, std::vector<vecto
fmhdr.output_id = it->second;
}
rec.loss = x.loss;
// do the flip here
if (netvalue_) { // get net gul value
rec.loss = guls[idx] - rec.loss;
}
outmap[fmhdr].push_back(rec); // neglible cost
}
if (x.allocrule_id == 1 || x.allocrule_id == 2) { // back allocate as a proportion of the total of the original guls
Expand All @@ -584,6 +601,9 @@ void fmcalc::dofm(int event_id, const std::vector<int> &items, std::vector<vecto
if (gultotal > 0) prop = guls[gidx] / gultotal;
// fmhdr.output_id = items[gidx];
rec.loss = x.loss * prop;
if (netvalue_) { // get net gul value
rec.loss = guls[idx] - rec.loss;
}
if (rec.loss > 0.0 || rec.sidx < 0) {
fmxref_key k;
k.layer_id = layer;
Expand Down Expand Up @@ -631,10 +651,15 @@ void fmcalc::dofm(int event_id, const std::vector<int> &items, std::vector<vecto
void fmcalc::init_policytc(int maxRunLevel)
{
if (maxRunLevel == -1) maxRunLevel = 10000;
FILE *fin = NULL;
std::string file = FMPOLICYTC_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");

FILE *fin = fopen(FMPOLICYTC_FILE, "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, FMPOLICYTC_FILE);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -669,9 +694,15 @@ void fmcalc::init_policytc(int maxRunLevel)

void fmcalc::init_programme(int maxRunLevel)
{
FILE *fin = fopen(FMPROGRAMME_FILE, "rb");

FILE *fin = NULL;
std::string file = FMPROGRAMME_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL){
fprintf(stderr, "%s: cannot open %s\n", __func__, FMPROGRAMME_FILE);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -715,12 +746,18 @@ inline void add_tc(unsigned char tc_id, OASIS_FLOAT &tc_val, std::vector<tc_rec>

bool fmcalc::loadcoverages(std::vector<OASIS_FLOAT> &coverages)
{
FILE *fin = fopen(COVERAGES_FILE, "rb");

FILE *fin = NULL;
std::string file = COVERAGES_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, COVERAGES_FILE);
exit(-1);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

flseek(fin, 0L, SEEK_END);
long long sz = fltell(fin);
flseek(fin, 0L, SEEK_SET);
Expand All @@ -740,17 +777,43 @@ bool fmcalc::loadcoverages(std::vector<OASIS_FLOAT> &coverages)
fclose(fin);
return true;

}
int fmcalc::getmaxnoofitems()
{

FILE *fin = NULL;
std::string file = ITEMS_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

flseek(fin, 0L, SEEK_END);
long long p = fltell(fin);
p = p / sizeof(item);
fclose(fin);
return int(p);

}

void fmcalc::init_itemtotiv()
{
std::vector<OASIS_FLOAT> coverages;
loadcoverages(coverages);

FILE *fin = fopen(ITEMS_FILE, "rb");
FILE *fin = NULL;
std::string file = ITEMS_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, ITEMS_FILE);
exit(-1);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

flseek(fin, 0L, SEEK_END);
Expand All @@ -777,9 +840,15 @@ void fmcalc::init_itemtotiv()
}
void fmcalc::init_profile()
{
FILE *fin = fopen(FMPROFILE_FILE, "rb");

FILE *fin = NULL;
std::string file = FMPROFILE_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, FMPROFILE_FILE);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -812,11 +881,17 @@ void fmcalc::init_profile()

void fmcalc::init_fmxref()
{
FILE *fin = fopen(FMXREF_FILE, "rb");
FILE *fin = NULL;
std::string file = FMXREF_FILE;
if (inputpath_.length() > 0) {
file = inputpath_ + file.substr(5);
}
fin = fopen(file.c_str(), "rb");
if (fin == NULL) {
fprintf(stderr, "%s: cannot open %s\n", __func__, FMXREF_FILE);
fprintf(stderr, "%s: cannot open %s\n", __func__, file.c_str());
exit(EXIT_FAILURE);
}

fmXref f;
int i = fread(&f, sizeof(f), 1, fin);
while (i != 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/fmcalc/fmcalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ class fmcalc {
public:
void setmaxlevel(int maxlevel) { if (maxlevel > -1) maxLevel_ = maxlevel; }
void dofm(int event_id_, const std::vector<int> &items_, std::vector<std::vector<OASIS_FLOAT>> &event_guls_);
fmcalc(int maxRunLevel) { init(maxRunLevel); }
fmcalc(int maxRunLevel,const std::string &inputpath, bool netvalue): inputpath_(inputpath) , netvalue_(netvalue) { init(maxRunLevel); }
inline OASIS_FLOAT gettiv(int item_id) { return item_to_tiv_[item_id]; }
int getmaxnoofitems();
private:
std::vector<std::vector<std::vector<int>>> policy_tc_vec_vec_vec_; // policy_tc_vec_vec_vec_[level][agg_id][layer_id]
std::vector<int> level_to_maxagg_id_;
int max_layer_ = 0; // initialized from policy_tc
int max_agg_id_ = 0; // initialized from policy_tc
std::vector <profile_rec> profile_vec_;
int maxLevel_ = 0;
std::string inputpath_;
bool netvalue_ = false;
std::vector<std::vector<int>> pfm_vec_vec_; // initialized from fm/programme.bin pfm_vec_vec[level_id][item_id] returns agg_id
std::vector<OASIS_FLOAT> item_to_tiv_;
void init_programme(int maxrunLevel);
Expand Down
Loading

0 comments on commit a24b168

Please sign in to comment.