//Calculation of the collateral inclusive BVA at the termainal of each simulated path of montle carlo simulation Real BVA_calculator::Coll_incl_BVA_cal(pair<Real,Real> forward_pv,char CVA_DVA_ind, Real own_LGD, Real cpt_LGD) { Real CVADVA = 0; Real forw = forward_pv.first; Real col_v = forward_pv.second; //For the equations below, please refer to chapter 13 of Brigo's book on page 314 with the assumption that the exposure is measure in mid-market if(CVA_DVA_ind == 'c') //Collateral inclusive CVA CVADVA = cpt_LGD*(maximun((maximun(forw,0)-maximun(col_v,0)),0))+cpty_collateral_LGD*(maximun(minimun(forw,0)-minimun(col_v,0),0)); if(CVA_DVA_ind == 'd')//Collateral inclusive DVA CVADVA = own_LGD*(minimun((minimun(forw,0)-minimun(col_v,0)),0))+own_collateral_LGD*(minimun(maximun(forw,0)-maximun(col_v,0),0)); return CVADVA; }
int main() { int num1, num2, num3; printf("enter three integers:"); scanf("%d%d%d", &num1, &num2, &num3); printf("maximun is :%d\n", maximun(num1, num2, num3)); return 0; }
void delete_node(int input){//delete node Node* ser = find(input);// get the position of the node which will be deleted Node* temp = NULL; Node* y = NULL; if(ser->left == NULL && ser->right == NULL){//The situation of have not any child if(ser->parent->left == ser){//make its parent's left or right been NULL, and delete the node temp = ser->parent->left; ser->parent->left = NULL; free(temp); return; } else{ temp = ser->parent->right; ser->parent->right = NULL; free(temp); return; } } if(ser->left == NULL && ser->right != NULL)//Has right tree or node situation { if(ser->parent->left == ser){//make its parent's left or right pointer point to its right child temp = ser->parent->left; ser->parent->left = ser->right; free(temp); return; } else{ temp = ser->parent->right; ser->parent->right = ser->right; free(temp); return; } } if(ser->left != NULL && ser->right == NULL)//Has left tree or node situation { if(ser->parent->left == ser){//make its parent's left or right pointer point to its left child temp = ser->parent->left; ser->parent->left = ser->left; free(temp); return; } else{ temp = ser->parent->right; ser->parent->right = ser->left; free(temp); return; } } if(ser->left != NULL && ser->right != NULL){//Has both left and right child situation y = maximun(ser);//get the right tree's largest node ser->key = y->key; temp = y; y->parent->right = NULL; free(temp); return; } }
//to calculate the BVA for the forward with the data of the counterparty and our own vector<map<string,Real>> BVA_calculator::BVA_cal(Date maturity, char collateral_indicator, Real inv_col_LGD, Real cpty_col_LGD) { Real CVA = 0.0; Real DVA = 0.0; set_inv_coll_LGD(inv_col_LGD); //reset the investor's collateral LGD level set_cpty_coll_LGD(cpty_col_LGD); //reset the counterparty's collateral LGD level //initial the class for generating the default time of counterparty and our own based on the data in the class "HazardRateCalibrate" DefaultTimeGenerator oInvestorDefaultTime(myself->get_valueday(), myself->get_hazard_dates(), myself->oHazardRates); DefaultTimeGenerator oCounterptyDefaultTime(cpty->get_valueday(), cpty->get_hazard_dates(), cpty->oHazardRates); //get the Loss given default data for both counterparties Real Investor_LGD = 1-myself->recovery_rate; Real counterparty_LGD = 1-cpty->recovery_rate; // start the for loop to calculate expected EAD for (int iPath = 0; iPath < no_of_Date_path; ++iPath) { //calculate a sample of default times for both counterparties investorDefaultDate = oInvestorDefaultTime.GetDefaultTime(); counterptyDefaultDate = oCounterptyDefaultTime.GetDefaultTime(); //First to default check // check if there's any default event before maturity if (investorDefaultDate < maturity || counterptyDefaultDate < maturity) { //check if both counterparties default before the term of the forward if (investorDefaultDate < maturity && counterptyDefaultDate < maturity) { if (investorDefaultDate < counterptyDefaultDate) { //if the collateral option is selected ("Y/y"),the montle carlo simulation will include the calculation of the collateral amount if(collateral_indicator == 'Y' || collateral_indicator == 'y') DVA = DVA - Coll_incl_BVA_cal(gen->MCgeneration_collateral(no_of_stock_path, investorDefaultDate, 0),'d',Investor_LGD,counterparty_LGD); else //if our own default happens first, calculate the DVA (without collateral) DVA = DVA + (Investor_LGD*maximun(-(gen->MCgeneration(no_of_stock_path, investorDefaultDate, 0)),0)); } else {//if the collateral option is selected ("Y/y"),the montle carlo simulation will include the calculation of the collateral amount if(collateral_indicator == 'Y' || collateral_indicator == 'y') CVA = CVA + (Coll_incl_BVA_cal(gen->MCgeneration_collateral(no_of_stock_path, counterptyDefaultDate, 0),'c',Investor_LGD,counterparty_LGD)); else //if counterparty default first, calculate the CVA (without collateral) CVA = CVA + (counterparty_LGD*maximun(gen->MCgeneration(no_of_stock_path, counterptyDefaultDate, 0),0)); } } else {//check if there is only one of the counterparty default before the maturity of the forward if (investorDefaultDate < maturity) { if(collateral_indicator == 'Y' || collateral_indicator == 'y') DVA = DVA - Coll_incl_BVA_cal(gen->MCgeneration_collateral(no_of_stock_path, investorDefaultDate, 0),'d',Investor_LGD,counterparty_LGD); else DVA = DVA + (Investor_LGD*maximun(-(gen->MCgeneration(no_of_stock_path, investorDefaultDate, 0)),0)); } else { if(collateral_indicator == 'Y' || collateral_indicator == 'y') CVA = CVA + (Coll_incl_BVA_cal(gen->MCgeneration_collateral(no_of_stock_path, counterptyDefaultDate, 0),'c',Investor_LGD,counterparty_LGD)); else CVA = CVA + (counterparty_LGD*maximun(gen->MCgeneration(no_of_stock_path, counterptyDefaultDate, 0),0)); } } } // if no default event in path i, then skip to next iterator else { continue; } } // Expected values for the sampled CVA and DVA DVA = DVA/ no_of_Date_path; CVA = CVA/ no_of_Date_path; //save the CVA and DVA into the output vector map<string,Real> dummy_var; vector<map<string,Real>> BVA; dummy_var.insert(pair<string,Real>("CVA",CVA)); BVA.push_back(dummy_var); map<string,Real> dummy_var1; dummy_var1.insert(pair<string,Real>("DVA",DVA)); BVA.push_back(dummy_var1); return BVA; }