/*! Returns an approximation of the covariance defined as
         *  \f$ \sigma(t_0, \mathbf{x}_0)^2 \Delta t \f$. */
        public Matrix covariance(StochasticProcess process, double t0, Vector x0, double dt)
        {
            Matrix sigma  = process.diffusion(t0, x0);
            Matrix result = sigma * Matrix.transpose(sigma) * dt;

            return(result);
        }
Example #2
0
 protected MCVanillaEngine(StochasticProcess process, int?timeSteps, int?timeStepsPerYear, bool brownianBridge,
                           bool antitheticVariate, bool controlVariate, int requiredSamples, double requiredTolerance,
                           int maxSamples, ulong seed)
     : base(process, timeSteps, timeStepsPerYear, brownianBridge, antitheticVariate, controlVariate, requiredSamples,
            requiredTolerance, maxSamples, seed)
 {
 }
 protected MCLongstaffSchwartzEngine(StochasticProcess process, int timeSteps, int timeStepsPerYear,
                                     bool brownianBridge, bool antitheticVariate, bool controlVariate,
                                     int requiredSamples, double requiredTolerance, int maxSamples,
                                     ulong seed, int nCalibrationSamples) :
     base(process, timeSteps, timeStepsPerYear, brownianBridge, antitheticVariate, controlVariate,
          requiredSamples, requiredTolerance, maxSamples, seed, nCalibrationSamples)
 {
 }
        public MultiPathGeneratorMersenneTwister(QLNet.StochasticProcess process, TimeGrid grid, ulong seed, bool antitheticSampling)
        {
            _process            = process;
            _grid               = grid;
            _seed               = seed;
            _antitheticSampling = antitheticSampling;
            _antitheticVariate  = true;

            Reset();
        }
        public DiscretizedVanillaOption(VanillaOption.Arguments args, StochasticProcess process, TimeGrid grid) {
            arguments_ = args;

            stoppingTimes_ = new InitializedList<double>(args.exercise.dates().Count);
            for (int i=0; i<stoppingTimes_.Count; ++i) {
                stoppingTimes_[i] = process.time(args.exercise.date(i));
                if (!grid.empty()) {
                    // adjust to the given grid
                    stoppingTimes_[i] = grid.closestTime(stoppingTimes_[i]);
                }
            }
        }
Example #6
0
 // constructors
 public PathGenerator(StochasticProcess process, double length, int timeSteps, GSG generator, bool brownianBridge)
 {
     brownianBridge_ = brownianBridge;
     generator_      = generator;
     dimension_      = generator_.dimension();
     timeGrid_       = new TimeGrid(length, timeSteps);
     process_        = process as StochasticProcess1D;
     next_           = new Sample <IPath>(new Path(timeGrid_), 1.0);
     temp_           = new InitializedList <double>(dimension_);
     bb_             = new BrownianBridge(timeGrid_);
     Utils.QL_REQUIRE(dimension_ == timeSteps, () =>
                      "sequence generator dimensionality (" + dimension_ + ") != timeSteps (" + timeSteps + ")");
 }
 public MakeMCGenericScriptInstrument(StochasticProcess process)
 {
     process_        = process;
     antithetic_     = true;
     steps_          = null;
     stepsPerYear_   = null;
     samples_        = null;
     maxSamples_     = null;
     tolerance_      = null;
     brownianBridge_ = false;
     seed_           = 0;
     creditTS_       = null;
 }
 public MCGenericScriptInstrument(StochasticProcess process,
                                  bool antithetic,
                                  int?steps,
                                  int?stepsPerYear,
                                  int?samples,
                                  int?maxSamples,
                                  double?tolerance,
                                  bool brownianBridge,
                                  ulong seed,
                                  Handle <DefaultProbabilityTermStructure> creditTS = null)
     : base(process, antithetic, steps, stepsPerYear, samples, maxSamples, tolerance, brownianBridge, seed, creditTS)
 {
 }
Example #9
0
        public MultiPathGenerator(StochasticProcess process, TimeGrid times, GSG generator, bool brownianBridge)
        {
            brownianBridge_ = brownianBridge;
            process_        = process;
            generator_      = generator;
            next_           = new Sample <IPath>(new MultiPath(process.size(), times), 1.0);

            Utils.QL_REQUIRE(generator_.dimension() == process.factors() * (times.size() - 1), () =>
                             "dimension (" + generator_.dimension()
                             + ") is not equal to ("
                             + process.factors() + " * " + (times.size() - 1)
                             + ") the number of factors "
                             + "times the number of time steps");
            Utils.QL_REQUIRE(times.size() > 1, () => "no times given");
        }
Example #10
0
        public DiscretizedVanillaOption(VanillaOption.Arguments args, StochasticProcess process, TimeGrid grid)
        {
            arguments_ = args;

            stoppingTimes_ = new InitializedList <double>(args.exercise.dates().Count);
            for (int i = 0; i < stoppingTimes_.Count; ++i)
            {
                stoppingTimes_[i] = process.time(args.exercise.date(i));
                if (!grid.empty())
                {
                    // adjust to the given grid
                    stoppingTimes_[i] = grid.closestTime(stoppingTimes_[i]);
                }
            }
        }
Example #11
0
        public PathGenerator(StochasticProcess process, TimeGrid timeGrid, GSG generator, bool brownianBridge)
        {
            brownianBridge_ = brownianBridge;
            generator_      = generator;
            dimension_      = generator_.dimension();
            timeGrid_       = timeGrid;
            process_        = process as StochasticProcess1D;
            next_           = new Sample <IPath>(new Path(timeGrid_), 1.0);
            temp_           = new InitializedList <double>(dimension_);
            bb_             = new BrownianBridge(timeGrid_);

            if (dimension_ != timeGrid_.size() - 1)
            {
                throw new Exception("sequence generator dimensionality (" + dimension_
                                    + ") != timeSteps (" + (timeGrid_.size() - 1) + ")");
            }
        }
