In [1]:
NU_COPULA = 6.
N_SIM = 100000
In [2]:
import pandas as pd
import numpy as np
In [3]:
import os
dir_ = './transformed_input'
udls = pd.read_csv(os.path.join(dir_, 'UDL.csv'), header=0, index_col=0)
positions = pd.read_csv(os.path.join(dir_, 'positions.csv'), header=0, index_col=0)
In [4]:
udl_labels = udls.index.tolist()
In [5]:
correl_matrix = pd.read_excel('./input_files/correlation_matrix.xlsx')
correl_matrix.set_index('Pearson', inplace=True)
correl_matrix = correl_matrix[udl_labels]
correl_matrix = correl_matrix.transpose()[udl_labels]
covar_matrix = correl_matrix
In [6]:
from scipy.stats import multivariate_normal, chi2, t
gauss_rv = multivariate_normal(cov=covar_matrix).rvs(N_SIM).transpose()
chi2_rv = chi2(NU_COPULA).rvs(N_SIM)
mult_factor = np.sqrt(NU_COPULA / chi2_rv)
student_rvs = np.multiply(mult_factor, gauss_rv)
inv_student_rvs = t.cdf(student_rvs, NU_COPULA)
print inv_student_rvs.shape
gauss_rv = None
chi2_rv = None
mult_factor = None
student_rvs = None
(90L, 100000L)
In [7]:
losses_rvs = np.ndarray(inv_student_rvs.shape)
losses_rvs[:] = np.nan
udls_trans = udls.transpose()
for i, col in enumerate(udls_trans.columns):
params = udls_trans[col]
nu = params['Nu']
coeff = params['Coef']
val = params['UDL value']
crt_ppf = t.ppf(inv_student_rvs[i, :], nu)
losses_rvs[i, :] = -val * coeff * crt_ppf
losses_rvs
Out[7]:
array([[ -2.00918163e-01, -1.98904574e-01, 8.05547682e-01, ...,
7.02859260e-03, -3.34334806e-01, 3.81628349e-01],
[ -1.15058457e+00, -3.67421826e+00, 1.77541888e+00, ...,
7.59314048e+00, -2.28884150e-02, 1.00063290e+01],
[ -5.86133209e-02, -2.14075038e-01, 8.95697494e-01, ...,
2.77796415e-01, 5.93673260e-01, 5.19736725e-01],
...,
[ 1.81851590e-02, -7.23759219e-01, 1.95555926e+00, ...,
-1.62176401e-01, 5.58272297e-01, 7.09532721e-02],
[ -7.78091804e-02, -9.95199374e-01, 5.52746588e-01, ...,
3.99998478e-01, 9.13688940e-02, 1.36533745e+00],
[ -2.33881625e-01, -6.04025869e-02, -6.25818832e-04, ...,
4.72676402e-01, -1.15245243e+00, 7.15670886e-01]])
In [8]:
losses_df = pd.DataFrame(losses_rvs, index=udl_labels, columns=[r'$\omega_{%i}$' % i for i in xrange(N_SIM)])
losses_df
Out[8]:
$\omega_{0}$
$\omega_{1}$
$\omega_{2}$
$\omega_{3}$
$\omega_{4}$
$\omega_{5}$
$\omega_{6}$
$\omega_{7}$
$\omega_{8}$
$\omega_{9}$
...
$\omega_{99990}$
$\omega_{99991}$
$\omega_{99992}$
$\omega_{99993}$
$\omega_{99994}$
$\omega_{99995}$
$\omega_{99996}$
$\omega_{99997}$
$\omega_{99998}$
$\omega_{99999}$
AAI
-0.200918
-0.198905
0.805548
0.583668
-1.706628
-0.103197
0.201652
0.456547
0.640914
-0.505325
...
-0.078274
0.433510
-0.610485
-2.820888
1.985151
0.685405
0.003559
0.007029
-0.334335
0.381628
AEX
-1.150585
-3.674218
1.775419
3.664648
-6.985859
-2.927309
4.360093
0.959643
7.777305
-1.862348
...
0.578672
9.481559
-2.389425
-19.523459
15.682777
12.078140
-0.842923
7.593140
-0.022888
10.006329
AF
-0.058613
-0.214075
0.895697
0.269507
-0.315589
0.223574
0.277623
0.176089
0.149691
0.026785
...
0.000244
0.476596
-0.171294
-0.314697
0.383086
0.578728
-0.336255
0.277796
0.593673
0.519737
AGN
-0.026015
-0.138477
-0.099280
0.048946
-0.389255
0.095240
0.171380
0.146761
0.049076
-0.077695
...
0.019834
0.033442
-0.003632
-0.563368
0.931487
0.290534
0.027892
0.029634
0.158926
0.065737
AH
0.159044
-0.289840
1.701097
0.155359
-2.697122
-0.132729
0.242296
0.048544
0.368971
0.045669
...
0.140038
-0.144903
0.378085
0.039735
0.190374
0.162137
0.286393
0.991165
0.129587
0.751473
AHA
-0.073544
0.706702
0.143321
0.661208
0.674901
-1.047396
0.783650
0.249500
0.708006
-0.217687
...
-0.292071
2.313801
-1.198159
-0.472021
2.477632
1.177141
-0.368198
0.040826
1.827546
1.475319
AI
0.198540
-0.159675
-0.008743
-0.022520
-0.532079
-5.583746
0.675201
0.607766
-0.486662
-2.262635
...
0.777259
1.880116
0.539625
-3.751192
2.118027
1.207875
0.435211
2.028794
-3.808177
1.480489
AKZ
-0.774371
-0.257924
-0.697137
0.594904
-1.951710
-0.784442
0.962759
-1.325928
1.529619
-0.612038
...
1.241070
1.447305
-0.509786
0.819816
2.309963
1.017815
-0.886466
0.972120
-0.470946
2.013650
AL1
-0.108167
-0.366591
0.114330
-0.274369
0.498826
0.482861
-0.389156
0.090225
0.872098
0.215726
...
0.136355
0.006785
-0.848368
-0.841086
1.008975
0.436896
0.213393
0.242586
-0.064690
0.922087
ASL
-0.254408
1.470753
7.680641
2.308929
-0.608044
1.496898
1.358193
0.904747
1.379871
-2.390325
...
2.264694
4.211668
-1.458178
-11.921133
3.663944
1.775062
-0.844696
3.703363
-0.357974
2.882644
ASM
-0.299536
-1.004895
9.735716
1.154138
-0.481079
-0.393836
0.307606
-0.526609
0.325459
-0.763579
...
-0.257769
-0.105963
-0.778758
-1.732848
1.711813
0.353507
-1.685619
2.765604
0.058610
0.544559
ATO
-0.413164
0.153429
0.855195
-1.294039
-0.654207
-0.367193
2.997788
2.101002
1.134077
-2.286082
...
1.221900
2.518362
-6.321189
-0.077768
4.161350
2.670146
2.534764
-0.046750
-3.793378
0.978366
AVH
0.113835
-1.546836
3.857650
1.440852
-0.255862
-1.741676
6.879193
0.950529
2.174782
2.151362
...
-0.736939
1.077154
0.481388
3.799390
-0.623836
0.143220
-5.242203
1.927202
-3.752419
3.718366
BAM
-0.068603
0.026562
0.489512
0.006761
0.366066
0.024147
-0.020445
-0.033118
0.247195
-0.064473
...
-0.028030
0.069503
-0.080749
-0.143702
0.062074
0.104645
-0.054975
-0.000100
0.408074
0.333555
BAR
0.132430
-0.856018
0.366117
1.272816
-5.998561
0.549506
-1.108782
0.436722
1.439781
-1.597159
...
-0.035532
0.141559
-1.910118
-0.512683
4.833946
2.679616
-1.720667
1.299345
0.331174
0.472483
BB
-1.572356
0.519107
5.040959
2.068384
-6.238067
-2.465025
3.485083
2.279680
2.195151
-0.965226
...
0.399872
0.193490
-3.641736
-7.387778
0.033533
2.460572
-0.171410
-0.772302
-1.143783
0.938867
BEK
0.086800
-0.480602
2.956772
-0.177003
-1.623990
1.573807
-0.079612
0.120442
0.877555
0.128543
...
-0.138992
0.184025
-1.480620
-1.012101
0.234607
0.981785
0.369090
-0.109191
1.613610
-1.560710
BN
-0.079781
-0.660595
-1.479445
0.798434
-1.117221
-2.121631
1.628915
-0.844207
-0.285274
1.232441
...
0.018974
0.425481
-1.483677
-2.964977
-0.584602
1.043255
-0.503535
-0.554549
-0.491484
1.298159
BNP
0.596497
-0.441498
0.306445
0.370683
2.746545
0.948356
1.651866
0.349491
1.507117
-1.771856
...
0.510209
0.783741
-0.230620
-1.692155
0.792113
1.742136
0.624707
0.988971
0.587031
0.727634
BOS
0.900081
-0.271287
0.651853
0.013912
-0.356101
-0.528185
0.351328
0.414859
0.875029
1.092748
...
-0.172071
1.337340
-0.640684
-0.168353
0.379582
0.869163
0.180278
-0.424156
1.997144
0.119184
BXF
-14.483826
-21.668425
87.273929
2.873727
-123.584568
-48.559019
5.791191
36.534255
77.395753
-38.836974
...
16.597456
56.788972
-25.872995
-89.171291
114.408756
89.840610
-7.192457
43.382001
-19.843334
62.504639
CA
0.237566
-0.148538
0.194229
0.065682
-0.079773
-0.353125
0.157980
0.359627
0.406251
0.864992
...
0.076412
0.058973
-0.013110
-0.592434
0.443386
0.273511
-0.099891
0.764048
-0.885660
0.133139
CAP
0.079707
0.601684
3.035254
2.479179
-1.780283
1.188892
0.871184
0.091149
2.527491
-3.565229
...
0.076815
4.934916
-2.536456
-1.155974
1.744685
2.012367
2.364457
0.283376
-2.577404
0.014295
CDI
-0.862984
-0.176920
0.896863
-3.392808
-5.656407
-3.917106
0.068577
0.427108
-2.115314
0.972659
...
-3.223157
3.873340
-3.788922
-9.401747
-0.036307
3.070133
-3.845222
4.682509
0.628756
2.231460
CNP
0.187800
-0.396078
0.115493
-0.159103
-0.281120
0.182020
0.399780
-0.070963
-0.039907
0.229456
...
-0.108641
0.365089
0.287436
-0.916859
0.164017
0.291969
-0.089785
0.165009
0.906687
-0.334402
CO
-0.113809
-0.334092
0.312492
0.583414
-0.913314
-1.008196
-0.193902
0.505398
0.660383
1.585758
...
0.249290
1.297465
1.789699
-2.508144
0.003004
1.017442
-0.126177
0.927558
1.161878
-0.332913
COL
0.450340
0.054356
1.575202
0.401664
-2.998454
-1.856741
-0.135586
0.493699
-0.146949
0.579396
...
0.196221
0.142148
0.098479
-1.055964
2.492572
-0.418670
-0.384977
-0.524054
-1.490414
-0.041087
CS
-0.376098
-0.452577
-0.615855
0.059599
0.014708
-0.558930
1.032001
-0.076393
0.464252
0.150004
...
-0.039389
0.282777
-0.757022
-2.134649
1.572806
0.528994
-0.310473
0.204183
0.230110
1.175976
CSM
0.369717
-0.259020
1.224106
0.022930
-0.695584
-0.144863
-0.142883
-0.105869
0.207302
0.437195
...
-0.157714
-0.212606
-1.398624
-2.834212
0.074814
-0.000767
-0.347728
0.775057
0.927784
0.384991
DEL
-0.936892
-0.452105
17.553628
0.304245
-6.354163
-3.279686
0.287172
0.308972
0.258414
0.513039
...
0.488456
3.628985
1.724354
-1.678754
2.722676
0.635729
-2.329474
0.368574
0.303898
4.519532
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
OR
-0.044873
-0.057232
-0.889645
1.612586
-8.765690
-6.822400
3.698745
-1.089005
2.361205
-0.670506
...
0.370088
2.602380
-0.947107
-4.037065
2.441499
1.693553
0.252982
2.594222
-4.485401
3.996564
ORD
-0.020860
-0.007924
0.048265
0.030031
-0.023870
-0.028734
0.036800
-0.010299
0.017468
0.009954
...
0.013038
0.027444
0.010952
-0.021893
-0.004564
0.063471
0.012841
0.029926
0.038372
-0.017989
PHI
0.015741
-0.545194
1.173315
0.271707
0.808070
0.561893
0.593589
0.371435
0.782237
-0.666843
...
0.078968
-0.070289
-0.007731
-1.215765
1.553186
0.678214
0.660481
0.417872
-0.074437
0.379534
PNL
-0.082724
0.022301
0.329821
0.003291
0.021125
-0.151800
-0.184902
-0.018478
0.073717
-0.015548
...
0.049950
0.309943
-0.167834
0.003987
-0.004232
0.070759
-0.120369
0.006727
0.133933
-0.003042
PUB
0.552515
0.300948
1.639505
0.654687
0.905583
-2.434745
2.980476
0.642522
0.622377
0.100330
...
-0.533027
1.215193
-1.297729
-2.490538
0.036148
0.867005
-2.472146
1.529825
-0.118279
1.524164
RD
0.274118
-0.247439
-0.520078
-0.087975
-0.362189
-0.625856
0.283597
-0.025357
0.156887
0.179183
...
-0.073957
0.753188
-0.039212
-0.253677
0.609809
0.715079
0.224314
0.311719
0.202381
0.564220
REN
0.150875
-0.101258
0.305587
0.150175
-0.339026
0.272654
-0.041295
0.234462
-0.035605
-0.007486
...
-0.007442
0.934725
-0.135825
-0.940960
0.035284
0.080237
-0.321189
0.457092
-0.623070
0.793457
RF
0.702925
-0.469952
1.411956
0.230631
2.279152
-3.727481
0.645508
0.067672
1.241248
-0.111720
...
-0.443257
-0.101862
-0.669845
-1.567329
-0.377284
1.505464
-2.307964
-0.233842
-0.463716
0.557578
RI
1.219131
0.585964
-1.589302
0.541743
-5.058734
-0.551094
2.509028
-3.019176
-0.334357
1.465753
...
-1.024805
1.606499
-2.154360
-11.126903
1.647995
0.854699
0.669562
1.510696
-0.464921
2.271487
RND
-0.778436
-0.346213
0.261742
0.781133
1.029277
-2.715676
1.185107
-0.289487
0.333124
-0.549799
...
-0.514501
0.617662
-0.491343
-2.043270
1.045076
1.518583
-0.881875
0.215000
0.551049
0.760349
RNO
-0.194856
0.062256
-2.867984
2.323181
0.602192
-0.615856
1.003727
-1.397267
3.301889
1.493319
...
0.002128
3.674127
-2.824555
-2.371076
3.702946
1.165810
-2.318156
2.675858
-0.043864
2.045770
SAN
0.244815
-0.850133
1.440583
-0.828844
-1.558444
-2.541527
0.393547
0.162182
1.071964
-0.636866
...
1.511298
1.978561
1.892162
-1.798927
2.938058
-0.402133
-1.921829
2.073042
-2.051231
2.148191
SGO
-0.347777
-0.724985
1.050377
0.542594
0.787105
-0.284162
1.083463
0.134259
1.559060
-0.074004
...
0.638600
-0.308872
0.069854
-1.060679
3.601967
0.950123
-0.145001
0.103386
0.837444
1.009834
SOL
-1.495559
-0.730758
1.318354
-0.285353
-0.857310
-0.504597
3.068359
-0.046860
1.815915
-1.504802
...
-0.583116
0.889455
0.045932
-1.142907
-1.752353
2.866108
-0.099079
0.992459
-0.728672
-0.746490
STM
-0.154523
-0.208223
0.215401
0.101072
0.068219
-0.261173
0.096239
0.019747
0.010125
-0.067431
...
0.109044
0.320498
0.070353
-0.631154
0.507117
0.053438
-0.229314
0.051110
-0.062615
0.086331
SU
0.039163
-0.413854
0.878416
0.343706
0.951741
0.247133
0.520645
-1.271795
0.580924
0.315413
...
-0.069009
3.647471
-2.204494
-0.993717
1.877627
2.369745
-0.253924
0.678845
0.390973
1.884282
SW
0.340634
-1.327288
5.543592
0.325523
1.092717
-2.948123
1.598310
2.243454
-0.987655
1.540113
...
0.794086
3.674687
-3.491367
-6.173818
2.993616
1.784724
0.331201
0.609742
0.020712
-0.434805
TEC
-0.792307
0.522111
3.693638
-0.826640
2.111667
-4.831861
0.136252
0.891059
0.106413
0.889494
...
0.010310
1.518224
-0.722045
0.841777
-2.277615
0.908910
0.983677
0.539310
0.392166
0.223134
TF1
-0.289020
0.024489
-0.126959
0.307503
-0.273799
-0.904242
-0.083465
-0.131746
0.575314
0.046132
...
0.136141
0.148348
-0.115899
-1.061794
0.573967
0.267634
-0.204878
0.238652
-0.000574
0.511626
TMS
-0.032444
-0.030760
0.071137
-0.046980
-0.213721
0.184334
0.069756
-0.041513
0.389744
0.041091
...
-0.019492
0.319747
-0.021861
0.032563
-0.056310
0.040944
0.155085
0.112306
-0.232958
0.247171
UCB
0.165601
-1.244966
5.869959
-0.569444
-5.055884
-1.413877
-1.827721
0.598278
0.517445
-3.784811
...
0.767644
2.165486
-0.211768
-8.422222
-2.957288
1.607036
0.567263
-0.146360
-0.488299
1.534106
UG
-0.482653
-0.168170
0.369626
0.160717
0.768160
0.158803
-0.125060
-0.300848
0.650623
-0.059594
...
0.270680
1.382560
-0.664038
-0.853495
1.815311
-0.046421
-0.796271
-0.128929
0.056903
0.076328
UL
0.898758
5.595687
-8.297843
6.568877
-6.693574
-10.264399
-2.304061
3.993154
3.271074
5.413501
...
-1.328332
11.165030
3.323915
-15.855739
-3.993084
1.194553
-2.695589
1.159815
0.113600
2.578458
UMC
-0.290729
-1.256968
0.315197
-0.018198
0.094178
-1.984172
-0.067731
0.227104
0.774636
-0.043722
...
0.126982
-0.344313
-1.227569
-0.541083
0.541077
0.279461
-0.959730
-0.532138
1.474817
-0.009268
UN
0.282578
-0.189233
-0.832377
0.797065
-1.118597
-0.440169
0.329651
-0.687490
-0.187015
0.894632
...
0.016464
0.785723
-0.975700
-1.311309
0.816568
0.824138
-0.315522
0.462154
-0.114349
1.107700
VIE
-0.012961
-0.003058
-0.371667
0.030151
-0.109323
0.243127
-0.717132
0.522600
0.555502
-0.545803
...
0.146304
0.635160
-0.376326
-0.499309
-0.040553
0.311137
-0.198622
0.303280
-0.579662
0.451185
VK
0.097171
0.170287
0.420820
-0.200328
-0.175451
-0.025699
0.131035
0.030085
-0.099202
-0.012014
...
-0.085647
0.272048
0.108222
-0.146949
-0.338988
0.031967
-0.199285
-0.113774
0.451969
-0.130756
VPK
0.018185
-0.723759
1.955559
0.731852
-2.515818
-1.607548
2.514069
-0.601976
-0.722471
-0.226525
...
-0.291243
0.724098
-0.154210
-2.173382
-1.256619
0.623038
0.555820
-0.162176
0.558272
0.070953
WHV
-0.077809
-0.995199
0.552747
0.618339
-1.402888
-2.511030
0.039251
-0.161950
1.114364
0.138763
...
0.484991
-0.373422
-0.621862
-2.291437
2.627094
0.555246
0.262261
0.399998
0.091369
1.365337
WKL
-0.233882
-0.060403
-0.000626
0.153959
-1.098155
-1.251470
-0.217416
0.569506
0.426732
0.147845
...
-0.279064
0.469073
0.027563
0.250133
1.714120
0.637279
-0.452446
0.472676
-1.152452
0.715671
90 rows × 100000 columns
In [9]:
import os
dir_ = './simulations'
if not os.path.exists(dir_):
os.makedirs(dir_)
In [10]:
losses_df.to_csv(os.path.join(dir_, 'nu_eq_%i.csv' % NU_COPULA))
Content source: yarmenti/MSRA
Similar notebooks: