ICEACE Model: Closed Economy  1.0.0
Design Documentation of ICEACE Model
 All Data Structures Files Functions Variables Typedefs Macros Pages
household_functions_housing.c
Go to the documentation of this file.
1 #include "../header.h"
2 #include "../household_agent_header.h"
3 #include "../library_header.h"
4 #include <math.h>
5 
6 /*
7  * \fn: int household_housing_market_role()
8  * \brief: Sets the market role of the household. It is a fire seller, determined by financial distress. Otherwise, household plays role of a buyer or seller equally likely. iseseller, 'intention' variable is set accordingly.
9  */
11 {
12  double quarterly_income;
13 
14  quarterly_income = LABOUR_INCOME + CAPITAL_INCOME;
15 
16  /* Checking the financial distress for the fire sale case. */
17  if (HOUSING_PAYMENT > FIRE_SALE_THRESHOLD * quarterly_income){
18  if (HOUSING_UNITS == 0) { HMARKET_ROLE = 0; } else { HMARKET_ROLE = 1;}
19  return 0;
20  }
21 
22  /* Inactive */
23  if (random_int(0, 99) < (int)(100 - 2 * HOUSING_MARKET_ENTRANCE_PROB * 100)) {
24  HMARKET_ROLE = 0;
25  return 0;
26  }
27 
28  /* Seting a regular buyer or seller role, via flipping the coin. */
29  if (random_int(0, 99) > 50) {
30  /* Regular seller.*/
31  if (HOUSING_UNITS == 0) { HMARKET_ROLE = 0;} else { HMARKET_ROLE = 2;}
32  }
33  else {
34  /* Buyer */
35  HMARKET_ROLE = 3;
36  }
37 
38  return 0; /* Returning zero means the agent is not removed */
39 }
40 
41 /*
42  * \fn: int household_housing_check_wealth()
43  * \brief: The househld computes his equity ratio.
44  */
46 {
47  double wealth = 0;
48 
49  /* Capital income is not in the equation as it is liquidified quarterly as sooon as it is recieved.
50  */
51  wealth = LIQUIDITY + HOUSING_VALUE;
52 
53  if (fabs(wealth) <= 0.01){
54  EQUITY_RATIO = 0;
55  } else {
56  EQUITY_RATIO = EQUITY / wealth;
57  }
58 
59  if (EQUITY_RATIO <= 0 ) {
60  if (WARNING_MODE) {
61  //printf("Warning @household_housing_check_wealth(): Household %d has more mortgages than total assets. Equity = %f, Mortgages = %f \n", ID, EQUITY, MORTGAGES);
62  }
63  }
64 
65  return 0; /* Returning zero means the agent is not removed */
66 }
67 
68 
69 /*
70  * \fn: int household_housing_enter_market()
71  * \brief: the household enters the housing market as a buyer
72  */
74 {
75  double income, cash;
76 
77  income = LABOUR_INCOME + CAPITAL_INCOME;
78 
79  /* Cash rich households are allowed to use cash to finance housing.
80  This needs to be checked with the model design.
81 
82  double cash_allowance;
83  cash_allowance = 0.9 * HOUSING_PRICE;
84  if (LIQUIDITY > cash_allowance) {
85  cash = LIQUIDITY - cash_allowance;
86  } else {
87  cash = 0;
88  }
89  */
90 
91  /* Endogenous mortgage choice mechnism may be implemented at that point.*/
92 
93  cash = LIQUIDITY;
95 
96  return 0; /* Returning zero means the agent is not removed */
97 }
98 
99 /*
100  * \fn: int household_housing_bargain()
101  * \brief:
102  */
104 {
105  double mortgage_used, liquidity_spent, annuity;
106  mortgage_used = 0;
107  liquidity_spent = 0;
108 
110  /* The message is filtered via xmml. */
111  mortgage_used = bought_housing_message->mortgage_used;
112  liquidity_spent = bought_housing_message->liquidity_spent;
113  annuity = bought_housing_message->annuity;
115 
116  if (mortgage_used + liquidity_spent > 0){
117  HOUSING_UNITS += 1;
118  if (PRINT_DEBUG_MODE) {
119  printf("Household ID = %d has bought a new housing unit for %f. \n", ID, mortgage_used + liquidity_spent);
120  }
121  }
122 
123  if (liquidity_spent > 0){
124  LIQUIDITY -= liquidity_spent;
125  }
126 
127  if (mortgage_used > 0){
128  double quarterly_interest;
129  double quarterly_principal;
130  double d1, d2;
131  double used_interest_rate;
132 
133  /* Endogenous decision rule on which type of mortgage to take should be done here*/
135 
136  quarterly_interest = 0;
137  quarterly_principal = 0;
138 
139  if (MORTGAGE_CHOICE == 1) {
140  quarterly_interest = mortgage_used * MORTGAGES_INTEREST_RATE / 4;
141  quarterly_principal = (mortgage_used / annuity) - quarterly_interest;
142  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE, MORTGAGE_CHOICE);
143  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE);
144  }
145  else if (MORTGAGE_CHOICE == 2){
146  quarterly_interest = mortgage_used * MORTGAGES_INTEREST_RATE / 4;
147  quarterly_principal = (mortgage_used / 160);
148  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE, MORTGAGE_CHOICE);
149  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE);
150  }
151  else if (MORTGAGE_CHOICE == 3){
152  quarterly_interest = mortgage_used * 0.02/4;
153  d1 = 0.02/4;
154  d2 = d1 * pow((1 + d1), 160);
155  annuity = 1/d1 - 1/d2;
156  quarterly_principal = (mortgage_used / annuity) - quarterly_interest;
157  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
158  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, 0.02);
159  }
160  else if (MORTGAGE_CHOICE == 4){
161  quarterly_interest = mortgage_used * 0.02/4;
162  quarterly_principal = (mortgage_used / 160);
163  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
164  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, 0.02);
165  }
166  else if (MORTGAGE_CHOICE == 5){
167  used_interest_rate = MORTGAGES_INTEREST_RATE + 0.01;
168  quarterly_interest = mortgage_used * used_interest_rate/4;
169  d1 = (MORTGAGES_INTEREST_RATE + 0.01)/4;
170  d2 = d1 * pow((1 + d1), 160);
171  annuity = 1/d1 - 1/d2;
172  quarterly_principal = (mortgage_used / annuity) - quarterly_interest;
173  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, used_interest_rate, MORTGAGE_CHOICE);
174  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, used_interest_rate);
175  }
176  else if (MORTGAGE_CHOICE == 6){
177  used_interest_rate = MORTGAGES_INTEREST_RATE + 0.01;
178  quarterly_interest = mortgage_used * used_interest_rate / 4;
179  quarterly_principal = (mortgage_used / 160);
180  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, used_interest_rate, MORTGAGE_CHOICE);
181  //add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, used_interest_rate);
182  }
183  else if (MORTGAGE_CHOICE == 7){
184  d1 = 0.02/4;
185  d2 = d1 * pow((1 + d1), 160);
186  annuity = 1/d1 - 1/d2;
187  /* Interest payement includes index adjustment */
188  quarterly_interest = mortgage_used * 0.02/4 + mortgage_used * QUARTERLY_PRICE_CHANGE;
189  quarterly_principal = (mortgage_used / annuity) - (mortgage_used * 0.02/4);
190  add_mortgage(&MORTGAGES_LIST, BANK_ID, mortgage_used, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
191  }
192  else {
193  if (WARNING_MODE) {
194  printf("Warning @household_housing_buy(): Unexpected mortgage choice = %d \n", MORTGAGE_CHOICE);
195  }
196 
197  }
198 
199  /* Updating the cummulative mortgages and total mortgage payments. */
200  EXPECTED_HOUSING_PAYMENT += quarterly_interest + quarterly_principal;
201  MORTGAGES += mortgage_used;
202  if (PRINT_DEBUG_MODE) {
203  printf("Household ID = %d a new mortgage debt of %f. \n", ID, mortgage_used);
204  }
205  }
206 
207  return 0; /* Returning zero means the agent is not removed */
208 }
209 
210 
211 /*
212  * \fn: int household_housing_sell()
213  * \brief: a regular seller household, going with some price over the average.
214  */
216 {
217  if (HOUSING_UNITS == 0) {
218  return 0;
219  }
220 
221  double price_difference, price;
222 
223  price_difference = (((double)random_int(0, 100)) / 100.0) * HOUSING_PRICE_UP_RATE;
224 
225  price = HOUSING_PRICE * (1 + price_difference);
226 
228 
229  return 0; /* Returning zero means the agent is not removed */
230 }
231 
232 /*
233  * \fn: int household_housing_fire_sell()
234  * \brief: a fire seller household, going with some price below the average.
235  */
237 {
238  if (HOUSING_UNITS == 0) {
239  return 0;
240  }
241 
242  double price_difference, price;
243 
244  price_difference = (((double)random_int(0, 100)) / 100.0) * HOUSING_PRICE_DOWN_RATE;
245 
246  price = HOUSING_PRICE * (1 - price_difference);
247 
248  if (price > 0) {
250  }
251 
252  return 0; /* Returning zero means the agent is not removed */
253 }
254 
255 /*
256  * \fn: int household_housing_collect_sale_revenue()
257  * \brief:
258  */
260 {
261  double sale_price;
262  int sold = 0;
263 
265  /*The message is filtered via xmml. */
267  sale_price = sold_housing_message->price_sold;
269 
270  if (sold == 0){
271  return 0;
272  }
273 
274  /* In current implementation sold is either 0 or 1 */
275  HOUSING_UNITS -= sold;
276  if (PRINT_DEBUG_MODE) {
277  printf("Household ID = %d (%d) has gotten a house unit sold for %f \n", ID, HMARKET_ROLE, sale_price);
278  }
279 
280  if (MORTGAGES_LIST.size == 0) {
281  LIQUIDITY += sale_price;
282  return 0;
283  }
284 
285 // /* The most recent mortgage is assumed to be sold. */
286 // ind = MORTGAGES_LIST.size - 1;
287 // mort = MORTGAGES_LIST.array[ind];
288 // MORTGAGES -= mort.principal;
289 // /* If the sale is lower than the principal amount, the liquid asset is to be used. */
290 // add_mortgage_payment_from_sale_message(BANK_ID, mort.principal);
291 // LIQUIDITY += sale_price - mort.principal;
292 // remove_mortgage(&MORTGAGES_LIST, ind);
293 // free_mortgage(&mort);
294 // return 0;
295 
296  //int ind;
297  //mortgage mort;
298  //init_mortgage(&mort);
299 
300  double principal, new_principle;
301  double quarterly_interest, new_quarterly_interest;
302  double quarterly_principal, new_quarterly_principal;
303  double annuity;
304  double d1, d2;
305  double rate;
306  int quarters_left;
307 
308  /* Regular seller: */
309  /*if (HMARKET_ROLE == 2){
310  ind = MORTGAGES_LIST.size - 1;
311  mort = MORTGAGES_LIST.array[ind];
312  if (mort.principal <= sale_price){
313  add_mortgage_payment_from_sale_message(BANK_ID, mort.principal);
314  MORTGAGES -= mort.principal;
315  LIQUIDITY += sale_price - mort.principal;
316  remove_mortgage(&MORTGAGES_LIST, ind);
317  if (PRINT_DEBUG_MODE) {
318  printf("Regular Seller = %d has removed a mortgage = %f \n", ID, mort.principal);
319  }
320  }
321  else {
322  add_mortgage_payment_from_sale_message(BANK_ID, sale_price);
323  MORTGAGES -= sale_price;
324 
325  new_principle = mort.principal - sale_price;
326  d1 = MORTGAGES_INTEREST_RATE/4;
327  d2 = d1 * pow((1 + d1), mort.quarters_left);
328  annuity = 1/d1 - 1/d2;
329  new_quarterly_interest = new_principle * d1;
330  new_quarterly_principal = (new_principle / annuity) - new_quarterly_interest;
331  MORTGAGES_LIST.array[ind].principal = new_principle;
332  MORTGAGES_LIST.array[ind].quarterly_interest = new_quarterly_interest;
333  MORTGAGES_LIST.array[ind].quarterly_principal = new_quarterly_principal;
334  if (PRINT_DEBUG_MODE) {
335  printf("Regular Seller = %d has decreased a mortgage debt by a = %f amount. \n", ID, sale_price);
336  }
337  }
338  free_mortgage(&mort);
339  return 0;
340  }*/
341 
342  /* Fire seller:
343  Pays mortgages as long as sale revenue is enough to clear additional mortgages.
344  */
345 
346  while (sale_price > 0) {
347 
348  if (MORTGAGES_LIST.size == 0){ break;}
349 
350  /* pays the newest mortgage first! */
351  //ind = MORTGAGES_LIST.size - 1;
352  //mort = MORTGAGES_LIST.array[ind];
353  //EXPECTED_HOUSING_PAYMENT -= mort.quarterly_interest + mort.quarterly_principal;
354 
355  /* oldest mortgage is removed first. */
356  quarterly_interest = MORTGAGES_LIST.array[0].quarterly_interest;
357  quarterly_principal = MORTGAGES_LIST.array[0].quarterly_principal;
358  principal = MORTGAGES_LIST.array[0].principal;
359  EXPECTED_HOUSING_PAYMENT -= quarterly_principal + quarterly_interest;
360 
361  if (principal <= sale_price){
362  MORTGAGES -= principal;
364  sale_price -= principal;
365  if (PRINT_DEBUG_MODE) {
366  printf("Fire Seller = %d has removed the mortgage debt = %f \n", ID, principal);
367  }
369  }
370  else {
371  quarters_left = MORTGAGES_LIST.array[0].quarters_left;
372  rate = MORTGAGES_LIST.array[0].interestrate;
373 
375 
376  MORTGAGES -= sale_price;
377  new_principle = principal - sale_price;
378 
379  if (PRINT_DEBUG_MODE) {
380  printf("Fire Seller = %d decreases a mortgage debt from = %f to %f \n", ID, principal, new_principle);
381  }
382 
383  new_quarterly_interest = 0;
384  new_quarterly_principal = 0;
385  if (MORTGAGE_CHOICE == 1) {
387  d2 = d1 * pow((1 + d1), quarters_left);
388  annuity = 1/d1 - 1/d2;
389  new_quarterly_interest = new_principle * d1;
390  new_quarterly_principal = (new_principle / annuity) - new_quarterly_interest;
391 
392  }
393  else if (MORTGAGE_CHOICE == 2){
394  new_quarterly_interest = new_principle * MORTGAGES_INTEREST_RATE / 4;
395  new_quarterly_principal = new_principle / quarters_left;
396  }
397  else if (MORTGAGE_CHOICE == 3){
398  d1 = rate/4;
399  d2 = d1 * pow((1 + d1), quarters_left);
400  annuity = 1/d1 - 1/d2;
401 
402  new_quarterly_interest = new_principle * d1;
403  new_quarterly_principal = (new_principle / annuity) - new_quarterly_interest;
404  }
405  else if (MORTGAGE_CHOICE == 4){
406  new_quarterly_interest = new_principle * rate/4;
407  new_quarterly_principal = new_principle / quarters_left;
408  }
409  else if (MORTGAGE_CHOICE == 5){
410  d1 = rate/4;
411  d2 = d1 * pow((1 + d1), quarters_left);
412  annuity = 1/d1 - 1/d2;
413 
414  new_quarterly_interest = new_principle * d1;
415  new_quarterly_principal = (new_principle / annuity) - new_quarterly_interest;
416  }
417  else if (MORTGAGE_CHOICE == 6){
418  new_quarterly_interest = new_principle * rate/4;
419  new_quarterly_principal = new_principle / quarters_left;
420  }
421  else if (MORTGAGE_CHOICE == 7){
422  d1 = rate/4;
423  d2 = d1 * pow((1 + d1), quarters_left);
424  annuity = 1/d1 - 1/d2;
425 
426  new_quarterly_interest = (new_principle * rate/4) + new_principle * QUARTERLY_PRICE_CHANGE;
427  new_quarterly_principal = (new_principle / annuity) - (new_principle * rate/4);
428  }
429  else {
430  if (WARNING_MODE) {
431  printf("Warning @household_housing_buy(): Unexpected mortgage choice = %d \n", MORTGAGE_CHOICE);
432  }
433  }
434 
435  MORTGAGES_LIST.array[0].principal = new_principle;
436  MORTGAGES_LIST.array[0].quarterly_interest = new_quarterly_interest;
437  MORTGAGES_LIST.array[0].quarterly_principal = new_quarterly_principal;
438  EXPECTED_HOUSING_PAYMENT += new_quarterly_interest + new_quarterly_principal;
439  sale_price = 0;
440  }
441  }
442 
443  if (sale_price > 0) {
444  LIQUIDITY += sale_price;
445  }
446 
447  //free_mortgage(&mort);
448  return 0;
449 }
450 
451 /*
452  * \fn: int household_housing_update_market_price()
453  * \brief: Avg market price of housing is updated.
454  */
456 {
457  double pre_value;
458 
459  pre_value = HOUSING_UNITS * HOUSING_PRICE;
460 
462  HOUSING_PRICE = housing_price_message->price;
464 
466  DELTA_HOUSING_VALUE = HOUSING_VALUE - pre_value;
467 
468  return 0; /* Returning zero means the agent is not removed */
469 }
470 
471 /*
472  * \fn: int household_housing_pay_mortgages()
473  * \brief: mortgage payments are sent to bank.
474  */
476 {
477  int size;
478  size = MORTGAGES_LIST.size;
479  if (size == 0){ return 0;}
480 
481  //mortgage mort;
482  //init_mortgage(&mort);
483 
484  int i, bankid;
485  double total_interest_paid = 0;
486  double total_principal_paid = 0;
487  double interest_paid;
488  double principal_paid, principal_left;
489 
490  for (i = 0; i < size; i++) {
491  //mort = MORTGAGES_LIST.array[i];
492  if (MORTGAGES_LIST.array[i].principal < 0.001) { continue;}
493  bankid = MORTGAGES_LIST.array[i].bank_id;
494  interest_paid = MORTGAGES_LIST.array[i].quarterly_interest / 3;
495  principal_paid = MORTGAGES_LIST.array[i].quarterly_principal / 3;
496  principal_left = MORTGAGES_LIST.array[i].principal - principal_paid;
497  MORTGAGES_LIST.array[i].principal = principal_left;
498  add_mortgage_payment_message(bankid, interest_paid, principal_paid);
499  total_interest_paid += interest_paid;
500  total_principal_paid += principal_paid;
501  }
502 
505  MORTGAGE_COSTS[0] = total_principal_paid + total_interest_paid;
506 
507  MORTGAGES -= total_principal_paid;
509 
510 
511  HOUSING_PAYMENT = 0;
512  for (i = 0; i < 3; i++) {
514  }
515 
516  /* Finish off very little amount of mortgages.
517  if (MORTGAGES < LIQUIDITY) {
518  total_principal_paid = 0;
519  for (i = 0; i < size; i++) {
520  mort = MORTGAGES_LIST.array[i];
521  principal_paid = mort.principal;
522  add_mortgage_payment_message(mort.bank_id, 0, principal_paid);
523  total_principal_paid += principal_paid;
524  }
525  LIQUIDITY -= MORTGAGES;
526  MORTGAGES = 0;
527  MORTGAGE_COSTS[0] += total_principal_paid;
528  HOUSING_PAYMENT += total_principal_paid;
529  for (i = 0; i < size; i++) {remove_mortgage(&MORTGAGES_LIST, 0);}
530  }
531  */
532 
533  //free_mortgage(&mort);
534  return 0; /* Returning zero means the agent is not removed */
535 }
536 
537 /*
538  * \fn: household_housing_debt_writeoff()
539  * \brief: if household pays a significant amount of his income
540  * to mortgages a debt write off occures and its mortgage
541  * is restructured.
542  */
544 {
545  int size, ind;
546  double total_income;
547  double pre_mortgages, writeoff;
548  double quarterly_principal, quarterly_interest;
549 
550  size = MORTGAGES_LIST.size;
551  if (size == 0){
553  char * filename;
554  FILE * file1;
555  filename = malloc(100*sizeof(char));
556  filename[0]=0;
557  strcpy(filename, "./outputs/data/Household_Monthly_FirstDay.txt");
558  file1 = fopen(filename,"a");
559  double mcost = MORTGAGE_COSTS[0];
560  fprintf(file1,"%d %d %f %f %d %f %f %f\n",IT_NO, ID, MORTGAGES, mcost, HOUSING_UNITS, HOUSING_VALUE, EQUITY_RATIO, LIQUIDITY);
561  fclose(file1);
562  free(filename);
563  }
564  return 0;
565  }
566 
567  //mortgage mort;
568  //init_mortgage(&mort);
569 
570  total_income = LABOUR_INCOME + CAPITAL_INCOME;
571 
572 
574  {
575  pre_mortgages = MORTGAGES;
576  double annuity, d1, d2;
577  double used_interest_rate;
578 
579  for (ind = 0; ind < size; ind++) {
581  }
582 
583  quarterly_interest = 0;
584  quarterly_principal = 0;
585  if (MORTGAGE_CHOICE == 1) {
587  d2 = d1 * pow((1 + d1), 160);
588  annuity = 1/d1 - 1/d2;
589  MORTGAGES = total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW * annuity;
590  quarterly_interest = MORTGAGES * MORTGAGES_INTEREST_RATE / 4;
591  quarterly_principal = (MORTGAGES / annuity) - quarterly_interest;
592  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE, MORTGAGE_CHOICE);
593  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE);
594  }
595  else if (MORTGAGE_CHOICE == 2){
596  MORTGAGES = (total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW) / (MORTGAGES_INTEREST_RATE / 4 + 1/160);
597  quarterly_interest = MORTGAGES * MORTGAGES_INTEREST_RATE / 4;
598  quarterly_principal = (MORTGAGES / 160);
599  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE, MORTGAGE_CHOICE);
600  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, MORTGAGES_INTEREST_RATE);
601  }
602  else if (MORTGAGE_CHOICE == 3){
603  d1 = 0.02/4;
604  d2 = d1 * pow((1 + d1), 160);
605  annuity = 1/d1 - 1/d2;
606  MORTGAGES = total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW * annuity;
607  quarterly_interest = MORTGAGES * 0.02/4;
608  quarterly_principal = (MORTGAGES / annuity) - quarterly_interest;
609  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
610  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, 0.02);
611  }
612  else if (MORTGAGE_CHOICE == 4){
613  MORTGAGES = (total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW) / (0.02 / 4 + 1/160);
614  quarterly_interest = MORTGAGES * 0.02/4;
615  quarterly_principal = (MORTGAGES / 160);
616  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
617  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, 0.02);
618  }
619  else if (MORTGAGE_CHOICE == 5){
620  d1 = (MORTGAGES_INTEREST_RATE + 0.01)/4;
621  d2 = d1 * pow((1 + d1), 160);
622  annuity = 1/d1 - 1/d2;
623  used_interest_rate = MORTGAGES_INTEREST_RATE + 0.01;
624  MORTGAGES = total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW * annuity;
625  quarterly_interest = MORTGAGES * used_interest_rate / 4;
626  quarterly_principal = (MORTGAGES / annuity) - quarterly_interest;
627  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, used_interest_rate, MORTGAGE_CHOICE);
628  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, used_interest_rate);
629  }
630  else if (MORTGAGE_CHOICE == 6){
631  used_interest_rate = MORTGAGES_INTEREST_RATE + 0.01;
632  MORTGAGES = (total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW) / (used_interest_rate / 4 + 1/160);
633  quarterly_interest = MORTGAGES * (MORTGAGES_INTEREST_RATE + 0.01)/4;
634  quarterly_principal = (MORTGAGES / 160);
635  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, used_interest_rate, MORTGAGE_CHOICE);
636  //add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, used_interest_rate);
637  }
638  else if (MORTGAGE_CHOICE == 7){
639  d1 = 0.02/4;
640  d2 = d1 * pow((1 + d1), 160);
641  annuity = 1/d1 - 1/d2;
642  /* The new principal is gotten by adjusting the principal of a new annuity with respect to the current inflation. */
643  MORTGAGES = total_income * HOUSEHOLD_MORTGAGE_WRITEOFF_LOW * annuity / (1 + (annuity * QUARTERLY_PRICE_CHANGE));
644  quarterly_interest = MORTGAGES * 0.02/4 + MORTGAGES * QUARTERLY_PRICE_CHANGE;
645  quarterly_principal = (MORTGAGES / annuity) - (MORTGAGES * 0.02/4);
646  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal, 0.02, MORTGAGE_CHOICE);
647  }
648  else {
649  if (WARNING_MODE) {
650  printf("Warning @household_housing_debt_writeoff(): Unexpected mortgage choice = %d \n", MORTGAGE_CHOICE);
651  }
652 
653  }
654  /* Quarterly_Interest + Quarterly_Prinicipal = Quarterly_Income * Writeoff_Low
655  */
656  writeoff = pre_mortgages - MORTGAGES;
657  EXPECTED_HOUSING_PAYMENT = quarterly_interest + quarterly_principal;
658 
659  if (WARNING_MODE) {
660  if (writeoff < 0 || MORTGAGES < 0) {
661  printf("Warning @household_housing_debt_writeoff(): Unexpected negative values detected, Household = %d, Total income = %f, Mortgage Costs= %f, Writeoff = %f, Pre Mortgage = %f New Mortgage = %f, interest = %f. 50 percent of the mortgage cost will be written off. \n", ID, total_income, HOUSING_PAYMENT, writeoff, pre_mortgages, MORTGAGES, MORTGAGES_INTEREST_RATE);
662  }
663 
664  if (writeoff < 0) {
665  MORTGAGES = pre_mortgages * 0.5;
666  writeoff = pre_mortgages * 0.5;
667  }
668 
669  }
670 
671  /* All mortgages are acquired from the same bank. */
673 
674  if (DATA_COLLECTION_MODE) {
675  char * filename;
676  FILE * file1;
677  filename = malloc(100*sizeof(char));
678  filename[0]=0;
679  strcpy(filename, "./outputs/data/BankruptcyInspection.txt");
680  file1 = fopen(filename,"a");
681  fprintf(file1,"%d %d %s %s %d %f\n",IT_NO, ID, "Household", "Mortgage", BANK_ID, writeoff);
682  fclose(file1);
683  free(filename);
684  }
685 
686  if (PRINT_DEBUG_MODE) {
687  printf("Household ID = %d, Debts writeoff: %d mortgages affected. Loss on Bank ID = %d amounts to %f \n", ID, size, BANK_ID, writeoff);
688  }
689 
690 
691  /*if (MORTGAGES > 0) {
692  double quarterly_principal, quarterly_interest;
693  quarterly_interest = MORTGAGES * MORTGAGES_INTEREST_RATE / 4;
694  quarterly_principal = MORTGAGES / annuity - quarterly_interest;
695  add_mortgage(&MORTGAGES_LIST, BANK_ID, MORTGAGES, 160, quarterly_interest, quarterly_principal);
696  EXPECTED_HOUSING_PAYMENT = quarterly_interest + quarterly_principal;
697  }*/
698  }
699 
701  char * filename;
702  FILE * file1;
703  filename = malloc(100*sizeof(char));
704  filename[0]=0;
705  strcpy(filename, "./outputs/data/Household_Monthly_FirstDay.txt");
706  file1 = fopen(filename,"a");
707  double mcost = MORTGAGE_COSTS[0];
708  fprintf(file1,"%d %d %f %f %d %f %f %f\n",IT_NO, ID, MORTGAGES, mcost, HOUSING_UNITS, HOUSING_VALUE, EQUITY_RATIO, LIQUIDITY);
709  fclose(file1);
710  free(filename);
711  }
712 
713  //free_mortgage(&mort);
714 
715 
716  return 0; /* Returning zero means the agent is not removed */
717 }
718