SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DiscrepancyContainer.java
1/*
2 * Class: DiscrepancyContainer
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Adam L'Archevêque Gaudet
9 * @since January 2009
10
11 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24package umontreal.ssj.discrepancy;
25
26import umontreal.ssj.util.PrintfFormat;
27import umontreal.ssj.util.Num;
28import umontreal.ssj.charts.XYLineChart;
29import umontreal.ssj.functionfit.LeastSquares;
30import java.io.*;
31
48 private double[][] disc;
49 private Discrepancy[] discrepancies;
50 private double[] regression;
51 private int n, nbDisc;
52 private String title, xLabel, yLabel;
53
59 public DiscrepancyContainer(Discrepancy[] discrepancies) {
60 this.discrepancies = discrepancies;
61 nbDisc = discrepancies.length;
62 regression = new double[nbDisc + 1];
63 }
64
75 public void init(int n, String title, String xLabel, String yLabel) {
76 this.xLabel = xLabel;
77 this.yLabel = yLabel;
78 this.n = n;
79 disc = new double[nbDisc + 1][n];
80 reset();
81 }
82
89 public void init(int n) {
90 init(n, "", "Parameter", "Discrepancy");
91 }
92
98 public void reset(int i) {
99 for (int j = 0; j < nbDisc; ++j)
100 disc[j + 1][i] = 0.0;
101 }
102
106 public void reset() {
107 for (int i = 0; i < n; ++i)
108 reset(i);
109 }
110
119 public void compute(int i, double[] points, int n) {
120 for (int j = 0; j < nbDisc; ++j)
121 disc[j + 1][i] = discrepancies[j].compute(points, n);
122 }
123
133 public void compute(int i, double[][] points, int n, int s) {
134 for (int j = 0; j < nbDisc; ++j)
135 disc[j + 1][i] = discrepancies[j].compute(points, n, s);
136 }
137
148 public void add(int i, double[] points, int n) {
149 for (int j = 0; j < nbDisc; ++j)
150 disc[j + 1][i] += discrepancies[j].compute(points, n);
151 }
152
163 public void addSquare(int i, double[] points, int n) {
164 double discrepancy;
165 for (int j = 0; j < nbDisc; ++j) {
166 discrepancy = discrepancies[j].compute(points, n);
167 disc[j + 1][i] += discrepancy * discrepancy;
168 }
169 }
170
182 public void add(int i, double[][] points, int n, int s) {
183 for (int j = 0; j < nbDisc; ++j)
184 disc[j + 1][i] += discrepancies[j].compute(points, n, s);
185 }
186
199 public void addSquare(int i, double[][] points, int n, int s) {
200 double discrepancy;
201 for (int j = 0; j < nbDisc; ++j) {
202 discrepancy = discrepancies[j].compute(points, n, s);
203 disc[j + 1][i] += discrepancy * discrepancy;
204 }
205 }
206
213 public void scale(int i, double scale) {
214 for (int j = 0; j < nbDisc; ++j)
215 disc[j + 1][i] *= scale;
216 }
217
223 public void scale(double scale) {
224 for (int i = 0; i < n; ++i)
225 scale(i, scale);
226 }
227
233 public void log2(int i) {
234 for (int j = 0; j < nbDisc; ++j)
235 disc[j + 1][i] = Num.log2(disc[j + 1][i]);
236 }
237
243 public void square(int i) {
244 for (int j = 0; j < nbDisc; ++j)
245 disc[j + 1][i] *= disc[j + 1][i];
246 }
247
254 public void setParam(int i, double paramValue) {
255 disc[0][i] = paramValue;
256 }
257
258 // Removes infinite values caused by loss of precision in
259 // discrepancy computations.
260 private void removeInfinite() {
261 for (int i = 0; i < n; ++i) {
262 for (int j = 0; j < nbDisc; ++j)
263 if (disc[j + 1][i] == Double.NEGATIVE_INFINITY)
264 disc[j + 1][i] = 0.0;
265 }
266 }
267
272 protected void calcRegressionSlope() {
273 for (int j = 0; j < nbDisc; ++j)
274 regression[j] = LeastSquares.calcCoefficients(disc[0], disc[j + 1], 1)[1];
275 }
276
281 public String regressionToString() {
283 StringBuffer sb = new StringBuffer(
284 "***************************************************************" + PrintfFormat.NEWLINE);
285 sb.append("Linear regression slope" + PrintfFormat.NEWLINE);
286 for (int j = 0; j < nbDisc; ++j)
287 sb.append(PrintfFormat.s(15, discrepancies[j].toString()) + PrintfFormat.g(15, 6, regression[j])
289 return sb.toString();
290 }
291
300 public void toTexFile(String filename) {
301 removeInfinite();
302 // XYLineChart chart = new XYLineChart(title, xLabel, yLabel); // ambigu
303 XYLineChart chart = new XYLineChart();
304 chart.setTitle(title);
305 chart.getXAxis().setLabel(xLabel);
306 chart.getYAxis().setLabel(yLabel);
307 for (int j = 0; j < nbDisc; ++j)
308 chart.add(disc[0], disc[j + 1], discrepancies[j].toString(), "sharp plot");
309
310 try {
311 Writer file = new FileWriter(filename + ".tex");
312 file.write(chart.toLatex(9, 5));
313 file.close();
314 } catch (IOException e) {
315 e.printStackTrace();
316 }
317 }
318
327 public void toDatFile(String filename, String header) {
328 try {
329 Writer file = new FileWriter(filename + ".dat");
330 file.write(header);
331 file.write(toString());
332 file.write(regressionToString());
333 file.close();
334 } catch (IOException e) {
335 e.printStackTrace();
336 }
337 }
338
343 public String toString() {
344 StringBuffer sb = new StringBuffer(PrintfFormat.NEWLINE);
345 sb.append(PrintfFormat.s(35, xLabel));
346 for (int i = 0; i < n; ++i)
347 sb.append(PrintfFormat.g(15, 6, disc[0][i]));
348 sb.append(PrintfFormat.NEWLINE);
349
350 for (int j = 0; j < nbDisc; ++j) {
351 sb.append(PrintfFormat.s(15, yLabel));
352 sb.append(PrintfFormat.s(20, discrepancies[j].toString()));
353 for (int i = 0; i < n; ++i)
354 sb.append(PrintfFormat.g(15, 6, disc[j + 1][i]));
355 sb.append(PrintfFormat.NEWLINE);
356 }
357 return sb.toString();
358 }
359
360}
Provides tools to create and manage curve plots.
void log2(int i)
Takes the logarithm in base 2 of the discrepancy values at index i.
void scale(double scale)
Calls scale(i,scale) for all indices i.
String regressionToString()
Formats and returns a String containing the linear regression slopes for the discrepancies as functio...
void add(int i, double[] points, int n)
Computes the discrepancies of the first n values contained in points, and adds the values at index i.
void init(int n)
Calls init(n,"","Parameter", "Discrepancy").
void toDatFile(String filename, String header)
Creates a file named filename.dat and writes in it the given header, a table showing the discrepancie...
void setParam(int i, double paramValue)
Sets the parameter value at index i to parmValue.
void square(int i)
Squares the discrepancy values at index i.
void add(int i, double[][] points, int n, int s)
Computes the discrepancies of the first n values contained in points using the first s coordinates,...
DiscrepancyContainer(Discrepancy[] discrepancies)
Creates a DiscrepancyContainer for the given discrepancies.
void scale(int i, double scale)
Multiplies all the discrepancies at index i by scale;.
void compute(int i, double[][] points, int n, int s)
Computes the discrepancies of the first n values contained in points using the first s coordinates an...
void reset(int i)
Resets the values of the discrepancies at index i to 0.
void init(int n, String title, String xLabel, String yLabel)
Initialize the container with enough space for n values of the parameter and sets the values to 0.
void compute(int i, double[] points, int n)
Computes the discrepancies of the first n values contained in points and sets the values at index i.
void toTexFile(String filename)
Creates a file named filename.tex containing LaTeX code that can be compiled by pdfLaTeX to a graph o...
void calcRegressionSlope()
Computes the linear regression slope for the discrepancies as function of the parameter.
void reset()
Calls reset(i) for all indices i.
void addSquare(int i, double[][] points, int n, int s)
Computes the square discrepancies of the first n values contained in points using the first s coordin...
String toString()
Returns a String containing a table showing the discrepancies for the different values of the paramet...
void addSquare(int i, double[] points, int n)
Computes the square of the discrepancies of the first n values contained in points,...
This abstract class is the base class of all discrepancy classes.
This class implements different linear regression models, using the least squares method to estimate ...
static double[] calcCoefficients(double[] X, double[] Y)
Computes the regression coefficients using the least squares method.
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static double log2(double x)
Returns x .
Definition Num.java:405
This class acts like a StringBuffer which defines new types of append methods.
static String s(String str)
Same as s(0, str).
static final String NEWLINE
End-of-line symbol or line separator.
static String g(double x)
Same as g(0, 6, x).
Methods to compute various types of discrepancies for quasi-Monte Carlo point sets.