コード例 #1
0
        public void TestMethod4()
        {
            DateTime baseDate       = new DateTime(2015, 2, 1);
            int      exerciseCount  = 1;
            int      resetCount     = 6;
            double   swapRate       = 0.01;
            double   divideInterval = 60;

            Debug.Assert(resetCount >= exerciseCount);
            DateTime[]      exerciseDates   = Enumerable.Range(0, exerciseCount).Select(x => baseDate.AddMonths(x * 6)).ToArray();
            DateTime[]      resetDates      = Enumerable.Range(1, resetCount + 1).Select(x => baseDate.AddMonths(x * 6).AddDays(2)).ToArray();
            double[]        divideIntervals = resetDates.Select(x => divideInterval).ToArray();
            List <Cashflow> cashflows       = new List <Cashflow>();

            for (int i = 0; i < resetDates.Length - 1; ++i)
            {
                cashflows.Add(new Cashflow(resetDates[i], resetDates[i + 1], swapRate));
            }
            SimpleBermudanSwaption sbs = new SimpleBermudanSwaption();

            sbs.DivideTimeIntervals(baseDate, exerciseDates, cashflows.ToArray(), divideIntervals);
            sbs.OutputCsvExerciseDates("BermudanSwaptionExerciseDates.csv");
            sbs.OutputCsvCashflows("BermudanSwaptionCashflows.csv");
            sbs.OutputCsvTimeIntervals("BermudanSwaptionTimeIntervals.csv");
            sbs.SetTreeTimes();
            double r = 0.01;

            double[]  times      = sbs.GetTreeTimes();
            double[]  bondPrices = times.Select(x => Math.Exp(-r * x)).ToArray();
            double[]  a          = times.Select(x => 0.005).ToArray();
            double[]  sigma      = times.Select(x => 0.5).ToArray();
            Stopwatch stopWatch  = new Stopwatch();

            stopWatch.Start();
            sbs.InitializeTree(a, sigma);
            sbs.SetBondPrices(bondPrices);
            sbs.FitToBondPrices();
            sbs.SetPayerOrReceiver(false);
            Console.WriteLine("PV \t\t ={0}", sbs.ComputePV());
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0003));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            stopWatch.Stop();
            Console.WriteLine("{0}ms", stopWatch.ElapsedMilliseconds);
            sbs._Tree.OutputCsvTreeBackBones("BermudanSwaptionTreeBackBones.csv");
            sbs._Tree.OutputCsvTreeNodes("BermudanSwaptionTreeNodes.csv");
        }
コード例 #2
0
        public void TestMethod1()
        {
            DateTime baseDate       = new DateTime(2015, 2, 1);
            double   divideInterval = 60;

            int cashflowNumber      = 6;
            int resetIntervalMonths = 6;

            for (int i = 0; i < cashflowNumber - 1; ++i)
            {
                DateTime[] exerciseDates;
                Cashflow[] cashflows;
                CalibrationHelper.GetSwaptionCondition(baseDate.AddMonths(resetIntervalMonths * (i + 1))
                                                       , resetIntervalMonths, cashflowNumber - i, out exerciseDates, out cashflows);
                double[] divideIntervals   = cashflows.Select(x => divideInterval).ToArray();
                SimpleBermudanSwaption sbs = new SimpleBermudanSwaption();
                sbs.DivideTimeIntervals(baseDate, exerciseDates, cashflows, divideIntervals);
                sbs.OutputCsvExerciseDates(string.Format("BermudanSwaptionExerciseDates{0}.csv", i));
                sbs.OutputCsvCashflows(string.Format("BermudanSwaptionCashflows{0}.csv", i));
                sbs.OutputCsvTimeIntervals(string.Format("BermudanSwaptionTimeIntervals{0}.csv", i));
            }
        }
