Ejemplo n.º 1
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));
            }
Ejemplo n.º 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 <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
                })));
            }
Ejemplo n.º 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 <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));
            }