Example #1
0
        public override void calculate(IPricingEngineResults r)
        {
            OneAssetOption.Results results = r as OneAssetOption.Results;
            Utils.QL_REQUIRE(results != null, () => "incorrect results type");

            double beginDate, endDate;
            int    dateNumber        = stoppingTimes_.Count;
            bool   lastDateIsResTime = false;
            int    firstIndex        = -1;
            int    lastIndex         = dateNumber - 1;
            bool   firstDateIsZero   = false;
            double firstNonZeroDate  = getResidualTime();

            double dateTolerance = 1e-6;
            int    j;

            if (dateNumber > 0)
            {
                Utils.QL_REQUIRE(getDividendTime(0) >= 0, () => "first date (" + getDividendTime(0) + ") cannot be negative");
                if (getDividendTime(0) < getResidualTime() * dateTolerance)
                {
                    firstDateIsZero = true;
                    firstIndex      = 0;
                    if (dateNumber >= 2)
                    {
                        firstNonZeroDate = getDividendTime(1);
                    }
                }

                if (Math.Abs(getDividendTime(lastIndex) - getResidualTime()) < dateTolerance)
                {
                    lastDateIsResTime = true;
                    lastIndex         = dateNumber - 2;
                }

                if (!firstDateIsZero)
                {
                    firstNonZeroDate = getDividendTime(0);
                }

                if (dateNumber >= 2)
                {
                    for (j = 1; j < dateNumber; j++)
                    {
                        Utils.QL_REQUIRE(getDividendTime(j - 1) < getDividendTime(j), () =>
                                         "dates must be in increasing order: " + getDividendTime(j - 1) +
                                         " is not strictly smaller than " + getDividendTime(j));
                    }
                }
            }

            double dt = getResidualTime() / (timeStepPerPeriod_ * (dateNumber + 1));

            // Ensure that dt is always smaller than the first non-zero date
            if (firstNonZeroDate <= dt)
            {
                dt = firstNonZeroDate / 2.0;
            }

            setGridLimits();
            initializeInitialCondition();
            initializeOperator();
            initializeBoundaryConditions();
            initializeModel();
            initializeStepCondition();

            prices_ = (SampledCurve)intrinsicValues_.Clone();
            if (lastDateIsResTime)
            {
                executeIntermediateStep(dateNumber - 1);
            }

            j = lastIndex;
            object temp;

            do
            {
                if (j == dateNumber - 1)
                {
                    beginDate = getResidualTime();
                }
                else
                {
                    beginDate = getDividendTime(j + 1);
                }

                if (j >= 0)
                {
                    endDate = getDividendTime(j);
                }
                else
                {
                    endDate = dt;
                }

                temp = prices_.values();
                model_.rollback(ref temp, beginDate, endDate, timeStepPerPeriod_, stepCondition_);
                prices_.setValues((Vector)temp);

                if (j >= 0)
                {
                    executeIntermediateStep(j);
                }
            } while (--j >= firstIndex);

            temp = prices_.values();
            model_.rollback(ref temp, dt, 0, 1, stepCondition_);
            prices_.setValues((Vector)temp);

            if (firstDateIsZero)
            {
                executeIntermediateStep(0);
            }

            results.value = prices_.valueAtCenter();
            results.delta = prices_.firstDerivativeAtCenter();
            results.gamma = prices_.secondDerivativeAtCenter();
            results.additionalResults["priceCurve"] = prices_;
        }
Example #2
0
        public override void calculate(IPricingEngineResults r)
        {
            OneAssetOption.Results results = r as OneAssetOption.Results;
            setGridLimits();
            initializeInitialCondition();
            initializeOperator();
            initializeBoundaryConditions();
            initializeStepCondition();

            List <IOperator>          operatorSet  = new List <IOperator>();
            List <Vector>             arraySet     = new List <Vector>();
            BoundaryConditionSet      bcSet        = new BoundaryConditionSet();
            StepConditionSet <Vector> conditionSet = new StepConditionSet <Vector>();

            prices_ = (SampledCurve)intrinsicValues_.Clone();

            controlPrices_   = (SampledCurve)intrinsicValues_.Clone();
            controlOperator_ = (TridiagonalOperator)finiteDifferenceOperator_.Clone();
            controlBCs_[0]   = BCs_[0];
            controlBCs_[1]   = BCs_[1];

            operatorSet.Add(finiteDifferenceOperator_);
            operatorSet.Add(controlOperator_);

            arraySet.Add(prices_.values());
            arraySet.Add(controlPrices_.values());

            bcSet.Add(BCs_);
            bcSet.Add(controlBCs_);

            conditionSet.Add(stepCondition_);
            conditionSet.Add(new NullCondition <Vector>());

            var model = new FiniteDifferenceModel <ParallelEvolver <CrankNicolson <TridiagonalOperator> > >(operatorSet, bcSet);

            object temp = arraySet;

            model.rollback(ref temp, getResidualTime(), 0.0, timeSteps_, conditionSet);
            arraySet = (List <Vector>)temp;

            prices_.setValues(arraySet[0]);
            controlPrices_.setValues(arraySet[1]);

            StrikedTypePayoff striked_payoff = payoff_ as StrikedTypePayoff;

            Utils.QL_REQUIRE(striked_payoff != null, () => "non-striked payoff given");

            double variance         = process_.blackVolatility().link.blackVariance(exerciseDate_, striked_payoff.strike());
            double dividendDiscount = process_.dividendYield().link.discount(exerciseDate_);
            double riskFreeDiscount = process_.riskFreeRate().link.discount(exerciseDate_);
            double spot             = process_.stateVariable().link.value();
            double forwardPrice     = spot * dividendDiscount / riskFreeDiscount;

            BlackCalculator black = new BlackCalculator(striked_payoff, forwardPrice, Math.Sqrt(variance), riskFreeDiscount);

            results.value = prices_.valueAtCenter()
                            - controlPrices_.valueAtCenter()
                            + black.value();
            results.delta = prices_.firstDerivativeAtCenter()
                            - controlPrices_.firstDerivativeAtCenter()
                            + black.delta(spot);
            results.gamma = prices_.secondDerivativeAtCenter()
                            - controlPrices_.secondDerivativeAtCenter()
                            + black.gamma(spot);
            results.additionalResults["priceCurve"] = prices_;
        }