Exemplo n.º 1
0
        public void CheckZeroVolForward(AssetMarket assetMkt)
        {
            var zcCurve = assetMkt.RiskFreeDiscount;
            var market  = new Market(new[] { zcCurve }, new[] { assetMkt });

            var zeroVol           = new MapRawDatas <DateOrDuration, double>(new[] { new DateOrDuration(assetMkt.RefDate) }, new[] { 0.0 });
            var blackScholesDesc  = new BlackScholesModelDescription(assetMkt.Asset.Name, zeroVol, true);
            var mcConfig          = new MonteCarloConfig(1, RandomGenerators.GaussianSobol(SobolDirection.Kuo3));
            var blackScholesModel = ModelFactory.Instance.Build(blackScholesDesc, market);

            var fwdDates = new[] { Duration.Month, 6 * Duration.Month, Duration.Year, 2 * Duration.Year, 5 * Duration.Year }
            .Map(d => assetMkt.RefDate + d);

            IProduct    fwdLeg      = ForwardLeg(fwdDates);
            PriceResult priceResult = (PriceResult)McPricer.WithDetails(mcConfig).Price(fwdLeg, blackScholesModel, market);

            double[] fwds = priceResult.Details.Map(kv => kv.Item3.Value);

            var assetFwdCurve = assetMkt.Forward();

            double[] refFwds = fwdDates.Map(d => assetFwdCurve.Fwd(d) * zcCurve.Zc(d));

            foreach (var i  in Enumerable.Range(0, fwdDates.Length))
            {
                var err = Math.Abs(fwds[i] / refFwds[i] - 1.0);
                Assert.LessOrEqual(err, 20.0 * DoubleUtils.MachineEpsilon);
            }
        }
Exemplo n.º 2
0
        public void Test()
        {
            var market = Market();

            const double lambda   = 0.01;
            var          sigma    = new StepFunction(new[] { 0.0, 1.0, 2.0 }, new[] { 0.007, 0.004, 0.0065 }, 0.0);
            var          hw1      = new Hw1Model(TimeMeasure.Act365(market.RefDate), Currency.Eur, lambda, sigma);
            var          mcConfig = new MonteCarloConfig(20000,
                                                         RandomGenerators.GaussianSobol(SobolDirection.JoeKuoD5));

            var mcPricer = McPricer.WithDetails(mcConfig);

            var fixedLeg      = FixedLeg(market.RefDate);
            var mcPriceResult = (PriceResult)mcPricer.Price(fixedLeg, hw1, market);

            var mcCoupons  = mcPriceResult.Details.Map(p => p.Item3.Value);
            var refCoupons = mcPriceResult.Details.Map(pi => market.DiscountCurve(pi.Item2.Financing).Zc(pi.Item2.Date));

            var errAbs = Math.Abs(mcCoupons.Sum() - refCoupons.Sum());

            Assert.LessOrEqual(errAbs, 7.0e-5);

            var errRel = Math.Abs(mcCoupons.Sum() / refCoupons.Sum() - 1.0);

            Assert.LessOrEqual(errRel, 8.0e-6);
        }
Exemplo n.º 3
0
        private static Task <IPricer> BuildPricer(TaskFactory taskFactory, object requestObj, object[,] algorithmBag)
        {
            return(taskFactory.ComputationTaskWithLog("pricer preparation", () =>
            {
                INumericalMethodConfig algorithm = AlgorithmFactory.Instance.Build(algorithmBag);

                var mcConfig = algorithm as MonteCarloConfig;
                if (mcConfig != null)
                {
                    if (!requestObj.ToString().Equals("price", StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new Exception("Unknow request");
                    }

                    return (IPricer)McPricer.WithDetails(mcConfig);
                }

                throw new Exception(string.Format("Unknown numerical method !"));
            }));
        }