File: rk_sobol.h

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (173 lines) | stat: -rw-r--r-- 5,162 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Random kit 1.6 */

/*
 * Copyright (c) 2004-2006, Jean-Sebastien Roy (js@jeannot.org)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* @(#) $Jeannot: rk_sobol.h,v 1.7 2006/02/19 13:48:34 js Exp $ */

/*
 * Typical use:
 * 
 * int dimension = 2;
 * rk_sobol_state s;
 * rk_sobol_error rc;
 * double x[dimension], y[dimension];
 * 
 * // Init
 * if (rc = rk_sobol_init(dimension, &s, NULL, NULL, NULL))
 * {
 *   fprintf(stderr, "%s\n", rk_sobol_strerror[rc]);
 *   abort();
 * }
 *   
 * // Draw uniform quasirandom doubles
 * if (rc = rk_sobol_double(&s, x))
 * {
 *   fprintf(stderr, "%s\n", rk_sobol_strerror[rc]);
 *   abort();
 * }
 *   
 * // Draw gaussian quasirandom doubles
 * if (rc = rk_sobol_gauss(&s, y))
 * {
 *   fprintf(stderr, "%s\n", rk_sobol_strerror[rc]);
 *   abort();
 * }
 * 
 * // Free allocated memory
 * rk_sobol_free(&s);
 */


#ifndef _RK_SOBOL_
#define _RK_SOBOL_

#include "rk_mt.h"

typedef enum {
  RK_SOBOL_OK      = 0, /* No error */
  RK_SOBOL_EINVAL  = 1, /* Invalid dimension (<= 0 or too large) */
  RK_SOBOL_EXHAUST = 2, /* Too many number generated */
  RK_SOBOL_ENOMEM  = 3, /* Not enough memory */
  RK_SOBOL_ERR_MAX = 4
} rk_sobol_error;

/* error strings */
extern char *rk_sobol_strerror[];

typedef struct
{
  size_t dimension;
  unsigned long *direction;
  unsigned long *numerator;
  unsigned long count;
  unsigned long gcount;
} rk_sobol_state;

#ifdef __cplusplus
extern "C" {
#endif

/* Sobol directions initializations (zero terminated lists) */

/*
 * Sobol/Levitan coefficients of the free direction integers as given
 * by Bratley, P., Fox, B.L. (1988)
 * Defined up to dimension 40.
 */
extern const unsigned long rk_sobol_SLdirections[];

/*
 * Lemieux coefficients of the free direction integers as given
 * in QuantLib by Christiane Lemieux, private communication, September 2004
 * Defined up to dimension 360.
 */
extern const unsigned long rk_sobol_Ldirections[];

/*
 * Peter Jckel coefficients of the free direction integers as given
 * in "Monte Carlo Methods in Finance", by Peter Jckel, section 8.3
 * Defined up to dimension 32.
 */
extern const unsigned long rk_sobol_Jdirections[];

/*
 * Initialize a sobol quasirandom number generator. 
 * 1 <= dimension <= the number of primitive polylonimals of degree < LONG_BIT
 * If directions == NULL (or more directions than provided are required),
 * the directions are picked at random using rs_dir.
 * If rs_dir == NULL, it is initialized using rk_randomseed.
 * polynomials is a zero terminated list of primitive polynomials to use if
 * it is != NULL to speed up initialization for dimension > 1024.
 */
extern rk_sobol_error rk_sobol_init(size_t dimension, rk_sobol_state *s, 
  rk_state *rs_dir, const unsigned long *directions,
  const unsigned long *polynomials);

/*
 * Reinitialize the random generator with same directions.
 */
extern void rk_sobol_reinit(rk_sobol_state *s);

/*
 * You can change the starting rank in the sequence by changing s->count.
 */
extern void rk_sobol_setcount(rk_sobol_state *s, unsigned long count);

/*
 * XOR the numerators at random using rs_num.
 * To be used once, after (re-)initialization.
 * Useful for randomized quasi monte carlo.
 * If rs_num == NULL, it is initialized using rk_randomseed.
 */
extern void rk_sobol_randomshift(rk_sobol_state *s, rk_state *rs_num);

/*
 * Copy a sobol generator.
 * Can be used to avoid the time consuming initialization.
 */
extern rk_sobol_error rk_sobol_copy(rk_sobol_state *copy, rk_sobol_state *orig);

/*
 * Free the memory allocated by rk_sobol_init
 */
extern void rk_sobol_free(rk_sobol_state *s);

/*
 * return a vector of dimension quasirandom uniform deviates between 0 and 1
 */
extern rk_sobol_error rk_sobol_double(rk_sobol_state *s, double *x);

/*
 * return a vector of dimension quasirandom gaussian deviates
 * with variance unity and zero mean.
 * On Windows, the standard function erfc is missing, which results in
 * lower precision (9 digits instead of full precision).
 */
extern rk_sobol_error rk_sobol_gauss(rk_sobol_state *s, double *x);

#ifdef __cplusplus
}
#endif

#endif /* _RK_SOBOL_ */