forked from asavine/CompFinance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlExport.cpp
1616 lines (1361 loc) · 48.6 KB
/
xlExport.cpp
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: AAD and Parallel Simulations
Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
// Excel export wrappers to functions in main.h
#pragma warning(disable:4996)
#pragma warning(disable:4267)
#include "ThreadPool.h"
#include "main.h"
#include "toyCode.h"
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include "xlcall.h"
#include "xlframework.h"
#include "xlOper.h"
// Helpers
NumericalParam xl2num(
const double useSobol,
const double seed1,
const double seed2,
const double numPath,
const double parallel)
{
NumericalParam num;
num.numPath = static_cast<int>(numPath + EPS);
num.parallel = parallel > EPS;
if (seed1 >= 1)
{
num.seed1 = static_cast<int>(seed1 + EPS);
}
else
{
num.seed1 = 1234;
}
if (seed2 >= 1)
{
num.seed2 = static_cast<int>(seed2 + EPS);
}
else
{
num.seed2 = num.seed1 + 1;
}
num.useSobol = useSobol > EPS;
return num;
}
// Wrappers
// change number of threads in the pool
extern "C" __declspec(dllexport)
double xRestartThreadPool(
double xNthread)
{
const int numThread = int(xNthread + EPS);
ThreadPool::getInstance()->stop();
ThreadPool::getInstance()->start(numThread);
return numThread;
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutDupire(
// model parameters
double spot,
FP12* spots,
FP12* times,
FP12* vols,
double maxDt,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (maxDt <= 0.0 || id.empty()) return TempErr12(xlerrNA);
// Unpack
if (spots->rows * spots->columns * times->rows * times->columns != vols->rows * vols->columns)
{
return TempErr12(xlerrNA);
}
vector<double> vspots = to_vector(spots);
vector<double> vtimes = to_vector(times);
matrix<double> vvols = to_matrix(vols);
// Call and return
putDupire(spot, vspots, vtimes, vvols, maxDt, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutBlackScholes(
double spot,
double vol,
double qSpot,
double rate,
double div,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
// Call and return
putBlackScholes(spot, vol, qSpot > 0, rate, div, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutDLM(
// model parameters
LPXLOPER12 assets,
FP12* spots,
FP12* atms,
FP12* skews,
double discRate,
FP12* repoSpreads,
FP12* divDates,
FP12* divs,
FP12* correl,
double lambda,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
// Assets
vector<string> vassets = to_strVector(assets);
if (vassets.empty()) return TempErr12(xlerrNA);
for (const auto& asset : vassets) if (asset.empty()) return TempErr12(xlerrNA);
const size_t vnumassets = vassets.size();
// Check dimensions
if ( spots->rows * spots->columns != vnumassets
|| repoSpreads->rows * repoSpreads->columns != vnumassets
|| atms->rows * atms->columns != vnumassets
|| skews->rows * skews->columns != vnumassets
|| divs->rows != divDates->rows || divs->columns != vnumassets || divDates->columns != 1
|| correl->rows != vnumassets || correl->columns != vnumassets)
{
return TempErr12(xlerrNA);
}
// Unpack
vector<double> vspots = to_vector(spots);
vector<double> vspreads = to_vector(repoSpreads);
vector<double> vatms = to_vector(atms);
vector<double> vskews = to_vector(skews);
vector<double> vdivtimes = to_vector(divDates);
matrix<double> vdivs = to_matrix(divs);
matrix<double> vcorrel = to_matrix(correl);
// Call and return
putDisplaced(vassets, vspots, vatms, vskews, discRate, vspreads, vdivtimes, vdivs, vcorrel, lambda, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xViewDLM(
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
try
{
Model<double>* mdl = const_cast<Model<double>*>(getModel<double>(id));
// Make sure we have a model
if (!mdl) return TempErr12(xlerrNA);
MultiDisplaced<double>* dlm = dynamic_cast<MultiDisplaced<double>*>(mdl);
// Make sure it is a DLM
if (!dlm) return TempErr12(xlerrNA);
// Initialize it
vector<Time> timeline = { 1.0 };
vector<SampleDef> defline(1);
defline[0].numeraire = false;
defline[0].discountMats = {};
defline[0].liborDefs = {};
defline[0].forwardMats = vector<vector<Time>>(dlm->numAssets(), { 1.0 });
dlm->allocate(timeline, defline);
dlm->init(timeline, defline);
const size_t n = dlm->numAssets() + 1, m = 4;
LPXLOPER12 oper = TempXLOPER12();
resize(oper, n, m);
setString(oper, "dynamics", 0, 1);
setString(oper, "alpha", 0, 2);
setString(oper, "beta", 0, 3);
for (size_t i = 0; i < dlm->numAssets(); ++i)
{
setString(oper, dlm->assetNames()[i], i + 1, 0);
switch(dlm->dynamics()[i])
{
case 0:
setString(oper, "lognormal", i + 1, 1);
break;
case 1:
setString(oper, "normal", i + 1, 1);
break;
case 2:
setString(oper, "surnormal", i + 1, 1);
break;
case 3:
setString(oper, "subnormal", i + 1, 1);
break;
default:
setString(oper, "invalid", i + 1, 1);
break;
}
setNum(oper, dlm->alphas()[i], i + 1, 2);
setNum(oper, dlm->betas()[i], i + 1, 3);
}
return oper;
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutEuropean(
double strike,
double exerciseDate,
double settlementDate,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
if (settlementDate <= 0) settlementDate = exerciseDate;
// Call and return
putEuropean(strike, exerciseDate, settlementDate, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutBarrier(
double strike,
double barrier,
double maturity,
double monitorFreq,
double smoothing,
LPXLOPER12 xcallput,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
// Call or put?
const string cpStr = getString(xcallput);
bool callPut = !cpStr.empty() && (cpStr[0] == 'p' || cpStr[0] == 'P');
// Call and return
putBarrier(strike, barrier, maturity, monitorFreq, smoothing, callPut, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutContingent(
double coupon,
double maturity,
double payFreq,
double smoothing,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
// Call and return
putContingent(coupon, maturity, payFreq, smoothing, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutEuropeans(
FP12* maturities,
FP12* strikes,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
// Make sure we have options
if (!strikes->rows || !strikes->columns) return TempErr12(xlerrNA);
// Maturities and strikes, removing blanks
vector<double> vmats;
vector<double> vstrikes;
{
size_t rows = maturities->rows;
size_t cols = maturities->columns;
if (strikes->rows * strikes->columns != rows * cols) return TempErr12(xlerrNA);
double* mats= maturities->array;
double* strs = strikes->array;
for (size_t i = 0; i < cols * rows; ++i)
{
if (mats[i] > EPS && strs[i] > EPS)
{
vmats.push_back(mats[i]);
vstrikes.push_back(strs[i]);
}
}
}
// Make sure we still have options
if (vmats.empty()) return TempErr12(xlerrNA);
// Call and return
putEuropeans(vmats, vstrikes, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutMultiStats(
LPXLOPER12 assets,
FP12* fix,
FP12* fwd,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
vector<string> vassets = to_strVector(assets);
// Make sure we have assets
if (vassets.empty()) return TempErr12(xlerrNA);
// Fixing and fwd dates, removing blanks and zeros (not today)
vector<Time> vfix;
vector<Time> vfwd;
{
size_t rows = fix->rows;
size_t cols = fix->columns;
if (fwd->rows * fwd->columns != rows * cols) return TempErr12(xlerrNA);
Time* fixs = fix->array;
Time* fwds = fwd->array;
for (size_t i = 0; i < cols * rows; ++i)
{
if (fixs[i] > EPS && fwds[i] > EPS)
{
// Check
if (i > 0 && fixs[i] <= fixs[i-1]) return TempErr12(xlerrNA);
if (fixs[i] > fwds[i]) return TempErr12(xlerrNA);
// Set
vfix.push_back(fixs[i]);
vfwd.push_back(fwds[i]);
}
}
}
// Call and return
putMultiStats(vassets, vfix, vfwd, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutBaskets(
LPXLOPER12 assets,
FP12* weights,
double maturity,
FP12* strikes,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
vector<string> vassets = to_strVector(assets);
// Make sure we have assets
if (vassets.empty()) return TempErr12(xlerrNA);
// Maturity
if (maturity <= 0) return TempErr12(xlerrNA);
// Weights and strikes
vector<double> vweights = to_vector(weights);
vector<double> vstrikes = to_vector(strikes);
if (vweights.empty() || vstrikes.empty()) TempErr12(xlerrNA);
// Call and return
putBaskets(vassets, vweights, maturity, vstrikes, id);
return TempStr12(id);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xPutAutocall(
LPXLOPER12 assets,
FP12* refs,
double maturity,
double periods,
double ko,
double strike,
double cpn,
double smooth,
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
vector<string> vassets = to_strVector(assets);
vector<double> vrefs = to_vector(refs);
// Make sure we have assets and dimensions are consistent
if (vassets.empty()) return TempErr12(xlerrNA);
if (vassets.size() != vrefs.size()) return TempErr12(xlerrNA);
// Maturity
if (maturity <= 0) return TempErr12(xlerrNA);
if (int(periods + EPS) <= 0) return TempErr12(xlerrNA);
// Call and return
putAutocall(vassets, vrefs, maturity, int(periods + EPS), ko, strike, cpn, smooth, id);
return TempStr12(id);
}
// Access payoff identifiers and parameters
extern "C" __declspec(dllexport)
LPXLOPER12 xPayoffIds(
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
const auto* prd = getProduct<double>(id);
// Make sure we have a product
if (!prd) return TempErr12(xlerrNA);
return from_strVector(prd->payoffLabels());
}
extern "C" __declspec(dllexport)
LPXLOPER12 xParameters(
LPXLOPER12 xid)
{
FreeAllTempMemory();
const string id = getString(xid);
// Make sure we have an id
if (id.empty()) return TempErr12(xlerrNA);
Model<double>* mdl = const_cast<Model<double>*>(getModel<double>(id));
// Make sure we have a model
if (!mdl) return TempErr12(xlerrNA);
const auto& paramLabels = mdl->parameterLabels();
const auto& params = mdl->parameters();
vector<double> paramsCopy(params.size());
transform(params.begin(), params.end(), paramsCopy.begin(), [](const double* p) { return *p; });
return from_labelsAndNumbers(paramLabels, paramsCopy);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xValue(
LPXLOPER12 modelid,
LPXLOPER12 productid,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const auto* prd = getProduct<double>(pid);
// Make sure we have a product
if (!prd) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
Model<double>* mdl = const_cast<Model<double>*>(getModel<double>(mid));
// Make sure we have a model
if (!mdl) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
// Call and return;
try
{
auto results = value(mid, pid, num);
return from_labelsAndNumbers(results.identifiers, results.values);
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xValueTime(
LPXLOPER12 modelid,
LPXLOPER12 productid,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const auto* prd = getProduct<double>(pid);
// Make sure we have a product
if (!prd) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
Model<double>* mdl = const_cast<Model<double>*>(getModel<double>(mid));
// Make sure we have a model
if (!mdl) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
// Call and return;
try
{
clock_t t0 = clock();
auto results = value(mid, pid, num);
clock_t t1 = clock();
LPXLOPER12 oper = TempXLOPER12();
resize(oper, results.values.size() + 1, 1);
for (int i = 0; i < results.values.size(); ++i) setNum(oper, results.values[i], i, 0);
setNum(oper, t1 - t0, results.values.size(), 0);
return oper;
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xAADrisk(
LPXLOPER12 modelid,
LPXLOPER12 productid,
LPXLOPER12 xRiskPayoff,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
// Risk payoff
const string riskPayoff = getString(xRiskPayoff);
try
{
auto results = AADriskOne(mid, pid, num, riskPayoff);
const size_t n = results.risks.size(), N = n + 1;
LPXLOPER12 oper = TempXLOPER12();
resize(oper, N, 2);
setString(oper, "value", 0, 0);
setNum(oper, results.riskPayoffValue, 0, 1);
for (size_t i = 0; i < n; ++i)
{
setString(oper, results.paramIds[i], i + 1, 0);
setNum(oper, results.risks[i], i + 1, 1);
}
return oper;
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xAADriskAggregate(
LPXLOPER12 modelid,
LPXLOPER12 productid,
LPXLOPER12 xPayoffs,
FP12* xNotionals,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
// Payoffs and notionals, removing blanks
map<string, double> notionals;
{
size_t rows = getRows(xPayoffs);
size_t cols = getCols(xPayoffs);
if (rows * cols == 0 ||
xNotionals->rows * xNotionals->columns != rows * cols)
return TempErr12(xlerrNA);
size_t idx = 0;
for (size_t i = 0; i < rows; ++i) for (size_t j = 0; j < cols; ++j)
{
string payoff = getString(xPayoffs, i, j);
double notional = xNotionals->array[idx++];
if (payoff != "" && fabs(notional) > EPS)
{
notionals[payoff] = notional;
}
}
}
try
{
auto results = AADriskAggregate(mid, pid, notionals, num);
const size_t n = results.risks.size(), N = n + 1;
LPXLOPER12 oper = TempXLOPER12();
resize(oper, N, 2);
setString(oper, "value", 0, 0);
setNum(oper, results.riskPayoffValue, 0, 1);
for (size_t i = 0; i < n; ++i)
{
setString(oper, results.paramIds[i], i + 1, 0);
setNum(oper, results.risks[i], i + 1, 1);
}
return oper;
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
unordered_map<string, RiskReports> riskStore;
extern "C" __declspec(dllexport)
LPXLOPER12 xBumprisk(
LPXLOPER12 modelid,
LPXLOPER12 productid,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel,
// display now or put in memory?
double displayNow,
LPXLOPER12 storeid)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
try
{
auto results = bumpRisk(mid, pid, num);
if (displayNow > 0.5)
{
return from_labelledMatrix(results.params, results.payoffs, results.risks, "value", results.values);
}
else
{
const string riskId = getString(storeid);
if (riskId == "") return TempErr12(xlerrNA);
riskStore[riskId] = results;
return storeid;
}
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xAADriskMulti(
LPXLOPER12 modelid,
LPXLOPER12 productid,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel,
// display now or put in memory?
double displayNow,
LPXLOPER12 storeid)
{
FreeAllTempMemory();
const string pid = getString(productid);
// Make sure we have an id
if (pid.empty()) return TempErr12(xlerrNA);
const string mid = getString(modelid);
// Make sure we have an id
if (mid.empty()) return TempErr12(xlerrNA);
// Numerical params
const auto num = xl2num(useSobol, seed1, seed2, numPath, parallel);
// Make sure we have a numPath
if (!num.numPath) return TempErr12(xlerrNA);
try
{
auto results = AADriskMulti(mid, pid, num);
if (displayNow > 0.5)
{
return from_labelledMatrix(results.params, results.payoffs, results.risks, "value", results.values);
}
else
{
const string riskId = getString(storeid);
if (riskId == "") return TempErr12(xlerrNA);
riskStore[riskId] = results;
return storeid;
}
}
catch (const exception&)
{
return TempErr12(xlerrNA);
}
}
extern "C" __declspec(dllexport)
LPXLOPER12 xDisplayRisk(
LPXLOPER12 riskid,
LPXLOPER12 displayid)
{
FreeAllTempMemory();
RiskReports* results;
const string riskId = getString(riskid);
auto it = riskStore.find(riskId);
if (it == riskStore.end()) return TempErr12(xlerrNA);
else results = &it->second;
const vector<string> riskIds = to_strVector(displayid);
vector<size_t> riskCols;
for (const auto& id : riskIds)
{
auto it2 = find(results->payoffs.begin(), results->payoffs.end(), id);
if (it2 == results->payoffs.end()) return TempErr12(xlerrNA);
riskCols.push_back(distance(results->payoffs.begin(), it2));
}
if (riskCols.empty()) return TempErr12(xlerrNA);
const size_t nParam = results->risks.rows();
const size_t nPayoffs = riskCols.size();
vector<double> showVals(nPayoffs);
matrix<double> showRisks(nParam, nPayoffs);
for (size_t j = 0; j < nPayoffs; ++j)
{
for (size_t i = 0; i < nParam; ++i)
{
showRisks[i][j] = results->risks[i][riskCols[j]];
}
showVals[j] = results->values[riskCols[j]];
}
return from_labelledMatrix(results->params, riskIds, showRisks, "value", showVals);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xDupireCalib(
// Merton market parameters
const double spot,
const double vol,
const double jmpIntens,
const double jmpAverage,
const double jmpStd,
// Discretization
FP12* spots,
const double maxDs,
FP12* times,
const double maxDt)
{
FreeAllTempMemory();
// Make sure the last input is given
if (maxDs == 0 || maxDt == 0) return 0;
// Unpack
vector<double> vspots;
{
size_t rows = spots->rows;
size_t cols = spots->columns;
double* numbers = spots->array;
vspots.resize(rows*cols);
copy(numbers, numbers + rows*cols, vspots.begin());
}
vector<double> vtimes;
{
size_t rows = times->rows;
size_t cols = times->columns;
double* numbers = times->array;
vtimes.resize(rows*cols);
copy(numbers, numbers + rows*cols, vtimes.begin());
}
auto results = dupireCalib(vspots, maxDs, vtimes, maxDt,
spot, vol, jmpIntens, jmpAverage, jmpStd);
// Return
return from_labelledMatrix(results.spots, results.times, results.lVols);
}
extern "C" __declspec(dllexport)
LPXLOPER12 xDupireSuperbucket(
// Merton market parameters
const double spot,
const double vol,
const double jmpIntens,
const double jmpAverage,
const double jmpStd,
// risk view
FP12* strikes,
FP12* mats,
// calibration
FP12* spots,
double maxDs,
FP12* times,
double maxDtVol,
// MC
double maxDtSimul,
// product
LPXLOPER12 productid,
LPXLOPER12 xPayoffs,
FP12* xNotionals,
// numerical parameters
double useSobol,
double seed1,
double seed2,
double numPath,
double parallel,
// bump or AAD?
double bump)
{
FreeAllTempMemory();
// Make sure the last input is given
if (numPath <= 0) return TempErr12(xlerrNA);