-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.py
1317 lines (1053 loc) · 69.2 KB
/
main.py
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
from imports import *
list_of_countries = ['AUS', 'CAN', 'JAP', 'NOR', 'SWE', 'SWI', 'UK','USA']
list_of_currencies = ['AUD', 'CAD', 'JPY', 'NOK', 'SEK', 'CHF', 'GBP','USD']
#####################
#Load Data
#####################
xls = pd.ExcelFile('data/parameters_data.xlsx')
x = pd.read_excel(xls, 'AUS')
x.head()
x.loc[:,'Sigma_2_AUS':'Unnamed: 25']
#Extract K0P_1
K0P_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K0P_1_' + country
K0P_1[country] = np.matrix(df[location]).reshape((3,1))
K0P_1['AUS'].shape
#Extract K0P_2
K0P_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K0P_2_' + country
K0P_2[country] = np.matrix(df[location]).reshape((3,1))
K0P_2['AUS'].shape
#Extract K1P_1
K1P_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
K1P_1[country] = np.matrix(df.iloc[:,2:5])
K1P_1['AUS'].shape
#Extract K1P_1
K1P_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
K1P_2[country] = np.matrix(df.iloc[:,5:8])
K1P_2['AUS'].shape
#Extract roh0_1
rho0_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'rho0_1_' + country
rho0_1[country] = np.matrix(df[location].dropna()).reshape((1,1))
rho0_1['AUS'].shape
rho0_1['AUS']
#Extract roh0_2
rho0_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'rho0_2_' + country
rho0_2[country] = np.matrix(df[location].dropna()).reshape((1,1))
rho0_2['AUS'].shape
#Extract roh1_1
rho1_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'rho1_1_' + country
rho1_1[country] = np.matrix(df[location].dropna()).reshape((3,1))
rho1_1['AUS'].shape
#Extract roh1_2
rho1_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'rho1_2_' + country
rho1_2[country] = np.matrix(df[location].dropna()).reshape((3,1))
rho1_2['AUS'].shape
#Extract K0Q_1
K0Q_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K0Q_1_' + country
K0Q_1[country] = np.matrix(df[location].dropna()).reshape((3,1))
K0Q_1['AUS'].shape
#Extract K0Q_2
K0Q_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K0Q_2_' + country
K0Q_2[country] = np.matrix(df[location].dropna()).reshape((3,1))
K0Q_2["AUS"].shape
#Extract K1Q_1
K1Q_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K1Q_1_' + country
K1Q_1[country] = np.matrix(df.loc[:,location:'Unnamed: 16'].dropna())
K1Q_1['AUS'].shape
#Extract K1Q_2
K1Q_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'K1Q_2_' + country
K1Q_2[country] = np.matrix(df.loc[:,location:'Unnamed: 19'].dropna())
K1Q_2['AUS'].shape
#Extract Sigma_1
Sigma_1 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'Sigma_1_' + country
Sigma_1[country] = np.matrix(df.loc[:,location:'Unnamed: 22'].dropna())
Sigma_1['AUS'].shape
#Extract Sigma_2
Sigma_2 = {}
for country in list_of_countries[:-1]:
df = pd.read_excel(xls, country)
location = 'Sigma_2_' + country
Sigma_2[country] = np.matrix(df.loc[:,location:'Unnamed: 25'].dropna())
Sigma_2['AUS'].shape
#Create lambda_0_1
lambda_0_1 = {}
for country in list_of_countries[:-1]:
lambda_0_1[country] = K0P_1[country] - K0Q_1[country]
lambda_0_1['AUS'].shape
#Create lambda_0_2
lambda_0_2 = {}
for country in list_of_countries[:-1]:
lambda_0_2[country] = K0P_2[country] - K0Q_2[country]
lambda_0_2['AUS'].shape
#Create lambda_1_1
lambda_1_1 = {}
for country in list_of_countries[:-1]:
lambda_1_1[country] = K1P_1[country] - K1Q_1[country]
lambda_1_1['AUS'].shape
#Create lambda_1_2
lambda_1_2 = {}
for country in list_of_countries[:-1]:
lambda_1_2[country] = K1P_2[country] - K1Q_2[country]
lambda_1_2['AUS'].shape
#####################
#End of Load Data
#####################
#####################
#Equations
#####################
#Defining the omega_2 Functions
def omega_2_1(country, k):
identity = np.identity(3)
part1 = lambda_1_1[country].T * np.linalg.inv(Sigma_1[country].T)
final_calc = np.zeros((3,3))
for j in list(range(2,k+1)):
calc1 = np.power((identity + K1P_1[country]).T,j-1)
calc2 = np.power((identity + K1P_1[country]),j-1)
calc3 = calc1 * calc2
final_calc += calc3
part2 = identity + final_calc
part3 = np.linalg.inv(Sigma_1[country]) * lambda_1_1[country]
result = part1 * part2 * part3
return result
omega_2_1("JAP", 1)
def omega_2_2(country, k):
identity = np.identity(3)
part1 = lambda_1_2[country].T * np.linalg.inv(Sigma_2[country].T)
final_calc = np.zeros((3,3))
for j in list(range(2,k+1)):
calc1 = np.power((identity + K1P_2[country]).T,j-1)
calc2 = np.power((identity + K1P_2[country]),j-1)
calc3 = calc1 * calc2
final_calc += calc3
part2 = identity + final_calc
part3 = np.linalg.inv(Sigma_2[country]) * lambda_1_2[country]
result = part1 * part2 * part3
return result
omega_2_2('CAN', 1)
#Defining the omega_1 Functions
def omega_1_1(country, k):
identity = np.identity(3)
part1 = rho1_1[country].T + (lambda_0_1[country].T * np.linalg.inv(Sigma_1[country].T) * np.linalg.inv(Sigma_1[country]) * lambda_1_1[country])
final_calc = np.zeros((3,3))
for j in list(range(2,k+1)):
calc1 = np.power((identity + K1P_1[country]),j-1)
final_calc += calc1
part2 = identity + final_calc
final_calc_2 = np.zeros((1,3))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc2 = (K0P_1[country].T) * np.power((identity + K1P_1[country]).T, i-1) * (lambda_1_1[country].T * np.linalg.inv(Sigma_1[country].T) \
* np.linalg.inv(Sigma_1[country]) * lambda_1_1[country]) * np.power((identity + K1P_1[country]), j-1)
final_calc_2 += calc2
result = (part1 * part2) + final_calc_2
return result
omega_1_1('CAN', 1)
omega_1_1('JAP', 1).shape
def omega_1_2(country, k):
identity = np.identity(3)
part1 = rho1_2[country].T + (lambda_0_2[country].T * np.linalg.inv(Sigma_2[country].T) * np.linalg.inv(Sigma_2[country]) * lambda_1_2[country])
final_calc = np.zeros((3,3))
for j in list(range(2,k+1)):
calc1 = np.power((identity + K1P_2[country]),j-1)
final_calc += calc1
part2 = identity + final_calc
final_calc_2 = np.zeros((1,3))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc2 = (K0P_2[country].T) * np.power((identity + K1P_2[country]).T, i-1) * (lambda_1_2[country].T * np.linalg.inv(Sigma_2[country].T) \
* np.linalg.inv(Sigma_2[country]) * lambda_1_2[country]) * np.power((identity + K1P_2[country]), j-1)
final_calc_2 += calc2
result = (part1 * part2) + final_calc_2
return result
omega_1_2('AUS', 3)
def omega_0(country, k): #Does the structure make a difference?
identity = np.identity(3)
final_calc_1 = np.zeros((3,1))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc1 = np.power((identity + K1P_1[country]), i-1) * K0P_1[country]
final_calc_1 += calc1
mini_part1_1 = (k * (rho0_1[country] - rho0_2[country])) + (rho1_1[country].T * final_calc_1)
final_calc_2 = np.zeros((3,1))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc2 = np.power((identity + K1P_2[country]), i-1) * K0P_2[country]
final_calc_2 += calc2
mini_part1_2 = rho1_2[country].T * final_calc_2
part1 = mini_part1_1 - mini_part1_2
if k == 1:
mini_part2_1 = (1/2) * lambda_0_1[country].T * np.linalg.inv(Sigma_1[country].T) * np.linalg.inv(Sigma_1[country]) * lambda_0_1[country]
mini_part2_2 = (1/2) * lambda_0_2[country].T * np.linalg.inv(Sigma_2[country].T) * np.linalg.inv(Sigma_2[country]) * lambda_0_2[country]
else:
mini_part2_1 = ((1+k)/2) * lambda_0_1[country].T * np.linalg.inv(Sigma_1[country].T) * np.linalg.inv(Sigma_1[country]) * lambda_0_1[country]
mini_part2_2 = ((1+k)/2) * lambda_0_2[country].T * np.linalg.inv(Sigma_2[country].T) * np.linalg.inv(Sigma_2[country]) * lambda_0_2[country]
part2 = mini_part2_1 - mini_part2_2
mini_part3_1 = lambda_0_1[country].T * np.linalg.inv(Sigma_1[country].T) * np.linalg.inv(Sigma_1[country]) * lambda_1_1[country] * final_calc_1
mini_part3_2 = lambda_0_2[country].T * np.linalg.inv(Sigma_2[country].T) * np.linalg.inv(Sigma_2[country]) * lambda_1_2[country] * final_calc_2
part3 = mini_part3_1 - mini_part3_2
final_calc_3 = np.zeros((1,1))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc3 = K0P_1[country].T * np.power((identity + K1P_1[country]).T, i-1) * lambda_1_1[country].T * np.linalg.inv(Sigma_1[country].T) \
* np.linalg.inv(Sigma_1[country]) * lambda_1_1[country] * np.power((identity + K1P_1[country]), i-1) * K0P_1[country]
final_calc_3 += calc3
mini_part4_1 = 1/2 * final_calc_3
final_calc_5 = np.zeros((1,1))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc5 = K0P_2[country].T * np.power((identity + K1P_2[country]).T, i-1) * lambda_1_2[country].T * np.linalg.inv(Sigma_2[country].T) \
* np.linalg.inv(Sigma_2[country]) * lambda_1_2[country] * np.power((identity + K1P_2[country]), i-1) * K0P_2[country]
final_calc_5 += calc5
mini_part4_2 = 1/2 * final_calc_5
part4 = mini_part4_1 - mini_part4_2
result = part1 + part2 + part3 + part4
return result
omega_0('AUS',4)
def xi(country, k):
identity = np.identity(3)
final_calc_1 = np.zeros((3,3))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc1 = Sigma_1[country].T * np.power((identity + K1P_1[country]).T, i-1) * lambda_1_1[country].T * np.linalg.inv(Sigma_1[country].T) \
* np.linalg.inv(Sigma_1[country]) * lambda_1_1[country] * np.power((identity + K1P_1[country]), i-1) * Sigma_1[country]
final_calc_1 += calc1
part1 = np.trace(1/2 * final_calc_1)
final_calc_3 = np.zeros((3,3))
for j in list(range(2,k+1)):
for i in list(range(1, j)):
calc3 = Sigma_2[country].T * np.power((identity + K1P_2[country]).T, i-1) * lambda_1_2[country].T * np.linalg.inv(Sigma_2[country].T) \
* np.linalg.inv(Sigma_2[country]) * lambda_1_2[country] * np.power((identity + K1P_2[country]), i-1) * Sigma_2[country]
final_calc_3 += calc3
part2 = np.trace(1/2 * final_calc_3)
result = part1 - part2
return result
xi('AUS', 1)
#################
#PCA Calculator
################
ylds_start_date = {}
ylds_end_date = {}
xlsx = pd.ExcelFile('data/data_xrates_yields.xlsx')
yields_data = {}
for country in list_of_countries:
yields_data[country] = pd.read_excel(xlsx, 'yields_'+country)
yields_data[country]['date'] = yields_data[country].iloc[:,0] #Set first column as date
yields_data[country] = yields_data[country].set_index('date')
yields_data[country] = yields_data[country].iloc[:,1:]
ylds_start_date.update({country: yields_data[country].index[0]})
ylds_end_date.update({country: yields_data[country].index[-1]})
ylds_start_date
ylds_end_date
pca_dates = pd.read_excel('data/pca_model_dates.xlsx') #Load the pca model dates to be used
pca_dates['NOR']
#Adjust the Dates as per the peremeter estimation Dates
for country in list_of_countries:
yields_data[country] = yields_data[country].loc[pca_dates[country][0]:pca_dates[country][1]]
ylds_start_date.update({country: yields_data[country].index[0]})
ylds_end_date.update({country: yields_data[country].index[-1]})
ylds_start_date
ylds_end_date
#Create a list of forecasting dates, that will hold all the dates for which we need to create forecasts
# Remember that '2015-12-31' is the date where our constant values calculation ends and from this on we want to use our model to forecast values
forecasting_dates = list(yields_data['AUS'].loc['2015-12-31':].index)
forecasting_dates
#Rename Columns
for country in list_of_countries:
if country == 'USA':
yields_data[country] = yields_data[country].rename(columns = {'US03M': '03M',
'US06M': '06M',
'US01Y': '01Y',
'US02Y': '02Y',
'US03Y': '03Y',
'US04Y': '04Y',
'US05Y': '05Y',
'US06Y': '06Y',
'US07Y': '07Y',
'US08Y': '08Y',
'US09Y': '09Y',
'US10Y': '10Y'})
else:
yields_data[country] = yields_data[country].rename(columns = {country+'03M': '03M',
country+'06M': '06M',
country+'01Y': '01Y',
country+'02Y': '02Y',
country+'03Y': '03Y',
country+'04Y': '04Y',
country+'05Y': '05Y',
country+'06Y': '06Y',
country+'07Y': '07Y',
country+'08Y': '08Y',
country+'09Y': '09Y',
country+'10Y': '10Y'})
#Standardize Data
#It is always good to standardize data before running PCA
def standardize_data(df):
column_names = list(df.columns)
x = StandardScaler().fit_transform(df.values)
result = pd.DataFrame(data = x, columns = column_names)
result.index = df.index
return result
def PCA_analysis(df, standardize = False):
if standardize == True:
data = standardize_data(df)
else:
data = df.copy()
data = df.copy()
cov = np.cov(data.T) / data.shape[0]
v, w = np.linalg.eig(cov)
idx = v.argsort()[::-1] # Sort descending and get sorted indices
v = v[idx] # Use indices on eigv vector
w = w[:,idx] #
pca_value = data.dot(w[:, :3])
principalComponents = {'level': pca_value[0], 'slope': pca_value[1], 'curvature': pca_value[2]}
principalDf = pd.DataFrame.from_dict(principalComponents)
principalDf.index = df.index
return principalDf
def forecasting_model(pca_data, pca_data_usa, country, k):
omega_0_value = omega_0(country, k)
omega_1_1_value = omega_1_1(country, k)
omega_1_2_value = omega_1_2(country, k)
omega_2_1_value = omega_2_1(country, k)
omega_2_2_value = omega_2_2(country, k)
xi_value = xi(country, k)
result = omega_0_value + (omega_1_1_value * pca_data_usa) - (omega_1_2_value * pca_data) + \
(1/2*((pca_data_usa.T * omega_2_1_value * pca_data_usa) - (pca_data.T * omega_2_2_value * pca_data))) + xi_value
return result/12
#Create the final_forecasting_model:
def forecast(yields_data, country, forecasting_dates, normalize = False):
#Create empty list containers that will hold the forecasting values
one_month = []
two_month = []
three_month = []
four_month = []
five_month = []
six_month = []
seven_month = []
eight_month = []
nine_month = []
ten_month = []
eleven_month = []
twelve_month = []
for date in forecasting_dates:
#Calculate PCA Data for country of interest
data = yields_data[country].loc[:str(date)[:7]] #Extract data yup until the date we want to forecast for
pca = PCA_analysis(data, False) #Calculate PCA for the extracted data
pca_data = np.matrix(pca.loc[str(date)[:7]]).reshape(3,1) #Extract the forecasting dates pca values into a matrix for forecasting
#The reason we convert date to str and only take values up until index 6 is because we want to only focus on the months and not the days because based on country some dates end on
#31 while others end on the 30th.
#Calculate PCA data for USA
data_usa = yields_data['USA'].loc[:str(date)[:7]] #Extract data yup until the date we want to forecast for
pca_usa = PCA_analysis(data_usa, False) #Calculate PCA for the extracted data
pca_data_usa = np.matrix(pca_usa.loc[str(date)[:7]]).reshape(3,1) #Extract the forecasting dates pca values into a matrix for forecasting
predictions = [] #Create an empty prediction list that will hold all the predicted values
for i in list(range(1,13)):
predictions.append(float(forecasting_model(pca_data, pca_data_usa, country, i)))
#Extract predictions values and append them to their respective lists
one_month.append(predictions[0])
two_month.append(predictions[1])
three_month.append(predictions[2])
four_month.append(predictions[3])
five_month.append(predictions[4])
six_month.append(predictions[5])
seven_month.append(predictions[6])
eight_month.append(predictions[7])
nine_month.append(predictions[8])
ten_month.append(predictions[9])
eleven_month.append(predictions[10])
twelve_month.append(predictions[11])
if normalize:
one_month = preprocessing.scale(one_month)
two_month = preprocessing.scale(two_month)
three_month = preprocessing.scale(three_month)
four_month = preprocessing.scale(four_month)
five_month = preprocessing.scale(five_month)
six_month = preprocessing.scale(six_month)
seven_month = preprocessing.scale(seven_month)
eight_month = preprocessing.scale(eight_month)
nine_month = preprocessing.scale(nine_month)
ten_month = preprocessing.scale(ten_month)
eleven_month = preprocessing.scale(eleven_month)
twelve_month = preprocessing.scale(twelve_month)
forecast_dict = {'date': forecasting_dates,
'one_month': one_month, #Organize the results into a dictionary
'two_month': two_month,
'three_month': three_month,
'four_month': four_month,
'five_month': five_month,
'six_month': six_month,
'seven_month': seven_month,
'eight_month': eight_month,
'nine_month': nine_month,
'ten_month': ten_month,
'eleven_month': eleven_month,
'twelve_month': twelve_month}
forecast_df = pd.DataFrame.from_dict(forecast_dict) #Convert the results from a dictionary to a dataframe
forecast_df.set_index('date', inplace=True) #Set the date column as index
return forecast_df
#Functions for plot_forecasts
def round_up(n, decimals=0):
multiplier = 10 ** decimals
return math.ceil(n * multiplier) / multiplier
def round_down(n, decimals=0):
multiplier = 10 ** decimals
return math.floor(n * multiplier) / multiplier
def plot_forecasts(title, df):
#Find optimal X ticks_to_use
dates = list(df.index)
ticks_location = int(len(dates)/4) - 1
ticks_to_use = [dates[0], dates[ticks_location], dates[ticks_location*2], dates[ticks_location*3], dates[-1]]
# Initialize the figure
plt.style.use('seaborn-darkgrid')
# create a color palette
palette = plt.get_cmap('tab20b')
# multiple line plot
num=0
fig = plt.figure(figsize=(20,18))
for column in df:
num+=1
# Find the right spot on the plot
fig.add_subplot(6,2, num)
# Plot the lineplot
plt.plot(df[column], marker='', color=palette(num), linewidth=2.0, alpha=0.9, label=column)
plt.locator_params(axis = 'x', nticks=10)
# Same limits for everybody!
if min(df.min()) > 0 and max(df.max()) > 0:
plt.ylim(round_up(min(df.min()),-1),round_up(max(df.max()),-1))
if min(df.min()) < 0 and max(df.max()) < 0:
plt.ylim(round_down(min(df.min()),-1),round_down(max(df.max()),-1))
if min(df.min()) > 0 and max(df.max()) < 0:
plt.ylim(round_up(min(df.min()),-1),round_down(max(df.max()),-1))
else:
plt.ylim(round_down(min(df.min()),-1),round_up(max(df.max()),-1))
plt.xticks(ticks_to_use)
# Not ticks everywhere
if not num in [11,12] :
plt.tick_params(labelbottom=False)
# Not ticks everywhere
if not num in [1,3,5,7,9,11] :
plt.tick_params(labelleft=False)
# Add title
plt.title(column, loc='left', fontsize=12, fontweight=0, color=palette(num) )
# general title
plt.suptitle(title, fontsize=16, fontweight=0, color='black', style='italic')
# Axis title
plt.text(0.5, 0.02, 'Time', ha='center', va='center')
plt.text(0.06, 0.5, 'Note', ha='center', va='center', rotation='vertical')
#Get Forecasts for every country in the dataset
forecast_data = {}
for country in tqdm.tqdm(list_of_countries[:-1]):
forecast_data[country] = forecast(yields_data, country, forecasting_dates, True)
forecast_data['AUS']
#Load Exchange Rate Data
exchange_rates = pd.read_excel(xlsx, 'xrates')
exchange_rates.head()
# Set Date as index
exchange_rates['date'] = exchange_rates.iloc[:,0] #Set first column as date
exchange_rates = exchange_rates.set_index('date')
exchange_rates = exchange_rates.iloc[:,1:]
exchange_rates
er = {}
for i in range(len(list_of_countries[:-1])):
er[list_of_countries[i]] = exchange_rates[list_of_currencies[i]+'USD Curncy']
er = pd.DataFrame.from_dict(er) #Convert dictionary into dataframe
er.tail()
#real_values = er.loc[str(add_months(forecasting_dates[0],-12))[:7]:] #set the end date for dataframe 1 year from 2015
er['JAP']
def add_months(sourcedate, months):
month = sourcedate.month - 1 + months
year = sourcedate.year + month // 12
month = month % 12 + 1
day = min(sourcedate.day, calendar.monthrange(year,month)[1])
return datetime.date(year, month, day)
def calculate_rv(exchange_rate_data, forecasting_dates, normalize = False):
#Create empty list containers that will hold the real change values values
one_month = []
two_month = []
three_month = []
four_month = []
five_month = []
six_month = []
seven_month = []
eight_month = []
nine_month = []
ten_month = []
eleven_month = []
twelve_month = []
for date in forecasting_dates:
one_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,1))[:7]]))- float(np.log(exchange_rate_data.loc[str(date)[:7]])))
two_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,2))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
three_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,3))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
four_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,4))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
five_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,5))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
six_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,6))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
seven_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,7))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
eight_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,8))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
nine_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,9))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
ten_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,10))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
eleven_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,11))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
twelve_month.append(float(np.log(exchange_rate_data.loc[str(add_months(date,12))[:7]])) - float(np.log(exchange_rate_data.loc[str(date)[:7]])))
if normalize:
one_month = preprocessing.scale(one_month)
two_month = preprocessing.scale(two_month)
three_month = preprocessing.scale(three_month)
four_month = preprocessing.scale(four_month)
five_month = preprocessing.scale(five_month)
six_month = preprocessing.scale(six_month)
seven_month = preprocessing.scale(seven_month)
eight_month = preprocessing.scale(eight_month)
nine_month = preprocessing.scale(nine_month)
ten_month = preprocessing.scale(ten_month)
eleven_month = preprocessing.scale(eleven_month)
twelve_month = preprocessing.scale(twelve_month)
rv_dict = {'date': forecasting_dates,
'one_month': one_month, #Organize the results into a dictionary
'two_month': two_month,
'three_month': three_month,
'four_month': four_month,
'five_month': five_month,
'six_month': six_month,
'seven_month': seven_month,
'eight_month': eight_month,
'nine_month': nine_month,
'ten_month': ten_month,
'eleven_month': eleven_month,
'twelve_month': twelve_month}
rv_df = pd.DataFrame.from_dict(rv_dict) #Convert the results from a dictionary to a dataframe
rv_df.set_index('date', inplace=True) #Set the date column as index
return rv_df
def calculate_rw(exchange_rate_data, forecasting_dates, normalize = False):
#Create empty list containers that will hold the real change values values
one_month = []
two_month = []
three_month = []
four_month = []
five_month = []
six_month = []
seven_month = []
eight_month = []
nine_month = []
ten_month = []
eleven_month = []
twelve_month = []
for date in forecasting_dates:
one_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-1))[:7]])))
two_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-2))[:7]])))
three_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-3))[:7]])))
four_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-4))[:7]])))
five_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-5))[:7]])))
six_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-6))[:7]])))
seven_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-7))[:7]])))
eight_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-8))[:7]])))
nine_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-9))[:7]])))
ten_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-10))[:7]])))
eleven_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-11))[:7]])))
twelve_month.append(float(np.log(exchange_rate_data.loc[str(date)[:7]])) - float(np.log(exchange_rate_data.loc[str(add_months(date,-12))[:7]])))
if normalize:
one_month = preprocessing.scale(one_month)
two_month = preprocessing.scale(two_month)
three_month = preprocessing.scale(three_month)
four_month = preprocessing.scale(four_month)
five_month = preprocessing.scale(five_month)
six_month = preprocessing.scale(six_month)
seven_month = preprocessing.scale(seven_month)
eight_month = preprocessing.scale(eight_month)
nine_month = preprocessing.scale(nine_month)
ten_month = preprocessing.scale(ten_month)
eleven_month = preprocessing.scale(eleven_month)
twelve_month = preprocessing.scale(twelve_month)
rw_dict = {'date': forecasting_dates,
'one_month': one_month, #Organize the results into a dictionary
'two_month': two_month,
'three_month': three_month,
'four_month': four_month,
'five_month': five_month,
'six_month': six_month,
'seven_month': seven_month,
'eight_month': eight_month,
'nine_month': nine_month,
'ten_month': ten_month,
'eleven_month': eleven_month,
'twelve_month': twelve_month}
rw_df = pd.DataFrame.from_dict(rw_dict) #Convert the results from a dictionary to a dataframe
rw_df.set_index('date', inplace=True) #Set the date column as index
return rw_df
rv = {}
for country in list_of_countries[:-1]:
rv[country] = calculate_rv(er[country], forecasting_dates, True)
forecast_data['AUS']
rv['AUS']
rw = {}
for country in list_of_countries[:-1]:
rw[country] = calculate_rw(er[country], forecasting_dates, True)
# multiple line plot
def plot_results(country, month):
plt.plot( forecast_data[country].index, forecast_data[country][month], color='skyblue', linewidth=2, linestyle='dashed', label='Forecast')
plt.plot( rw[country].index, rw[country][month], marker='', color='olive', linewidth=2,linestyle='dashed', label='Random Walk')
plt.plot( rv[country].index, rv[country][month], marker='', color='olive', linewidth=2, label="Real Value")
plt.legend()
plot_results('JAP', 'twelve_month')
######################################
#Accuracy metrics [Regression Metrics]
######################################
score = {}
for model in ['forecast_model', 'random_walk']:
score_dict = {}
for country in list_of_countries[:-1]:
one_month = []
two_month = []
three_month = []
four_month = []
five_month = []
six_month = []
seven_month = []
eight_month = []
nine_month = []
ten_month = []
eleven_month = []
twelve_month = []
test_name = []
#explained variance score : Best possible score is 1.0, lower values are worse.
if model == 'forecast_model':
#root mean_squared_error : Mean squared error regression loss
test_name.append('root mean squared error')
one_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['one_month'], forecast_data[country]['one_month'])))
two_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['two_month'], forecast_data[country]['two_month'])))
three_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['three_month'], forecast_data[country]['three_month'])))
four_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['four_month'], forecast_data[country]['four_month'])))
five_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['five_month'], forecast_data[country]['five_month'])))
six_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['six_month'], forecast_data[country]['six_month'])))
seven_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['seven_month'], forecast_data[country]['seven_month'])))
eight_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['eight_month'], forecast_data[country]['eight_month'])))
nine_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['nine_month'], forecast_data[country]['nine_month'])))
ten_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['ten_month'], forecast_data[country]['ten_month'])))
eleven_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['eleven_month'], forecast_data[country]['eleven_month'])))
twelve_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['twelve_month'], forecast_data[country]['twelve_month'])))
#median absolute error :Median absolute error output is non-negative floating point. The best value is 0.0.
test_name.append('median absolute error')
one_month.append(metrics.median_absolute_error(rv[country]['one_month'], forecast_data[country]['one_month']))
two_month.append(metrics.median_absolute_error(rv[country]['two_month'], forecast_data[country]['two_month']))
three_month.append(metrics.median_absolute_error(rv[country]['three_month'], forecast_data[country]['three_month']))
four_month.append(metrics.median_absolute_error(rv[country]['four_month'], forecast_data[country]['four_month']))
five_month.append(metrics.median_absolute_error(rv[country]['five_month'], forecast_data[country]['five_month']))
six_month.append(metrics.median_absolute_error(rv[country]['six_month'], forecast_data[country]['six_month']))
seven_month.append(metrics.median_absolute_error(rv[country]['seven_month'], forecast_data[country]['seven_month']))
eight_month.append(metrics.median_absolute_error(rv[country]['eight_month'], forecast_data[country]['eight_month']))
nine_month.append(metrics.median_absolute_error(rv[country]['nine_month'], forecast_data[country]['nine_month']))
ten_month.append(metrics.median_absolute_error(rv[country]['ten_month'], forecast_data[country]['ten_month']))
eleven_month.append(metrics.median_absolute_error(rv[country]['eleven_month'], forecast_data[country]['eleven_month']))
twelve_month.append(metrics.median_absolute_error(rv[country]['twelve_month'], forecast_data[country]['twelve_month']))
else:
#root mean_squared_error : Mean squared error regression loss
test_name.append('root mean squared error')
one_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['one_month'], rw[country]['one_month'])))
two_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['two_month'], rw[country]['two_month'])))
three_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['three_month'], rw[country]['three_month'])))
four_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['four_month'], rw[country]['four_month'])))
five_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['five_month'], rw[country]['five_month'])))
six_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['six_month'], rw[country]['six_month'])))
seven_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['seven_month'], rw[country]['seven_month'])))
eight_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['eight_month'], rw[country]['eight_month'])))
nine_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['nine_month'], rw[country]['nine_month'])))
ten_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['ten_month'], rw[country]['ten_month'])))
eleven_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['eleven_month'], rw[country]['eleven_month'])))
twelve_month.append(np.sqrt(metrics.mean_squared_error(rv[country]['twelve_month'], rw[country]['twelve_month'])))
#median absolute error :Median absolute error output is non-negative floating point. The best value is 0.0.
test_name.append('median absolute error')
one_month.append(metrics.median_absolute_error(rv[country]['one_month'], rw[country]['one_month']))
two_month.append(metrics.median_absolute_error(rv[country]['two_month'], rw[country]['two_month']))
three_month.append(metrics.median_absolute_error(rv[country]['three_month'], rw[country]['three_month']))
four_month.append(metrics.median_absolute_error(rv[country]['four_month'], rw[country]['four_month']))
five_month.append(metrics.median_absolute_error(rv[country]['five_month'], rw[country]['five_month']))
six_month.append(metrics.median_absolute_error(rv[country]['six_month'], rw[country]['six_month']))
seven_month.append(metrics.median_absolute_error(rv[country]['seven_month'], rw[country]['seven_month']))
eight_month.append(metrics.median_absolute_error(rv[country]['eight_month'], rw[country]['eight_month']))
nine_month.append(metrics.median_absolute_error(rv[country]['nine_month'], rw[country]['nine_month']))
ten_month.append(metrics.median_absolute_error(rv[country]['ten_month'], rw[country]['ten_month']))
eleven_month.append(metrics.median_absolute_error(rv[country]['eleven_month'], rw[country]['eleven_month']))
twelve_month.append(metrics.median_absolute_error(rv[country]['twelve_month'], rw[country]['twelve_month']))
score_dict[country] = {'test_name': test_name,
'one_month': one_month, #Organize the results into a dictionary
'two_month': two_month,
'three_month': three_month,
'four_month': four_month,
'five_month': five_month,
'six_month': six_month,
'seven_month': seven_month,
'eight_month': eight_month,
'nine_month': nine_month,
'ten_month': ten_month,
'eleven_month': eleven_month,
'twelve_month': twelve_month}
score[model] = score_dict
random_walk_scores = pd.DataFrame.from_dict({(i,j): score['random_walk'][i][j]
for i in score['random_walk'].keys()
for j in score['random_walk'][i].keys()},
orient='index')
random_walk_scores.index = pd.MultiIndex.from_tuples(random_walk_scores.index)
random_walk_scores.columns = random_walk_scores.loc['AUS'].loc['test_name']
random_walk_scores.drop(index='test_name', level=1, inplace = True)
forecast_model_scores = pd.DataFrame.from_dict({(i,j): score['forecast_model'][i][j]
for i in score['forecast_model'].keys()
for j in score['forecast_model'][i].keys()},
orient='index')
forecast_model_scores.index = pd.MultiIndex.from_tuples(forecast_model_scores.index)
forecast_model_scores.columns = forecast_model_scores.loc['AUS'].loc['test_name']
forecast_model_scores.drop(index='test_name', level=1, inplace = True)
#Create directional variable:
forecast_directional = {}
for country in list_of_countries[:-1]:
forecast_directional[country] = {}
forecast_directional[country]['one_month'] = np.where(forecast_data[country]['one_month'].isnull(), np.nan,
np.where(forecast_data[country]['one_month'] > 0, 1, -1))
forecast_directional[country]['two_month'] = np.where(forecast_data[country]['two_month'].isnull(), np.nan,
np.where(forecast_data[country]['two_month'] > 0, 1, -1))
forecast_directional[country]['three_month'] = np.where(forecast_data[country]['three_month'].isnull(), np.nan,
np.where(forecast_data[country]['three_month'] > 0, 1, -1))
forecast_directional[country]['four_month'] = np.where(forecast_data[country]['four_month'].isnull(), np.nan,
np.where(forecast_data[country]['four_month'] > 0, 1, -1))
forecast_directional[country]['five_month'] = np.where(forecast_data[country]['five_month'].isnull(), np.nan,
np.where(forecast_data[country]['five_month'] > 0, 1, -1))
forecast_directional[country]['six_month'] = np.where(forecast_data[country]['six_month'].isnull(), np.nan,
np.where(forecast_data[country]['six_month'] > 0, 1, -1))
forecast_directional[country]['seven_month'] = np.where(forecast_data[country]['seven_month'].isnull(), np.nan,
np.where(forecast_data[country]['seven_month'] > 0, 1, -1))
forecast_directional[country]['eight_month'] = np.where(forecast_data[country]['eight_month'].isnull(), np.nan,
np.where(forecast_data[country]['eight_month'] > 0, 1, -1))
forecast_directional[country]['nine_month'] = np.where(forecast_data[country]['nine_month'].isnull(), np.nan,
np.where(forecast_data[country]['nine_month'] > 0, 1, -1))
forecast_directional[country]['ten_month'] = np.where(forecast_data[country]['ten_month'].isnull(), np.nan,
np.where(forecast_data[country]['ten_month'] > 0, 1, -1))
forecast_directional[country]['eleven_month'] = np.where(forecast_data[country]['eleven_month'].isnull(), np.nan,
np.where(forecast_data[country]['eleven_month'] > 0, 1, -1))
forecast_directional[country]['twelve_month'] = np.where(forecast_data[country]['twelve_month'].isnull(), np.nan,
np.where(forecast_data[country]['twelve_month'] > 0, 1, -1))
forecast_directional[country] = pd.DataFrame.from_dict(forecast_directional[country])
forecast_directional[country].index = forecast_data[country].index
rv_directional = {}
for country in list_of_countries[:-1]:
rv_directional[country] = {}
rv_directional[country]['one_month'] = np.where(rv[country]['one_month'].isnull(), np.nan,
np.where(rv[country]['one_month'] > 0, 1, -1))
rv_directional[country]['two_month'] = np.where(rv[country]['two_month'].isnull(), np.nan,
np.where(rv[country]['two_month'] > 0, 1, -1))
rv_directional[country]['three_month'] = np.where(rv[country]['three_month'].isnull(), np.nan,
np.where(rv[country]['three_month'] > 0, 1, -1))
rv_directional[country]['four_month'] = np.where(rv[country]['four_month'].isnull(), np.nan,
np.where(rv[country]['four_month'] > 0, 1, -1))
rv_directional[country]['five_month'] = np.where(rv[country]['five_month'].isnull(), np.nan,
np.where(rv[country]['five_month'] > 0, 1, -1))
rv_directional[country]['six_month'] = np.where(rv[country]['six_month'].isnull(), np.nan,
np.where(rv[country]['six_month'] > 0, 1, -1))
rv_directional[country]['seven_month'] = np.where(rv[country]['seven_month'].isnull(), np.nan,
np.where(rv[country]['seven_month'] > 0, 1, -1))
rv_directional[country]['eight_month'] = np.where(rv[country]['eight_month'].isnull(), np.nan,
np.where(rv[country]['eight_month'] > 0, 1, -1))
rv_directional[country]['nine_month'] = np.where(rv[country]['nine_month'].isnull(), np.nan,
np.where(rv[country]['nine_month'] > 0, 1, -1))
rv_directional[country]['ten_month'] = np.where(rv[country]['ten_month'].isnull(), np.nan,
np.where(rv[country]['ten_month'] > 0, 1, -1))
rv_directional[country]['eleven_month'] = np.where(rv[country]['eleven_month'].isnull(), np.nan,
np.where(rv[country]['eleven_month'] > 0, 1, -1))
rv_directional[country]['twelve_month'] = np.where(rv[country]['twelve_month'].isnull(), np.nan,
np.where(rv[country]['twelve_month'] > 0, 1, -1))
rv_directional[country] = pd.DataFrame.from_dict(rv_directional[country])
rv_directional[country].index = rv[country].index
rw_directional = {}