Example #12
0
        public DiscretizedBarrierOption(BarrierOption.Arguments args, StochasticProcess process, TimeGrid grid = null)
        {
            arguments_ = args;
            vanilla_   = new DiscretizedVanillaOption(arguments_, process, grid);

            Utils.QL_REQUIRE(args.exercise.dates().Count > 0, () => "specify at least one stopping date");

            stoppingTimes_ = new InitializedList <double>(args.exercise.dates().Count);
            for (int i = 0; i < stoppingTimes_.Count; ++i)
            {
                stoppingTimes_[i] = process.time(args.exercise.date(i));
                if (grid != null && !grid.empty())
                {
                    // adjust to the given grid
                    stoppingTimes_[i] = grid.closestTime(stoppingTimes_[i]);
                }
            }
        }
Example #13
0
        public MultiPathGenerator(StochasticProcess process, TimeGrid times, GSG generator, bool brownianBridge)
        {
            brownianBridge_ = brownianBridge;
            process_        = process;
            generator_      = generator;
            next_           = new Sample <IPath>(new MultiPath(process.size(), times), 1.0);

            if (generator_.dimension() != process.factors() * (times.size() - 1))
            {
                throw new Exception("dimension (" + generator_.dimension()
                                    + ") is not equal to ("
                                    + process.factors() + " * " + (times.size() - 1)
                                    + ") the number of factors "
                                    + "times the number of time steps");
            }
            if (!(times.size() > 1))
            {
                throw new Exception("no times given");
            }
        }
Example #14
0
        public void testMultiple(StochasticProcess process,
                                 string tag,
                                 double[] expected, 
                                 double[] antithetic )
        {
            ulong seed = 42;
            double length = 10;
            int timeSteps = 12;
            int assets = process.size();

            var rsg = (InverseCumulativeRsg<RandomSequenceGenerator<MersenneTwisterUniformRng>
                                                                    ,InverseCumulativeNormal>)
                       new PseudoRandom().make_sequence_generator(timeSteps*assets, seed);

            MultiPathGenerator<IRNG> generator=new MultiPathGenerator<IRNG>(process,
                                                                            new TimeGrid(length, timeSteps),
                                                                            rsg, false);
            int i;
            for (i=0; i<100; i++)
                generator.next();

            Sample<MultiPath> sample = generator.next();
            Vector calculated = new Vector(assets);
            double error, tolerance = 2.0e-7;

            for (int j=0; j<assets; j++)
                calculated[j] = sample.value[j].back() ;

            for (int j=0; j<assets; j++) {
                error = Math.Abs(calculated[j]-expected[j]);
                if (error > tolerance) {
                    Assert.Fail("using " + tag + " process "
                                + "(" + j+1 + " asset:)\n"
                                //+ std::setprecision(13)
                                + "    calculated: " + calculated[j] + "\n"
                                + "    expected:   " + expected[j] + "\n"
                                + "    error:      " + error + "\n"
                                + "    tolerance:  " + tolerance);
                }
            }

            sample = generator.antithetic();
            for (int j=0; j<assets; j++)
                calculated[j] = sample.value[j].back();
            for (int j=0; j<assets; j++) {
                error = Math.Abs(calculated[j]-antithetic[j]);
                if (error > tolerance) {
                    Assert.Fail("using " + tag + " process "
                                + "(" + j+1 + " asset:)\n"
                                + "antithetic sample:\n"
                                //+ std::setprecision(13)
                                + "    calculated: " + calculated[j] + "\n"
                                + "    expected:   " + antithetic[j] + "\n"
                                + "    error:      " + error + "\n"
                                + "    tolerance:  " + tolerance);
                }
            }
        }
Example #15
0
 /*! Returns an approximation of the covariance defined as
     \f$ \sigma(t_0, \mathbf{x}_0)^2 \Delta t \f$. */
 public Matrix covariance(StochasticProcess process, double t0, Vector x0, double dt)
 {
     Matrix sigma = process.diffusion(t0, x0);
     Matrix result = sigma * Matrix.transpose(sigma) * dt;
     return result;
 }
 /*! Returns an approximation of the diffusion defined as
  *  \f$ \sigma(t_0, \mathbf{x}_0) \sqrt{\Delta t} \f$. */
 public Matrix diffusion(StochasticProcess process, double t0, Vector x0, double dt)
 {
     return(process.diffusion(t0, x0) * Math.Sqrt(dt));
 }
 /*! Returns an approximation of the drift defined as
  *  \f$ \mu(t_0, \mathbf{x}_0) \Delta t \f$. */
 public Vector drift(StochasticProcess process, double t0, Vector x0, double dt)
 {
     return(process.drift(t0, x0) * dt);
 }
Example #18
0
 /*! Returns an approximation of the diffusion defined as
     \f$ \sigma(t_0, \mathbf{x}_0) \sqrt{\Delta t} \f$. */
 public Matrix diffusion(StochasticProcess process, double t0, Vector x0, double dt)
 {
     return process.diffusion(t0, x0) * Math.Sqrt(dt);
 }
Example #19
0
 public DiscretizedDermanKaniBarrierOption(BarrierOption.Arguments args,
                                           StochasticProcess process, TimeGrid grid = null)
 {
     unenhanced_ = new DiscretizedBarrierOption(args, process, grid);
 }
Example #20
0
 /*! Returns an approximation of the drift defined as
     \f$ \mu(t_0, \mathbf{x}_0) \Delta t \f$. */
 public Vector drift(StochasticProcess process, double t0, Vector x0, double dt)
 {
     return process.drift(t0, x0)*dt;
 }