public ScheduleInput FormScheduleFromARITE(RITE aRite, out bool existed) { ScheduleInput sInput = null; existed = false; HashSet <RITE> riteSet = new HashSet <RITE>() { aRite }; //check if already exists foreach (IScheduleInput schedule in OriginalSchedules) { if (schedule.NumOfRites == 1 && schedule.GetScheduleRITEList().SetEquals(riteSet)) { existed = true; break; } } if (!existed) { string sName = "exploded." + aRite.ExposureID; ScheduleOfRITEs sOfRite = new ScheduleOfRITEs(sName, riteSet, aRite.RiskCharacteristics); sInput = new ScheduleInput(sOfRite); WholeSchedules.Add(sInput); } return(sInput); }
private ScheduleOfRITEs BuildScheduleFromRITChars(string name, HashSet <long> EDSschedule) { ScheduleOfRITEs newSchedule = new ScheduleOfRITEs(name); foreach (long RITCharID in EDSschedule) { long RITEID = EDSDataExtract.CoverageIdAttrMap[RITCharID].RITExposureId; RITCharacteristic characteristicToAdd = _characteristics.Where(ritchar => ritchar.ID == RITCharID).FirstOrDefault(); RITE ritetoAdd = _rites.Where(rite => rite.ExposureID == RITEID).FirstOrDefault(); if (characteristicToAdd != null) { newSchedule.AddCharacteristic(characteristicToAdd); } else { throw new IndexOutOfRangeException("Error building schedule with RITCharacteristics: Cannot find RITCharacteristic with id = " + RITCharID + " in Exposure Data"); } if (ritetoAdd != null) { newSchedule.AddItem(ritetoAdd); } else { throw new IndexOutOfRangeException("Error building schedule with RITEs: Cannot find RITE with id = " + RITEID + " in Exposure Data"); } } return(newSchedule); }
//public double[] GetGUForNode(GraphNode node) //{ // PrimarySubject sub = (PrimarySubject)node.Subject; // int numBldgs = sub.Schedule.ActNumOfBldgs; // double[] subjectloss = new double[numBldgs]; // List<RITCharacteristic> LeftoverRiteChars = sub.Schedule.RITChars.ToList(); // foreach (GraphNode childnode in graph.GetChildrenForNode(node)) // { // double[] nodeLosses = GetGUForNode(childnode); // if (childnode is TermNode) // { // TermNode termNode = childnode as TermNode; // if (numBldgs == 1) // { // subjectloss[0] += nodeLosses.Sum(); // } // else if (numBldgs == nodeLosses.Count()) // { // subjectloss.Zip(nodeLosses, (a, b) => a + b); // } // } // else // { // CoverNode covNode = childnode as CoverNode; // subjectloss[0] += covNode.Payout; // } // LeftoverRiteChars = LeftoverRiteChars.Except(childnode.Subject.Schedule.RITChars).ToList(); // } // foreach (RITCharacteristic RITChar in LeftoverRiteChars) // { // foreach (string subperil in sub.CauseOfLossSet.GetSubperils()) // { // if (sub.ExposureTypes.Contains(RITChar.ExpType)) // { // double[] RITELoss; // uint timestamp; // string error; // if (GetRITCharacteristicLoss(RITChar, subperil, out RITELoss, out timestamp, out error, numBldgs)) // subjectloss = subjectloss.Zip(RITELoss, (x, y) => x + y).ToArray(); // else // throw new GUInputEngineException(error); // } // } // } // return subjectloss; //} private bool GetRITCharacteristicLoss(RITCharacteristic RITChar, string subperil, out double[] loss, out uint timestamp, out string error, int numBldgs) { bool success = true; error = ""; long CharId = RITChar.ID; double BuildingTIV = RITChar.TIV / RITChar.ParentRITE.NumOfTrueBldgs; loss = new double[1]; loss[0] = 0; timestamp = 0; Dictionary <int, Dictionary <long, Tuple <double, uint, List <float> > > > AfterCOL; if (GULosses.TryGetValue(subperil, out AfterCOL)) { Dictionary <long, Tuple <double, uint, List <float> > > AfterSample; if (AfterCOL.TryGetValue(0, out AfterSample)) { Tuple <double, uint, List <float> > DamageRatio; if (AfterSample.TryGetValue(CharId, out DamageRatio)) { timestamp = DamageRatio.Item2; if (DamageRatio.Item3.Count == numBldgs) { double[] multiplyArr = RITE.GenerateMultiplierArr(RITChar.ParentRITE.NumOfSampleBldgs); loss = DamageRatio.Item3.Zip(multiplyArr, (d1, d2) => (double)d1 * d2).ToArray(); //loss = Array.ConvertAll(DamageRatio.Item2.ToArray(), x => (double)x); success = true; } else if (numBldgs == 1) { double[] multiplyArr = RITE.GenerateMultiplierArr(RITChar.ParentRITE.NumOfSampleBldgs); loss[0] = DamageRatio.Item3.Zip(multiplyArr, (d1, d2) => d1 * d2).Sum(); //loss[0] = DamageRatio.Item2.ToArray().Sum(); success = true; } else { success = false; error = "Error getting GU for RITE ID: " + RITChar.ID + ". GU losses specified not matching for NumBldgs..."; } } else { success = false; error = "Error getting GU for RITE ID: " + RITChar.ID + ". Cannot find RITE ID in GU Input Loss dictionary"; } } else { success = false; error = "Error getting GU for RITE ID: " + RITChar.ID + ". Cannot find Sample 0 in GU Input Loss dictionary, currenlty hard-coded to take sample id = 0"; } } return(success); }
public CoverageAtomicRITE(string _subperil, ExposureType _expType, RITE _rite, long ID) { SubPeril = _subperil; ExpType = _expType; RITE = _rite; RITCharacterisiticID = ID; CurrentAllocationStateCollection = new AllocationStateCollection2(RITE.ActNumOfSampleBldgs); //CurrentAllocationStateSummed = new AllocationState(); CurrentLossStateCollection = new LossStateCollection2(RITE.ActNumOfSampleBldgs); }
public ScheduleInput FormScheduleFromARITE2(RITE aRite) { string sName = "exploded." + aRite.ExposureID; HashSet <RITE> riteSet = new HashSet <RITE>() { aRite }; ScheduleOfRITEs sOfRite = new ScheduleOfRITEs(sName, riteSet, aRite.RiskCharacteristics); ScheduleInput sInput = new ScheduleInput(sOfRite); return(sInput); }
private bool FindScheduleWithRite(RITE rite, out ScheduleOfRITEs schedule) { schedule = null; bool found = false; foreach (ScheduleOfRITEs sch in ExpData.Schedules) { if (sch.IsLocation && sch.ScheduleList.First().Equals(rite)) { schedule = sch; found = true; } } return(found); }
private ScheduleOfRITEs BuildScheduleFromRITs(string name, HashSet <long> EDSschedule) { ScheduleOfRITEs newSchedule = new ScheduleOfRITEs(name); foreach (long riteID in EDSschedule) { RITE ritetoAdd = _rites.Where(rite => rite.ExposureID == riteID).FirstOrDefault(); if (ritetoAdd != null) { newSchedule.AddItem(ritetoAdd); newSchedule.RITChars.UnionWith(ritetoAdd.RiskCharacteristics); } else { throw new IndexOutOfRangeException("Error building schedule with RITEs: Cannot find RITE with id = " + riteID + " in Exposure Data"); } } return(newSchedule); }
private void ExecuteOverlappedTermNode(TermNode currTermNode, List <GraphNode> childrenNodes, GUInputEngine guLossesEngine, HashSet <CoverageAtomicRITE> SubjectCRITEs) { //find its AtomicRITEs // HashSet<AtomicRITE> SubjectARITEs = currTermNode.Subject.GetAtomicRites(); //get the node's Subject loss double[] inputlosses; //inputlosses = guLossesEngine.GetGUForSubject(currTermNode.PrimarySubject).Zip(currTermNode.PrimarySubject.Schedule.MultiplierArr, (d1, d2) => d1 * d2).ToArray(); //inputlosses = guLossesEngine.GetGUForSubject(currTermNode.PrimarySubject); inputlosses = graph.GetNodeSubjectLoss(currTermNode).AllLoss(); if (currTermNode.IsPerRisk) { currTermNode.CurrentLossStateCollection.SetSubjectLosses(inputlosses); } else { currTermNode.CurrentLossStateCollection.SetSubjectLosses(new double[] { inputlosses.Sum() }); } //if (isLowestLevel) //initialize to GU loss //{ foreach (CoverageAtomicRITE cRite in SubjectCRITEs) { //double[] GULoss = guLossesEngine.GetGUForCoverageRITE(CoverageAtomicRITE cRITE); //if (cRite.GetLossState().collection[0].S == 0) //if not assign the GU loss yet if (cRite.GetLossState().GetTotalSum.S == 0) //if not assign the GU loss yet { guLossesEngine.GetGUForCoverageRITE(cRite); int[] multiArr = RITE.GenerateMultiplierArr(cRite.RITE.ActNumOfBldgs).ToArray(); for (int i = 0; i < cRite.RITE.ActNumOfBldgs; i++) { //cRite.GetLossState().collection[i].S = GULoss[i] * multiArr[i]; cRite.GetLossState().collection[i].S = cRite.GetLossState().collection[i].S * multiArr[i]; cRite.GetLossState().collection[i].R = cRite.GetLossState().collection[i].S; cRite.GetLossState().collection[i].D = 0; cRite.GetLossState().collection[i].X = 0; } } for (int i = 0; i < cRite.RITE.ActNumOfBldgs; i++) { //init the allocation state cRite.GetAllocState().collection[i].S = cRite.GetLossState().collection[i].S; } //cRite.CurrentLossState = cRite.CurrentLossStateCollection.GetTotalSum; } //} Aggregation aggType = Aggregation.Summed; if (currTermNode.IsPerRisk && currTermNode.PrimarySubject.Schedule.ActNumOfBldgs > 1) { aggType = Aggregation.PerBuilding; } //else //{ //need reset the lossState to act as only one building //LossStateCollection tempLossStateCollection = new LossStateCollection(1); //tempLossStateCollection.collection[0] = currTermNode.CurrentLossStateCollection.GetTotalSum; //currTermNode.CurrentLossStateCollection = tempLossStateCollection; //} //if no childrenNodes, nothing to do with the Interaction: the interaction terms are all zeros. if (SubjectCRITEs.Count > 0) { //initialize InterObj InteractionObject[] InterObj = GetInterObjForOverlap(currTermNode, childrenNodes, aggType, SubjectCRITEs); TermFunctionalEngine tFunEng = new TermFunctionalEngine(currTermNode, aggType); tFunEng.TermFunction(InterObj); } else { TermFunctionalEngine tFunEng = new TermFunctionalEngine(currTermNode, aggType); tFunEng.TermFunction(new InteractionObject[0]); } //Final Adjustment for (int i = 0; i < currTermNode.CurrentLossStateCollection.NumBldgs; i++) { currTermNode.CurrentLossStateCollection.collection[i].AdjustR(); } }
public override int GetHashCode() { return(SubPeril.GetHashCode() ^ ExpType.GetHashCode() ^ (int)RITCharacterisiticID ^ RITE.GetHashCode()); }
public void GetPrimaryData() { if (TreatyExposure) { throw new InvalidOperationException("Cannot use this method to get exposure data for reinsurance contract"); } EDSDataExtract.ExtractRiteMap(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var LocationGroups = EDSDataExtract.CoverageIdAttrMap.GroupBy(pair => pair.Value.RITExposureId); foreach (var LocGroup in LocationGroups) { RITE newRITE = BuildRITE(LocGroup.First()); _rites.Add(newRITE); _ritesDict.Add(LocGroup.Key, newRITE); foreach (KeyValuePair <long, RiskItemCharacteristicIDAttributes> pair in LocGroup) { RITCharacteristic newRITChar = BuildRITCharacteristic(pair); _characteristics.Add(newRITChar); _characteristicsDict.Add(pair.Key, newRITChar); newRITE.AddCharacteristic(newRITChar); } } foreach (KeyValuePair <string, Subschedule> EDSschedule in EDSDataExtract.SubSchedule) { ScheduleOfRITEs newSchedule; HashSet <long> Ids = (EDSschedule.Value.Ids != null) ? EDSschedule.Value.Ids : new HashSet <long>(EDSschedule.Value.CompressedIds.Enumerable()); if (EDSschedule.Value.Type == SubscheduleType.RITCHARACTERISTICS) { newSchedule = BuildScheduleFromRITChars2(EDSschedule.Key, Ids); } else if (EDSschedule.Value.Type == SubscheduleType.RITEXPOSURES) { newSchedule = BuildScheduleFromRITs2(EDSschedule.Key, Ids); } else { throw new InvalidOperationException("Cannot support building schedules of type: " + EDSschedule.Value.Type.ToString()); } _schedules.Add(newSchedule); _schedulesDict.Add(newSchedule.Name, newSchedule); } stopwatch.Stop(); ////////////////////////OLD OLD OLD Version////////////////////////////////// //_schedules.Clear(); //_rites.Clear(); //_characteristics.Clear(); //stopwatch.Reset(); //stopwatch.Start(); //foreach (KeyValuePair<long, RiskItemCharacteristicIDAttributes> RiteAttribute in EDSDataExtract.RiskItemCharacteristicIDAttributeMap) //{ // RITCharacteristic newRITChar = BuildRITCharacteristic(RiteAttribute); // _characteristics.Add(newRITChar); // RITE parentRITE = _rites.Where(rite => rite.ExposureID == RiteAttribute.Value.RITExposureId).FirstOrDefault(); // if (parentRITE != null) // { // parentRITE.AddCharacteristic(newRITChar); // } // else // { // RITE newRITE = BuildRITE(RiteAttribute); // _rites.Add(newRITE); // newRITE.AddCharacteristic(newRITChar); // } //} //foreach (KeyValuePair<string, Subschedule> EDSschedule in EDSDataExtract.SubSchedule) //{ // ScheduleOfRITEs newSchedule; // HashSet<long> Ids = (EDSschedule.Value.Ids != null) ? EDSschedule.Value.Ids : new HashSet<long>(EDSschedule.Value.CompressedIds.Enumerable()); // if (EDSschedule.Value.Type == SubscheduleType.RITCHARACTERISTICS) // newSchedule = BuildScheduleFromRITChars(EDSschedule.Key, Ids); // else if (EDSschedule.Value.Type == SubscheduleType.RITEXPOSURES) // newSchedule = BuildScheduleFromRITs(EDSschedule.Key, Ids); // else // throw new InvalidOperationException("Cannot support building schedules of type: " + EDSschedule.Value.Type.ToString()); // _schedules.Add(newSchedule); //} //stopwatch.Stop(); }
//public ExposureDataAdaptor(ContractExposure contractExposure, NGFMPrototype _ParsingAPI) //{ // //ParsingAPI = _ParsingAPI; // UseJSContext = false; // _contractExposure = contractExposure; // _contractJSON = GetJSONForContract(); // EDSDataExtract = new ExposureDataExtractor(_contractExposure); // _characteristics = new HashSet<RITCharacteristic>(); // _rites = new HashSet<RITE>(); // _schedules = new HashSet<ScheduleOfRITEs>(); // _characteristicsDict = new Dictionary<long, RITCharacteristic>(); // _ritesDict = new Dictionary<long, RITE>(); // _schedulesDict = new Dictionary<string, ScheduleOfRITEs>(); // if (_contractExposure.ContractType.IsReinsuranceContract()) // TreatyExposure = true; // else // TreatyExposure = false; //} public void GetPrimaryData() { if (TreatyExposure) { throw new InvalidOperationException("Cannot use this method to get exposure data for reinsurance contract"); } EDSDataExtract.ExtractRiteMap(); Dictionary <long, ExtractRITCharInfo> CoverageIdAttrMap = EDSDataExtract.CoverageIdAttrMap; if (UseAggregatedRITEs) { CoverageIdAttrMap = AggregateRITChars(CoverageIdAttrMap); } else { foreach (KeyValuePair <long, ExtractRITCharInfo> pair in CoverageIdAttrMap) { RiskCharIDtoAggregateID.Add(pair.Key, pair.Key); AggregateIDtoDisaggIDs.Add(pair.Key, new HashSet <long> { pair.Key }); } } #region old RITE aggreagation code //foreach (var LocGroup in LocationGroups) //{ // if (UseAggregatedRITEs) // { // long RiteID = LocGroup.First().Value.RITExposureId; // long OriginalRiteID = (long)LocGroup.First().Value.OriginalRITExposureId; // if (RiteID == OriginalRiteID) // { // RITE newRITE = BuildRITE(LocGroup.First()); // _rites.Add(newRITE); // _ritesDict.Add(LocGroup.Key, newRITE); // foreach (KeyValuePair<long, ExtractRITCharInfo> pair in LocGroup) // { // RITCharacteristic newRITChar = BuildRITCharacteristic(pair); // _characteristics.Add(newRITChar); // _characteristicsDict.Add(pair.Key, newRITChar); // newRITE.AddCharacteristic(newRITChar); // } // } // else // { // foreach (KeyValuePair<long, ExtractRITCharInfo> pair in LocGroup) // { // RiskCharIDtoAggregateID.Add(pair.Key, (long)pair.Value.OriginalRITECharacteristicId); // } // } // } // else // { // RITE newRITE = BuildRITE(LocGroup.First()); // _rites.Add(newRITE); // _ritesDict.Add(LocGroup.Key, newRITE); // foreach (KeyValuePair<long, ExtractRITCharInfo> pair in LocGroup) // { // RITCharacteristic newRITChar = BuildRITCharacteristic(pair); // _characteristics.Add(newRITChar); // _characteristicsDict.Add(pair.Key, newRITChar); // newRITE.AddCharacteristic(newRITChar); // } // } //} #endregion var LocationGroups = CoverageIdAttrMap.GroupBy(pair => pair.Value.RITExposureId); foreach (var LocGroup in LocationGroups) { RITE newRITE = BuildRITE(LocGroup.First()); _rites.Add(newRITE); _ritesDict.Add(LocGroup.Key, newRITE); foreach (KeyValuePair <long, ExtractRITCharInfo> pair in LocGroup) { RITCharacteristic newRITChar = BuildRITCharacteristic(pair); _characteristics.Add(newRITChar); _characteristicsDict.Add(pair.Key, newRITChar); newRITE.AddCharacteristic(newRITChar); } } foreach (KeyValuePair <string, Subschedule> EDSschedule in EDSDataExtract.SubSchedule) { ScheduleOfRITEs newSchedule; HashSet <long> Ids = (EDSschedule.Value.Ids != null) ? EDSschedule.Value.Ids : new HashSet <long>(EDSschedule.Value.CompressedIds.Enumerable()); if (EDSschedule.Value.Type == SubscheduleType.RITCHARACTERISTICS) //|| EDSschedule.Value.Name == "Blanket_FL" || EDSschedule.Value.Name == "Blanket_EQ" || EDSschedule.Value.Name == "Blanket_WS") //Sunny Hack { newSchedule = BuildScheduleFromRITChars2(EDSschedule.Key, Ids); } else if (EDSschedule.Value.Type == SubscheduleType.RITEXPOSURES) { try { newSchedule = BuildScheduleFromRITs2(EDSschedule.Key, Ids); } catch (IndexOutOfRangeException exp) { newSchedule = BuildScheduleFromRITChars2(EDSschedule.Key, Ids); } } else { throw new InvalidOperationException("Cannot support building schedules of type: " + EDSschedule.Value.Type.ToString()); } _schedules.Add(newSchedule); _schedulesDict.Add(newSchedule.Name, newSchedule); } }