コード例 #3
0
        public void TestMethod1()
        {
            DateTime baseDate       = new DateTime(2015, 2, 1);
            int      exerciseCount  = 2;
            int      resetCount     = 6;
            double   swapRate       = 0.01;
            double   divideInterval = 30;

            Debug.Assert(resetCount >= exerciseCount);
            DateTime[] exerciseDates   = Enumerable.Range(1, exerciseCount).Select(x => baseDate.AddMonths(x * 6)).ToArray();
            DateTime[] resetDates      = Enumerable.Range(1, resetCount + 1).Select(x => baseDate.AddMonths(x * 6).AddDays(2)).ToArray();
            double[]   divideIntervals = resetDates.Select(x => divideInterval).ToArray();
            //divideIntervals[0] *= 0.1 * divideIntervals[0];
            //divideIntervals[1] *= 0.1 * divideIntervals[1];
            List <Cashflow> cashflows = new List <Cashflow>();

            for (int i = 0; i < resetDates.Length - 1; ++i)
            {
                cashflows.Add(new Cashflow(resetDates[i], resetDates[i + 1], swapRate));
            }
            SimpleBermudanSwaption sbs = new SimpleBermudanSwaption();

            sbs.DivideTimeIntervals(baseDate, exerciseDates, cashflows.ToArray(), divideIntervals);
            sbs.OutputCsvExerciseDates("BermudanSwaptionExerciseDates.csv");
            sbs.OutputCsvCashflows("BermudanSwaptionCashflows.csv");
            sbs.OutputCsvTimeIntervals("BermudanSwaptionTimeIntervals.csv");
            sbs.SetTreeTimes();
            double r = 0.01;

            double[]  times      = sbs.GetTreeTimes();
            double[]  bondPrices = times.Select(x => Math.Exp(-r * x)).ToArray();
            double[]  a          = times.Select(x => 0.005).ToArray();
            double[]  sigma      = times.Select(x => 0.5).ToArray();
            Stopwatch stopWatch  = new Stopwatch();

            stopWatch.Start();
            sbs.InitializeTree(a, sigma);
            sbs.SetBondPrices(bondPrices);
            sbs.FitToBondPrices();
            sbs.SetPayerOrReceiver(true);
            Console.WriteLine("PV \t\t ={0}", sbs.ComputePV());
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0003));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            stopWatch.Stop();
            Console.WriteLine("{0}ms", stopWatch.ElapsedMilliseconds);

            Console.WriteLine("InitializeTree(double, double)");
            stopWatch.Reset();
            stopWatch.Start();
            sbs.InitializeTree(a[0], sigma[0]);
            sbs.SetBondPrices(bondPrices);
            sbs.FitToBondPrices();
            sbs.SetPayerOrReceiver(true);
            Console.WriteLine("PV \t\t ={0}", sbs.ComputePV());
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0003));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0002));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0001));
            Console.WriteLine("PVShifted \t ={0}", sbs.ComputeSigmaShiftedPV(0.0000));
            stopWatch.Stop();
            Console.WriteLine("{0}ms", stopWatch.ElapsedMilliseconds);
            sbs._Tree.OutputCsvTreeBackBones("BermudanSwaptionTreeBackBones.csv");
            sbs._Tree.OutputCsvTreeNodes("BermudanSwaptionTreeNodes.csv");

            SimpleBermudanSwaption sbs2 = new SimpleBermudanSwaption();

            sbs2.DivideTimeIntervals(baseDate, exerciseDates, cashflows.ToArray()
                                     , divideIntervals.Select(x => 0.5 * x).ToArray());
            sbs2.SetTreeTimes();
            sbs2.SetBondPrices(sbs2.GetTreeTimes().Select(x => Math.Exp(-r * x)).ToArray());
            sbs2.SetPayerOrReceiver(true);
            using (var sw = new System.IO.StreamWriter("PVSigma.csv", false))
            {
                double unit = 0.01;
                sw.WriteLine("sigma,pv,pv2");
                for (int i = 1; i <= 1D / unit; ++i)
                {
                    sbs.InitializeTree(a[0], i * unit);
                    sbs.FitToBondPrices();
                    sbs2.InitializeTree(a[0], i * unit);
                    sbs2.FitToBondPrices();
                    sw.WriteLine("{0},{1},{2}", i * unit, sbs.ComputePV(), sbs2.ComputePV());
                }
            }
        }
