Beispiel #1
0
        public void C9_9_4_European()
        {
            var opt = _testOption;

            var k           = opt.T / numberOfSteps;
            var discounting = Math.Exp(-opt.r * k);

            var binParams = new CRRStrategy(opt.sig, opt.r, k);;  // Factory
            var bn        = new BinomialMethod(discounting, binParams, numberOfSteps);

            bn.ModifyLattice(opt.K);

            // Phase III: Backward Induction and compute option price
            Vector <double> RHS = new Vector <double>(bn.BasePyramidVector());

            if (binParams.BinomialType == BinomialType.Additive)
            {
                RHS[RHS.MinIndex] = opt.K * Math.Exp(numberOfSteps * binParams.DownValue);
                for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++)
                {
                    RHS[j] = RHS[j - 1] * Math.Exp(binParams.UpValue - binParams.DownValue);
                }
            }

            var pay = opt.PayoffVector(RHS);

            var pr = bn.GetPrice(pay);

            Assert.AreEqual(pr, 2.8307, 0.01);
        }
Beispiel #2
0
        public void C9_9_9_2_TheLeisenReimerMethod()
        {
            var opt           = _testOption;
            var numberOfSteps = 99;

            var k           = opt.T / numberOfSteps;
            var discounting = Math.Exp(-opt.r * k);

            var S = opt.K;

            var binParams = new LeisenReimerStrategy(numberOfSteps, S, opt.K, opt.T, opt.r, opt.sig);
            var bn        = new BinomialMethod(discounting, binParams, numberOfSteps);

            bn.ModifyLattice(S);

            // Phase III: Backward Induction and compute option price
            var RHS = new Vector <double>(bn.BasePyramidVector());
            var pay = opt.PayoffVector(RHS);
            var pr  = bn.GetPrice(pay);

            Assert.AreEqual(pr, 2.8307, 0.01);
        }
Beispiel #3
0
        public void C9_9_ExcelOutput()
        {
            var opt = _testOption;

            var steps = 50;
            var S     = opt.K;

            double k = opt.T / steps;

            double discounting = Math.Exp(-opt.r * k);

            var binParams = new CRRStrategy(opt.sig, opt.r, k);;  // Factory
            var bn        = new BinomialMethod(discounting, binParams, steps, opt.EarlyImpl);

            bn.ModifyLattice(opt.K);

            // Phase III: Backward Induction and compute option price
            Vector <double> RHS = new Vector <double>(bn.BasePyramidVector());

            if (binParams.BinomialType == BinomialType.Additive)
            {
                RHS[RHS.MinIndex] = S * Math.Exp(steps * binParams.DownValue);
                for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++)
                {
                    RHS[j] = RHS[j - 1] * Math.Exp(binParams.UpValue - binParams.DownValue);
                }
            }

            var pay = opt.PayoffVector(RHS);

            var pr = bn.GetPrice(pay);

            // Display lattice in Excel
            var file = Path.GetTempFileName();

            file = Path.ChangeExtension(file, "xlsx");
            ExcelMechanisms exl = new ExcelMechanisms(file);

            try
            {
                // Display in Excel; first create array of asset mesh points
                int             startIndex = 0;
                Vector <double> xarr       = new Vector <double>(steps + 1, startIndex);
                xarr[xarr.MinIndex] = 0.0;
                for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++)
                {
                    xarr[j] = xarr[j - 1] + k;
                }

                string sheetName = "Lattice";

                exl.printLatticeInExcel(bn.GetOptionLattice, xarr, sheetName);

                exl.Save();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Assert.AreEqual(pr, 3.0732, 0.01);
        }
Beispiel #4
0
        public void C9_9_7_PadeApproximation()
        {
            var opt = new Library.Binominal.Option
            {
                K    = 65,
                type = 1,
                T    = 0.25,
                r    = 0.08,
                sig  = 0.3
            };

            double PriceWithJR(int numSteps, int type)
            {
                opt.type = type;

                var    steps = numSteps;
                var    S     = 60;
                double k     = opt.T / steps;

                double discounting = Math.Exp(-opt.r * k);

                var binParams = new PadeJRStrategy(opt.sig, opt.r, k); // Factory
                var bn        = new BinomialMethod(discounting, binParams, steps);

                bn.ModifyLattice(S);

                // Phase III: Backward Induction and compute option price
                var RHS = new Vector <double>(bn.BasePyramidVector());

                var pay = opt.PayoffVector(RHS);

                return(bn.GetPrice(pay));
            }

            double PriceWithCRR(int numSteps, int type)
            {
                opt.type = type;

                var    steps = numSteps;
                var    S     = 60;
                double k     = opt.T / steps;

                double discounting = Math.Exp(-opt.r * k);

                var binParams = new PadeCRRStrategy(opt.sig, opt.r, k); // Factory
                var bn        = new BinomialMethod(discounting, binParams, steps);

                bn.ModifyLattice(S);

                // Phase III: Backward Induction and compute option price
                var RHS = new Vector <double>(bn.BasePyramidVector());

                var pay = opt.PayoffVector(RHS);

                return(bn.GetPrice(pay));
            }

            Assert.AreEqual(PriceWithCRR(100, 1), 2.1399, 0.01);
            Assert.AreEqual(PriceWithCRR(100, 2), 5.8527, 0.01);
            Assert.AreEqual(PriceWithCRR(200, 1), 2.1365, 0.01);
            Assert.AreEqual(PriceWithCRR(200, 2), 5.8494, 0.01);

            Assert.AreEqual(PriceWithJR(100, 1), 2.1386, 0.01);
            Assert.AreEqual(PriceWithJR(100, 2), 5.8516, 0.01);
            Assert.AreEqual(PriceWithJR(200, 1), 2.1344, 0.01);
            Assert.AreEqual(PriceWithJR(200, 2), 5.8473, 0.01);
        }