SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
AsianGBM2.java
1package tutorial;
2
3import umontreal.ssj.rng.*;
4
5import java.io.IOException;
6
7import umontreal.ssj.probdist.NormalDist;
8import umontreal.ssj.mcqmctools.*;
9import umontreal.ssj.stat.Tally;
10import umontreal.ssj.util.*;
11
12// Same as AsianGBM, but this version implements the interface MonteCarloModelDouble.
13public class AsianGBM2 implements MonteCarloModelDouble {
14 double strike; // Strike price.
15 int s; // Number of observation times.
16 double discount; // Discount factor exp(-r * zeta[t]).
17 double[] muDelta; // Differences * (r - sigma^2/2).
18 double[] sigmaSqrtDelta; // Square roots of differences * sigma.
19 double[] logS; // Log of the GBM process: logS[t] = log (S[t]).
20
21 // Array zeta[0..s] must contain zeta[0]=0.0, plus the s observation times.
22 public AsianGBM2(double r, double sigma, double strike, double s0, int s, double[] zeta) {
23 this.strike = strike;
24 this.s = s;
25 discount = Math.exp(-r * zeta[s]);
26 double mu = r - 0.5 * sigma * sigma;
27 muDelta = new double[s];
28 sigmaSqrtDelta = new double[s];
29 logS = new double[s + 1];
30 double delta;
31 for (int j = 0; j < s; j++) {
32 delta = zeta[j + 1] - zeta[j];
33 muDelta[j] = mu * delta;
34 sigmaSqrtDelta[j] = sigma * Math.sqrt(delta);
35 }
36 logS[0] = Math.log(s0);
37 }
38
39 // Generates the process S.
40 public void simulate(RandomStream stream) {
41 for (int j = 0; j < s; j++)
42 logS[j + 1] = logS[j] + muDelta[j] + sigmaSqrtDelta[j] * NormalDist.inverseF01(stream.nextDouble());
43 }
44
45 // Computes and returns the discounted option payoff.
46 public double getPerformance() {
47 double average = 0.0; // Average of the GBM process.
48 for (int j = 1; j <= s; j++)
49 average += Math.exp(logS[j]);
50 average /= s;
51 if (average > strike)
52 return discount * (average - strike);
53 else
54 return 0.0;
55 }
56
57 public int getDimension() {
58 return s;
59 }
60
61 public String toString() {
62 return "Asian option under GBM, for testing";
63 }
64
65 public String getTag() {
66 return "AsianGBM";
67 }
68
69 public static void main(String args[]) throws IOException {
70 int s = 12;
71 double[] zeta = new double[s + 1];
72 zeta[0] = 0.0;
73 for (int j = 1; j <= s; j++)
74 zeta[j] = (double) j / (double) s;
75 AsianGBM2 model = new AsianGBM2(0.05, 0.5, 100.0, 100.0, s, zeta);
76 Tally statValue = new Tally("Stats on value of Asian option");
77 Chrono timer = new Chrono();
78 int n = 100000;
79 MonteCarloExperiment.simulateRuns(model, n, new LFSR113(), statValue);
81 System.out.println(statValue.report(0.95, 3));
82 System.out.println("Total CPU time: " + timer.format() + "\n");
83 }
84}
String getTag()
Returns a short model name (tag) to be used in reports.
double getPerformance()
Recovers and returns the realization of the performance measure, of type double.
void simulate(RandomStream stream)
Simulates the model for one run.
String toString()
Returns a description of the model and its parameters.
Provides generic tools to perform simple Monte Carlo experiments with a simulation model that impleme...
static void simulateRuns(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue)
Performs n simulation runs of model using stream and collects statistics in statValue.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
Extends RandomStreamBase using a composite linear feedback shift register (LFSR) (or Tausworthe) RNG ...
Definition LFSR113.java:47
A subclass of StatProbe.
Definition Tally.java:47
void setConfidenceIntervalStudent()
Indicates that a confidence interval on the true mean, based on the normality assumption,...
Definition Tally.java:582
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397
String format()
Converts the CPU time used by the program since its last call to init for this AbstractChrono to a St...
The Chrono class extends the umontreal.ssj.util.AbstractChrono class and computes the CPU time for th...
Definition Chrono.java:37
An interface for a very simple simulation model for which Monte Carlo (MC) and RQMC experiments are t...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...