コード例 #4
0
        public void TestMethod2()
        {
            DateTime baseDate       = new DateTime(2015, 2, 1);
            double   divideInterval = 30;
            double   r = 0.01;

            /// キャリブレーション用ツリーによるヨーロピアンスワップション評価オブジェクトの用意
            int cashflowNumber      = 1;
            int resetIntervalMonths = 6;

            SimpleBermudanSwaption[] sbss = new SimpleBermudanSwaption[cashflowNumber];
            for (int i = 1; i <= cashflowNumber; ++i)
            {
                DateTime[] exerciseDates;
                Cashflow[] cashflows;
                CalibrationHelper.GetSwaptionCondition(baseDate.AddMonths(resetIntervalMonths * i)
                                                       , resetIntervalMonths, cashflowNumber - i + 1, out exerciseDates, out cashflows);
                /// スワップレートの設定
                List <double> bondPricesAtCashflowDates = cashflows.Select(x => Math.Exp(-r * (x.ResetDate - baseDate).Days / 365D)).ToList();
                bondPricesAtCashflowDates.Add(Math.Exp(-r * (cashflows.Last().SettlementDate - baseDate).Days / 365D));
                double[] bondPricesForSwapRate    = bondPricesAtCashflowDates.ToArray();
                double[] yearFractionsForSwapRate = bondPricesForSwapRate.Select(x => resetIntervalMonths / 12D).ToArray();
                double   swapRate = CalibrationHelper.GetForwardSwapRate(bondPricesForSwapRate, yearFractionsForSwapRate);
                Console.WriteLine("SwapRate={0}", swapRate);
                foreach (Cashflow cf in cashflows)
                {
                    cf.SwapRate = swapRate;
                }
                /// スワップションオブジェクトの生成
                double[] divideIntervals = cashflows.Select(x => divideInterval).ToArray();
                divideIntervals[0] /= 3;
                SimpleBermudanSwaption sbs = new SimpleBermudanSwaption();
                sbs.DivideTimeIntervals(baseDate, exerciseDates, cashflows, divideIntervals);
                double[] times      = sbs.GetTreeTimes();
                double[] bondPrices = times.Select(x => Math.Exp(-r * x)).ToArray();
                sbs.SetBondPrices(bondPrices);
                sbs.SetPayerOrReceiver(true);
                sbss[i - 1] = sbs;
            }
            /// ダミーの市場価格
            /// 本当はボラから計算する必要がある。
            double[] PVs = sbss.Select(x => 0.01D).ToArray();
            double   a, sigma;

            a     = 0.005;
            sigma = 0.01;
            // a, sigmaでcalibration
            //CalibrationHelper.CalibrateToSwaptionValues(PVs, sbss, 0, 0.2D, out a, out sigma);
            /// sigmaのみでcalibration
            CalibrationHelper.CalibrateToSwaptionValues(PVs, sbss, a, sigma, out sigma);
            Console.WriteLine("a={0}, sigma={1}", a, sigma);
            for (int i = 0; i < sbss.Length; ++i)
            {
                sbss[i].OutputCsvCashflows(string.Format("ESwaptionCashflow{0}.csv", i));
                sbss[i].OutputCsvExerciseDates(string.Format("ESwaptionExerciseDates{0}.csv", i));
                Console.WriteLine("{0}, Input : {1}, Fitted : {2}", i, PVs[i], sbss[i]._Tree._TreeNodes[0][0].ContingentClaimValue);
            }
            /// 目的関数のsigma依存性を見る
            CalibrationHelper.OutputCsvCalibrationObjectiveValues("Js.csv", PVs, sbss
                                                                  , Enumerable.Range(1, 100).Select(x => 0.01D * x).ToArray()
                                                                  , Enumerable.Range(1, 100).Select(x => 0.01D * x).ToArray());
        }