private Sample <IPath> next(bool antithetic) { if (brownianBridge_) { Utils.QL_FAIL("Brownian bridge not supported"); return(null); } Sample <List <double> > sequence_ = antithetic ? generator_.lastSequence() : generator_.nextSequence(); int m = process_.size(); int n = process_.factors(); MultiPath path = (MultiPath)next_.value; Vector asset = process_.initialValues(); for (int j = 0; j < m; j++) { path[j].setFront(asset[j]); } Vector temp; next_.weight = sequence_.weight; TimeGrid timeGrid = path[0].timeGrid(); double t, dt; for (int i = 1; i < path.pathSize(); i++) { int offset = (i - 1) * n; t = timeGrid[i - 1]; dt = timeGrid.dt(i - 1); if (antithetic) { temp = -1 * new Vector(sequence_.value.GetRange(offset, n)); } else { temp = new Vector(sequence_.value.GetRange(offset, n)); } asset = process_.evolve(t, asset, dt, temp); for (int j = 0; j < m; j++) { path[j][i] = asset[j]; } } return(next_); }
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"); }
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"); } }
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); } } }
public override void Reset() { PseudoRandom rsg = new PseudoRandom(); _pg = rsg.make_sequence_generator(_process.size() * (_grid.size() - 1), _seed) as MultiPathGenerator <InverseCumulativeRsg <RandomSequenceGenerator <MersenneTwisterUniformRng>, InverseCumulativeNormal> >; }