public static List <Sensitivity> NetSensitivities(Currency calculationCurrency, List <Sensitivity> sensitivities) { if (calculationCurrency == null) { throw new ArgumentNullException(nameof(calculationCurrency)); } if (sensitivities == null) { throw new ArgumentNullException(nameof(sensitivities)); } if (sensitivities.Count == 0) { return(new List <Sensitivity>(0)); } List <Sensitivity> sensitivitiesNetted = new List <Sensitivity>(0) .Concat ( sensitivities .Where(x => x.Category == SensitivityCategory.BaseCorrelation) .GroupBy(x => x.Qualifier) .Select(x => Sensitivity.BaseCorrelation(x.Key, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Risk == SensitivityRisk.Commodity) .GroupBy(x => new { x.Category, x.Qualifier, Bucket = (BucketCommodity)x.Bucket }) .Select(x => Sensitivity.Commodity(x.Key.Category, x.Key.Qualifier, x.Key.Bucket, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Risk == SensitivityRisk.CreditNonQualifying) .GroupBy(x => new { x.Category, x.Qualifier, Bucket = (BucketCreditNonQualifying)x.Bucket, x.Tenor }) .Select(x => Sensitivity.CreditNonQualifying(x.Key.Category, x.Key.Qualifier, x.Key.Bucket, x.Key.Tenor, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => (x.Risk == SensitivityRisk.CreditQualifying) && (x.Category != SensitivityCategory.BaseCorrelation)) .GroupBy(x => new { x.Category, x.Qualifier, Bucket = (BucketCreditQualifying)x.Bucket, x.Tenor, x.Label2 }) .Select(x => Sensitivity.CreditQualifying(x.Key.Category, x.Key.Qualifier, x.Key.Bucket, x.Key.Tenor, x.Key.Label2, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Subrisk == SensitivitySubrisk.CrossCurrencyBasis) .GroupBy(x => (Currency)x.Bucket) .Select(x => Sensitivity.CrossCurrencyBasis(x.Key, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Risk == SensitivityRisk.Equity) .GroupBy(x => new { x.Category, x.Qualifier, Bucket = (BucketEquity)x.Bucket }) .Select(x => Sensitivity.Equity(x.Key.Category, x.Key.Qualifier, x.Key.Bucket, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Risk == SensitivityRisk.Fx) .GroupBy(x => new { x.Category, x.ThresholdIdentifier }) .Select(x => Sensitivity.Fx(x.Key.Category, x.Key.ThresholdIdentifier, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Subrisk == SensitivitySubrisk.Inflation) .GroupBy(x => new { x.Category, Bucket = (Currency)x.Bucket }) .Select(x => Sensitivity.Inflation(x.Key.Category, x.Key.Bucket, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .Concat ( sensitivities .Where(x => x.Subrisk == SensitivitySubrisk.InterestRate) .GroupBy(x => new { x.Category, Bucket = (Currency)x.Bucket, x.Label2, x.Tenor }) .Select(x => Sensitivity.InterestRate(x.Key.Category, x.Key.Bucket, x.Key.Tenor, x.Key.Label2, Amount.Sum(x.Select(s => s.Amount), calculationCurrency))) ) .ToList(); return(sensitivitiesNetted); }