Example #1
0
        private static List <MarginRisk> CalculateRiskMargins(Currency calculationCurrency, FxRatesProvider ratesProvider, List <Sensitivity> sensitivities)
        {
            List <SensitivityRisk> risks       = sensitivities.Select(x => x.Risk).Distinct().OrderBy(x => x.ToString()).ToList();
            List <MarginRisk>      riskMargins = new List <MarginRisk>(risks.Count);

            foreach (SensitivityRisk risk in risks)
            {
                List <Sensitivity> sensitivitiesByRisk = sensitivities.Where(x => x.Risk == risk).ToList();

                List <SensitivityCategory> sensitivityCategories = sensitivitiesByRisk.Select(x => x.Category).Distinct().OrderBy(x => x.ToString()).ToList();
                List <MarginSensitivity>   sensitivityMargins    = new List <MarginSensitivity>(sensitivityCategories.Count);

                foreach (SensitivityCategory category in sensitivityCategories)
                {
                    List <Sensitivity> sensitivitiesByClass = sensitivitiesByRisk.Where(x => x.Category == category).ToList();
                    MarginSensitivity  sensitivityMargin    = s_MarginCalculators[category].CalculateMargin(calculationCurrency, ratesProvider, risk, category, sensitivitiesByClass);

                    sensitivityMargins.Add(sensitivityMargin);
                }

                Amount riskMarginAmount = Amount.Sum(sensitivityMargins.Select(x => x.Value), calculationCurrency);

                riskMargins.Add(MarginRisk.Of(risk, riskMarginAmount, sensitivityMargins));
            }

            return(riskMargins);
        }
Example #2
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <IBucket>      buckets       = sensitivities.Select(x => x.Bucket).Distinct().OrderBy(x => x.Name).ToList();
                List <MarginBucket> bucketMargins = new List <MarginBucket>(buckets.Count);

                foreach (IBucket bucket in buckets)
                {
                    List <Sensitivity> sensitivitiesByBucket = sensitivities.Where(x => x.Bucket == bucket).ToList();

                    List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(SensitivityCategory.Curvature, sensitivitiesByBucket);
                    Amount sumSquared         = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                    Amount sumCorrelated      = CalculateCorrelatedSumWeights(calculationCurrency, risk, weightingMargins);
                    Amount bucketMarginAmount = Amount.SquareRoot(sumSquared + sumCorrelated);

                    bucketMargins.Add(MarginBucket.Of(bucket, bucketMarginAmount, weightingMargins));
                }

                Amount marginNonResidual       = CalculateMarginByBuckets(calculationCurrency, risk, false, bucketMargins);
                Amount marginResidual          = CalculateMarginByBuckets(calculationCurrency, risk, true, bucketMargins);
                Amount sensitivityMarginAmount = ModelParameters.GetCurvatureScaleFactor(risk) * (marginNonResidual + marginResidual);

                return(MarginSensitivity.Of(SensitivityCategory.Curvature, sensitivityMarginAmount, bucketMargins));
            }
Example #3
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(SensitivityCategory.BaseCorrelation, sensitivities);
                Amount sumSquared    = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                Amount sumCorrelated = CalculateCorrelatedSumWeights(calculationCurrency, weightingMargins);
                Amount amount        = Amount.SquareRoot(sumSquared + sumCorrelated);

                MarginBucket bucketMargin = MarginBucket.Of(Placeholder.Instance, amount, weightingMargins);

                return(MarginSensitivity.Of(SensitivityCategory.BaseCorrelation, amount, (new List <MarginBucket> {
                    bucketMargin
                })));
            }
Example #4
0
            public override MarginSensitivity CalculateMargin(Currency calculationCurrency, FxRatesProvider ratesProvider, SensitivityRisk risk, SensitivityCategory category, List <Sensitivity> sensitivities)
            {
                if (calculationCurrency == null)
                {
                    throw new ArgumentNullException(nameof(calculationCurrency));
                }

                if (ratesProvider == null)
                {
                    throw new ArgumentNullException(nameof(ratesProvider));
                }

                if (!Enum.IsDefined(typeof(SensitivityRisk), risk))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity risk specified.");
                }

                if (!Enum.IsDefined(typeof(SensitivityCategory), category))
                {
                    throw new InvalidEnumArgumentException("Invalid sensitivity category specified.");
                }

                if (sensitivities == null)
                {
                    throw new ArgumentNullException(nameof(sensitivities));
                }

                List <IBucket>      buckets       = sensitivities.Select(x => x.Bucket).Distinct().OrderBy(x => x.Name).ToList();
                List <MarginBucket> bucketMargins = new List <MarginBucket>(buckets.Count);
                Dictionary <IBucket, Dictionary <String, Decimal> > thresholdFactors = new Dictionary <IBucket, Dictionary <String, Decimal> >();

                foreach (IBucket bucket in buckets)
                {
                    List <Sensitivity> sensitivitiesByBucket = sensitivities.Where(x => x.Bucket == bucket).ToList();

                    thresholdFactors[bucket] = sensitivitiesByBucket
                                               .GroupBy(x => new { x.Bucket, x.Qualifier })
                                               .ToDictionary(x => x.Key.Qualifier, x => CalculateThresholdFactor(calculationCurrency, ratesProvider, risk, category, x.ToList()));

                    List <MarginWeighting> weightingMargins = CalculateMarginsWeighting(category, thresholdFactors[bucket], sensitivitiesByBucket);
                    Amount sumSquared         = Amount.Sum(weightingMargins.Select(x => Amount.Square(x.Value)), calculationCurrency);
                    Amount sumCorrelated      = CalculateCorrelatedSumWeights(calculationCurrency, risk, weightingMargins, thresholdFactors[bucket]);
                    Amount bucketMarginAmount = Amount.SquareRoot(sumSquared + sumCorrelated);

                    bucketMargins.Add(MarginBucket.Of(bucket, bucketMarginAmount, weightingMargins));
                }

                List <MarginBucket> bucketMarginsNonResidual = bucketMargins.Where(x => !x.Bucket.IsResidual).ToList();
                Amount sumSquaredNonResidual = Amount.Sum(bucketMarginsNonResidual.Select(x => Amount.Square(x.Value)), calculationCurrency);

                List <MarginBucket> bucketMarginsResidual = bucketMargins.Where(x => x.Bucket.IsResidual).ToList();
                Amount sumSquaredResidual = Amount.Sum(bucketMarginsResidual.Select(x => Amount.Square(x.Value)), calculationCurrency);

                Amount sumCorrelatedNonResidual;

                if ((risk == SensitivityRisk.Rates) && (category == SensitivityCategory.Delta))
                {
                    Dictionary <IBucket, Decimal> bucketThresholdFactors = thresholdFactors.ToDictionary(x => x.Key, x => x.Value.Values.Single());
                    sumCorrelatedNonResidual = CalculateCorrelatedSumBuckets(calculationCurrency, risk, bucketThresholdFactors, bucketMarginsNonResidual);
                }
                else
                {
                    sumCorrelatedNonResidual = CalculateCorrelatedSumBuckets(calculationCurrency, risk, bucketMarginsNonResidual);
                }

                Amount sensitivityMarginAmount = Amount.SquareRoot(sumSquaredNonResidual + sumCorrelatedNonResidual) + Amount.SquareRoot(sumSquaredResidual);

                return(MarginSensitivity.Of(category, sensitivityMarginAmount, bucketMargins));
            }