-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLV_calibration_WR_test_main.cpp
1557 lines (1350 loc) · 48.2 KB
/
SLV_calibration_WR_test_main.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
#include <cmath>
#include <iostream>
#include <algorithm>
#include <memory>
#include <vector>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp> //subrange,subslice
#include <boost/numeric/ublas/matrix_proxy.hpp> //w = row(A, i); w = column(A, j); // a row or column of matrix as a vector
#include <boost/numeric/ublas/banded.hpp>//ublas::banded_matrix<double> M(nx, nx, 1, 1);//size1,size2,lower,upper; 是否有求解三对角矩阵的线性方程组程序?
#include <boost/numeric/ublas/lu.hpp>//permutation_matrix,用于LU分解
#include <boost/numeric/ublas/triangular.hpp>//solve函数,用于求解上三角或下三角线性方程组:V = solve(M, f, ublas::lower_tag());
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/assignment.hpp>//赋值符号<<==给matrix和vector赋值
#include <boost/numeric/ublas/io.hpp> //使用std::cout输出matrix和vector
#include "CumNorm.h"
//#include <math.h>
namespace ublas = boost::numeric::ublas;
namespace interp = boost::math::interpolators;
using namespace std;
#include<Core>
#include<SVD>
#include<Dense>
struct hestonParas
{
double theta;
double kappa;
double VoV; //xi
double rho;
double Y0; // v0
};
template<typename _Matrix_Type_>
_Matrix_Type_ pseudoInverse(const _Matrix_Type_ &a, double epsilon =
std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}
void fnexp(
ublas::vector<double> b,
ublas::vector<double>& d
)
{
for (size_t i = 0; i < b.size(); ++i)d(i) = exp(b(i));
}
void stackvector(
ublas::vector<double> Y,
double para,
ublas::matrix<double>& matrix0
)
{
//throw"The para must be set to 1 or 2.";
size_t n = Y.size();
matrix0.resize(n, n);
if (para == 1)
{
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)matrix0(i, j) = Y[i];
}
else if (para == 2)
{
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < n; ++j)matrix0(i, j) = Y[j];
}
matrix0 = ublas::trans(matrix0);
}
//create the initial prob density
void yutian_initial(
ublas::vector<double> S, //stock direction grid
ublas::vector<double> z, //v direction grid
hestonParas hp, //heston paramaters
double r, //interest rate
double q, //dividend
double sigma_initial, //sigma at t = 0
double dt, //
ublas::matrix<double>& initial_p //return: initial matrix of prob density
)
{
//S is a column vector, z is a row vector
//sigma_initial = sigma(S0, 0)
size_t rows = S.size();
size_t cols = z.size();
double mu_S = ((r - q) - 0.5 * pow(sigma_initial, 2.0) * hp.Y0) * dt;
double sigma_S = sigma_initial * sqrt(hp.Y0) * sqrt(dt);
double mu_z = ((hp.kappa * hp.theta - 0.5 * pow(hp.VoV, 2)) / hp.Y0 - hp.kappa) * dt;
double sigma_z = hp.VoV * sqrt(dt / hp.Y0);
initial_p.resize(rows, cols);
double PI = boost::math::constants::pi<double>();
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < z.size(); ++j)
{
initial_p(i, j) = 1 / (2 * PI * sigma_S * sigma_z * sqrt(1.0 - pow(hp.rho, 2))) *
exp(
-(
pow((S(i) - mu_S), 2) / pow(sigma_S, 2) +
(pow(z(j) - mu_z, 2) / pow(sigma_z, 2) - (2 * hp.rho * (S(i) - mu_S) * (z(j) - mu_z) / (sigma_S * sigma_z))) / (2 * (1 - pow(hp.rho, 2)))
)
);
}
}
}
void bisection_initial(
ublas::vector<double> S, //stock direction grid
ublas::vector<double> z, //v direction grid
hestonParas hp, //heston paramaters
double r, //interest rate
double q, //dividend
double sigma_initial, //sigma at t = 0
double& dt, //return: initial value: 1e-5
ublas::matrix<double>& initial_p //return: initial matrix of prob density
)
{
double dt1 = 1e-5;
double dt2 = 0.1;
double er = 1e-10;
double int_diff = 1.0;
size_t nS = S.size();
size_t nz = z.size();
double S_max = S[nS - 1], S_min = S[0]; // 最大最小值,s,z是顺序生成的?
double z_max = z[nz - 1], z_min = z[0];
double dS = (S_max - S_min) / (nS - 1);
double dz = (z_max - z_min) / (nz - 1);
dt = dt1;
double t = 1.0;
//这里如果条件不加<15就会一直循环下去
while ((int_diff) > er && t<15)
{
yutian_initial(S, z, hp, r, q, sigma_initial, dt, initial_p);
double int_p = 0;
for (size_t i = 0; i < nS - 1; ++i)
{
for (size_t j = 0; j < nz - 1; ++j)
{
int_p = int_p + (1.0 / 4.0) * (initial_p(i, j) + initial_p(i + 1, j) + initial_p(i, j + 1), initial_p(i + 1, j + 1)) * dz * dS;
}
}
int_diff = int_p - 1;
if (int_diff < 0)dt2 = dt;
else dt1 = dt;
dt = (dt1 + dt2) / 2.0;
t = t + 1;
if (t > 15)throw"The partition is too sparse";
}
for (size_t i = 0; i < initial_p.size1(); ++i)
for (size_t j = 0; j < initial_p.size2(); ++j)
if (initial_p(i, j) < 0)initial_p(i, j) = 0;
}
void identify_neighbors(
ublas::matrix<double> A,
ublas::matrix<double> nan_list,
ublas::matrix<double> talks_to,
ublas::matrix<double>& neighbor_list
)
{
size_t n1 = A.size1();
size_t n2 = A.size2();
size_t n3 = nan_list.size1();
if (n3 != 0)
{
size_t nan_count = nan_list.size1();
size_t talk_count = talks_to.size1();
ublas::matrix<double> nn(nan_count * talk_count, 2, 0);
//可能有误
for (size_t i = 0; i < talk_count; ++i)
{
for (size_t k = 0; k < nan_count; ++k)
{
nn(i * nan_count + k, 0) = nan_list(k, 1) + talks_to(i, 0);
nn(i * nan_count + k, 1) = nan_list(k, 2) + talks_to(i, 1);
}
}
//drop those nodes which fall outside the bounds of the original array
double t1 = 0;
for (size_t i = 0; i < nn.size1(); i++)
{
if (nn(i, 0) < 1 || nn(i, 0) > n1 || nn(i, 1) < 1 || nn(i, 1) > n2)t1 = t1 + 1;
}
ublas::matrix<double> L(nn.size1() - t1, 2, 0);
double count = 0;
for (size_t i = 0; i < nn.size1(); i++) //删掉没有在范围内的点,得到更新后的nn,也就是L
{
if (nn(i, 0) < 1 || nn(i, 0) > n1 || nn(i, 1) < 1 || nn(i, 1) > n2) {}
else
{
L(count, 0) = nn(i, 0);
L(count, 1) = nn(i, 1);
count = count + 1;
}
}
ublas::matrix<double> neighbor_list0(L.size1(), 3);
for (size_t i = 0; i < L.size1(); i++)
{
for (size_t j = 0; j < 3; j++)
{
if (j > 0) {
neighbor_list0(i, j) = L(i, j - 1);
}
else {
neighbor_list0(i, j) = (L(i, 1) - 1) *L.size1() + L(i, 0);
}
}
}
//把ublas vector 转成 std vector做了,因为前者好像不能用unique这些函数
// 两者之间函数可以封成函数,但目前没有做
//把neighbor_list转换
vector<vector<int> > v1(neighbor_list0.size1()); //行
for (int i = 0; i < v1.size(); i++) {
v1[i].resize(neighbor_list0.size2()); //列
for (int j = 0; j < v1[i].size(); j++) {
v1[i][j] = neighbor_list0(i, j); //赋值
}
}
sort(v1.begin(), v1.end());
v1.erase(unique(v1.begin(), v1.end()), v1.end()); // unique那一步
//把nan_list转换
vector<vector<int> > v2(nan_list.size1()); //行
for (int i = 0; i < v2.size(); i++) {
v2[i].resize(nan_list.size2()); //列
for (int j = 0; j < v2[i].size(); j++) {
v2[i][j] = nan_list(i, j); //赋值
}
}
sort(v2.begin(), v2.end());
v2.erase(unique(v2.begin(), v2.end()), v2.end());
//比较二者差异
vector<vector<int> >diff;
std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::inserter(diff, diff.begin()));
//把得到的结果再转回到ublas vector
ublas::matrix<double> trans(diff.size(), diff[0].size());
for (size_t i = 0; i < diff.size(); i++)
{
for (int j = 0; j < diff[i].size(); j++) {
trans(i, j) = diff[i][j]; //赋值
}
}
ublas::matrix<double> neighbor_list = trans;
}
else {
ublas::vector<double> neighbor_list(0);
}
}
void inpaint_nans(
ublas::matrix<double> A,
int k,
ublas::matrix<double>& B
)
{
size_t n1 = A.size1();
size_t n2 = A.size2();
vector<vector<double> > nan_list;
vector<vector<double> > known_list;
double k1 = 0;
double k2 = 0;
for (size_t j = 0; j < n2; j++)
{
for (size_t i = 0; i < n1; i++)
{
//if (std::isnan(A(i, j)))
if (std::isnan(A(i, j)) || std::isinf(A(i, j)))
{
nan_list.push_back(vector<double>(3));
nan_list[k1][0] = j * n1 + i;
nan_list[k1][1] = i;
nan_list[k1][2] = j;
k1 = k1 + 1;
}
else
{
known_list.push_back(vector<double>(2));
known_list[k2][0] = j * n1 + i;
known_list[k2][1] = A(i, j);
k2 = k2 + 1;
}
}
}
B.resize(A.size1(), A.size2());
B=A;
if(nan_list.size()==0){}
else{
//把 标准vector转化成 ublas
ublas::matrix<double> nan_list1(nan_list.size(), 3);
for (int i = 0; i < nan_list.size(); i++)
{
for (int j = 0; j < 3; j++) {
nan_list1(i, j) = nan_list[i][j]; //赋值
}
}
ublas::matrix<double> talks_to(12, 2);
talks_to <<= -2, 0,
-1, -1,
-1, 0,
-1, 1,
0, -2,
0, -1,
0, 1,
0, 2,
1, -1,
1, 0,
1, 1,
2, 0;
ublas::matrix<double> neighbor_list;
identify_neighbors(A, nan_list1, talks_to, neighbor_list);
//可用subrange优化
ublas::matrix<double> all_list(nan_list1.size1() + neighbor_list.size1(), 3);
for (size_t i = 0; i < all_list.size1(); i++)
{
for (size_t j = 0; j < 3; j++)
{
if (i < nan_list1.size1())
{
all_list(i, j) = nan_list1(i, j);
}
else
{
all_list(i, j) = neighbor_list(i - nan_list1.size1(), j);
}
}
}
vector<double> L1, L2, L3, L4;
for (size_t i = 0; i < all_list.size1(); i++)
{
if (all_list(i, 1) >= 3 && all_list(i, 1) <= n1-2 && all_list(i, 2) >= 3 && all_list(i, 2) <= n2 - 2)
{
L1.push_back(i);
}
if ((((all_list(i, 1) == 2) || (all_list(i, 1) == (n1 - 1))) && (all_list(i, 2) >= 2) && (all_list(i, 2) <= (n2 - 1))) || (((all_list(i, 2) == 2) || (all_list(i, 2) == (n2 - 1))) && (all_list(i, 1) >= 2) && (all_list(i, 1) <= (n1 - 1))))
{
L2.push_back(i);
}
if (((all_list(i, 1) == 1) || (all_list(i, 1) == n1)) && (all_list(i, 2) >= 2) && (all_list(i, 2) <= (n2 - 1)))
{
L3.push_back(i);
}
if (((all_list(i, 2) == 1) || (all_list(i, 2) == n2)) && (all_list(i, 1) >= 2) && (all_list(i, 1) <= (n1 - 1)))
{
L4.push_back(i);
}
}
ublas::compressed_matrix<double> fda(n1 * n2, n1 * n2);
if (L1.size() > 0)
{
ublas::vector<double> T1(13);
T1 <<= -2 * n1, -n1 - 1, -n1, -n1 + 1, -2, -1, 0, 1, 2, n1 - 1, n1, n1 + 1, 2 * n1;
ublas::vector<double> T2(13);
T2 <<= 1, 2, -8, 2, 1, -8, 20, -8, 1, 2, -8, 2, 1;
for (size_t i = 0; i < L1.size(); i++)
{
for (size_t j = 0; j < 13; j++)
{
fda(all_list(L1[i]), all_list(L1[i]) + T1(j)) = T2(j);
}
}
}
else
{
ublas::compressed_matrix<double> fda(n1 * n2, n1 * n2, all_list.size1() * 5);
}
if (L2.size() > 0)
{
ublas::vector<double> T1(5);
T1 <<= -n1, -1, 0, 1, n1;
ublas::vector<double> T2(5);
T2 <<= 1, 1, -4, 1, 1;
for (size_t i = 0; i < L2.size(); i++)
{
for (size_t j = 0; j < 5; j++)
{
fda(all_list(L2[i]), all_list(L2[i]) + T1(j)) += T2(j);
}
}
}
if (L3.size() > 0)
{
ublas::vector<double> T1(3);
T1 <<= -n1, -0, n1;
ublas::vector<double> T2(3);
T2 <<= 1, -2, 1;
for (size_t i = 0; i < L3.size(); i++)
{
for (size_t j = 0; i < 3; i++)
{
fda(all_list(L3[i]), all_list(L3[i]) + T1(j)) += T2(j);
}
}
}
if (L4.size() > 0)
{
ublas::vector<double> T1(3);
T1 <<= -1, -0, 1;
ublas::vector<double> T2(3);
T2 <<= 1, -2, 1;
for (size_t i = 0; i < L4.size(); i++)
{
for (size_t j = 0; i < 3; i++)
{
fda(all_list(L4[i]), all_list(L4[i]) + T1(j)) += T2(j);
}
}
}
ublas::matrix<double> fdaknown(fda.size1(), known_list.size());
for (size_t i = 0; i < fdaknown.size1(); i++)
{
for (size_t j = 0; j < fdaknown.size2(); j++) {
fdaknown(i, j) = fda(i, known_list[j][0]);
}
}
ublas::vector<double> Aknown(known_list.size());
for (size_t i = 0; i < Aknown.size(); i++)Aknown(i) = known_list[i][1];
ublas::vector<double> rhs = -prod(fdaknown, Aknown);
// //找到非零行
vector<double> k0;
for (size_t i = 0; i < n1 * n2; i++)
{
for (size_t j = 0; j < nan_list1.size1(); j++)
{
if (fda(i, nan_list1(j, 0)) == 0)
{
k0.push_back(i);
break;
}
}
}
// cout << k0.size() << std::endl;
//cout << nan_list.size() << std::endl;
Eigen::MatrixXd A0(k0.size(),nan_list1.size1());
for (size_t i = 0; i < k0.size(); i++)
{
for (size_t j = 0; j < nan_list1.size1(); j++)
{
A0(i,j)=1/i+1/j;
(k0[i],nan_list1(j,0));
}
}
Eigen::VectorXd rhs0(k0.size());
for (size_t i = 0; i < k0.size(); i++)
{
rhs0(i)=i;
//rhs(k0[i]);
}
Eigen::VectorXd b2(nan_list.size());
b2=pseudoInverse(A0)*rhs0;
for (size_t i = 0; i < nan_list1.size1(); i++)
{
B(nan_list1(i,0))=b2(i);
}
//cout << A << std::endl;
}
}
void OptionPrice(
ublas::vector<double>x,
ublas::vector<double>z,
ublas::vector<double>L,
ublas::vector<double>V_initial,
ublas::matrix<double>psi,
double T1,
double T2,
size_t nt,
double r,
double q,
double y0,
int call_put,//call:1, put:-1
ublas::vector<double>& V //return
)
{
// if (call_put != 1 || call_put != -1) throw"error of option type, call_put must set to 1(call), or -1(put).";
//M为稀疏矩阵
size_t nx = x.size();
size_t nz = z.size();
double x_max = x[nx - 1], x_min = x[0];
double z_max = z[nz - 1], z_min = z[0];
double dx = (x_max - x_min) / (nx - 1);
double dt = (T2 - T1) / (nt - 1);
double dz = (z_max - z_min) / (nz - 1);
ublas::vector<double>z1(z.size());
fnexp(z,z1);
ublas::vector<double>z2(z.size(),1);
ublas::vector<double>C(z.size());
ublas::vector<double>kb1=prod(psi,z1);
ublas::vector<double>kb2=prod(psi,z2);
C = y0 * element_div( kb1* dz, kb2* dz);
//这里本不应注释
// ublas::matrix<double> C1(C.size(),1);
// for (size_t i = 0; i < C1.size1(); i++)C1(i,0)=C(i);
// //std::cout<< C1<<std::endl;
// inpaint_nans(C1, 3, C1);
// ublas::vector<double> C2(C1.size1());
// for (size_t i = 0; i < C2.size(); i++)C2(i)=C1(i,0);
// C=C2;
for (size_t i = 0; i < C.size(); i++)
{
if (C(i)< 0)C(i) = 0;
}
ublas::vector<double> A(nx - 2), A_plus(nx - 2), A_minus(nx - 2);
for (size_t i = 0; i < A.size(); ++i)
{
A(i) = 1 + L(i + 1) * L(i + 1) * C(i + 1) * dt / dx / dx + dt * q;
A_plus(i) = (r - q + 0.5 * L(i + 1) * L(i + 1) * C(i + 1)) * dt / 2 / dx - 0.5 * L(i + 1) * L(i + 1) * C(i + 1) * dt / dx / dx;
A_minus(i) = -(r - q + 0.5 * L(i + 1) * L(i + 1) * C(i + 1)) * dt / 2 / dx - 0.5 * L(i + 1) * L(i + 1) * C(i + 1) * dt / dx / dx;
}
//以三对角矩阵(banded_matrix)存储M
//ublas::banded_matrix<double> M(nx, nx, 1, 1);//size1,size2,lower,upper; Allocates an uninitialized banded_matrix that holds (lower + 1 + upper) diagonals
ublas::matrix<double> M(nx, nx,0);
M(0, 0) = 1.0; M(0, 1) = -1.0;
M(nx - 1, nx - 1) = 1.0; M(nx - 1, nx - 2) = -1.0;
for (size_t i = 1; i < nx - 1; ++i)
{
for (size_t j = i - 1; j <= i + 1; ++j)
{
if (j == i - 1) M(i, j) = A_minus(i - 1); //upper, (i,j)=(1,0)处存储的是A_minus(0)
else if (j == i)M(i, j) = A(i - 1); //mid, (i,j)=(1,1)处存储的是A(0)
else if (j == i + 1) M(i, j) = A_plus(i - 1); //lower, (i,j)=(1,2)处存储的是A_plus(0)
}
}
V = V_initial;
for (size_t t = 1; t < nt; ++t)
{
//ublas::vector<double> f = V;
if (call_put == 1)
{
V(0) = exp(x(0)) * exp(-r * (T1 + t * dt)) * dx + 1.0 / 2.0 * exp(x(0)) * exp(-r * (T1 + t * dt)) * pow(dx, 2) + 1 / 6 * exp(x(0)) * exp(-r * (T1 + t * dt)) * pow(dx, 3) +
1.0 / 24.0 * exp(x(0)) * exp(-r * (T1 + t * dt)) * pow(dx, 4) + 1.0 / 120.0 * exp(x(0)) * exp(-r * (T1 + t * dt)) * pow(dx, 5);
V(V.size() - 1) = 0.0;
}
else if (call_put == -1)
{
V(0) = 0.0;
V(V.size() - 1) = exp(x(nx - 1)) * exp(-r * (T1 + t * dt)) * dx + 1.0 / 2.0 * exp(x(nx - 1)) * exp(-r * (T1 + t * dt)) * pow(dx, 2) + 1 / 6 * exp(x(nx - 1)) * exp(-r * (T1 + t * dt)) * pow(dx, 3) +
1.0 / 24.0 * exp(x(nx - 1)) * exp(-r * (T1 + t * dt)) * pow(dx, 4) + 1.0 / 120.0 * exp(x(nx - 1)) * exp(-r * (T1 + t * dt)) * pow(dx, 5);
}
//Matlab: V = M\f
//===============LU分解求解线性方程组======================
//link1: https://stackoverflow.com/questions/1225411/boosts-linear-algebra-solution-for-y-ax(关于ublas中如何使用LU分解)
//link2:https://stackoverflow.com/questions/26404106/what-does-lu-factorize-return(关于LU分解)
ublas::permutation_matrix<size_t> pm(M.size1());//列主元的高斯消元法
int res = lu_factorize(M, pm);//LU分解并将L(对角线为1)保存在M左下角;U(对角线不为1)保存在M右上角
if (res != 0)throw"error in LV decomposition.";
else {
lu_substitute(M, pm, V);//求解线性方程组,MV=f,并将结果保存在f中
}
// //===============LU分解求解线性方程组======================
for (size_t i = 0; i < V.size(); i++)
{
if(V(i)<0)V(i)=0;
}
// // for (auto iter = V.begin(); iter < V.end(); ++iter)
// // if (*iter < 0)*iter = 0.0;
}
}
void finite_difference_mat_b2(
ublas::vector<double> x,
ublas::vector<double> z,
hestonParas hp,
ublas::matrix<double>& T_x1,
ublas::matrix<double>& T_x2,
ublas::matrix<double>& T_z1,
ublas::matrix<double>& T_z2,
ublas::matrix<double>& T_zb1,
ublas::matrix<double>& T_zb2
)
{
size_t nx1 = x.size();
size_t nz1 = z.size();
ublas::vector<double> dx = subrange(x, 1, nx1) - subrange(x, 0, nx1 - 1);
ublas::vector<double> dx_i_minus = subrange(dx, 0, dx.size() - 1);
ublas::vector<double> dx_i = subrange(dx, 1, dx.size());
ublas::vector<double> c_x_minus = -element_div(dx_i, element_prod(dx_i_minus, dx_i_minus + dx_i));
ublas::vector<double> c_x = element_div(dx_i - dx_i_minus, element_prod(dx_i_minus, dx_i));
ublas::vector<double> c_x_plus = element_div(dx_i_minus, element_prod(dx_i, dx_i_minus + dx_i));
c_x(0) = c_x(0) + c_x_minus(0);
c_x(c_x.size() - 1) = c_x(c_x.size() - 1) + c_x_plus(c_x_plus.size() - 1);
ublas::vector<double> one1(dx_i_minus.size(), 2);
ublas::vector<double> x_x_minus = element_div(one1, element_prod(dx_i_minus, dx_i_minus + dx_i));
ublas::vector<double> x_x = -element_div(one1, element_prod(dx_i_minus, dx_i));
ublas::vector<double> x_x_plus = element_div(one1, element_prod(dx_i, dx_i_minus + dx_i));
x_x(0) = x_x(0) + x_x_minus(0);
x_x(x_x.size() - 1) = x_x(x_x.size() - 1) + x_x_plus(x_x_plus.size() - 1);
//这段还未检查
T_x1.resize(nx1 - 2, nx1 - 2);
T_x2.resize(nx1 - 2, nx1 - 2);
for (size_t i = 1; i < nx1 - 3; ++i)
{
for (size_t j = i - 1; j <= i + 1; ++j)
{
if (j == i - 1) T_x1(i, j) = c_x_minus(i - 1), T_x2(i, j) = x_x_minus(i - 1);
else if (j == i)T_x1(i, j) = c_x(i - 1), T_x2(i, j) = x_x(i - 1);
else if (j == i + 1) T_x1(i, j) = c_x_plus(i - 1), T_x2(i, j) = x_x_plus(i - 1);
}
}
ublas::vector<double> dz = subrange(z, 1, nz1) - subrange(z, 0, nz1 - 1);
ublas::vector<double> dz_i_minus = subrange(dz, 0, dz.size() - 1);
ublas::vector<double> dz_i = subrange(dz, 1, dz.size());
ublas::vector<double> c_z_minus = -element_div(dz_i, element_prod(dz_i_minus, dz_i_minus + dz_i));
ublas::vector<double> c_z = element_div(dz_i - dz_i_minus, element_prod(dz_i_minus, dz_i));
ublas::vector<double> c_z_plus = element_div(dz_i_minus, element_prod(dz_i, dz_i_minus + dz_i));
// here we give a boundary condition for pe^z
ublas::vector<double> c_z_b = c_z;
c_z_b(0) = c_z_b(0) + (1 + dz_i_minus(0) - dz_i_minus(0) * 2 * hp.kappa * hp.theta / pow(hp.VoV, 2)) * c_z_minus(0);
//
// c_z(0) = c_z(0) + c_z_minus(0);
// c_z(c_z.size() - 1) = c_z(c_z.size() - 1) + c_z_plus(c_z_plus.size() - 1);
c_z=c_z_b;
ublas::vector<double> one0(dz_i_minus.size(), 2);
ublas::vector<double> x_z_minus = element_div(one0, element_prod(dz_i_minus, dz_i_minus + dz_i));
ublas::vector<double> x_z = -element_div(one0, element_prod(dz_i_minus, dz_i));
ublas::vector<double> x_z_plus = element_div(one0, element_prod(dz_i, dz_i_minus + dz_i));
// here we give a boundary condition for pe^-z from zero flux condition
ublas::vector<double> x_z_b = x_z;
x_z_b(0) = x_z_b(0) + (1 + dz_i_minus(0) - dz_i_minus(0) * 2 * hp.kappa * hp.theta / pow(hp.VoV, 2)) * x_z_minus(0);
//
// x_z(0) = x_z(0) + x_z_minus(0);
// x_z(x_z.size() - 1) = x_z(x_z.size() - 1) + x_z_plus(x_z_plus.size() - 1);
x_z=x_z_b;
// //这里还没有检查?
T_z1.resize(nz1 - 2, nz1 - 2);
T_z2.resize(nz1 - 2, nz1 - 2);
T_zb1.resize(nz1 - 2, nz1 - 2);
T_zb2.resize(nz1 - 2, nz1 - 2);
for (size_t i = 1; i < nz1 - 3; ++i)
{
for (size_t j = i - 1; j <= i + 1; ++j)
{
if (j == i - 1){
T_z1(i, j) = c_z_minus(i - 1);
T_z2(i, j) = x_z_minus(i - 1);
T_zb1(i, j) = c_z_minus(i - 1);
T_zb2(i, j) = x_z_minus(i - 1);
}
else if (j == i){
T_z1(i, j) = c_z(i - 1);
T_z2(i, j) = x_z(i - 1);
T_zb1(i, j) = c_z_b(i - 1);
T_zb2(i, j) = x_z_b(i - 1);
}
else if (j == i + 1){
T_z1(i, j) = c_z_plus(i - 1);
T_z2(i, j) = x_z_plus(i - 1);
T_zb1(i, j) = c_z_plus(i - 1);
T_zb2(i, j) = x_z_plus(i - 1);
}
}
}
}
void adi_p_b(ublas::vector<double> x,
ublas::vector<double> z,
ublas::vector<double> sigma,
ublas::matrix<double> initial_p,
double t_start,
double t_end,
double dt,
hestonParas& hp,
double r,
double q,
double alpha,
ublas::matrix<double>& psi
)
{
size_t nt = floor((t_end - t_start) / dt + 1);
ublas::vector<double> t(nt);
for (size_t i = 0; i < nt; ++i)t(i) = t_start + i * dt;
size_t nx2 = x.size();
size_t nz2 = z.size();
double dx = (x(nx2 - 1) - x(0)) / (nx2 - 1);
double dz = (z(nz2 - 1) - z(0)) / (nz2 - 1);
ublas::matrix<double> T_S1, T_S2, T_z1, T_z2, T_zb1, T_zb2;
finite_difference_mat_b2(x, z, hp, T_S1, T_S2, T_z1, T_z2, T_zb1, T_zb2);
ublas::matrix<double> p = subrange(initial_p, 1, nx2 - 1, 1, nz2 - 1);//这里或许可以改进用切片
ublas::vector<double> x_inner(nx2 - 2);//可以改进用切片
ublas::vector<double> z_inner(nz2 - 2);
ublas::vector<double> sigma_inner(sigma.size() - 2);
for (size_t i = 0; i < x_inner.size(); ++i)x_inner(i) = x(i + 1);
for (size_t i = 0; i < z_inner.size(); ++i)z_inner(i) = z(i + 1);
for (size_t i = 0; i < sigma_inner.size(); ++i)sigma_inner(i) = sigma(i + 1);
psi.resize(nx2, nz2);
for (size_t i = 0; i <nx2; i++)
{
for (size_t j = 0; j < nz2; j++)
{
psi(i,j)=0;
}
}
for (size_t i = 0; i < nt - 1; ++i)//共nt-1次循环
{
//std::cout << 5 << std::endl;
ublas::matrix<double> sv;
stackvector(sigma_inner, 1, sv);
ublas::matrix<double> f0p1=element_prod(sv, T_S1);
ublas::matrix<double> f0p2=prod(f0p1,p);
ublas::matrix<double> F0p = hp.VoV * hp.rho * prod(f0p2, T_z1);
// ublas::matrix<double> F0p = hp.VoV * hp.rho *p;
ublas::vector<double> temp(z_inner.size());
for (size_t j = 0; j < z_inner.size(); ++j)temp(j) = exp(-z_inner(j)) / hp.Y0;
stackvector(temp, 2, sv);
ublas::matrix<double> F1 = -(hp.kappa * hp.theta - 0.5 * pow(hp.VoV,2)) * element_prod(T_zb1, sv) + hp.kappa * T_z1 + 0.5 * pow(hp.VoV,2)* element_prod(T_zb2, sv);
//这里如果添加上后两项会变得不ok
temp.resize(sigma_inner.size());
for (size_t j = 0; j < sigma_inner.size(); ++j)temp(j) = sigma_inner(j) * sigma_inner(j);
stackvector(temp, 1, sv);
ublas::matrix<double> temp1(z_inner.size(),z_inner.size());
for (size_t i = 0; i < temp1.size1(); i++)
{
temp1(i,i) = exp(z_inner(i)) * hp.Y0;
}
ublas::matrix<double> f2p1=element_prod(sv, T_S1);
ublas::matrix<double> f2p2=prod(f2p1, p);
ublas::matrix<double> f2p3=element_prod(sv, T_S2);
ublas::matrix<double> f2p4=prod(f2p3, p);
ublas::matrix<double> F2p = (r - q) * prod(T_S1, p) + 0.5 * prod(f2p2, temp1) + 0.5 * prod(f2p4, temp1);
ublas::matrix<double> A = p + dt * (F0p + prod(p, F1) + F2p);
ublas::matrix<double> B1 = (A - alpha * dt * prod(p, F1));
ublas::matrix<double> B2(nz2 - 2, nz2 - 2);
for (size_t i = 0; i < nz2 - 2; i++)B2(i, i) = 1;
ublas::matrix<double> B3 = B2 - alpha * dt * F1;
ublas::permutation_matrix<size_t> pm2(B3.size1());
int res3 = lu_factorize(B3, pm2);
if (res3 != 0)throw"error in LV decomposition.";
ublas::matrix<double> B3inv = ublas::identity_matrix<double>(B3.size1());
lu_substitute(B3, pm2, B3inv);
ublas::matrix<double> B = prod(B1, B3inv);
// // std::cout << B.size1() <<std::endl;
//ublas::matrix<double> B=A;
ublas::compressed_matrix<double>I1(nx2 - 2, nx2 - 2);
for (size_t i = 0; i < nx2 - 2; i++)I1(i, i) = 1;
ublas::compressed_matrix<double>K1((nx2 - 2) * (nz2 - 2), (nx2 - 2) * (nz2 - 2), 0);
ublas::compressed_matrix<double>K2((nx2 - 2) * (nz2 - 2), (nx2 - 2) * (nz2 - 2), 0);
ublas::compressed_matrix<double>K4((nx2 - 2) * (nz2 - 2), (nx2 - 2) * (nz2 - 2), 0);
ublas::compressed_matrix<double>K3((nx2 - 2) * (nz2 - 2), (nx2 - 2) * (nz2 - 2), 0);
for (size_t i = 0; i < nz2 - 2; i++)
{
subrange(K1, i * (nx2 - 2), (i + 1) * (nx2 - 2) , i * (nx2 - 2), (i + 1) * (nx2 - 2) ) = T_S1;
subrange(K2, i * (nx2 - 2), (i + 1) * (nx2 - 2) , i * (nx2 - 2), (i + 1) * (nx2 - 2)) = f2p1;
subrange(K4, i * (nx2 - 2), (i + 1) * (nx2 - 2) , i * (nx2 - 2), (i + 1) * (nx2 - 2)) = f2p3;
subrange(K3, i * (nx2 - 2), (i + 1) * (nx2 - 2) , i * (nx2 - 2), (i + 1) * (nx2 - 2)) = exp(z_inner(i)) * hp.Y0 * I1;
}
ublas::compressed_matrix<double>I2((nx2 - 2)*(nz2-2), (nx2 - 2)*(nz2-2),0);
for (size_t i = 0; i < (nx2 - 2)*(nz2-2); i++)I2(i, i) = 1;
ublas::compressed_matrix<double> Cnew((nx2 - 2)*(nz2 - 2), (nx2 - 2)*(nz2 - 2),0);
Cnew= I2-alpha*dt*(-(r-q)*K1+0.5*prod(K3,K2)+0.5*prod(K3,K4));
//Cnew=I2;
ublas::vector<double> resh((nx2-2)*(nz2-2));
for (size_t i = 0; i < nx2-2; i++)
{
for (size_t j = 0; j < nz2-2; j++)
{
resh(j*(nx2-2)+i)=B(i,j)-alpha*dt*F2p(i,j);
}
}
ublas::permutation_matrix<size_t> pm1(Cnew.size1());
int res2 = lu_factorize(Cnew, pm1);
lu_substitute(Cnew, pm1, resh);
ublas::matrix<double> p(nx2 - 2, nz2 - 2);
for (size_t i = 0; i < nx2-2; i++)
{
for (size_t j = 0; j < nz2-2; j++)
{
if(resh(j*(nx2-2)+i)<0)p(i,j)=0;
//else p(i,j)=resh(j*(nx2-2)+i);
p(i,j)=1;
}
}
}
subrange(psi, 1, nx2 - 1, 1, nz2 - 1) = p;
}
void meshgrid(ublas::vector<double>& x, ublas::vector<double>& y, ublas::matrix<double>& X, ublas::matrix<double>& Y)
{
/*
* X : x作为每一行,共y.size()行
* Y :y作为每一列, 共x.size()列
*
* matlab:
[A,B]=Meshgrid(a,b)
生成size(b)Xsize(a)大小的矩阵A和B。它相当于a从一行重复增加到size(b)行,把b转置成一列再重复增加到size(a)列
*/
size_t nx = x.size();
size_t ny = y.size();
X.resize(ny, nx);
Y.resize(ny, nx);
for (size_t i = 0; i < ny; ++i)
for (size_t j = 0; j < nx; ++j)
{
X(i, j) = x[j];
Y(i, j) = y[i];
}
}
static double Mtools_T[] = {
9.60497373987051638749E0, 9.00260197203842689217E1, 2.23200534594684319226E3,
7.00332514112805075473E3, 5.55923013010394962768E4 };
static double Mtools_U[] = {/* 1.00000000000000000000E0,*/
3.35617141647503099647E1, 5.21357949780152679795E2, 4.59432382970980127987E3,
2.26290000613890934246E4, 4.92673942608635921086E4 };
static double Mtools_nep[] = {
2.46196981473530512524E-10, 5.64189564831068821977E-1, 7.46321056442269912687E0,
4.86371970985681366614E1, 1.96520832956077098242E2, 5.26445194995477358631E2,
9.34528527171957607540E2, 1.02755188689515710272E3, 5.57535335369399327526E2 };
static double Mtools_neq[] = {/* 1.00000000000000000000E0,*/
1.32281951154744992508E1, 8.67072140885989742329E1, 3.54937778887819891062E2,
9.75708501743205489753E2, 1.82390916687909736289E3, 2.24633760818710981792E3,
1.65666309194161350182E3, 5.57535340817727675546E2 };
static double Mtools_ner[] = {
5.64189583547755073984E-1, 1.27536670759978104416E0, 5.01905042251180477414E0,
6.16021097993053585195E0, 7.40974269950448939160E0, 2.97886665372100240670E0 };
static double Mtools_nes[] = {/* 1.00000000000000000000E0,*/
2.26052863220117276590E0, 9.39603524938001434673E0, 1.20489539808096656605E1,
1.70814450747565897222E1, 9.60896809063285878198E0, 3.36907645100081516050E0 };
double normal(double a)
{
double x;
double y;
double z;
x = a * DBA_SQRT2OVER2;
z = (x > 0 ? x : -x);
if (z < DBA_SQRT2OVER2)
y = 0.5 + 0.5 * normal_error(x);
else {
y = 0.5 * normal_error_comp(z);
if (x > 0.0)
y = 1.0 - y;
}
return(y);
}
inline
double polynomial_1(double x, double coef[], int N)
{
double* p = coef;
double ans = x + *p++;
int i = N - 1;
do ans = ans * x + *p++;
while (--i);
return(ans);
}
inline
double polynomial(double x, double coef[], int N)
{
double* p = coef;
double ans = *p++;
do ans = ans * x + *p++;
while (--N);
return(ans);
}
double normal_error(double x)
{
double y;
double z;
z = x * x;
if (z > 1.0)
return(1.0 - normal_error_comp(x));
y = x * polynomial(z, Mtools_T, 4) / polynomial_1(z, Mtools_U, 5);
return(y);
}
double normal_error_comp(double a)
{
double p;
double q;
double x;
double y;
double z;
x = (a > 0.0 ? a : -a);
if (x < 1.0)
return(1.0 - normal_error(a));
z = -a * a;
if (z < -DBA_MAXLOG) {
under:
dba_error("normal_error_comp", DBA_ERROR_UNDERFLOW);
return(0.0);
}
z = exp(z);
if (x < 8.0) {
p = polynomial(x, Mtools_nep, 8);
q = polynomial_1(x, Mtools_neq, 8);
}
else {
p = polynomial(x, Mtools_ner, 5);
q = polynomial_1(x, Mtools_nes, 6);
}
y = (z * p) / q;
if (a < 0.0)
y = 2.0 - y;
if (y == 0.0)
goto under;
return(y);
}