import umontreal.iro.lecuyer.rng.*; import umontreal.iro.lecuyer.hups.*; import umontreal.iro.lecuyer.randvar.NormalGen; import umontreal.iro.lecuyer.probdist.NormalDist; import umontreal.iro.lecuyer.stat.Tally; import umontreal.iro.lecuyer.util.Chrono; //AsianQMC hérite de la classe Asian public class AsianQMC extends Asian { public AsianQMC (double r, double sigma, double strike, double s0, int s, double[] zeta) { // Appelle le constructeur de Asian super (r, sigma, strike, s0, s, zeta); } // On fait m randomisations indépendantes du PointSet p en utilisant // un stream quelconque. Pour chacune de ces randomisations, une // simulation sur chaque point de p, et on ajouter la moyenne de // tout cela au collecteur statistique. public void simulateQMC (int m, PointSet p, PointSetRandomization r, Tally statQMC) { Tally statValue = new Tally ("stat on value of Asian option"); PointSetIterator stream = p.iterator (); for (int j = 0; j < m; j++) { r.randomize (p); stream.resetStartStream(); // Appelle la méthode dans Asian simulateRuns (p.getNumPoints(), stream, statValue); statQMC.add (statValue.average()); } } public static void main (String[] args) { // Lecture des paramètres entrés sur la ligne de commande if (args.length != 2) { System.err.println ("Usage: java AsianQMC "); System.exit (1); } int m = Integer.parseInt (args[0]); int s = 12; double[] zeta = new double[s+1]; for (int j=0; j<=s; j++) zeta[j] = (double)j / (double)s; AsianQMC process = new AsianQMC (0.05, 0.5, 100.0, 100.0, s, zeta); Tally statValue = new Tally ("value of Asian option"); Tally statQMC = new Tally ("QMC averages for Asian option"); Chrono timer = new Chrono(); int n = 100000; System.out.println ("Ordinary MC:\n"); process.simulateRuns (n, new MRG32k3a(), statValue); statValue.setConfidenceIntervalStudent(); System.out.println (statValue.report (0.95, 3)); System.out.println ("Total CPU time: " + timer.format()); double varMC = statValue.variance(); double cpuMC = timer.getSeconds() / n; // CPU seconds per run. System.out.println ("------------------------\n"); // Si l'usager a choisi "sobol", c'est ici... if (args[1].equals ("sobol")) { timer.init(); DigitalNet p = new SobolSequence (16, 31, s); // 2^{16} points. // Left matrix scramble followed by a random digital shift PointSetRandomization r = new LMScrambleShift (new MRG32k3a()); // On peut aussi effectuer le random digital shift seul //PointSetRandomization r = new RandomShift (new MRG32k3a()); n = p.getNumPoints(); process.simulateQMC (m, p, r, statQMC); System.out.println ("QMC with Sobol point set with " + n + " points and random digital shift:\n"); statQMC.setConfidenceIntervalStudent(); System.out.println (statQMC.report (0.95, 3)); System.out.println ("Total CPU time: " + timer.format() + "\n"); double varQMC = p.getNumPoints() * statQMC.variance(); double cpuQMC = timer.getSeconds() / (m * n); System.out.printf ("Variance ratio: %9.4g%n", varMC/varQMC); System.out.printf ("Efficiency ratio: %9.4g%n", (varMC * cpuMC) / (varQMC * cpuQMC)); } //S'il a choisi la methode "korobov" c'est ici sinon. else if (args[1].equals ("korobov")) { timer.init(); KorobovLattice k = new KorobovLattice ((int)Math.pow (2, 16), 47, s); PointSetRandomization r = new RandomShift (new MRG32k3a()); PointSet kb = new BakerTransformedPointSet (k); n = k.getNumPoints(); process.simulateQMC (m, kb, r, statQMC); System.out.println ("QMC with korobov lattice with " + n + " points and baker transformation:\n"); statQMC.setConfidenceIntervalStudent(); System.out.println (statQMC.report (0.95, 3)); System.out.println ("Total CPU time: " + timer.format() + "\n"); double varQMC = k.getNumPoints() * statQMC.variance(); double cpuQMC = timer.getSeconds() / (m * n); System.out.printf ("Variance ratio: %9.4g%n", varMC/varQMC); System.out.printf ("Efficiency ratio: %9.4g%n", (varMC * cpuMC) / (varQMC * cpuQMC)); } } }