SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
AsianGBM.java
1package tutorial;
2
3import java.io.IOException;
4import umontreal.ssj.rng.*;
5import umontreal.ssj.probdist.NormalDist;
6import umontreal.ssj.stat.Tally;
7import umontreal.ssj.util.*;
8
9public class AsianGBM {
10 double strike; // Strike price.
11 int d; // Number of observation times.
12 double discount; // Discount factor, exp(-r * zeta[d]).
13 double[] muDelta; // muDelta[j] = (zeta[j+1] - zeta[j]) * (r - sigma^2/2).
14 double[] sigmaSqrtDelta; // sqrt(zeta[j+1] - zeta[j]) * sigma.
15 double[] logS; // Log of the GBM process: logS[t] = log (S[t]).
16
17 // Array zeta[0..s] must contain zeta[0]=0.0, plus the d observation times.
18 // This constructor precomputes several quantities to speedup the simulation.
19 public AsianGBM(double r, double sigma, double strike, double s0, int d, double[] zeta) {
20 this.strike = strike;
21 this.d = d;
22 discount = Math.exp(-r * zeta[d]);
23 double mu = r - 0.5 * sigma * sigma;
24 muDelta = new double[d];
25 sigmaSqrtDelta = new double[d];
26 logS = new double[d + 1];
27 double delta;
28 for (int j = 0; j < d; j++) {
29 delta = zeta[j + 1] - zeta[j];
30 muDelta[j] = mu * delta;
31 sigmaSqrtDelta[j] = sigma * Math.sqrt(delta);
32 }
33 logS[0] = Math.log(s0);
34 }
35
36 // Generates the log of the process S.
37 public void generatePath(RandomStream stream) {
38 for (int j = 0; j < d; j++)
39 logS[j + 1] = logS[j] + muDelta[j] + sigmaSqrtDelta[j] * NormalDist.inverseF01(stream.nextDouble());
40 }
41
42 // Computes and returns the discounted option payoff.
43 public double getPayoff() {
44 double average = 0.0; // Average of the GBM process.
45 for (int j = 1; j <= d; j++)
46 average += Math.exp(logS[j]);
47 average /= d;
48 if (average > strike)
49 return discount * (average - strike);
50 else
51 return 0.0;
52 }
53
54 // Performs n simulation runs using stream and collects statistics in statValue.
55 public void simulateRuns(int n, RandomStream stream, Tally statValue) {
56 statValue.init();
57 for (int i = 0; i < n; i++) {
58 generatePath(stream);
59 statValue.add(getPayoff());
60 stream.resetNextSubstream();
61 }
62 }
63
64 public static void main(String[] args) throws IOException {
65 int d = 12;
66 double[] zeta = new double[d + 1];
67 zeta[0] = 0.0;
68 for (int j = 1; j <= d; j++)
69 zeta[j] = (double) j / (double) d;
70 AsianGBM process = new AsianGBM(0.05, 0.5, 100.0, 100.0, d, zeta);
71 Tally statValue = new Tally("Stats on value of Asian option");
72 Chrono timer = new Chrono();
73 int n = 100000;
74 process.simulateRuns(n, new LFSR113(), statValue);
76 System.out.println(statValue.report(0.95, 3));
77 System.out.println("Total CPU time: " + timer.format() + "\n");
78 }
79}
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
void init()
Initializes the statistical collector.
Definition Tally.java:91
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
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
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,...
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream: