Пример #1
0
        public override Lattice tree(TimeGrid grid)
        {
            TermStructureFittingParameter phi = new TermStructureFittingParameter(termStructure());

            ShortRateDynamics numericDynamics =
                new Dynamics(phi, a(), sigma());

            TrinomialTree trinomial =
                new TrinomialTree(numericDynamics.process(), grid);
            ShortRateTree numericTree =
                new ShortRateTree(trinomial, numericDynamics, grid);

            TermStructureFittingParameter.NumericalImpl impl =
                (TermStructureFittingParameter.NumericalImpl)phi.implementation();
            impl.reset();
            double value = 1.0;
            double vMin  = -50.0;
            double vMax  = 50.0;

            for (int i = 0; i < (grid.size() - 1); i++)
            {
                double discountBond = termStructure().link.discount(grid[i + 1]);
                double xMin         = trinomial.underlying(i, 0);
                double dx           = trinomial.dx(i);
                Helper finder       = new Helper(i, xMin, dx, discountBond, numericTree);
                Brent  s1d          = new Brent();
                s1d.setMaxEvaluations(1000);
                value = s1d.solve(finder, 1e-7, value, vMin, vMax);
                impl.setvalue(grid[i], value);
            }
            return(numericTree);
        }
Пример #2
0
        public override Lattice tree(TimeGrid grid)
        {
            TermStructureFittingParameter phi             = new TermStructureFittingParameter(termStructure());
            ShortRateDynamics             numericDynamics = new Dynamics(phi, a(), sigma());
            TrinomialTree trinomial   = new TrinomialTree(numericDynamics.process(), grid);
            ShortRateTree numericTree = new ShortRateTree(trinomial, numericDynamics, grid);

            TermStructureFittingParameter.NumericalImpl impl =
                (TermStructureFittingParameter.NumericalImpl)phi.implementation();
            impl.reset();
            for (int i = 0; i < (grid.size() - 1); i++)
            {
                double discountBond = termStructure().link.discount(grid[i + 1]);
                Vector statePrices  = numericTree.statePrices(i);
                int    size         = numericTree.size(i);
                double dt           = numericTree.timeGrid().dt(i);
                double dx           = trinomial.dx(i);
                double x            = trinomial.underlying(i, 0);
                double value        = 0.0;
                for (int j = 0; j < size; j++)
                {
                    value += statePrices[j] * Math.Exp(-x * dt);
                    x     += dx;
                }
                value = Math.Log(value / discountBond) / dt;
                impl.setvalue(grid[i], value);
            }
            return(numericTree);
        }