-
Notifications
You must be signed in to change notification settings - Fork 43
/
quantmath.h
245 lines (220 loc) · 10.8 KB
/
quantmath.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Generated with cbindgen:0.6.2 */
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
/*
* Controls what to do if we are reading or writing a recursive structure
* and we hit a node that is the same as the type we are controlling.
* ErrorIfMissing requires all subnodes to be supplied externally. Inline
* requires all subnodes to be supplied inline. WriteOnce allows nodes to be
* supplied either inline or externally. When writing, WriteOnce writes each
* unique node once, the first time it is encounted, and thereafter does not
* write them.
*/
enum QmDedupControl {
QmErrorIfMissing,
QmInline,
QmWriteOnce,
};
/*
* Compares two reports for equality. If they are equal, it returns a handle of type Empty, which
* is not an error. If they are not equal, it returns a handle of type Error, where the error
* message details the differences between the reports. This is normally used for testing.
*
* Both of the report handles passed into the function are freed by the function, so you must not
* invoke free_handle on them. The tolerances are absolute, and specify the allowed differences:
* tol_price is the allowed difference in the price -- for Monte-Carlo this must be larger than the
* expected Monte-Carlo noise; tol_ccy_risk is the allowed difference in risks that are measured in
* units of currency such as Vega or PV01 -- this is likely to be smaller than tol_price, as the same
* Monte-Carlo paths are used for different bumped paths in risk calculations; tol_unit_risk is the
* allowed difference to risks that are measured in non-dimensional units such as delta -- this does
* not scale with the notional.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_assert_approx_eq_reports(uint64_t reports_freed,
uint64_t expected_freed,
double tol_price,
double tol_ccy_risk,
double tol_unit_risk);
/*
* Calculates a set of reports on the given data. The pricer_factory specifies how
* pricing is to be done; the instrument specifies what is to be priced; the fixing_table
* and market_data specify the context for pricing, and the report generators specify what
* is to be calculated.
*
* The results are returned as a handle containing reports. These can be viewed as JSON by
* invoking qm_reports_as_json_string, or can be compared with other reports by invoking
* qm_assert_approx_eq_reports, which will free the reports handle for you.
*
* Reports are not cloneable, and are generally consumed by functions that use them, except for
* simple ones such as is_error or qm_reports_as_json_string. If you do not invoke a method such as
* qm_assert_approx_eq_reports, you must manually free the reports using free_handle. If the reports
* are badly formed, the method may return a handle of type error. You can
* test for errors immediately after invoking this method, by calling qm_is_error, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_calculate(uint64_t pricer_factory,
uint64_t instrument,
uint64_t fixing_table,
uint64_t market_data,
uint32_t number_of_report_generators,
const uint64_t *report_generators);
/*
* Returns a clone of the given handle. The clone must also be eventually freed using qm_free_handle.
* Any handle can be cloned except for one containing a set of reports. Cloning that would end up
* with a handle representing an error. Error handles can also be cloned.
*/
uint64_t qm_clone(uint64_t handle);
/*
* Creates a handle (an opaque 64bit unsigned int) that represents a currency. For example, it can
* be passed into the instrument creation functions. The source is a UTF8-encoded filename referring
* to a text file containing UTF8-encoded JSON. (UTF8 is the same as ASCII for the standard characters
* 0..127.).
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_currency_from_json_file(const char *source);
/*
* Same as qm_currency_from_json_file, but taking inline text.
*/
uint64_t qm_currency_from_json_string(const char *source);
/*
* Returns the error string associated with this handle. If the handle does not represent
* an error, the call will give you an error message anyway, for using a handle of the
* wrong type.
*
* The resulting string must be freed using qm_free_string
*/
char *qm_error_string(uint64_t handle);
/*
* Loads a fixing table from a JSON UTF8 file.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_fixing_table_from_json_file(const char *source);
/*
* Same as qm_fixing_table_from_json_file, but taking inline text.
*/
uint64_t qm_fixing_table_from_json_string(const char *source);
/*
* Frees a handle that was created by most of the other methods in this interface. Handles ought to be
* freed, or there is a memory leak. Handles must not be freed more than once -- trying to do so is
* likely to result in a core dump.
*/
void qm_free_handle(uint64_t handle);
/*
* Frees a string that was allocated by a method such as qm_error_string. Strings must
* only be freed once.
*/
void qm_free_string(char *string);
/*
* Loads an instrument from a JSON UTF8 file. Currencies and subinstruments can
* be supplied inline or provided in arrays, according to the dedup parameters.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_instrument_from_json_file(const char *source,
enum QmDedupControl dedup_ccy,
uint32_t number_of_currencies,
const uint64_t *currencies,
enum QmDedupControl dedup_instr,
uint32_t number_of_instruments,
const uint64_t *instruments);
/*
* Same as qm_instrument_from_json_file, but taking inline text.
*/
uint64_t qm_instrument_from_json_string(const char *source,
enum QmDedupControl dedup_ccy,
uint32_t number_of_currencies,
const uint64_t *currencies,
enum QmDedupControl dedup_instr,
uint32_t number_of_instruments,
const uint64_t *instruments);
/*
* Tests whether a handle represents an error. If it does, you should normally invoke
* qm_error_string to find out what the error is.
*/
bool qm_is_error(uint64_t handle);
/*
* Loads a market data collection from a JSON UTF8 file.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_market_data_from_json_file(const char *source);
/*
* Same as qm_market_data_from_json_file, but taking inline text.
*/
uint64_t qm_market_data_from_json_string(const char *source);
/*
* Loads a pricer factory from a JSON UTF8 file. Pricer factories specify how pricing is to be done. For
* example, a Monte-Carlo pricer factory specifies the number of paths, and the stochastic model to be
* used.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_pricer_factory_from_json_file(const char *source);
/*
* Same as qm_pricer_factory_from_json_file, but taking inline text.
*/
uint64_t qm_pricer_factory_from_json_string(const char *source);
/*
* Loads a report generator from a JSON UTF8 file. Report generators are supplied to the
* qm_calculate function, and specify which reports are to be calculated, such as DeltaGamma for
* all assets, etc.
*
* The resulting handle must be freed with free_handle. In the event of error, the
* resulting handle is of type error, and returns true for qm_is_error. You can
* test for errors immediately after invoking this method, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_report_generator_from_json_file(const char *source);
/*
* Same as qm_report_generator_from_json_file, but taking inline text.
*/
uint64_t qm_report_generator_from_json_string(const char *source);
/*
* Converts a handle representing a set of reports into a string. The string must
* be freed using qm_free_string. The handle is left unchanged and unfreed by this
* call.
*
* If the handle is an error or does not contain a set of reports, an error message
* is returned instead. The string must be freed using qm_free_string whether it is
* an error or not.
*/
char *qm_reports_as_json_string(uint64_t handle);
/*
* Loads a set of reports from a JSON UTF8 file. Reports are normally generated by a qm_calculate,
* so the only reason you would load them from a file is for testing or for distribution.
*
* Reports are not cloneable, and are generally consumed by functions that use them, except for
* simple ones such as is_error or qm_reports_as_json_string. If you do not invoke a method such as
* qm_assert_approx_eq_reports, you must manually free the reports using free_handle. If the reports
* are badly formed, the method may return a handle of type error. You can
* test for errors immediately after invoking this method, by calling qm_is_error, or you can simply use
* the handle and propagate the error.
*/
uint64_t qm_reports_from_json_file(const char *source);
/*
* Same as qm_reports_from_json_file, but taking inline text.
*/
uint64_t qm_reports_from_json_string(const char *source);