forked from CaptorAB/quantlib-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
408 lines (361 loc) · 12.2 KB
/
main.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
var QuantLib = require("./quantlib-embind");
const dateToSerialNumber = (d) => d.getTime() / 86400000 + 25569;
const serialNumberToDate = (n) => new Date((n - 25569) * 86400000);
const addDays = (d, n) => new Date(d.getTime() + 86400000 * n);
const toWasmIntVector = (arr) => {
var v = QuantLib.createIntVector(arr.length);
for (let i = 0; i < arr.length; i++) {
v.set(i, arr[i]);
}
return v;
};
const toWasmDoubleVector = (arr) => {
var v = QuantLib.createDoubleVector(arr.length);
for (let i = 0; i < arr.length; i++) {
v.set(i, arr[i]);
}
return v;
};
const toCurveItem = (date, value) => ({ date, value });
function performanceTest() {
const { sqrt } = Math;
const sqr = (x) => x * x;
const stdev = (xs) => {
var n = xs.length;
var sx = 0;
var sy = 0;
var sx2 = 0;
for (let i = 0; i < n; i++) {
let x = xs[i];
sx += x;
sx2 += sqr(x);
}
return sqrt((sx2 - sqr(sx) / n) / (n - 1));
};
const hrtimeDiffToMs = (t0, t1) => 1000 * (t1[0] - t0[0]) + (t1[1] - t0[1]) / 1000000;
var arr = [...Array(400000)].map((d, i) => i + 1);
var t0 = process.hrtime();
var s0 = stdev(arr);
var t1 = process.hrtime();
var s1 = QuantLib.stdev(toWasmDoubleVector(arr));
var t2 = process.hrtime();
var s2 = QuantLib.stdevDummy(toWasmDoubleVector(arr));
var t3 = process.hrtime();
console.log(s0, s1);
console.log("JavaScript " + hrtimeDiffToMs(t0, t1));
console.log("WebAssembly " + hrtimeDiffToMs(t1, t2));
console.log("WebAssembly excl copy data " + (hrtimeDiffToMs(t1, t2) - hrtimeDiffToMs(t2, t3)));
}
function bytesDiff(m1, m0) {
return m1.uordblks - m0.uordblks + (m1.hblkhd - m0.hblkhd);
}
class Dummy {
delete() {}
}
function replicateSwapExample2() {
var ms = [];
ms.push(QuantLib.mallinfo());
const {
Date,
Period,
TimeUnit,
BusinessDayConvention,
DateGenerationRule,
Schedule,
VanillaSwapType,
VanillaSwap,
setValuationDate,
Thirty360,
Actual360,
Euribor
} = QuantLib;
var valuationDate = Date.fromISOString("2012-12-31");
setValuationDate(valuationDate);
var nominal = 1000000.0;
var previousResetDate = Date.fromISOString("2012-11-20");
var maturity = Date.fromISOString("2022-11-20");
var spread = 0.02;
var fixedRate = 0.04;
var previousResetValue = 0.01;
var fixedTenor = new Period(1, TimeUnit.Years);
var floatTenor = new Period(3, TimeUnit.Months);
var curveDates = new QuantLib.vector$Date$(3);
var curveDateObjs = [Date.fromISOString("2013-12-31"), Date.fromISOString("2024-12-31")];
curveDates.set(0, valuationDate);
curveDateObjs.forEach((d, i) => curveDates.set(i + 1, d));
var forwardCurveDfs = new QuantLib.vector$double$(3);
forwardCurveDfs.set(0, 1);
forwardCurveDfs.set(1, 0.99);
forwardCurveDfs.set(2, 0.8);
var discountCurveDfs = new QuantLib.vector$double$(3);
discountCurveDfs.set(0, 1);
discountCurveDfs.set(1, 0.999);
discountCurveDfs.set(2, 0.89);
var actual360 = new Actual360();
var forwardingTermStructure = QuantLib.createLogLinearYieldTermStructure(curveDates, forwardCurveDfs, actual360);
var discountTermStructure = QuantLib.createLogLinearYieldTermStructure(curveDates, discountCurveDfs, actual360);
var calendar = QuantLib.Sweden;
var convention = BusinessDayConvention.ModifiedFollowing;
var terminationDateConvention = BusinessDayConvention.ModifiedFollowing;
var rule = DateGenerationRule.Forward;
var endOfMonth = false;
var firstDate = new Date();
var nextToLastDate = new Date();
var fixedDayCount = new Thirty360();
var floatingDayCount = new Actual360();
var fixedSchedule = new Schedule(
previousResetDate,
maturity,
fixedTenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth,
firstDate,
nextToLastDate
);
var floatSchedule = new Schedule(
previousResetDate,
maturity,
floatTenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth,
firstDate,
nextToLastDate
);
var previousResetValue = 0.01;
var euribor = new Euribor(floatTenor, forwardingTermStructure);
var previousFixingDate = euribor.fixingDate(previousResetDate);
euribor.addFixing(previousFixingDate, previousResetValue, true);
var swap = new VanillaSwap(
VanillaSwapType.Payer,
nominal,
fixedSchedule,
fixedRate,
fixedDayCount,
floatSchedule,
euribor,
spread,
floatingDayCount
);
swap.setPricingEngine(discountTermStructure);
var v = swap.NPV();
ms.push(QuantLib.mallinfo());
[
...curveDateObjs,
valuationDate,
maturity,
fixedTenor,
floatTenor,
curveDates,
forwardCurveDfs,
discountCurveDfs,
forwardingTermStructure,
discountTermStructure,
firstDate,
nextToLastDate,
fixedDayCount,
fixedSchedule,
floatingDayCount,
floatSchedule,
previousResetDate,
euribor,
previousFixingDate,
swap,
actual360
].forEach((d) => d.delete());
ms.push(QuantLib.mallinfo());
// ms.forEach((d) => console.log(JSON.stringify(d)));
// console.log(
// ms
// .filter((d, i) => i !== 0)
// .map((d) => bytesDiff(d, ms[0]))
// .join(", ")
// );
return v;
}
function replicateSwapExample1() {
var valuationDate = new Date("2012-12-31");
var nominal = 1000000.0;
var previousResetDate = new Date("2012-11-20");
var maturity = new Date("2022-11-20");
var spread = 0.02;
var fixedRate = 0.04;
var previousResetValue = 0.01;
var fixedScheduleCount = 1;
var fixedScheduleTimeUnit = QuantLib.TimeUnit.Years;
var floatScheduleCount = 3;
var floatScheduleTimeUnit = QuantLib.TimeUnit.Months;
var forwardCurveDayCountConvention = QuantLib.DayCountConvention.Actual360;
var oisCurveDayCountConvention = QuantLib.DayCountConvention.Actual360;
var d0 = new Date("2013-12-31");
var d1 = new Date("2024-12-31");
var forwardCurve = [toCurveItem(valuationDate, 1), toCurveItem(d0, 0.99), toCurveItem(d1, 0.8)];
var oisCurve = [toCurveItem(valuationDate, 1), toCurveItem(d0, 0.999), toCurveItem(d1, 0.89)];
var wasmValuationDate = dateToSerialNumber(valuationDate);
var wasmPreviousResetDate = dateToSerialNumber(previousResetDate);
var wasmMaturity = dateToSerialNumber(maturity);
var wasmForwardCurveDates = toWasmIntVector(forwardCurve.map((d) => dateToSerialNumber(d.date)));
var wasmForwardCurveDfs = toWasmDoubleVector(forwardCurve.map((d) => d.value));
var wasmOisCurveDates = toWasmIntVector(oisCurve.map((d) => dateToSerialNumber(d.date)));
var wasmOisCurveDfs = toWasmDoubleVector(oisCurve.map((d) => d.value));
console.log(
QuantLib.swapNpv(
nominal,
fixedRate,
spread,
wasmValuationDate,
wasmMaturity,
wasmPreviousResetDate,
previousResetValue,
fixedScheduleCount,
fixedScheduleTimeUnit,
floatScheduleCount,
floatScheduleTimeUnit,
forwardCurveDayCountConvention,
oisCurveDayCountConvention,
wasmForwardCurveDates,
wasmForwardCurveDfs,
wasmOisCurveDates,
wasmOisCurveDfs
)
);
}
function generateSchedule() {
const { Month, Date, Period, TimeUnit, BusinessDayConvention, DateGenerationRule, Schedule } = QuantLib;
var effectiveDate = new Date(15, Month.May, 2019);
var terminationDate = new Date(31, Month.May, 2023);
var calendar = QuantLib.Sweden;
var tenor = new Period(6, TimeUnit.Months);
var convention = BusinessDayConvention.ModifiedFollowing;
var terminationDateConvention = BusinessDayConvention.ModifiedFollowing;
var rule = DateGenerationRule.Backward;
var endOfMonth = false;
var firstDate = new Date();
var nextToLastDate = new Date();
var schedule = new QuantLib.Schedule(
effectiveDate,
terminationDate,
tenor,
calendar,
convention,
terminationDateConvention,
rule,
endOfMonth,
firstDate,
nextToLastDate
);
var dates = schedule.dates();
for (let i = 0; i < dates.size(); i++) {
console.log(dates.get(i).toISOString());
}
var schedule2 = new QuantLib.Schedule(dates);
var dates2 = schedule2.dates();
for (let i = 0; i < dates2.size(); i++) {
console.log(dates2.get(i).toISOString());
}
// cleanup
effectiveDate.delete();
terminationDate.delete();
tenor.delete();
firstDate.delete();
nextToLastDate.delete();
dates.delete();
schedule.delete();
dates2.delete();
schedule2.delete();
}
function testDate() {
const { Date, Month } = QuantLib;
var myDate = new Date(12, Month.Aug, 2009);
console.log(" Original Date: " + myDate.toISOString());
console.log(" Weekday: " + myDate.weekday());
console.log(" Day of Month: " + myDate.dayOfMonth());
console.log(" Day of Year: " + myDate.dayOfYear());
console.log(" Month: " + myDate.month());
var month = myDate.month();
console.log(" Month via Integer: " + month.value);
console.log(" Year: " + myDate.year());
var serialNum = myDate.serialNumber();
console.log("Serial Number: " + serialNum);
}
function testVector() {
for (let i = 0; i < 10; i++) {
let n = 500000;
let arr = new QuantLib.vector$double$(n);
for (let j = 0; j < n; j++) {
arr.set(j, j + 1);
}
arr.delete();
var v = QuantLib.mallinfo();
console.log(JSON.stringify(v));
}
}
var QuantLibLoader = QuantLib();
QuantLibLoader.onRuntimeInitialized = () => {
QuantLib = QuantLibLoader;
// testVector();
// var d = new QuantLib.Date(34000);
// console.log(d.toISOString());
// generateSchedule();
// BusinessDayConvention terminationDateConvention, DateGeneration::Rule rule, bool endOfMonth,
// int firstDateAsSerialNumber = 0, int nextToLastDateAsSerialNumber = 0
// var arr = [...Array(100000)].map((d, i) => i + 1);
// for (let index = 0; index < 1000; index++) {
// let a = toWasmDoubleVector(arr);
// let v = QuantLib.stdev(a);
// a.delete();
// }
// var s = QuantLib.createScheduleFromDates(toWasmIntVector([35000, 36000]));
// Works fine
// const { MyClassA, MyClassB } = QuantLib;
// var m0, m1, m2;
// for (let i = 0; i < 1000000; i++) {
// m0 = QuantLib.mallinfo();
// let a = new MyClassA(10, "hello");
// a.incrementX();
// let x = a.x; // 11
// a.x = 758; // 20
// let s = MyClassA.getStringFromInstance(a); // "hello"
// let b = new MyClassB(a);
// // console.log(b.x);
// b.x = 3;
// // console.log(b.x);
// if (i === 0) {
// m1 = QuantLib.mallinfo();
// }
// a.delete();
// b.delete();
// if (i === 0) {
// m2 = QuantLib.mallinfo();
// console.log(JSON.stringify(m0));
// console.log(JSON.stringify(m1));
// console.log(JSON.stringify(m2));
// console.log(m1.uordblks - m0.uordblks + (m1.hblkhd - m0.hblkhd));
// console.log(m2.uordblks - m0.uordblks + (m2.hblkhd - m0.hblkhd));
// }
// }
var v;
var m0 = QuantLib.mallinfo();
var n = 225;
var m1;
for (let i = 0; i < n; i++) {
v = replicateSwapExample2();
if (i === n - 2) {
m1 = QuantLib.mallinfo();
}
}
var m2 = QuantLib.mallinfo();
console.log(JSON.stringify(m0));
console.log(JSON.stringify(m1));
console.log(JSON.stringify(m2));
console.log(m2.uordblks - m0.uordblks + (m2.hblkhd - m0.hblkhd));
console.log(m2.time - m0.time);
console.log(v);
// performanceTest();
// testDate();
};