Esempio n. 1
0
        private Sample <IPath> next(bool antithetic)
        {
            Sample <List <double> > sequence_ =
                antithetic
            ? generator_.lastSequence()
            : generator_.nextSequence();

            if (brownianBridge_)
            {
                bb_.transform(sequence_.value, temp_);
            }
            else
            {
                temp_ = new List <double>(sequence_.value);
            }

            next_.weight = sequence_.weight;

            Path path = (Path)next_.value;

            path.setFront(process_.x0());

            for (int i = 1; i < path.length(); i++)
            {
                double t  = timeGrid_[i - 1];
                double dt = timeGrid_.dt(i - 1);
                path[i] = process_.evolve(t, path[i - 1], dt,
                                          antithetic
                                      ? -temp_[i - 1]
                                      : temp_[i - 1]);
            }
            return(next_);
        }
Esempio n. 2
0
 protected BinomialTree(StochasticProcess1D process, double end, int steps)
     : base(steps + 1)
 {
     x0_           = process.x0();
     dt_           = end / steps;
     driftPerStep_ = process.drift(0.0, x0_) * dt_;
 }
Esempio n. 3
0
        public TrinomialTree(StochasticProcess1D process,
                             TimeGrid timeGrid,
                             bool isPositive /*= false*/)
            : base(timeGrid.size())
        {
            branchings_ = new List <Branching>();
            dx_         = new InitializedList <double>(1);
            timeGrid_   = timeGrid;
            x0_         = process.x0();

            int nTimeSteps = timeGrid.size() - 1;
            int jMin       = 0;
            int jMax       = 0;

            for (int i = 0; i < nTimeSteps; i++)
            {
                double t  = timeGrid[i];
                double dt = timeGrid.dt(i);

                //Variance must be independent of x
                double v2 = process.variance(t, 0.0, dt);
                double v  = Math.Sqrt(v2);
                dx_.Add(v * Math.Sqrt(3.0));

                Branching branching = new Branching();
                for (int j = jMin; j <= jMax; j++)
                {
                    double x    = x0_ + j * dx_[i];
                    double m    = process.expectation(t, x, dt);
                    int    temp = (int)(Math.Floor((m - x0_) / dx_[i + 1] + 0.5));

                    if (isPositive)
                    {
                        while (x0_ + (temp - 1) * dx_[i + 1] <= 0)
                        {
                            temp++;
                        }
                    }

                    double e  = m - (x0_ + temp * dx_[i + 1]);
                    double e2 = e * e;
                    double e3 = e * Math.Sqrt(3.0);

                    double p1 = (1.0 + e2 / v2 - e3 / v) / 6.0;
                    double p2 = (2.0 - e2 / v2) / 3.0;
                    double p3 = (1.0 + e2 / v2 + e3 / v) / 6.0;

                    branching.add(temp, p1, p2, p3);
                }
                branchings_.Add(branching);

                jMin = branching.jMin();
                jMax = branching.jMax();
            }
        }
Esempio n. 4
0
        public FdmSimpleProcess1DMesher(int size,
                                        StochasticProcess1D process,
                                        double maturity, int tAvgSteps = 10,
                                        double epsilon        = 0.0001,
                                        double?mandatoryPoint = null)
            : base(size)
        {
            locations_ = new InitializedList <double>(locations_.Count, 0.0);
            for (int l = 1; l <= tAvgSteps; ++l)
            {
                double t = (maturity * l) / tAvgSteps;

                double mp = (mandatoryPoint != null) ? mandatoryPoint.Value
                        : process.x0();

                double qMin = Math.Min(Math.Min(mp, process.x0()),
                                       process.evolve(0, process.x0(), t,
                                                      new InverseCumulativeNormal().value(epsilon)));
                double qMax = Math.Max(Math.Max(mp, process.x0()),
                                       process.evolve(0, process.x0(), t,
                                                      new InverseCumulativeNormal().value(1 - epsilon)));

                double dp = (1 - 2 * epsilon) / (size - 1);
                double p  = epsilon;
                locations_[0] += qMin;

                for (int i = 1; i < size - 1; ++i)
                {
                    p             += dp;
                    locations_[i] += process.evolve(0, process.x0(), t,
                                                    new InverseCumulativeNormal().value(p));
                }
                locations_[locations_.Count - 1] += qMax;
            }
            locations_ = locations_.Select(x => x / tAvgSteps).ToList();
            for (int i = 0; i < size - 1; ++i)
            {
                dminus_[i + 1] = dplus_[i] = locations_[i + 1] - locations_[i];
            }

            dplus_[dplus_.Count - 1] = null;
            dminus_[0] = null;
        }