Table (B.16) Scenario analysis of the exit strategy based on VIX These tables report the mean return andp-value of the 1-month rebalance exit
strategies based on VIXt−1 in the sample period between 28-05-1999 and 29-05-2020. The p-values are reported in parenthesis below the mean return. The lowest and highest mean returns of the five strategies are marked with a coloured cell. The abbreviations used to denote the strategies are described in Appendix
A.1.
(a) Exit1M1C
Standard deviation
Windowsize
1 1.5 2
1 4.09% 3.97% 4.01%
(0.001) (0.003) (0.003) 1.5 3.99% 4.15% 3.95%
(0.001) (0.001) (0.003) 2 4.28% 4.36% 3.96%
(0.000) (0.000) (0.003)
(b) Exit1M2C
Standard deviation
Windowsize
1 1.5 2
1 3.89% 3.72% 3.81%
(0.000) (0.000) (0.000) 1.5 3.93% 3.87% 3.59%
(0.000) (0.000) (0.001) 2 3.82% 4.00% 3.65%
(0.000) (0.000) (0.000)
(c) Exit1M3C
Standard deviation
Windowsize
1 1.5 2
1 3.81% 3.81% 3.91%
(0.000) (0.000) (0.000) 1.5 3.98% 3.83% 3.73%
(0.000) (0.000) (0.000) 2 3.69% 3.92% 3.77%
(0.000) (0.000) (0.000)
(d) Exit1M4C
Standard deviation
Windowsize(year) 1 1.5 2
1 3.99% 3.90% 4.02%
(0.000) (0.000) (0.000) 1.5 4.24% 3.98% 3.82%
(0.000) (0.000) (0.000) 2 3.80% 4.05% 3.85%
(0.000) (0.000) (0.000)
(e) Exit1M5C
Standard deviation
Windowsize
1 1.5 2
1 3.94% 3.90% 3.95%
(0.000) (0.000) (0.000) 1.5 4.16% 3.92% 3.79%
(0.000) (0.000) (0.000) 2 3.72% 3.93% 3.84%
(0.000) (0.000) (0.000)
Table (B.17) Scenario analysis of the exit strategy based on VIX This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance exit- and static carry trade strategies. The results for the exit strategies are based on the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two days lagged over the full sample period from 30-06-1999 to 29-05-2020.
The highest mean return for each of the strategies is marked by a coloured cell.
The abbreviations used to denote the strategies are described in Appendix A.1.
Carry1M Reverse1M
Signal VIXt−2 VIXt−1 VIXt
1C
Mean return 2.84% 3.63% 4.15% 3.73%
Sharpe ratio 0.417 0.616 0.727 0.650 nominalp-value 0.057 0.005 0.001 0.003 fraction negative 38.5% 32.5% 32.9% 33.3%
2C
Mean return 2.60% 3.53% 3.87% 3.54%
Sharpe ratio 0.498 0.759 0.839 0.761 nominalp-value 0.023 0.001 0.000 0.001 fraction negative 38.9% 32.5% 32.1% 32.1%
3C
Mean return 2.67% 3.78% 3.83% 3.63%
Sharpe ratio 0.546 0.886 0.893 0.843 nominalp-value 0.013 0.000 0.000 0.000 fraction negative 40.9% 33.7% 34.1% 33.3%
4C
Mean return 2.82% 3.99% 3.98% 3.80%
Sharpe ratio 0.581 0.937 0.925 0.881 nominalp-value 0.008 0.000 0.000 0.000 fraction negative 39.3% 32.1% 32.1% 32.1%
5C
Mean return 2.84% 3.96% 3.92% 3.74%
Sharpe ratio 0.577 0.906 0.881 0.839 nominalp-value 0.009 0.000 0.000 0.000 fraction negative 40.48% 33.3% 33.3% 33.3%
Table (B.18) Scenario analysis of the exit strategy based on VXY These tables report the mean return andp-value of the 1-month rebalance exit
strategies based on VXYt−1 in the sample period between 28-05-1999 and 29-05-2020. The p-values are reported in parenthesis below the mean return. The lowest and highest mean returns of the five strategies are marked with a coloured cell. The abbreviations used to denote the strategies are described in Appendix
A.1.
(a) Exit1M1C
Standard deviation
Windowsize
1 1.5 2
1 5.21% 5.03% 4.72%
(0.000) (0.000) (0.000) 1.5 5.35% 5.03% 4.62%
(0.000) (0.000) (0.000) 2 4.73% 4.75% 4.22%
(0.000) (0.000) (0.000)
(b) Exit1M2C
Standard deviation
Windowsize
1 1.5 2
1 4.44% 4.34% 3.94%
(0.000) (0.000) (0.000) 1.5 4.39% 4.22% 3.84%
(0.000) (0.000) (0.000) 2 3.89% 4.00% 3.60%
(0.000) (0.000) (0.001)
(c) Exit1M3C
Standard deviation
Windowsize
1 1.5 2
1 4.24% 4.18% 3.93%
(0.000) (0.000) (0.000) 1.5 4.07% 4.14% 3.53%
(0.000) (0.000) (0.000) 2 3.71% 3.83% 3.43%
(0.000) (0.000) (0.001)
(d) Exit1M4C
Standard deviation
Windowsize
1 1.5 2
1 4.21% 4.12% 3.97%
(0.000) (0.000) (0.000) 1.5 4.04% 4.11% 3.52%
(0.000) (0.000) (0.000) 2 3.74% 3.80% 3.42%
(0.000) (0.000) (0.001)
(e) Exit1M5C
Standard deviation
Windowsize
1 1.5 2
1 4.09% 3.98% 3.86%
(0.000) (0.000) (0.000) 1.5 3.90% 3.96% 3.29%
(0.000) (0.000) (0.000) 2 3.63% 3.63% 3.28%
(0.000) (0.000) (0.002)
Table (B.19) Scenario analysis of the exit strategy based on VXY This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance exit- and static carry trade strategy. The results for the reverse strategies are based on the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two days lagged over the full sample period from 30-06-1999 to 29-05-2020.
The highest mean return for each of the strategies is marked by a coloured cell.
The abbreviations used to denote the strategies are described in Appendix A.1.
Carry1M Reverse1M
Signal VXYt−2 VXYt−1 VXYt
1C
Mean return 2.84% 4.94% 5.03% 4.96%
Sharpe ratio 0.417 0.886 0.903 0.886 nominal p-value 0.057 0.000 0.000 0.000 fraction negative 38.5% 32.1% 31.7% 32.1%
2C
Mean return 2.60% 4.12% 4.22% 4.18%
Sharpe ratio 0.498 0.903 0.924 0.915 nominal p-value 0.023 0.000 0.000 0.000 fraction negative 38.9% 32.1% 31.7% 32.1%
3C
Mean return 2.67% 4.08% 4.14% 4.08%
Sharpe ratio 0.546 0.962 0.979 0.960 nominal p-value 0.013 0.000 0.000 0.000 fraction negative 40.9% 33.7% 33.3% 33.7%
4C
Mean return 2.82% 4.11% 4.11% 4.02%
Sharpe ratio 0.581 0.961 0.967 0.940 nominal p-value 0.008 0.000 0.000 0.000 fraction negative 39.3% 32.9% 32.5% 32.9%
5C
Mean return 2.84% 3.97% 3.96% 3.86%
Sharpe ratio 0.577 0.916 0.917 0.890 nominal p-value 0.009 0.000 0.000 0.000 fraction negative 40.48% 33.7% 33.7% 34.1%
B.8 Sub sample test of the dynamic strategies based on the change in VIX
Table (B.20) Performance matrix of reverse strategies based on VIX over sub-periods
This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance reverse strategies. The results are based on the level and change VIXt−1 signal over the three sub-sample periods: 1999-2006, 2006-2013 and 2013-2020. The mean returns
are marked by a 3-colour scale, using the 50th percentile. The abbreviations used to denote the strategies are described in Appendix A.1
VIXt−1
Sub periods 1999-2006 2006-2013 2013-2020 Level Change Level Change Level Change Reverse1M1C
Mean Return 3.39% 5.39% 8.92% 6.96% 3.40% 1.83%
Sharpe Ratio 0.554 0.898 1.111 0.850 0.602 0.321 Nominal p-value 0.144 0.019 0.004 0.026 0.115 0.399 Fraction negative 37.6% 34.1% 35.3% 35.3% 41.7% 41.7%
Reverse1M2C
Mean Return 4.40% 4.82% 7.79% 5.96% 2.68% 2.17%
Sharpe Ratio 0.976 1.078 1.237 0.921 0.655 0.527 Nominal p-value 0.011 0.005 0.001 0.016 0.087 0.167 Fraction negative 34.1% 32.9% 31.8% 36.5% 42.9% 42.9%
Reverse1M3C
Mean Return 5.75% 5.81% 6.76% 4.75% 1.94% 2.51%
Sharpe Ratio 1.266 1.281 1.184 0.809 0.519 0.675 Nominal p-value 0.001 0.001 0.002 0.034 0.174 0.078 Fraction negative 31.8% 30.6% 35.3% 40.0% 47.6% 45.2%
Reverse1M4C
Mean Return 6.36% 6.10% 6.62% 4.43% 1.97% 2.20%
Sharpe Ratio 1.367 1.302 1.215 0.787 0.522 0.583 Nominal p-value 0.000 0.001 0.002 0.039 0.171 0.127 Fraction negative 31.8% 30.6% 31.8% 36.5% 44.0% 41.7%
Reverse1M5C
Mean Return 6.70% 6.44% 6.05% 3.75% 1.83% 2.29%
Sharpe Ratio 1.386 1.325 1.084 0.654 0.494 0.622 Nominal p-value 0.000 0.001 0.005 0.086 0.195 0.104 Fraction negative 32.9% 31.8% 32.9% 37.6% 45.2% 42.9%
Table (B.21) Performance matrix of exit strategies based on VIX over sub periods
This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance reverse strategies. The results are based on the level and change VIXt−1 signal over the three sub-sample periods: 1999-2006, 2006-2013 and 2013-2020. The mean returns
are marked by a 3-colour scale, using the 50th percentile. The abbreviations used to denote the strategies are described in Appendix A.1
VIXt−1
Sub periods 1999-2006 2006-2013 2013-2020 Level Change Level Change Level Change Exit1M1C
Mean Return 4.47% 5.46% 5.16% 4.18% 2.19% 1.41%
Sharpe Ratio 0.762 0.913 0.781 0.588 0.477 0.260 Nominal p-value 0.046 0.017 0.041 0.121 0.210 0.494 Fraction negative 31.8% 30.6% 31.8% 34.1% 36.9% 38.1%
Exit1M2C
Mean Return 4.33% 4.54% 4.74% 3.83% 2.04% 1.78%
Sharpe Ratio 0.996 1.042 0.834 0.645 0.563 0.471 Nominal p-value 0.010 0.007 0.029 0.090 0.140 0.217 Fraction negative 30.6% 30.6% 30.6% 35.3% 36.9% 38.1%
Exit1M3C
Mean Return 5.49% 5.52% 3.86% 2.86% 1.68% 1.96%
Sharpe Ratio 1.261 1.277 0.767 0.536 0.504 0.594 Nominal p-value 0.001 0.001 0.044 0.157 0.186 0.120 Fraction negative 28.2% 28.2% 34.1% 38.8% 41.7% 41.7%
Exit1M4C
Mean Return 6.09% 5.96% 3.92% 2.83% 1.50% 1.61%
Sharpe Ratio 1.344 1.328 0.802 0.536 0.454 0.480 Nominal p-value 0.001 0.001 0.036 0.157 0.233 0.207 Fraction negative 28.2% 28.2% 31.8% 36.5% 38.1% 38.1%
Exit1M15
Mean Return 6.44% 6.32% 3.47% 2.33% 1.45% 1.68%
Sharpe Ratio 1.362 1.343 0.691 0.427 0.427 0.505 Nominal p-value 0.000 0.001 0.070 0.258 0.262 0.185 Fraction negative 29.4% 29.4% 32.9% 37.6% 39.3% 39.3%
B.9 Sub-sample test of the dynamic strategies based on the change in VXY
Table (B.22) Performance matrix of reverse strategies based on VXY over sub periods
This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance exit strategies.
The results are based on the level and change VXYt−1 signal over the three sub-sample periods: 1999-2006, 2006-2013 and 2013-2020. The mean returns are marked by a 3-colour scale, using the 50th percentile. The abbreviations used to
denote the strategies are described in Appendix A.1 VXYt−1
Sub periods 1999-2006 2006-2013 2013-2020 Level Change Level Change Level Change Reverse1M1C
Mean Return 6.18% 5.35% 11.42% 7.06% 3.49% 2.15%
Sharpe Ratio 1.040 0.890 1.466 0.860 0.628 0.384 Nominal p-value 0.007 0.020 0.000 0.025 0.100 0.312 Fraction negative 32.9% 31.8% 31.8% 37.6% 42.9% 41.7%
Reverse1M2C
Mean Return 5.33% 4.66% 9.45% 6.91% 2.42% 2.16%
Sharpe Ratio 1.204 1.039 1.546 1.081 0.602 0.538 Nominal p-value 0.002 0.007 0.000 0.005 0.115 0.159 Fraction negative 29.4% 32.9% 31.8% 35.3% 45.2% 44.0%
Reverse1M3C
Mean Return 5.82% 5.40% 8.15% 5.66% 2.63% 2.16%
Sharpe Ratio 1.282 1.179 1.467 0.974 0.725 0.593 Nominal p-value 0.001 0.002 0.000 0.011 0.059 0.121 Fraction negative 31.8% 30.6% 34.1% 37.6% 44.0% 47.6%
Reverse1M4C
Mean Return 6.24% 5.47% 7.50% 4.70% 2.30% 1.80%
Sharpe Ratio 1.338 1.151 1.401 0.837 0.624 0.484 Nominal p-value 0.001 0.003 0.000 0.029 0.103 0.204 Fraction negative 31.8% 32.9% 34.1% 37.6% 42.9% 44.0%
Reverse1M5C
Mean Return 6.41% 5.47% 6.59% 4.24% 2.11% 2.02%
Sharpe Ratio 1.318 1.103 1.193 0.742 0.583 0.557 Nominal p-value 0.001 0.004 0.002 0.052 0.127 0.144 Fraction negative 32.9% 34.1% 35.3% 36.5% 44.0% 45.2%
Table (B.23) Performance matrix of exit strategies based on VXY over sub periods
This table reports the mean return, Sharpe ratio, nominalp-value of mean return and the fraction of negative returns of the five 1-month rebalance exit strategies.
The results are based on the level and change VXYt−1 signal over the three sub-sample periods: 1999-2006, 2006-2013 and 2013-2020. The mean returns are marked by a 3-colour scale, using the 50th percentile. The abbreviations used to
denote the strategies are described in Appendix A.1 . VXYt−1
Sub periods 1999-2006 2006-2013 2013-2020 Level Change Level Change Level Change Exit1M1C
Mean Return 5.86% 5.44% 6.31% 4.13% 2.40% 1.73%
Sharpe Ratio 1.000 0.914 1.052 0.596 0.499 0.311 Nominal p-value 0.009 0.017 0.006 0.117 0.191 0.414 Fraction negative 29.4% 30.6% 28.2% 34.1% 39.3% 38.1%
Exit1M2C
Mean Return 4.79% 4.46% 5.52% 4.26% 2.07% 1.94%
Sharpe Ratio 1.102 0.995 1.032 0.748 0.542 0.490 Nominal p-value 0.004 0.010 0.007 0.050 0.155 0.198 Fraction negative 28.2% 31.8% 29.4% 34.1% 39.3% 38.1%
Exit1M3C
Mean Return 5.52% 5.31% 4.55% 3.30% 2.17% 1.94%
Sharpe Ratio 1.243 1.165 0.995 0.633 0.608 0.552 Nominal p-value 0.001 0.003 0.010 0.096 0.111 0.148 Fraction negative 28.2% 29.4% 31.8% 36.5% 41.7% 42.9%
Exit1M4C
Mean Return 6.03% 5.64% 4.37% 2.97% 1.80% 1.54%
Sharpe Ratio 1.324 1.212 0.969 0.571 0.510 0.440 Nominal p-value 0.001 0.002 0.012 0.133 0.181 0.248 Fraction negative 28.2% 30.6% 30.6% 35.3% 39.3% 39.3%
Exit1M5C
Mean Return 6.30% 5.83% 3.79% 2.61% 1.71% 1.67%
Sharpe Ratio 1.343 1.220 0.836 0.487 0.488 0.485 Nominal p-value 0.001 0.002 0.029 0.199 0.200 0.203 Fraction negative 29.4% 31.8% 31.8% 35.3% 40.5% 40.5%
Figures
C.1 Autocorrelation function plots
Figure (C.1) ACF plots for the returns of 3-month rebalance carry trade strategies
This figure presents the autocorrelation function plots of the monthly natural log-returns and squared natural log-returns up to 30 lags for the five 1-month rebalance strategies along with a 95%-confidence band. The abbreviations used for
each of the five strategies are described in Appendix A.1
Figure (C.2) ACF plots for the returns of 6-month rebalance carry trade strategies
This figure presents the autocorrelation function plots of the monthly natural log-returns and squared natural log-returns up to 30 lags for the five 6-month rebalance strategies along with a 95%-confidence band. The abbreviations used for
each of the five strategies are described in Appendix A.1
Figure (C.3) ACF plots for the returns of 12-month rebalance carry trade strategies
This figure presents the autocorrelation function plots of the monthly natural log-returns and squared natural log-returns up to 30 lags for the five 12-month rebalance strategies along with a 95%-confidence band. The abbreviations used for
each of the five strategies are described in Appendix A.1
C.2 Cumulative returns
Figure (C.4) Cumulative returns of 3-month rebalance strategies This figure presents the cumulative returns generated from the 3-month rebalancing carry trade strategies in the sample period between 30-06-1999 and 29-05-2020. The abbreviations used for each of the five strategies are described in
Appendix A.1
Figure (C.5) Cumulative returns of 6-month rebalance strategies This figure presents the cumulative returns generated from the 6-month rebalancing carry trade strategies in the sample period between 30-06-1999 and 29-05-2020. The abbreviations used for each of the five strategies are described in
Appendix A.1
C.3 Plot of the performance measures using different timing signals of VIX for differ-ent standard deviations
Figure (C.6) Exit1M1C
This figure presents the mean returns of Exit1M1C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.7) Exit1M2C
This figure presents the mean returns of Exit1M2C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.8) Exit1M3C
This figure presents the mean returns of Exit1M3C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.9) Exit1M4C
This figure presents the mean returns of Exit1M4C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.10) Exit1M5C
This figure presents the mean returns of Exit1M3C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.11) Reverse1M1C
This figure presents the mean returns of Reverse1M1C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.12) Reverse1M2C
This figure presents the mean returns of Reverse1M2C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.13) Reverse1M3C
This figure presents the mean returns of Reverse1M3C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.14) Reverse1M4C
This figure presents the mean returns of Reverse1M4C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.15) Reverse1M5C
This figure presents the mean returns of Reverse1M5C obtained from using the VIX signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
C.4 Plot of the performance measures using different timing signals of VXY for differ-ent standard deviations
Figure (C.16) Exit1M1C
This figure presents the mean returns of Exit1M1C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.17) Exit1M2C
This figure presents the mean returns of Exit1M2C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.18) Exit1M3C
This figure presents the mean returns of Exit1M3C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.19) Exit1M4C
This figure presents the mean returns of Exit1M4C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.20) Exit1M5C
This figure presents the mean returns of Exit1M5C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one-and two day lagged.
Figure (C.21) Reverse1M2C
This figure presents the mean returns of Reverse1M2C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.22) Reverse1M3C
This figure presents the mean returns of Reverse1M3C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
Figure (C.23) Reverse1M4C
This figure presents the mean returns of Reverse1M4C obtained from using the VXY signal that occurred contemporaneously with the rebalance days, as well as
one- and two day lagged.
C.5 Histogram of bootstrapped 3-, 6- and 12 month rebalance carry strategies
Figure (C.24) Probability density function
This figure shows the distribution of Z-scores of the mean returns for the 3-, 6-and 12 month rebalance static carry trade strategies, in the colour grey. The distribution of the bootstrapped Z-scores are shown with the blue lines. The red lines indicate where the x-axes are equal to 1.96 and -1.96. The abbreviations used
to denote the strategies are described in Appendix A.1
Python codes
This appendix contain a selection of the total code for this thesis. The aim is to give the reader the opportunity to understand the underlying code for some of the key elements of the methodology. In Appendix D.1 the python code of static carry trade construction is shown. All calculations are based on transaction cost. Appendix D.2 shows the construction and return calculation of the exit- and reverse strategy based on VIX. The last part, Appendix D.3 shows the bootstrap resample of the static carry trade. The same procedure is used for the exit- and reverse strategy.
D.1 Portfolio construction and carry trade re-turn calculations based on 1-month rebal-ance
1 #! / u s r / b i n / env python
2 # c o d i n g : u t f−8
3
4 # I n [ 3 ] :
5 6
7 i m p o r t pandas a s pd # pandas f o r w o r k i n g w i t h d a t a
8 i m p o r t m a t p l o t l i b . p y p l o t a s p l t # M a t p l o t l i b f o r p l o t t i n g
9 i m p o r t numpy a s np #Numpy i s a l i b r a r y f o r w o r k i n g w i t h n u m e r i c a l d a t a
10 i m p o r t s c i p y . s t a t s
11 i m p o r t s e a b o r n a s s n s
12 i m p o r t o s
13 i m p o r t s y s
14 i m p o r t m a t p l o t l i b . p y p l o t a s p l t
15 from m a t p l o t l i b . t i c k e r i m p o r t P e r c e n t F o r m a t t e r
16
17 p l t . r c (’ x t i c k ’, l a b e l s i z e =13)
18 p l t . r c (’ y t i c k ’, l a b e l s i z e =13)
19 p l t . r c (’ l e g e n d ’, f o n t s i z e =14)
20 p l t . r c (’ a x e s ’, t i t l e s i z e =12)
21 p l t . r c (’ a x e s ’, l a b e l s i z e =14)
22 23
24 # I n [ 2 ] :
25 26
27 o s . c h d i r ( o s . getcwd ( ) )
28
29 F o r w a r d r a t e s = pd . r e a d e x c e l (”Fwd r a t e s USD . x l s x ”)
30 S p o t r a t e s = pd . r e a d e x c e l (” Spot r a t e s USD . x l s x ”)
31 Date = pd . r e a d e x c e l (” Vol i n d i c e s . x l s x ”, u s e c o l s= [ 0 ] )
32
33 # Only u s e d a t e s t h a t a r e i n a v a l i a b l e f o r i n d i c e s
34 S p o t r a t e s = S p o t r a t e s . merge ( Date , on=’ Date ’, how=’ r i g h t ’) . dropna ( ) . s e t i n d e x (’ Date ’)
35 F o r w a r d r a t e s = F o r w a r d r a t e s . merge ( Date , on=’ Date ’, how=’ r i g h t ’) . dropna ( ) . s e t i n d e x (’ Date ’)
36
37 # Only u s e max v a l u e o f e a c h month ”M”
38 s p o t r a t e s 1 M = pd . DataFrame ( )
39 s p o t r a t e s 1 M =( S p o t r a t e s . l o c [ S p o t r a t e s . groupby ( S p o t r a t e s . i n d e x . t o p e r i o d (’M’) ) .a p p l y(lambda x : x . i n d e x .max( ) ) ] )
40 s p o t r a t e s 1 M . r e s e t i n d e x ( i n p l a c e=True )
41 Forward rates1M = pd . DataFrame ( )
42 Forward rates1M =( F o r w a r d r a t e s . l o c [ F o r w a r d r a t e s . groupby (
F o r w a r d r a t e s . i n d e x . t o p e r i o d (’M’) ) .a p p l y(lambda x : x . i n d e x .max ( ) ) ] )
43 Forward rates1M . r e s e t i n d e x ( i n p l a c e=True )
44 45
46 # I n [ 3 ] :
47 48
49 # c a l c u l a t e l o g r e t u r n
50
51 c o l s F 1 = Forward rates1M . columns [ 1 : ]
52
53 f o r column i n c o l s F 1 :
54 Forward rates1M [ column ] = np . l o g ( Forward rates1M [ column ] )
55
56 c o l s S 1 = s p o t r a t e s 1 M . columns [ 1 : ]
57
58 f o r column i n c o l s S 1 :
59 s p o t r a t e s 1 M [ column ] = np . l o g ( s p o t r a t e s 1 M [ column ] )
60 61
62 # I n [ 4 ] :
63 64
65 # o n l y t a k i n g t h e mid p r i c e : I t i s u s e d f o r t h e f o r w a r d d i s c o u n t r a t e
66 M i d c o l s = [ c o l f o r c o l i n Forward rates1M . columns i f ’ MID ’ i n c o l ]
67
68 Fwd MID1M=pd . DataFrame ( )
69
70 f o r column i n M i d c o l s :
71 Fwd MID1M [ column ] = Forward rates1M [ column ]
72
73 M i d c o l s s p o t = [ c o l f o r c o l i n s p o t r a t e s 1 M . columns i f ’ mid ’ i n c o l ]
74
75 spot MID1M=pd . DataFrame ( )
76
77 f o r column i n M i d c o l s s p o t :
78 spot MID1M [ column ] = s p o t r a t e s 1 M [ column ]
79 80
81 # I n [ 5 ] :
82 83
84 Fwd disc 1M=pd . DataFrame ( )
85
86 f o r c o l i n spot MID1M . columns :
87 c u r r = c o l . s p l i t (’ ’) [ 0 ]
88 c u r r c o l s = [ c o l f o r c o l i n Fwd MID1M . columns i f c u r r i n c o l ]
89 f o r c o l 1 i n c u r r c o l s :
90 Fwd disc 1M [ c o l 1 ] = Fwd MID1M [ c o l 1 ] . v a l u e s−spot MID1M [ c o l ] . v a l u e s
91 92
93 # I n [ 6 ] :
94 95
96 c o l = [ c o l f o r c o l i n Fwd disc 1M . columns i f ’ 1M’ i n c o l ]
97
98 Fwd d1M1M=pd . DataFrame ( )
99
100 f o r column i n c o l :
101 Fwd d1M1M [ column ] = Fwd disc 1M [ column ]
102 103
104 date1M=Forward rates1M [” Date ”] . t o f r a m e ( )
105
106 Fwd dis 1M = pd . c o n c a t ( [ date1M , Fwd d1M1M ] , a x i s =1, s o r t=F a l s e )
107 108
109 # I n [ 8 ] :
110 111
112 ########P o r t f o l i o c o n s t r u c t i o n 1M r e b a l a n c e 5 c u r r e n c i e s#######
113 # 5 c u r r e n c y i n t h e h i g h and low p o r t f o l i o
114 h i g h l i s t = [ ]
115 l o w l i s t = [ ]
116
117 c o l n a m e s = Fwd dis 1M . columns [ 1 : ]
118 f o r d a t e i n Fwd dis 1M [’ Date ’ ] :
119 temp = Fwd dis 1M [ Fwd dis 1M [’ Date ’]== d a t e ] . v a l u e s [ 0 ] [ 1 : ]
120 temp1 = temp<0
121 temp2 = temp>0
122 low5 = c o l n a m e s [ temp1 ]
123 t o p 5 = c o l n a m e s [ temp2 ]
124 l i s t c r e a t e r = [ d a t e ]
125 i f l e n( t o p 5 )<5:
126 f o r v a l i n t o p 5 :
127 l i s t c r e a t e r . append ( v a l )
128 n u m z e r o s = 5−l e n( t o p 5 )
129 f o r i i n r a n g e( n u m z e r o s ) :
130 l i s t c r e a t e r . append ( 0 )
131 e l i f l e n( t o p 5 )>5:
132 o r d e r i n g = np . a r g s o r t ( temp [ temp2 ] )
133 f o r v a l i n t o p 5 [ o r d e r i n g [−5 : ] ] :
134 l i s t c r e a t e r . append ( v a l )
135 e l s e:
136 f o r v a l i n t o p 5 :
137 l i s t c r e a t e r . append ( v a l )
138
139 l i s t c r e a t e r 1 = [ d a t e ]
140 i f l e n( low5 )<5:
141 f o r v a l i n low5 :
142 l i s t c r e a t e r 1 . append ( v a l )
143 n u m z e r o s = 5−l e n( low5 )
144 f o r i i n r a n g e( n u m z e r o s ) :
145 l i s t c r e a t e r 1 . append ( 0 )
146 e l i f l e n( low5 )>5:
147 o r d e r i n g = np . a r g s o r t ( temp [ temp1 ] )
148 f o r v a l i n low5 [ o r d e r i n g [ : 5 ] ] :
149 l i s t c r e a t e r 1 . append ( v a l )
150 e l s e:
151 f o r v a l i n low5 :
152 l i s t c r e a t e r 1 . append ( v a l )
153
154 h i g h l i s t . append ( l i s t c r e a t e r )
155 l o w l i s t . append ( l i s t c r e a t e r 1 )
156
157 highDF = pd . DataFrame ( h i g h l i s t )
158 lowDF = pd . DataFrame ( l o w l i s t )
159 160
161 # I n [ 1 1 ] :
162 163
164 ########################Carry t r a d e############################
165 #C a l c u l a t e r e t u r n o f t h e h i g h e s t p o r t f o l i o
166 R l o n g = [ ]
167 f o r i n d e x , row i n highDF . i t e r r o w s ( ) :
168 d a t e l i s t = [ ]
169 i f i n d e x==l e n( highDF )−1:
170 b r e a k
171
172 f o r i i n r a n g e( 1 ,l e n( row ) ) :
173 i f row [ i ]==0:
174 d a t e l i s t . append ( np . nan )
175 c o n t i n u e
176 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
177 temp1 = temp+’ BID 1M ’
178 f b i d = Forward rates1M [ temp1 ] [ i n d e x ]
179
180 i f row [ i ] i n highDF . i l o c [ i n d e x + 1 ] . v a l u e s :
181 temp2 = temp+’ mid ’
182 s m i d = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
183 d a t e l i s t . append ( f b i d−s m i d )
184 e l s e:
185 temp2 = temp+’ a s k ’
186 s a s k = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
187 d a t e l i s t . append ( f b i d−s a s k )
188 R l o n g . append ( d a t e l i s t )
189
190
191 # I n [ 1 2 ] :
192 193
194 #C a l c u l a t e r e t u r n o f t h e l o w e s t p o r t f o l i o
195 R s h o r t = [ ]
196 f o r i n d e x , row i n lowDF . i t e r r o w s ( ) :
197 d a t e l i s t = [ ]
198 i f i n d e x==l e n( lowDF )−1:
199 b r e a k
200 #p r i n t ( i n d e x )
201
202 f o r i i n r a n g e( 1 ,l e n( row ) ) :
203 i f row [ i ]==0:
204 d a t e l i s t . append ( np . nan )
205 c o n t i n u e
206 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
207 temp1 = temp+’ b i d ’
208 s b i d = s p o t r a t e s 1 M [ temp1 ] [ i n d e x +1]
209
210 i f row [ i ] i n lowDF . i l o c [ i n d e x + 1 ] . v a l u e s :
211 temp2 = temp+’ MID 1M ’
212 f m i d = Forward rates1M [ temp2 ] [ i n d e x ]
213 d a t e l i s t . append(−f m i d+s b i d )
214 e l s e:
215 temp2 = temp+’ ASK 1M ’
216 f a s k = Forward rates1M [ temp2 ] [ i n d e x ]
217 d a t e l i s t . append(−f a s k+s b i d )
218 R s h o r t . append ( d a t e l i s t )
219 220
221 # I n [ 1 3 ] :
222 223
224 o u t 1 = pd . DataFrame ( R s h o r t )
225 o u t 2 = pd . DataFrame ( R l o n g )
226 r e s u l t = pd . c o n c a t ( [ o u t 1 , o u t 2 ] , a x i s =1, s o r t=F a l s e )
227 carry1M = pd . DataFrame ( )
228 carry1M [’ Date ’] = highDF [ 0 ] [ 1 : ] . v a l u e s
229 carry1M [’ carry1M5C ’]= r e s u l t . mean ( a x i s =1, s k i p n a=True )
230 231
232 # I n [ 1 5 ] :
233 234
235 ########P o r t f o l i o c o n s t r u c t i o n 1M r e b a l a n c e 4 c u r r e n c i e s#######
236 # 4 c u r r e n c y i n t h e h i g h and low p o r t f o l i o
237
238 h i g h l i s t = [ ]
239 l o w l i s t = [ ]
240
241 c o l n a m e s = Fwd dis 1M . columns [ 1 : ]
242 f o r d a t e i n Fwd dis 1M [’ Date ’ ] :
243 temp = Fwd dis 1M [ Fwd dis 1M [’ Date ’]== d a t e ] . v a l u e s [ 0 ] [ 1 : ]
244 temp1 = temp<0
245 temp2 = temp>0
246 low4 = c o l n a m e s [ temp1 ]
247 t o p 4 = c o l n a m e s [ temp2 ]
248 l i s t c r e a t e r = [ d a t e ]
249 i f l e n( t o p 4 )<4:
250 f o r v a l i n t o p 4 :
251 l i s t c r e a t e r . append ( v a l )
252 n u m z e r o s = 4−l e n( t o p 4 )
253 f o r i i n r a n g e( n u m z e r o s ) :
254 l i s t c r e a t e r . append ( 0 )
255 e l i f l e n( t o p 4 )>4:
256 o r d e r i n g = np . a r g s o r t ( temp [ temp2 ] )
257 f o r v a l i n t o p 4 [ o r d e r i n g [−4 : ] ] :
258 l i s t c r e a t e r . append ( v a l )
259 e l s e:
260 f o r v a l i n t o p 4 :
261 l i s t c r e a t e r . append ( v a l )
262
263 l i s t c r e a t e r 1 = [ d a t e ]
264 i f l e n( low4 )<4:
265 f o r v a l i n low4 :
266 l i s t c r e a t e r 1 . append ( v a l )
267 n u m z e r o s = 4−l e n( low4 )
268 f o r i i n r a n g e( n u m z e r o s ) :
269 l i s t c r e a t e r 1 . append ( 0 )
270 e l i f l e n( low4 )>4:
271 o r d e r i n g = np . a r g s o r t ( temp [ temp1 ] )
272 f o r v a l i n low4 [ o r d e r i n g [ : 4 ] ] :
273 l i s t c r e a t e r 1 . append ( v a l )
274 e l s e:
275 f o r v a l i n low4 :
276 l i s t c r e a t e r 1 . append ( v a l )
277
278 h i g h l i s t . append ( l i s t c r e a t e r )
279 l o w l i s t . append ( l i s t c r e a t e r 1 )
280
281 highDF = pd . DataFrame ( h i g h l i s t )
282 lowDF = pd . DataFrame ( l o w l i s t )
283 284
285 # I n [ 1 7 ] :
286 287
288 ########################Carry t r a d e############################
289 R l o n g = [ ]
290 f o r i n d e x , row i n highDF . i t e r r o w s ( ) :
291 d a t e l i s t = [ ]
292 i f i n d e x==l e n( highDF )−1:
293 b r e a k
294
295 f o r i i n r a n g e( 1 ,l e n( row ) ) :
296 i f row [ i ]==0:
297 d a t e l i s t . append ( np . nan )
298 c o n t i n u e
299 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
300 temp1 = temp+’ BID 1M ’
301 f b i d = Forward rates1M [ temp1 ] [ i n d e x ]
302
303 i f row [ i ] i n highDF . i l o c [ i n d e x + 1 ] . v a l u e s :
304 temp2 = temp+’ mid ’
305 s m i d = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
306 d a t e l i s t . append ( f b i d−s m i d )
307 e l s e:
308 temp2 = temp+’ a s k ’
309 s a s k = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
310 d a t e l i s t . append ( f b i d−s a s k )
311 R l o n g . append ( d a t e l i s t )
312 313
314 # I n [ 1 8 ] :
315 316
317 R s h o r t = [ ]
318 f o r i n d e x , row i n lowDF . i t e r r o w s ( ) :
319 d a t e l i s t = [ ]
320 i f i n d e x==l e n( lowDF )−1:
321 b r e a k
322 #p r i n t ( i n d e x )
323
324 f o r i i n r a n g e( 1 ,l e n( row ) ) :
325 i f row [ i ]==0:
326 d a t e l i s t . append ( np . nan )
327 c o n t i n u e
328 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
329 temp1 = temp+’ b i d ’
330 s b i d = s p o t r a t e s 1 M [ temp1 ] [ i n d e x +1]
331
332 i f row [ i ] i n lowDF . i l o c [ i n d e x + 1 ] . v a l u e s :
333 temp2 = temp+’ MID 1M ’
334 f m i d = Forward rates1M [ temp2 ] [ i n d e x ]
335 d a t e l i s t . append(−f m i d+s b i d )
336 e l s e:
337 temp2 = temp+’ ASK 1M ’
338 f a s k = Forward rates1M [ temp2 ] [ i n d e x ]
339 d a t e l i s t . append(−f a s k+s b i d )
340 R s h o r t . append ( d a t e l i s t )
341 342
343 # I n [ 1 9 ] :
344 345
346 o u t 1 = pd . DataFrame ( R s h o r t )
347 o u t 2 = pd . DataFrame ( R l o n g )
348 r e s u l t = pd . c o n c a t ( [ o u t 1 , o u t 2 ] , a x i s =1, s o r t=F a l s e )
349 carry1M [’ carry1M4C ’]= r e s u l t . mean ( a x i s =1, s k i p n a=True )
350 351
352 # I n [ 2 2 ] :
353 354
355 ########P o r t f o l i o c o n s t r u c t i o n 1M r e b a l a n c e 3 c u r r e n c i e s#######
356 # 3 c u r r e n c y i n t h e h i g h and low p o r t f o l i o
357 h i g h l i s t = [ ]
358 l o w l i s t = [ ]
359
360 c o l n a m e s = Fwd dis 1M . columns [ 1 : ]
361 f o r d a t e i n Fwd dis 1M [’ Date ’ ] :
362 temp = Fwd dis 1M [ Fwd dis 1M [’ Date ’]== d a t e ] . v a l u e s [ 0 ] [ 1 : ]
363 temp1 = temp<0
364 temp2 = temp>0
365 low3 = c o l n a m e s [ temp1 ]
366 t o p 3 = c o l n a m e s [ temp2 ]
367 l i s t c r e a t e r = [ d a t e ]
368 i f l e n( t o p 3 )<3:
369 f o r v a l i n t o p 3 :
370 l i s t c r e a t e r . append ( v a l )
371 n u m z e r o s = 3−l e n( t o p 3 )
372 f o r i i n r a n g e( n u m z e r o s ) :
373 l i s t c r e a t e r . append ( 0 )
374 e l i f l e n( t o p 3 )>3:
375 o r d e r i n g = np . a r g s o r t ( temp [ temp2 ] )
376 f o r v a l i n t o p 3 [ o r d e r i n g [−3 : ] ] :
377 l i s t c r e a t e r . append ( v a l )
378 e l s e:
379 f o r v a l i n t o p 3 :
380 l i s t c r e a t e r . append ( v a l )
381
382 l i s t c r e a t e r 1 = [ d a t e ]
383 i f l e n( low3 )<3:
384 f o r v a l i n low3 :
385 l i s t c r e a t e r 1 . append ( v a l )
386 n u m z e r o s = 3−l e n( low3 )
387 f o r i i n r a n g e( n u m z e r o s ) :
388 l i s t c r e a t e r 1 . append ( 0 )
389 e l i f l e n( low3 )>3:
390 o r d e r i n g = np . a r g s o r t ( temp [ temp1 ] )
391 f o r v a l i n low3 [ o r d e r i n g [ : 3 ] ] :
392 l i s t c r e a t e r 1 . append ( v a l )
393 e l s e:
394 f o r v a l i n low3 :
395 l i s t c r e a t e r 1 . append ( v a l )
396
397 h i g h l i s t . append ( l i s t c r e a t e r )
398 l o w l i s t . append ( l i s t c r e a t e r 1 )
399
400 highDF = pd . DataFrame ( h i g h l i s t )
401 lowDF = pd . DataFrame ( l o w l i s t )
402 403
404 # I n [ 2 4 ] :
405 406
407 ########################Carry t r a d e############################
408 R l o n g = [ ]
409 f o r i n d e x , row i n highDF . i t e r r o w s ( ) :
410 d a t e l i s t = [ ]
411 i f i n d e x==l e n( highDF )−1:
412 b r e a k
413
414 f o r i i n r a n g e( 1 ,l e n( row ) ) :
415 i f row [ i ]==0:
416 d a t e l i s t . append ( np . nan )
417 c o n t i n u e
418 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
419 temp1 = temp+’ BID 1M ’
420 f b i d = Forward rates1M [ temp1 ] [ i n d e x ]
421
422 i f row [ i ] i n highDF . i l o c [ i n d e x + 1 ] . v a l u e s :
423 temp2 = temp+’ mid ’
424 s m i d = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
425 d a t e l i s t . append ( f b i d−s m i d )
426 e l s e:
427 temp2 = temp+’ a s k ’
428 s a s k = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
429 d a t e l i s t . append ( f b i d−s a s k )
430 R l o n g . append ( d a t e l i s t )
431 432
433 # I n [ 2 5 ] :
434 435
436 R s h o r t = [ ]
437 f o r i n d e x , row i n lowDF . i t e r r o w s ( ) :
438 d a t e l i s t = [ ]
439 i f i n d e x==l e n( lowDF )−1:
440 b r e a k
441 #p r i n t ( i n d e x )
442
443 f o r i i n r a n g e( 1 ,l e n( row ) ) :
444 i f row [ i ]==0:
445 d a t e l i s t . append ( np . nan )
446 c o n t i n u e
447 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
448 temp1 = temp+’ b i d ’
449 s b i d = s p o t r a t e s 1 M [ temp1 ] [ i n d e x +1]
450
451 i f row [ i ] i n lowDF . i l o c [ i n d e x + 1 ] . v a l u e s :
452 temp2 = temp+’ MID 1M ’
453 f m i d = Forward rates1M [ temp2 ] [ i n d e x ]
454 d a t e l i s t . append(−f m i d+s b i d )
455 e l s e:
456 temp2 = temp+’ ASK 1M ’
457 f a s k = Forward rates1M [ temp2 ] [ i n d e x ]
458 d a t e l i s t . append(−f a s k+s b i d )
459 R s h o r t . append ( d a t e l i s t )
460 461
462 # I n [ 2 6 ] :
463 464
465 o u t 1 = pd . DataFrame ( R s h o r t )
466 o u t 2 = pd . DataFrame ( R l o n g )
467 r e s u l t = pd . c o n c a t ( [ o u t 1 , o u t 2 ] , a x i s =1, s o r t=F a l s e )
468 carry1M [’ carry1M3C ’]= r e s u l t . mean ( a x i s =1, s k i p n a=True )
469 470
471 # I n [ 2 8 ] :
472 473
474 ########P o r t f o l i o c o n s t r u c t i o n 1M r e b a l a n c e 2 c u r r e n c i e s#######
475 # 2 c u r r e n c y i n t h e h i g h and low p o r t f o l i o
476 h i g h l i s t = [ ]
477 l o w l i s t = [ ]
478
479 c o l n a m e s = Fwd dis 1M . columns [ 1 : ]
480 f o r d a t e i n Fwd dis 1M [’ Date ’ ] :
481 temp = Fwd dis 1M [ Fwd dis 1M [’ Date ’]== d a t e ] . v a l u e s [ 0 ] [ 1 : ]
482 temp1 = temp<0
483 temp2 = temp>0
484 low2 = c o l n a m e s [ temp1 ]
485 t o p 2 = c o l n a m e s [ temp2 ]
486 l i s t c r e a t e r = [ d a t e ]
487 i f l e n( t o p 2 )<2:
488 f o r v a l i n t o p 2 :
489 l i s t c r e a t e r . append ( v a l )
490 n u m z e r o s = 2−l e n( t o p 2 )
491 f o r i i n r a n g e( n u m z e r o s ) :
492 l i s t c r e a t e r . append ( 0 )
493 e l i f l e n( t o p 2 )>2:
494 o r d e r i n g = np . a r g s o r t ( temp [ temp2 ] )
495 f o r v a l i n t o p 2 [ o r d e r i n g [−2 : ] ] :
496 l i s t c r e a t e r . append ( v a l )
497 e l s e:
498 f o r v a l i n t o p 2 :
499 l i s t c r e a t e r . append ( v a l )
500
501 l i s t c r e a t e r 1 = [ d a t e ]
502 i f l e n( low2 )<2:
503 f o r v a l i n low2 :
504 l i s t c r e a t e r 1 . append ( v a l )
505 n u m z e r o s = 2−l e n( low2 )
506 f o r i i n r a n g e( n u m z e r o s ) :
507 l i s t c r e a t e r 1 . append ( 0 )
508 e l i f l e n( low2 )>2:
509 o r d e r i n g = np . a r g s o r t ( temp [ temp1 ] )
510 f o r v a l i n low2 [ o r d e r i n g [ : 2 ] ] :
511 l i s t c r e a t e r 1 . append ( v a l )
512 e l s e:
513 f o r v a l i n low2 :
514 l i s t c r e a t e r 1 . append ( v a l )
515
516 h i g h l i s t . append ( l i s t c r e a t e r )
517 l o w l i s t . append ( l i s t c r e a t e r 1 )
518
519 highDF = pd . DataFrame ( h i g h l i s t )
520 lowDF = pd . DataFrame ( l o w l i s t )
521 522
523 # I n [ 3 0 ] :
524 525
526 ########################Carry t r a d e############################
527 R l o n g = [ ]
528 f o r i n d e x , row i n highDF . i t e r r o w s ( ) :
529 d a t e l i s t = [ ]
530 i f i n d e x==l e n( highDF )−1:
531 b r e a k
532
533 f o r i i n r a n g e( 1 ,l e n( row ) ) :
534 i f row [ i ]==0:
535 d a t e l i s t . append ( np . nan )
536 c o n t i n u e
537 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
538 temp1 = temp+’ BID 1M ’
539 f b i d = Forward rates1M [ temp1 ] [ i n d e x ]
540
541 i f row [ i ] i n highDF . i l o c [ i n d e x + 1 ] . v a l u e s :
542 temp2 = temp+’ mid ’
543 s m i d = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
544 d a t e l i s t . append ( f b i d−s m i d )
545 e l s e:
546 temp2 = temp+’ a s k ’
547 s a s k = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
548 d a t e l i s t . append ( f b i d−s a s k )
549 R l o n g . append ( d a t e l i s t )
550 551
552 # I n [ 3 1 ] :
553 554
555 R s h o r t = [ ]
556 f o r i n d e x , row i n lowDF . i t e r r o w s ( ) :
557 d a t e l i s t = [ ]
558 i f i n d e x==l e n( lowDF )−1:
559 b r e a k
560 #p r i n t ( i n d e x )
561
562 f o r i i n r a n g e( 1 ,l e n( row ) ) :
563 i f row [ i ]==0:
564 d a t e l i s t . append ( np . nan )
565 c o n t i n u e
566 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
567 temp1 = temp+’ b i d ’
568 s b i d = s p o t r a t e s 1 M [ temp1 ] [ i n d e x +1]
569
570 i f row [ i ] i n lowDF . i l o c [ i n d e x + 1 ] . v a l u e s :
571 temp2 = temp+’ MID 1M ’
572 f m i d = Forward rates1M [ temp2 ] [ i n d e x ]
573 d a t e l i s t . append(−f m i d+s b i d )
574 e l s e:
575 temp2 = temp+’ ASK 1M ’
576 f a s k = Forward rates1M [ temp2 ] [ i n d e x ]
577 d a t e l i s t . append(−f a s k+s b i d )
578 R s h o r t . append ( d a t e l i s t )
579 580
581 # I n [ 3 2 ] :
582 583
584 o u t 1 = pd . DataFrame ( R s h o r t )
585 o u t 2 = pd . DataFrame ( R l o n g )
586 r e s u l t = pd . c o n c a t ( [ o u t 1 , o u t 2 ] , a x i s =1, s o r t=F a l s e )
587 carry1M [’ carry1M2C ’]= r e s u l t . mean ( a x i s =1, s k i p n a=True )
588 589
590 # I n [ 3 4 ] :
591 592
593 ########P o r t f o l i o c o n s t r u c t i o n 1M r e b a l a n c e 1 c u r r e n c i e s#######
594 # 1 c u r r e n c y i n t h e h i g h and low p o r t f o l i o
595 h i g h l i s t = [ ]
596 l o w l i s t = [ ]
597
598 c o l n a m e s = Fwd dis 1M . columns [ 1 : ]
599 f o r d a t e i n Fwd dis 1M [’ Date ’ ] :
600 temp = Fwd dis 1M [ Fwd dis 1M [’ Date ’]== d a t e ] . v a l u e s [ 0 ] [ 1 : ]
601 temp1 = temp<0
602 temp2 = temp>0
603 low1 = c o l n a m e s [ temp1 ]
604 t o p 1 = c o l n a m e s [ temp2 ]
605 l i s t c r e a t e r = [ d a t e ]
606 i f l e n( t o p 1 )<1:
607 f o r v a l i n t o p 1 :
608 l i s t c r e a t e r . append ( v a l )
609 n u m z e r o s = 1−l e n( t o p 1 )
610 f o r i i n r a n g e( n u m z e r o s ) :
611 l i s t c r e a t e r . append ( 0 )
612 e l i f l e n( t o p 1 )>1:
613 o r d e r i n g = np . a r g s o r t ( temp [ temp2 ] )
614 f o r v a l i n t o p 1 [ o r d e r i n g [−1 : ] ] :
615 l i s t c r e a t e r . append ( v a l )
616 e l s e:
617 f o r v a l i n t o p 1 :
618 l i s t c r e a t e r . append ( v a l )
619
620 l i s t c r e a t e r 1 = [ d a t e ]
621 i f l e n( low1 )<1:
622 f o r v a l i n low5 :
623 l i s t c r e a t e r 1 . append ( v a l )
624 n u m z e r o s = 1−l e n( low1 )
625 f o r i i n r a n g e( n u m z e r o s ) :
626 l i s t c r e a t e r 1 . append ( 0 )
627 e l i f l e n( low1 )>1:
628 o r d e r i n g = np . a r g s o r t ( temp [ temp1 ] )
629 f o r v a l i n low1 [ o r d e r i n g [ : 1 ] ] :
630 l i s t c r e a t e r 1 . append ( v a l )
631 e l s e:
632 f o r v a l i n low1 :
633 l i s t c r e a t e r 1 . append ( v a l )
634
635 h i g h l i s t . append ( l i s t c r e a t e r )
636 l o w l i s t . append ( l i s t c r e a t e r 1 )
637
638 highDF = pd . DataFrame ( h i g h l i s t )
639 lowDF = pd . DataFrame ( l o w l i s t )
640 641
642 # I n [ 3 6 ] :
643 644
645 ########################Carry t r a d e############################
646 R l o n g = [ ]
647 f o r i n d e x , row i n highDF . i t e r r o w s ( ) :
648 d a t e l i s t = [ ]
649 i f i n d e x==l e n( highDF )−1:
650 b r e a k
651
652 f o r i i n r a n g e( 1 ,l e n( row ) ) :
653 i f row [ i ]==0:
654 d a t e l i s t . append ( np . nan )
655 c o n t i n u e
656 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
657 temp1 = temp+’ BID 1M ’
658 f b i d = Forward rates1M [ temp1 ] [ i n d e x ]
659
660 i f row [ i ] i n highDF . i l o c [ i n d e x + 1 ] . v a l u e s :
661 temp2 = temp+’ mid ’
662 s m i d = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
663 d a t e l i s t . append ( f b i d−s m i d )
664 e l s e:
665 temp2 = temp+’ a s k ’
666 s a s k = s p o t r a t e s 1 M [ temp2 ] [ i n d e x +1]
667 d a t e l i s t . append ( f b i d−s a s k )
668 R l o n g . append ( d a t e l i s t )
669 670
671 # I n [ 3 7 ] :
672 673
674 R s h o r t = [ ]
675 f o r i n d e x , row i n lowDF . i t e r r o w s ( ) :
676 d a t e l i s t = [ ]
677 i f i n d e x==l e n( lowDF )−1:
678 b r e a k
679 #p r i n t ( i n d e x )
680
681 f o r i i n r a n g e( 1 ,l e n( row ) ) :
682 i f row [ i ]==0:
683 d a t e l i s t . append ( np . nan )
684 c o n t i n u e
685 temp = row [ i ] . s p l i t (’ ’) [ 0 ]
686 temp1 = temp+’ b i d ’
687 s b i d = s p o t r a t e s 1 M [ temp1 ] [ i n d e x +1]
688
689 i f row [ i ] i n lowDF . i l o c [ i n d e x + 1 ] . v a l u e s :
690 temp2 = temp+’ MID 1M ’
691 f m i d = Forward rates1M [ temp2 ] [ i n d e x ]
692 d a t e l i s t . append(−f m i d+s b i d )
693 e l s e:
694 temp2 = temp+’ ASK 1M ’
695 f a s k = Forward rates1M [ temp2 ] [ i n d e x ]
696 d a t e l i s t . append(−f a s k+s b i d )
697 R s h o r t . append ( d a t e l i s t )
698 699
700 # I n [ 3 8 ] :
701 702
703 o u t 1 = pd . DataFrame ( R s h o r t )
704 o u t 2 = pd . DataFrame ( R l o n g )
705 r e s u l t = pd . c o n c a t ( [ o u t 1 , o u t 2 ] , a x i s =1, s o r t=F a l s e )
706 carry1M [’ carry1M1C ’]= r e s u l t . mean ( a x i s =1, s k i p n a=True )
707 708
709 # I n [ ] :
710 711
712 ################c u m u l a t i v e c a r r y t r a d e####################
713 carry1M [” cum return1M5C ”] = np . exp ( ( carry1M [’ carry1M5C ’] ) . cumsum ( ) )∗100
714 carry1M [” cum return1M4C ”] = np . exp ( ( carry1M [’ carry1M4C ’] ) . cumsum ( ) )∗100
715 carry1M [” cum return1M3C ”] = np . exp ( ( carry1M [’ carry1M3C ’] ) . cumsum ( ) )∗100
716 carry1M [” cum return1M2C ”] = np . exp ( ( carry1M [’ carry1M2C ’] ) . cumsum ( ) )∗100
717 carry1M [” cum return1M1C ”] = np . exp ( ( carry1M [’ carry1M1C ’] ) . cumsum ( ) )∗100
718 719
720 # I n [ 3 9 ] :
721 722
723 ##############Mean and Sharpe r a t i o ###########################
724 p r i n t( carry1M . mean ( )∗1 2 )
725 p r i n t( ( 1 2∗carry1M . mean ( ) ) / ( carry1M . s t d ( )∗np . s q r t ( 1 2 ) ) )
726 727
728 # I n [ 5 0 ] :
729 730
731 #############Norminal p−v a l u e from z−s c o r e ###################
732 p r i n t( s c i p y . s t a t s . norm . s f (abs( carry1M [’ carry1M1C ’] . mean ( )∗1 2 / ( ( 1 2∗ carry1M [’ carry1M1C ’] . s t d ( ) ) /np . s q r t (l e n( carry1M [’ carry1M1C ’] ) ) ) ) )∗2 )
733 p r i n t( s c i p y . s t a t s . norm . s f (abs( carry1M [’ carry1M2C ’] . mean ( )∗1 2 / ( ( 1 2∗ carry1M [’ carry1M2C ’] . s t d ( ) ) /np . s q r t (l e n( carry1M [’ carry1M2C ’] ) ) )
) )∗2 )
734 p r i n t( s c i p y . s t a t s . norm . s f (abs( carry1M [’ carry1M3C ’] . mean ( )∗1 2 / ( ( 1 2∗ carry1M [’ carry1M3C ’] . s t d ( ) ) /np . s q r t (l e n( carry1M [’ carry1M3C ’] ) ) ) ) )∗2 )
735 p r i n t( s c i p y . s t a t s . norm . s f (abs( carry1M [’ carry1M4C ’] . mean ( )∗1 2 / ( ( 1 2∗ carry1M [’ carry1M4C ’] . s t d ( ) ) /np . s q r t (l e n( carry1M [’ carry1M4C ’] ) ) ) ) )∗2 )
736 p r i n t( s c i p y . s t a t s . norm . s f (abs( carry1M [’ carry1M5C ’] . mean ( )∗1 2 / ( ( 1 2∗ carry1M [’ carry1M5C ’] . s t d ( ) ) /np . s q r t (l e n( carry1M [’ carry1M5C ’] ) ) ) ) )∗2 )
D.2 Exit- and Reverse Strategy 1-month re-balance based on VIX
1 #! / u s r / b i n / env python
2 # c o d i n g : u t f−8
3
4 # I n [ 1 ] :
5 6
7 i m p o r t pandas a s pd # pandas f o r w o r k i n g w i t h d a t a
8 i m p o r t m a t p l o t l i b . p y p l o t a s p l t # M a t p l o t l i b f o r p l o t t i n g
9 i m p o r t numpy a s np #Numpy i s a l i b r a r y f o r w o r k i n g w i t h n u m e r i c a l d a t a
10 i m p o r t o s
11 i m p o r t s c i p y . s t a t s
12 i m p o r t s y s
13
14 o s . c h d i r ( o s . getcwd ( ) )
15 carry1M = pd . r e a d e x c e l (” carry1M MID . x l s x ”, i n d e x c o l =0)
16 carry1M [’ Date ’]= carry1M [’ Date ’ ] . a s t y p e ( np . d a t e t i m e 6 4 )
17 18
19 # I n [ 2 ] :
20 21
22 #####V o l a t i l i t y i n d i c a t o r E x i t s t r a t e g y#####
23 v o l i n d e x = pd . r e a d e x c e l (” Vol i n d i c e s . x l s x ”)
24
25 s t d f r o m m e a n = 1 . 5
26 window = round( 2 5 3∗1 . 5 )
27
28 d e f g e t t h r e s h o l d ( v o l i n d e x , window , s t d f r o m m e a n ) :
29 r o l l i n g m e a n = v o l i n d e x [’ VIX I n d e x ’ ] . r o l l i n g ( window = window ) . mean ( )
30 r o l l i n g s t d = v o l i n d e x [’ VIX I n d e x ’ ] . r o l l i n g ( window = window ) . s t d ( )
31 t h r e s h o l d = ( s t d f r o m m e a n ∗ r o l l i n g s t d ) + r o l l i n g m e a n
32
33 r e t u r n t h r e s h o l d
34
35 v o l i n d e x [” T h r e s h o l d ”] = g e t t h r e s h o l d ( v o l i n d e x , window , s t d f r o m m e a n )
36 37
38 d e f g e t p o s i t i o n ( v o l i n d e x ) :
39 i n d e x a b o v e s t d = v o l i n d e x [’ VIX I n d e x ’] > v o l i n d e x [’ T h r e s h o l d ’]
40 i n d e x b e l o v s t d = v o l i n d e x [’ VIX I n d e x ’] < v o l i n d e x [’ T h r e s h o l d ’]
41 v o l i n d e x [’ pos ’] = ”NA”
42 v o l i n d e x [’ pos ’] = v o l i n d e x [’ pos ’ ] . where ( ˜ i n d e x a b o v e s t d , o t h e r = 0 )
43 v o l i n d e x [’ pos ’] = v o l i n d e x [’ pos ’ ] . where ( ˜ i n d e x b e l o v s t d , o t h e r = 1 )
44
45 r e t u r n v o l i n d e x [’ pos ’]
46
47 v o l i n d e x [’ pos ’] = g e t p o s i t i o n ( v o l i n d e x )
48 49
50 v o l i n d e x [’ D a t e s i g n a l ’] = v o l i n d e x [’ Date ’]
51 v o l i n d e x [’ Date ’] = v o l i n d e x [’ Date ’ ] . s h i f t (−1)
52 53
54 s t r a t e g y 1 M = carry1M
55 s t r a t e g y 1 M = v o l i n d e x . merge ( s t r a t e g y 1 M , on=’ Date ’, how=’ r i g h t ’)
56 57
58 # I n [ 3 ] :
59 60
61 ###########E x i t s t r a t e g y##########
62 s t r a t e g y 1 = [ ]
63 s t r a t e g y 2 = [ ]
64 s t r a t e g y 3 = [ ]
65 s t r a t e g y 4 = [ ]
66 s t r a t e g y 5 = [ ]
67 p o s i t i o n s = s t r a t e g y 1 M [’ pos ’ ] . v a l u e s
68 p o s i t i o n s d a t e = s t r a t e g y 1 M [’ Date ’ ] . v a l u e s
69 f o r i i n r a n g e( 0 ,l e n( p o s i t i o n s ) ) :
70 i f p o s i t i o n s [ i ]==1:
71 s t r a t e g y 1 . append ( 1∗s t r a t e g y 1 M [’ carry1M1C ’] . v a l u e s [ i ] )
72 s t r a t e g y 2 . append ( 1∗s t r a t e g y 1 M [’ carry1M2C ’] . v a l u e s [ i ] )
73 s t r a t e g y 3 . append ( 1∗s t r a t e g y 1 M [’ carry1M3C ’] . v a l u e s [ i ] )
74 s t r a t e g y 4 . append ( 1∗s t r a t e g y 1 M [’ carry1M4C ’] . v a l u e s [ i ] )
75 s t r a t e g y 5 . append ( 1∗s t r a t e g y 1 M [’ carry1M5C ’] . v a l u e s [ i ] )
76 e l s e:
77 s t r a t e g y 1 . append ( 0∗s t r a t e g y 1 M [’ carry1M1C ’] . v a l u e s [ i ] )
78 s t r a t e g y 2 . append ( 0∗s t r a t e g y 1 M [’ carry1M2C ’] . v a l u e s [ i ] )
79 s t r a t e g y 3 . append ( 0∗s t r a t e g y 1 M [’ carry1M3C ’] . v a l u e s [ i ] )
80 s t r a t e g y 4 . append ( 0∗s t r a t e g y 1 M [’ carry1M4C ’] . v a l u e s [ i ] )
81 s t r a t e g y 5 . append ( 0∗s t r a t e g y 1 M [’ carry1M5C ’] . v a l u e s [ i ] )
82
83 s t r a t e g y 1 M [’ E x i t strategy1M1C ’] = s t r a t e g y 1
84 s t r a t e g y 1 M [’ E x i t strategy1M2C ’] = s t r a t e g y 2
85 s t r a t e g y 1 M [’ E x i t strategy1M3C ’] = s t r a t e g y 3
86 s t r a t e g y 1 M [’ E x i t strategy1M4C ’] = s t r a t e g y 4
87 s t r a t e g y 1 M [’ E x i t strategy1M5C ’] = s t r a t e g y 5
88 89
90 # I n [ 4 ] :
91 92
93 #P e r f o r m a n c e m e a s u r e s : F u l l sample p e r i o d 1999−2020
94 p r i n t( s t r a t e g y 1 M . mean ( )∗1 2 )
95 p r i n t( ( 1 2∗s t r a t e g y 1 M . mean ( ) ) / ( s t r a t e g y 1 M . s t d ( )∗np . s q r t ( 1 2 ) ) )
96 97
98 # I n [ 5 ] :
99 100
101 #Nominal p−v a l u e
102 p r i n t(’ 1M1C’)
103 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ E x i t strategy1M1C ’] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ E x i t strategy1M1C ’ ] . s t d ( ) ) /np . s q r t (l e n( s t r a t e g y 1 M [’ E x i t strategy1M1C ’] ) ) ) ) )∗2 )
104 p r i n t(’ 1M2C’)
105 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ E x i t strategy1M2C ’] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ E x i t strategy1M2C ’ ] . s t d ( ) ) /np . s q r t (l e n( s t r a t e g y 1 M [’ E x i t strategy1M2C ’] ) ) ) ) )∗2 )
106 p r i n t(’ 1M3C’)
107 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ E x i t strategy1M3C ’] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ E x i t strategy1M3C ’ ] . s t d ( ) ) /np . s q r t (l e n( s t r a t e g y 1 M [’ E x i t strategy1M3C ’] ) ) ) ) )∗2 )
108 p r i n t(’ 1M4C’)
109 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ E x i t strategy1M4C ’] . mean
( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ E x i t strategy1M4C ’ ] . s t d ( ) ) /np . s q r t (l e n( s t r a t e g y 1 M [’ E x i t strategy1M4C ’] ) ) ) ) )∗2 )
110 p r i n t(’ 1M5C’)
111 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ E x i t strategy1M5C ’] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ E x i t strategy1M5C ’ ] . s t d ( ) ) /np . s q r t (l e n( s t r a t e g y 1 M [’ E x i t strategy1M5C ’] ) ) ) ) )∗2 )
112 113
114 # I n [ 1 2 ] :
115 116
117 #F r a c t i o n o f n e g a t i v e#
118 p r i n t(sum( s t r a t e g y 1 M [’ E x i t strategy1M1C ’]<0 ) / (sum( s t r a t e g y 1 M [’ E x i t strategy1M1C ’]>=0)+sum( s t r a t e g y 1 M [’ E x i t strategy1M1C ’]<0 ) ) )
119 p r i n t(sum( s t r a t e g y 1 M [’ E x i t strategy1M2C ’]<0 ) / (sum( s t r a t e g y 1 M [’ E x i t strategy1M2C ’]>=0)+sum( s t r a t e g y 1 M [’ E x i t strategy1M2C ’]<0 ) ) )
120 p r i n t(sum( s t r a t e g y 1 M [’ E x i t strategy1M3C ’]<0 ) / (sum( s t r a t e g y 1 M [’ E x i t strategy1M3C ’]>=0)+sum( s t r a t e g y 1 M [’ E x i t strategy1M3C ’]<0 ) ) )
121 p r i n t(sum( s t r a t e g y 1 M [’ E x i t strategy1M4C ’]<0 ) / (sum( s t r a t e g y 1 M [’ E x i t strategy1M4C ’]>=0)+sum( s t r a t e g y 1 M [’ E x i t strategy1M4C ’]<0 ) ) )
122 p r i n t(sum( s t r a t e g y 1 M [’ E x i t strategy1M5C ’]<0 ) / (sum( s t r a t e g y 1 M [’ E x i t strategy1M5C ’]>=0)+sum( s t r a t e g y 1 M [’ E x i t strategy1M5C ’]<0 ) ) )
123 124
125 # I n [ 7 ] :
126 127
128 ##############Sub sample t e s t###############
129 i n d i c e s = l i s t(r a n g e( 8 5 , 2 5 2 ) ) #p e r i o d 1999−2006
130 d r o p p e d d a t a 1 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
131 #P e r f o r m a n c e m e a s u r e s
132 mean = d r o p p e d d a t a 1 . mean ( )∗12
133 s r = 12∗d r o p p e d d a t a 1 . mean ( ) / ( d r o p p e d d a t a 1 . s t d ( )∗np . s q r t ( 1 2 ) )
134 135
136 # I n [ 9 ] :
137 138
139 #P−v a l u e
140 p r i n t(’ 1M1C’)
141 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’] ) ) ) ) )∗2 )
142 p r i n t(’ 1M2C’)
143 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’] ) ) ) ) )∗2 )
144 p r i n t(’ 1M3C’)
145 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’] ) ) ) ) )∗2 )
146 p r i n t(’ 1M4C’)
147 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’] ) ) ) ) )∗2 )
148 p r i n t(’ 1M5C’)
149 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’] ) ) ) ) )∗2 )
150 151
152 # I n [ 1 9 ] :
153 154
155 #F r a c t i o n o f n e g a t i v e#
156 p r i n t(sum( d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’]<0 ) / (sum( d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 1 [’ E x i t strategy1M1C ’ ]<0 ) ) )
157 p r i n t(sum( d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’]<0 ) / (sum( d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 1 [’ E x i t strategy1M2C ’ ]<0 ) ) )
158 p r i n t(sum( d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’]<0 ) / (sum( d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 1 [’ E x i t strategy1M3C ’ ]<0 ) ) )
159 p r i n t(sum( d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’]<0 ) / (sum( d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 1 [’ E x i t strategy1M4C ’ ]<0 ) ) )
160 p r i n t(sum( d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’]<0 ) / (sum( d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 1 [’ E x i t strategy1M5C ’ ]<0 ) ) )
161 162
163 # I n [ 2 0 ] :
164 165
166 i n d i c e s = l i s t(r a n g e( 0 , 1 6 8 ) ) #p e r i o d 2013−2020
167 d r o p p e d d a t a 2 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
168 #P e r f o r m a n c e m e a s u r e s
169 mean = d r o p p e d d a t a 2 . mean ( )∗12
170 s r = 12∗d r o p p e d d a t a 2 . mean ( ) / ( d r o p p e d d a t a 2 . s t d ( )∗np . s q r t ( 1 2 ) )
171 172
173 # I n [ 2 3 ] :
174 175
176 #P−v a l u e
177 p r i n t(’ 1M1C’)
178 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’] ) ) ) ) )∗2 )
179 p r i n t(’ 1M2C’)
180 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’] ) ) ) ) )∗2 )
181 p r i n t(’ 1M3C’)
182 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’] ) ) ) ) )∗2 )
183 p r i n t(’ 1M4C’)
184 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’] ) ) ) ) )∗2 )
185 p r i n t(’ 1M5C’)
186 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’] ) ) ) ) )∗2 )
187 188
189 # I n [ 2 4 ] :
190 191
192 #F r a c t i o n o f n e g a t i v e#
193 p r i n t(sum( d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’]<0 ) / (sum( d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 2 [’ E x i t strategy1M1C ’ ]<0 ) ) )
194 p r i n t(sum( d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’]<0 ) / (sum( d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 2 [’ E x i t strategy1M2C ’ ]<0 ) ) )
195 p r i n t(sum( d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’]<0 ) / (sum( d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 2 [’ E x i t strategy1M3C ’ ]<0 ) ) )
196 p r i n t(sum( d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’]<0 ) / (sum( d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 2 [’ E x i t strategy1M4C ’ ]<0 ) ) )
197 p r i n t(sum( d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’]<0 ) / (sum( d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 2 [’ E x i t strategy1M5C ’ ]<0 ) ) )
198
199
200 # I n [ 2 5 ] :
201 202
203 i n d i c e s = l i s t(r a n g e( 0 , 8 4 ) ) #p e r i o d 2006−2013
204 d r o p p e d d a t a 3 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
205 i n d i c e s 1=l i s t(r a n g e( 1 6 9 , 2 5 2 ) )
206 d r o p p e d d a t a 3 = d r o p p e d d a t a 3 . drop ( i n d e x=i n d i c e s 1 )
207 #P e r f o r m a n c e m e a s u r e s
208 mean = d r o p p e d d a t a 3 . mean ( )∗12
209 s r = 12∗d r o p p e d d a t a 3 . mean ( ) / ( d r o p p e d d a t a 3 . s t d ( )∗np . s q r t ( 1 2 ) )
210 211
212 # I n [ 3 0 ] :
213 214
215 #P−v a l u e
216 p r i n t(’ 1M1C’)
217 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’] ) ) ) ) )∗2 )
218 p r i n t(’ 1M2C’)
219 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’] ) ) ) ) )∗2 )
220 p r i n t(’ 1M3C’)
221 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’] ) ) ) ) )∗2 )
222 p r i n t(’ 1M4C’)
223 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’] ) ) ) ) )∗2 )
224 p r i n t(’ 1M5C’)
225 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’] . s t d ( ) ) /np .
s q r t (l e n( d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’] ) ) ) ) )∗2 )
226 227
228 # I n [ 3 1 ] :
229 230
231 #F r a c t i o n o f n e g a t i v e#
232 p r i n t(sum( d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’]<0 ) / (sum( d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 3 [’ E x i t strategy1M1C ’ ]<0 ) ) )
233 p r i n t(sum( d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’]<0 ) / (sum( d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 3 [’ E x i t strategy1M2C ’ ]<0 ) ) )
234 p r i n t(sum( d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’]<0 ) / (sum( d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 3 [’ E x i t strategy1M3C ’ ]<0 ) ) )
235 p r i n t(sum( d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’]<0 ) / (sum( d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 3 [’ E x i t strategy1M4C ’ ]<0 ) ) )
236 p r i n t(sum( d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’]<0 ) / (sum( d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 3 [’ E x i t strategy1M5C ’ ]<0 ) ) )
237 238
239 # I n [ 1 0 ] :
240 241
242 ########V o l a t i l i t y i n d i c a t o r R e v e r s e s t r a t e g y#########
243 v o l i n d e x = pd . r e a d e x c e l (” Vol i n d i c e s . x l s x ”)
244 245
246 s t d f r o m m e a n = 1 . 5
247 window = round( 2 5 3∗1 . 5 )
248
249 d e f g e t t h r e s h o l d ( v o l i n d e x , window , s t d f r o m m e a n ) :
250 r o l l i n g m e a n = v o l i n d e x [’ VIX I n d e x ’ ] . r o l l i n g ( window = window ) . mean ( )
251 r o l l i n g s t d = v o l i n d e x [’ VIX I n d e x ’ ] . r o l l i n g ( window = window ) . s t d ( )
252 t h r e s h o l d = ( s t d f r o m m e a n ∗ r o l l i n g s t d ) + r o l l i n g m e a n
253
254 r e t u r n t h r e s h o l d
255
256 v o l i n d e x [” T h r e s h o l d ”] = g e t t h r e s h o l d ( v o l i n d e x , window , s t d f r o m m e a n )
257 258
259 d e f g e t p o s i t i o n ( v o l i n d e x ) :
260 i n d e x a b o v e s t d = v o l i n d e x [’ VIX I n d e x ’] > v o l i n d e x [’ T h r e s h o l d ’]
261 i n d e x b e l o v s t d = v o l i n d e x [’ VIX I n d e x ’] < v o l i n d e x [’ T h r e s h o l d ’]
262 v o l i n d e x [’ pos ’] = ”NA”
263 v o l i n d e x [’ pos ’] = v o l i n d e x [’ pos ’ ] . where ( ˜ i n d e x a b o v e s t d , o t h e r = −1)
264 v o l i n d e x [’ pos ’] = v o l i n d e x [’ pos ’ ] . where ( ˜ i n d e x b e l o v s t d ,
o t h e r = 1 )
265
266 r e t u r n v o l i n d e x [’ pos ’]
267
268 v o l i n d e x [’ pos ’] = g e t p o s i t i o n ( v o l i n d e x )
269 270
271 v o l i n d e x [’ D a t e s i g n a l ’] = v o l i n d e x [’ Date ’]
272 v o l i n d e x [’ Date ’] = v o l i n d e x [’ Date ’ ] . s h i f t (−1)
273
274 s t r a t e g y 1 M = carry1M
275 s t r a t e g y 1 M = v o l i n d e x . merge ( s t r a t e g y 1 M , on=’ Date ’, how=’ r i g h t ’)
276 277
278 # I n [ 1 1 ] :
279 280
281 ###R e v e r s e s t r a t e g y####
282 s t r a t e g y 1 = [ ]
283 s t r a t e g y 2 = [ ]
284 s t r a t e g y 3 = [ ]
285 s t r a t e g y 4 = [ ]
286 s t r a t e g y 5 = [ ]
287 p o s i t i o n s = s t r a t e g y 1 M [’ pos ’ ] . v a l u e s
288 p o s i t i o n s d a t e = s t r a t e g y 1 M [’ Date ’ ] . v a l u e s
289 f o r i i n r a n g e( 0 ,l e n( p o s i t i o n s ) ) :
290 i f p o s i t i o n s [ i ]==1:
291 s t r a t e g y 1 . append ( 1∗s t r a t e g y 1 M [’ carry1M1C ’] . v a l u e s [ i ] )
292 s t r a t e g y 2 . append ( 1∗s t r a t e g y 1 M [’ carry1M2C ’] . v a l u e s [ i ] )
293 s t r a t e g y 3 . append ( 1∗s t r a t e g y 1 M [’ carry1M3C ’] . v a l u e s [ i ] )
294 s t r a t e g y 4 . append ( 1∗s t r a t e g y 1 M [’ carry1M4C ’] . v a l u e s [ i ] )
295 s t r a t e g y 5 . append ( 1∗s t r a t e g y 1 M [’ carry1M5C ’] . v a l u e s [ i ] )
296 e l s e:
297 s t r a t e g y 1 . append (−1∗s t r a t e g y 1 M [’ carry1M1C ’] . v a l u e s [ i ] )
298 s t r a t e g y 2 . append (−1∗s t r a t e g y 1 M [’ carry1M2C ’] . v a l u e s [ i ] )
299 s t r a t e g y 3 . append (−1∗s t r a t e g y 1 M [’ carry1M3C ’] . v a l u e s [ i ] )
300 s t r a t e g y 4 . append (−1∗s t r a t e g y 1 M [’ carry1M4C ’] . v a l u e s [ i ] )
301 s t r a t e g y 5 . append (−1∗s t r a t e g y 1 M [’ carry1M5C ’] . v a l u e s [ i ] )
302
303 s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’] = s t r a t e g y 1
304 s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’] = s t r a t e g y 2
305 s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’] = s t r a t e g y 3
306 s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’] = s t r a t e g y 4
307 s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’] = s t r a t e g y 5
308 309
310 # I n [ 1 3 ] :
311 312
313 #P e r f o r m a n c e m e a s u r e s : F u l l sample p e r i o d 1999−2020
314 p r i n t( s t r a t e g y 1 M . mean ( )∗1 2 )
315 p r i n t( ( 1 2∗s t r a t e g y 1 M . mean ( ) ) / ( s t r a t e g y 1 M . s t d ( )∗np . s q r t ( 1 2 ) ) )
316 317
318 # I n [ 1 6 ] :
319 320
321 #Nominal p−v a l u e
322 p r i n t(’ 1M1C’)
323 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’ ] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’ ] . s t d ( ) ) /np .
s q r t (l e n( s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’ ] ) ) ) ) )∗2 )
324 p r i n t(’ 1M2C’)
325 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’ ] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’ ] . s t d ( ) ) /np .
s q r t (l e n( s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’ ] ) ) ) ) )∗2 )
326 p r i n t(’ 1M3C’)
327 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’ ] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’ ] . s t d ( ) ) /np .
s q r t (l e n( s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’ ] ) ) ) ) )∗2 )
328 p r i n t(’ 1M4C’)
329 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’ ] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’ ] . s t d ( ) ) /np .
s q r t (l e n( s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’ ] ) ) ) ) )∗2 )
330 p r i n t(’ 1M5C’)
331 p r i n t( s c i p y . s t a t s . norm . s f (abs( s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’ ] . mean ( )∗1 2 / ( ( 1 2∗s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’ ] . s t d ( ) ) /np .
s q r t (l e n( s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’ ] ) ) ) ) )∗2 )
332 333
334 # I n [ 1 7 ] :
335 336
337 #F r a c t i o n o f n e g a t i v e
338 p r i n t(sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’]<0 ) / (sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M1C ’]>=0)+sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M1C
’]<0 ) ) )
339 p r i n t(sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’]<0 ) / (sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M2C ’]>=0)+sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M2C
’]<0 ) ) )
340 p r i n t(sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’]<0 ) / (sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M3C ’]>=0)+sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M3C
’]<0 ) ) )
341 p r i n t(sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’]<0 ) / (sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M4C ’]>=0)+sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M4C
’]<0 ) ) )
342 p r i n t(sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’]<0 ) / (sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M5C ’]>=0)+sum( s t r a t e g y 1 M [’ R e v e r s e strategy1M5C
’]<0 ) ) )
343 344
345 # I n [ 1 8 ] :
346 347
348 ########S p l i t i n t o 3 sample p e r i o d s##########
349 i n d i c e s = l i s t(r a n g e( 8 5 , 2 5 2 ) ) #p e r i o d 1999−2006
350 d r o p p e d d a t a 1 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
351 #P e r f o r m a n c e m e a s u r e s
352 mean = d r o p p e d d a t a 1 . mean ( )∗12
353 s r = 12∗d r o p p e d d a t a 1 . mean ( ) / ( d r o p p e d d a t a 1 . s t d ( )∗np . s q r t ( 1 2 ) )
354 355
356 # I n [ 2 1 ] :
357 358
359 #Nominal p−v a l u e
360 p r i n t(’ 1M1C’)
361 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’ ] ) ) ) ) )∗2 )
362 p r i n t(’ 1M2C’)
363 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’ ] ) ) ) ) )∗2 )
364 p r i n t(’ 1M3C’)
365 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’ ] ) ) ) ) )∗2 )
366 p r i n t(’ 1M4C’)
367 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’ ] ) ) ) ) )∗2 )
368 p r i n t(’ 1M5C’)
369 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’ ] ) ) ) ) )∗2 )
370 371
372 # I n [ 2 2 ] :
373 374
375 #F r a c t i o n o f n e g a t i v e
376 p r i n t(sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’]<0 ) / (sum(
d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M1C ’]<0 ) ) )
377 p r i n t(sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’]<0 ) / (sum(
d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M2C ’]<0 ) ) )
378 p r i n t(sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’]<0 ) / (sum(
d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M3C ’]<0 ) ) )
379 p r i n t(sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’]<0 ) / (sum(
d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M4C ’]<0 ) ) )
380 p r i n t(sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’]<0 ) / (sum(
d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 1 [’ R e v e r s e strategy1M5C ’]<0 ) ) )
381 382
383 # I n [ 2 3 ] :
384 385
386 i n d i c e s = l i s t(r a n g e( 0 , 1 6 8 ) ) #p e r i o d 2013−2020
387 d r o p p e d d a t a 2 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
388 #P e r f o r m a n c e m e a s u r e s
389 mean = d r o p p e d d a t a 2 . mean ( )∗12
390 s r = 12∗d r o p p e d d a t a 2 . mean ( ) / ( d r o p p e d d a t a 2 . s t d ( )∗np . s q r t ( 1 2 ) )
391 392
393 # I n [ 2 7 ] :
394 395
396 #Nominal p−v a l u e
397 p r i n t(’ 1M1C’)
398 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’ ] ) ) ) ) )∗2 )
399 p r i n t(’ 1M2C’)
400 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’ ] ) ) ) ) )∗2 )
401 p r i n t(’ 1M3C’)
402 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’ ] . s t d ( ) ) /
np . s q r t (l e n( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’ ] ) ) ) ) )∗2 )
403 p r i n t(’ 1M4C’)
404 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’ ] ) ) ) ) )∗2 )
405 p r i n t(’ 1M5C’)
406 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’ ] ) ) ) ) )∗2 )
407 408
409 # I n [ 2 8 ] :
410 411
412 #F r a c t i o n o f n e g a t i v e
413 p r i n t(sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’]<0 ) / (sum(
d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M1C ’]<0 ) ) )
414 p r i n t(sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’]<0 ) / (sum(
d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M2C ’]<0 ) ) )
415 p r i n t(sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’]<0 ) / (sum(
d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M3C ’]<0 ) ) )
416 p r i n t(sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’]<0 ) / (sum(
d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M4C ’]<0 ) ) )
417 p r i n t(sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’]<0 ) / (sum(
d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 2 [’ R e v e r s e strategy1M5C ’]<0 ) ) )
418 419
420 # I n [ 2 9 ] :
421 422
423 i n d i c e s = l i s t(r a n g e( 0 , 8 4 ) ) #p e r i o d 2006−2013
424 d r o p p e d d a t a 3 = s t r a t e g y 1 M . drop ( i n d e x=i n d i c e s )
425 i n d i c e s 1=l i s t(r a n g e( 1 6 9 , 2 5 2 ) )
426 d r o p p e d d a t a 3 = d r o p p e d d a t a 3 . drop ( i n d e x=i n d i c e s 1 )
427 #P e r f o r m a n c e m e a s u r e s
428 mean = d r o p p e d d a t a 3 . mean ( )∗12
429 s r = 12∗d r o p p e d d a t a 3 . mean ( ) / ( d r o p p e d d a t a 3 . s t d ( )∗np . s q r t ( 1 2 ) )
430 431
432 # I n [ 3 2 ] :
433
434
435 #Nominal p−v a l u e
436 p r i n t(’ 1M1C’)
437 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’ ] ) ) ) ) )∗2 )
438 p r i n t(’ 1M2C’)
439 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’ ] ) ) ) ) )∗2 )
440 p r i n t(’ 1M3C’)
441 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’ ] ) ) ) ) )∗2 )
442 p r i n t(’ 1M4C’)
443 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’ ] ) ) ) ) )∗2 )
444 p r i n t(’ 1M5C’)
445 p r i n t( s c i p y . s t a t s . norm . s f (abs( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’ ] . mean ( )∗1 2 / ( ( 1 2∗d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’ ] . s t d ( ) ) / np . s q r t (l e n( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’ ] ) ) ) ) )∗2 )
446 447
448 # I n [ 3 3 ] :
449 450
451 #F r a c t i o n o f n e g a t i v e
452 p r i n t(sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’]<0 ) / (sum(
d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’]>=0)+sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M1C ’]<0 ) ) )
453 p r i n t(sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’]<0 ) / (sum(
d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’]>=0)+sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M2C ’]<0 ) ) )
454 p r i n t(sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’]<0 ) / (sum(
d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’]>=0)+sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M3C ’]<0 ) ) )
455 p r i n t(sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’]<0 ) / (sum(
d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’]>=0)+sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M4C ’]<0 ) ) )
456 p r i n t(sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’]<0 ) / (sum(
d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’]>=0)+sum( d r o p p e d d a t a 3 [’ R e v e r s e strategy1M5C ’]<0 ) ) )
D.3 Bootstrap resample and confidence inter-val of static carry trade
1 #! / u s r / b i n / env python
2 # c o d i n g : u t f−8
3
4 # I n [ 1 ] :
5 6
7 ############B o o t s t r a p p i n g#####################
8 i m p o r t pandas a s pd # pandas f o r w o r k i n g w i t h d a t a
9 i m p o r t m a t p l o t l i b . p y p l o t a s p l t # M a t p l o t l i b f o r p l o t t i n g
10 i m p o r t numpy a s np #Numpy i s a l i b r a r y f o r w o r k i n g w i t h n u m e r i c a l d a t a
11 i m p o r t o s
12 i m p o r t s y s
13 i m p o r t s e a b o r n a s s n s
14 i m p o r t s c i p y . s t a t s
15
16 #Import Kevin Sheppard a r c h p a c k a g e : B o o t s t r a p p i n g
17 i m p o r t a r c h
18 from a r c h . b o o t s t r a p i m p o r t S t a t i o n a r y B o o t s t r a p #s t a t i o n a r y b o o t s t r a p P o l i t i s and Romona
19 from a r c h . b o o t s t r a p i m p o r t o p t i m a l b l o c k l e n g t h #C a l c u l a t e o p t i o m a l b l o c k l e n g h
20 o s . c h d i r ( o s . getcwd ( ) )
21
22 ###R e t u r n s o v e r t h e whole sample p e r i o d 1999−2020###
23 carry1M = pd . r e a d e x c e l (” carry1M MID . x l s x ”)
24 carry3M = pd . r e a d e x c e l (” carry3M MID . x l s x ”)
25 carry3M = carry3M [ :−1 ]
26 carry6M = pd . r e a d e x c e l (” carry6M MID . x l s x ”)
27 carry6M = carry6M [ :−1 ]
28 carry1Y = pd . r e a d e x c e l (” carry1Y MID . x l s x ”)
29 carry1Y = carry1Y [ :−1 ]
30 31
32 # I n [ 2 ] :
33 34
35 ################1 month r e b a l a n c e################
36 37
38 # I n [ 3 ] :
39
41 d e f s h a r p e r a t i o ( x ) :
42 mu , sigma = 12∗x . mean ( ) , np . s q r t ( 1 2∗x . v a r ( ) )
43 v a l u e s = np . a r r a y ( [ mu , sigma , mu / sigma ] ) . s q u e e z e ( )
44 i n d e x = [’ mu ’, ’ sigma ’, ’ SR ’]
45 r e t u r n pd . S e r i e s ( v a l u e s , i n d e x=i n d e x )
46 47
48 # I n [ 4 ] :
49 50
51 #8 7 7 7 8 8 3 8 8
52 e n t r o p y = [ 8 7 7 7 8 8 3 8 8 ]
53 a l l p a r a m s = {}
54 a l l m u=pd . DataFrame ( )
55 a l l s i g m a=pd . DataFrame ( )
56 a l l c i = np . z e r o s ( ( 2 , 3 ) )
57 f o r i , c o l i n enumerate( carry1M . columns [ 2 : 7 ] ) :
58 c a r r y 1 = carry1M [ c o l ] . to numpy ( )
59 o p t = o p t i m a l b l o c k l e n g t h ( c a r r y 1∗∗2 )
60 b l o c k l e n = o p t . l o c [ 0 ,’ s t a t i o n a r y ’]
61
62 r s = np . random . RandomState ( e n t r o p y )
63 bs = S t a t i o n a r y B o o t s t r a p ( b l o c k l e n , c a r r y 1 , r a n d o m s t a t e=r s )
64 r e s u l t s = bs .a p p l y( s h a r p e r a t i o , 1 0 0 0 )
65 a l l m u [ c o l ]= r e s u l t s [ : , 0 ]
66 a l l s i g m a [ c o l ]= r e s u l t s [ : , 1 ]
67 68
69 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , s h a r p e r a t i o ( c a r r y 1 ) . to numpy ( ) . r e s h a p e ( 1 , 3 ) ) )
70 c i = bs . c o n f i n t ( s h a r p e r a t i o , r e p s =10000, method=’ b a s i c ’)
71 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , c i ) )
72 73
74 a l l c i = a l l c i [ 2 : , : ]
75 a l l c i = pd . DataFrame ( a l l c i , i n d e x = [ [ v a l f o r s u b l i s t i n [ [’{}
params ’.f o r m a t( c o l ) ,’{} Lower ’.f o r m a t( c o l ) , ’{} Upper ’.f o r m a t( c o l ) ] f o r c o l i n carry1M . columns [ 2 : 7 ] ] f o r v a l i n s u b l i s t ] ] , columns =[’ mu ’, ’ sigma ’, ’ SR ’] )
76
77 p r i n t( a l l c i )
78 79
80 # I n [ 7 ] :
81 82
83 p l t . r c (’ t e x t ’, u s e t e x=F a l s e )
84 p l t . r c (’ f o n t ’, f a m i l y=’ s e r i f ’)
85 p l t . rcParams [’ a x e s . l i n e w i d t h ’] =0.9
86 p l t . rcParams [’ x t i c k . l a b e l s i z e ’]=27
87 p l t . rcParams [’ y t i c k . l a b e l s i z e ’]=27
88
89 f i g , a x e s = p l t . s u b p l o t s ( 1 , 5 , f i g s i z e = ( 4 5 , 1 2 ) , s h a r e y=True )
90 x1 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
91 s 1= s c i p y . s t a t s . norm . p d f ( x1 , l o c =( a l l c i . l o c [’ carry1M1C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1M1C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
92 s n s . k d e p l o t ( ( a l l m u [’ carry1M1C ’] / 1 2 ) / ( ( a l l s i g m a [’ carry1M1C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 0 ] , l e g e n d=
F a l s e )
93 a x e s [ 0 ] . f i l l b e t w e e n ( x1 , s 1 , c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
94 a x e s [ 0 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
95 a x e s [ 0 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
96
97 x2 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
98 s 2= s c i p y . s t a t s . norm . p d f ( x2 , l o c =( a l l c i . l o c [’ carry1M2C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1M2C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
99 s n s . k d e p l o t ( ( a l l m u [’ carry1M2C ’] / 1 2 ) / ( ( a l l s i g m a [’ carry1M2C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , ax=a x e s [ 1 ] , c o l o r=”#1f 3 b 4 d ”, l e g e n d=
F a l s e )
100 a x e s [ 1 ] . f i l l b e t w e e n ( x2 , s 2 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
101 a x e s [ 1 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
102 a x e s [ 1 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
103 104
105 x3 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
106 s 3= s c i p y . s t a t s . norm . p d f ( x3 , l o c =( a l l c i . l o c [’ carry1M3C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1M3C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
107 s n s . k d e p l o t ( ( a l l m u [’ carry1M3C ’] / 1 2 ) / ( ( a l l s i g m a [’ carry1M3C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 2 ] , l e g e n d=
F a l s e )
108 a x e s [ 2 ] . f i l l b e t w e e n ( x3 , s 3 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
109 a x e s [ 2 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
110 a x e s [ 2 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
111 112
113 x4 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
114 s 4= s c i p y . s t a t s . norm . p d f ( x4 , l o c =( a l l c i . l o c [’ carry1M4C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1M4C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
115 s n s . k d e p l o t ( ( a l l m u [’ carry1M4C ’] / 1 2 ) / ( ( a l l s i g m a [’ carry1M4C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 3 ] , l e g e n d=
F a l s e )
116 a x e s [ 3 ] . f i l l b e t w e e n ( x4 , s 4 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
117 a x e s [ 3 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
118 a x e s [ 3 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
119
120 x5 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
121 s 5= s c i p y . s t a t s . norm . p d f ( x5 , l o c =( a l l c i . l o c [’ carry1M5C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1M5C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
122 s n s . k d e p l o t ( ( a l l m u [’ carry1M5C ’] / 1 2 ) / ( ( a l l s i g m a [’ carry1M5C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 4 ] , l e g e n d=
F a l s e )
123 a x e s [ 4 ] . f i l l b e t w e e n ( x5 , s 5 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
124 a x e s [ 4 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
125 a x e s [ 4 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
126
127 a x e s [ 0 ] . s e t x l a b e l (’ Carry1M1C z−s c o r e ’, f o n t s i z e = 2 7 )
128 a x e s [ 1 ] . s e t x l a b e l (’ Carry1M2C z−s c o r e ’, f o n t s i z e = 2 7 )
129 a x e s [ 2 ] . s e t x l a b e l (’ Carry1M3C z−s c o r e ’, f o n t s i z e = 2 7 )
130 a x e s [ 3 ] . s e t x l a b e l (’ Carry1M4C z−s c o r e ’, f o n t s i z e = 2 7 )
131 a x e s [ 4 ] . s e t x l a b e l (’ Carry1M5C z−s c o r e ’, f o n t s i z e = 2 7 )
132
133 a x e s [ 0 ] . s e t y l a b e l (’ P r o b a b i l i t y D e n s i t y ’, f o n t s i z e = 2 7 )
134
135 p l t . s a v e f i g (’ pdf 1M . png ’)
136 137
138 # I n [ 6 ] :
139 140
141 ###############3 months r e b a l a n c e###############
142 e n t r o p y = [ 8 7 7 7 8 8 3 8 8 ]
143 a l l p a r a m s = {}
144 all mu3M=pd . DataFrame ( )
145 a l l s i g m a 3 M=pd . DataFrame ( )
146 a l l c i = np . z e r o s ( ( 2 , 3 ) )
147 f o r i , c o l i n enumerate( carry3M . columns [ 2 : 7 ] ) :
148 c a r r y 1 = carry3M [ c o l ] . to numpy ( )
149 o p t = o p t i m a l b l o c k l e n g t h ( c a r r y 1∗∗2 )
150 b l o c k l e n = o p t . l o c [ 0 ,’ s t a t i o n a r y ’]
151
152 r s = np . random . RandomState ( e n t r o p y )
153 bs = S t a t i o n a r y B o o t s t r a p ( b l o c k l e n , c a r r y 1 , r a n d o m s t a t e=r s )
154 r e s u l t s = bs .a p p l y( s h a r p e r a t i o , 1 0 0 0 )
155 all mu3M [ c o l ]= r e s u l t s [ : , 0 ]
156 a l l s i g m a 3 M [ c o l ]= r e s u l t s [ : , 1 ]
157
158 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , s h a r p e r a t i o ( c a r r y 1 ) . to numpy ( ) . r e s h a p e ( 1 , 3 ) ) )
159 c i = bs . c o n f i n t ( s h a r p e r a t i o , r e p s =10000, method=’ b a s i c ’)
160 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , c i ) )
161
162 a l l c i = a l l c i [ 2 : , : ]
163 a l l c i = pd . DataFrame ( a l l c i , i n d e x = [ [ v a l f o r s u b l i s t i n [ [’{}
params ’.f o r m a t( c o l ) ,’{} Lower ’.f o r m a t( c o l ) , ’{} Upper ’.f o r m a t( c o l ) ] f o r c o l i n carry3M . columns [ 2 : 7 ] ] f o r v a l i n s u b l i s t ] ] , columns =[’ mu ’, ’ sigma ’, ’ SR ’] )
164
165 p r i n t( a l l c i )
166 167
168 # I n [ 1 0 ] :
169 170
171 p l t . r c (’ t e x t ’, u s e t e x=F a l s e )
172 p l t . r c (’ f o n t ’, f a m i l y=’ s e r i f ’)
173 p l t . rcParams [’ a x e s . l i n e w i d t h ’] =0.9
174 p l t . rcParams [’ x t i c k . l a b e l s i z e ’]=27
175 p l t . rcParams [’ y t i c k . l a b e l s i z e ’]=27
176
177 f i g , a x e s = p l t . s u b p l o t s ( 1 , 5 , f i g s i z e = ( 4 5 , 1 2 ) , s h a r e y=True )
178 x1 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
179 s 1= s c i p y . s t a t s . norm . p d f ( x1 , l o c =( a l l c i . l o c [’ carry3M1C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry3M1C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
180 s n s . k d e p l o t ( ( all mu3M [’ carry3M1C ’] / 1 2 ) / ( ( a l l s i g m a 3 M [’ carry3M1C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 0 ] , l e g e n d=
F a l s e )
181 a x e s [ 0 ] . f i l l b e t w e e n ( x1 , s 1 , c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
182 a x e s [ 0 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
183 a x e s [ 0 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
184
185 x2 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
186 s 2= s c i p y . s t a t s . norm . p d f ( x2 , l o c =( a l l c i . l o c [’ carry3M2C params ’,’
mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry3M2C params ’,’ sigma ’ ] . v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
187 s n s . k d e p l o t ( ( all mu3M [’ carry3M2C ’] / 1 2 ) / ( ( a l l s i g m a 3 M [’ carry3M2C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , ax=a x e s [ 1 ] , c o l o r=”#1f 3 b 4 d ”, l e g e n d=
F a l s e )
188 a x e s [ 1 ] . f i l l b e t w e e n ( x2 , s 2 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
189 a x e s [ 1 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
190 a x e s [ 1 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
191 192
193 x3 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
194 s 3= s c i p y . s t a t s . norm . p d f ( x3 , l o c =( a l l c i . l o c [’ carry3M3C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry3M3C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
195 s n s . k d e p l o t ( ( all mu3M [’ carry3M3C ’] / 1 2 ) / ( ( a l l s i g m a 3 M [’ carry3M3C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 2 ] , l e g e n d=
F a l s e )
196 a x e s [ 2 ] . f i l l b e t w e e n ( x3 , s 3 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
197 a x e s [ 2 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
198 a x e s [ 2 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
199 200
201 x4 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
202 s 4= s c i p y . s t a t s . norm . p d f ( x4 , l o c =( a l l c i . l o c [’ carry3M4C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry3M4C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
203 s n s . k d e p l o t ( ( all mu3M [’ carry3M4C ’] / 1 2 ) / ( ( a l l s i g m a 3 M [’ carry3M4C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 3 ] , l e g e n d=
F a l s e )
204 a x e s [ 3 ] . f i l l b e t w e e n ( x4 , s 4 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
205 a x e s [ 3 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
206 a x e s [ 3 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
207
208 x5 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
209 s 5= s c i p y . s t a t s . norm . p d f ( x5 , l o c =( a l l c i . l o c [’ carry3M5C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry3M5C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
210 s n s . k d e p l o t ( ( all mu3M [’ carry3M5C ’] / 1 2 ) / ( ( a l l s i g m a 3 M [’ carry3M5C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 4 ] , l e g e n d=
F a l s e )
211 a x e s [ 4 ] . f i l l b e t w e e n ( x5 , s 5 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
212 a x e s [ 4 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
213 a x e s [ 4 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
214
215 a x e s [ 0 ] . s e t x l a b e l (’ Carry3M1C z−s c o r e ’, f o n t s i z e = 2 7 )
216 a x e s [ 1 ] . s e t x l a b e l (’ Carry3M2C z−s c o r e ’, f o n t s i z e = 2 7 )
217 a x e s [ 2 ] . s e t x l a b e l (’ Carry3M3C z−s c o r e ’, f o n t s i z e = 2 7 )
218 a x e s [ 3 ] . s e t x l a b e l (’ Carry3M4C z−s c o r e ’, f o n t s i z e = 2 7 )
219 a x e s [ 4 ] . s e t x l a b e l (’ Carry3M5C z−s c o r e ’, f o n t s i z e = 2 7 )
220
221 a x e s [ 0 ] . s e t y l a b e l (’ P r o b a b i l i t y D e n s i t y ’, f o n t s i z e = 2 7 )
222
223 p l t . s a v e f i g (’ pdf 3M . png ’)
224 225
226 # I n [ 1 1 ] :
227 228
229 ##################6 months r e b a l a n c e###############
230 e n t r o p y = [ 8 7 7 7 8 8 3 8 8 ]
231 a l l p a r a m s = {}
232 all mu6M=pd . DataFrame ( )
233 a l l s i g m a 6 M=pd . DataFrame ( )
234 a l l m u=pd . DataFrame ( )
235 a l l c i = np . z e r o s ( ( 2 , 3 ) )
236 f o r i , c o l i n enumerate( carry6M . columns [ 2 : 7 ] ) :
237 c a r r y 1 = carry6M [ c o l ] . to numpy ( )
238 o p t = o p t i m a l b l o c k l e n g t h ( c a r r y 1∗∗2 )
239 b l o c k l e n = o p t . l o c [ 0 ,’ s t a t i o n a r y ’]
240
241 r s = np . random . RandomState ( e n t r o p y )
242 bs = S t a t i o n a r y B o o t s t r a p ( b l o c k l e n , c a r r y 1 , r a n d o m s t a t e=r s )
243 r e s u l t s = bs .a p p l y( s h a r p e r a t i o , 1 0 0 0 )
244 SR = pd . DataFrame ( r e s u l t s [ : , −1 : ] , columns =[’ SR ’] )
245 all mu6M [ c o l ]= r e s u l t s [ : , 0 ]
246 a l l s i g m a 6 M [ c o l ]= r e s u l t s [ : , 1 ]
247
248 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , s h a r p e r a t i o ( c a r r y 1 ) . to numpy ( ) . r e s h a p e ( 1 , 3 ) ) )
249 c i = bs . c o n f i n t ( s h a r p e r a t i o , r e p s =10000, method=’ b a s i c ’)
250 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , c i ) )
251
252 a l l c i = a l l c i [ 2 : , : ]
253 a l l c i = pd . DataFrame ( a l l c i , i n d e x = [ [ v a l f o r s u b l i s t i n [ [’{}
params ’.f o r m a t( c o l ) ,’{} Lower ’.f o r m a t( c o l ) , ’{} Upper ’.f o r m a t( c o l ) ] f o r c o l i n carry6M . columns [ 2 : 7 ] ] f o r v a l i n s u b l i s t ] ] , columns =[’ mu ’, ’ sigma ’, ’ SR ’] )
254
255 p r i n t( a l l c i )
256 257
258 # I n [ 1 2 ] :
259 260
261 p l t . r c (’ t e x t ’, u s e t e x=F a l s e )
262 p l t . r c (’ f o n t ’, f a m i l y=’ s e r i f ’)
263 p l t . rcParams [’ a x e s . l i n e w i d t h ’] =0.9
264 p l t . rcParams [’ x t i c k . l a b e l s i z e ’]=27
265 p l t . rcParams [’ y t i c k . l a b e l s i z e ’]=27
266
267 f i g , a x e s = p l t . s u b p l o t s ( 1 , 5 , f i g s i z e = ( 4 5 , 1 2 ) , s h a r e y=True )
268 x1 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
269 s 1= s c i p y . s t a t s . norm . p d f ( x1 , l o c =( a l l c i . l o c [’ carry6M1C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry6M1C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
270 s n s . k d e p l o t ( ( all mu6M [’ carry6M1C ’] / 1 2 ) / ( ( a l l s i g m a 6 M [’ carry6M1C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 0 ] , l e g e n d=
F a l s e )
271 a x e s [ 0 ] . f i l l b e t w e e n ( x1 , s 1 , c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
272 a x e s [ 0 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
273 a x e s [ 0 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
274
275 x2 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
276 s 2= s c i p y . s t a t s . norm . p d f ( x2 , l o c =( a l l c i . l o c [’ carry6M2C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry6M2C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
277 s n s . k d e p l o t ( ( all mu6M [’ carry6M2C ’] / 1 2 ) / ( ( a l l s i g m a 6 M [’ carry6M2C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , ax=a x e s [ 1 ] , c o l o r=”#1f 3 b 4 d ”, l e g e n d=
F a l s e )
278 a x e s [ 1 ] . f i l l b e t w e e n ( x2 , s 2 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
279 a x e s [ 1 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
280 a x e s [ 1 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
281 282
283 x3 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
284 s 3= s c i p y . s t a t s . norm . p d f ( x3 , l o c =( a l l c i . l o c [’ carry6M3C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry6M3C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
285 s n s . k d e p l o t ( ( all mu6M [’ carry6M3C ’] / 1 2 ) / ( ( a l l s i g m a 6 M [’ carry6M3C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 2 ] , l e g e n d=
F a l s e )
286 a x e s [ 2 ] . f i l l b e t w e e n ( x3 , s 3 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non
b o o t s t r a p z−s c o r e ”)
287 a x e s [ 2 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
288 a x e s [ 2 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
289 290
291 x4 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
292 s 4= s c i p y . s t a t s . norm . p d f ( x4 , l o c =( a l l c i . l o c [’ carry6M4C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry6M4C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
293 s n s . k d e p l o t ( ( all mu6M [’ carry6M4C ’] / 1 2 ) / ( ( a l l s i g m a 6 M [’ carry6M4C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 3 ] , l e g e n d=
F a l s e )
294 a x e s [ 3 ] . f i l l b e t w e e n ( x4 , s 4 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
295 a x e s [ 3 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
296 a x e s [ 3 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
297
298 x5 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
299 s 5= s c i p y . s t a t s . norm . p d f ( x5 , l o c =( a l l c i . l o c [’ carry6M5C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry6M5C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
300 s n s . k d e p l o t ( ( all mu6M [’ carry6M5C ’] / 1 2 ) / ( ( a l l s i g m a 6 M [’ carry6M5C ’] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 4 ] , l e g e n d=
F a l s e )
301 a x e s [ 4 ] . f i l l b e t w e e n ( x5 , s 5 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
302 a x e s [ 4 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
303 a x e s [ 4 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
304
305 a x e s [ 0 ] . s e t x l a b e l (’ Carry6M1C z−s c o r e ’, f o n t s i z e = 2 7 )
306 a x e s [ 1 ] . s e t x l a b e l (’ Carry6M2C z−s c o r e ’, f o n t s i z e = 2 7 )
307 a x e s [ 2 ] . s e t x l a b e l (’ Carry6M3C z−s c o r e ’, f o n t s i z e = 2 7 )
308 a x e s [ 3 ] . s e t x l a b e l (’ Carry6M4C z−s c o r e ’, f o n t s i z e = 2 7 )
309 a x e s [ 4 ] . s e t x l a b e l (’ Carry6M5C z−s c o r e ’, f o n t s i z e = 2 7 )
310
311 a x e s [ 0 ] . s e t y l a b e l (’ P r o b a b i l i t y D e n s i t y ’, f o n t s i z e = 2 7 )
312
313 p l t . s a v e f i g (’ pdf 6M . png ’)
314 315
316 # I n [ 1 3 ] :
317 318
319 #######################1 y e a r r e b a l a n c e##############
320 e n t r o p y = [ 8 7 7 7 8 8 3 8 8 ]
321 a l l p a r a m s = {}
322 all mu12M=pd . DataFrame ( )
323 a l l s i g m a 1 2 M=pd . DataFrame ( )
324 a l l c i = np . z e r o s ( ( 2 , 3 ) )
325 f o r i , c o l i n enumerate( carry1Y . columns [ 2 : 7 ] ) :
326 c a r r y 1 = carry1Y [ c o l ] . to numpy ( )
327 o p t = o p t i m a l b l o c k l e n g t h ( c a r r y 1∗∗2 )
328 b l o c k l e n = o p t . l o c [ 0 ,’ s t a t i o n a r y ’]
329
330 r s = np . random . RandomState ( e n t r o p y )
331 bs = S t a t i o n a r y B o o t s t r a p ( b l o c k l e n , c a r r y 1 , r a n d o m s t a t e=r s )
332 r e s u l t s = bs .a p p l y( s h a r p e r a t i o , 1 0 0 0 )
333 SR = pd . DataFrame ( r e s u l t s [ : , −1 : ] , columns =[’ SR ’] )
334 all mu12M [ c o l ]= r e s u l t s [ : , 0 ]
335 a l l s i g m a 1 2 M [ c o l ]= r e s u l t s [ : , 1 ]
336 337
338 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , s h a r p e r a t i o ( c a r r y 1 ) . to numpy ( ) . r e s h a p e ( 1 , 3 ) ) )
339 c i = bs . c o n f i n t ( s h a r p e r a t i o , r e p s =10000, method=’ b a s i c ’)
340 a l l c i = np . c o n c a t e n a t e ( ( a l l c i , c i ) )
341
342 a l l c i = a l l c i [ 2 : , : ]
343 a l l c i = pd . DataFrame ( a l l c i , i n d e x = [ [ v a l f o r s u b l i s t i n [ [’{}
params ’.f o r m a t( c o l ) ,’{} Lower ’.f o r m a t( c o l ) , ’{} Upper ’.f o r m a t( c o l ) ] f o r c o l i n carry1Y . columns [ 2 : 7 ] ] f o r v a l i n s u b l i s t ] ] , columns =[’ mu ’, ’ sigma ’, ’ SR ’] )
344
345 p r i n t( a l l c i )
346 347
348 # I n [ 1 4 ] :
349 350
351 p l t . r c (’ t e x t ’, u s e t e x=F a l s e )
352 p l t . r c (’ f o n t ’, f a m i l y=’ s e r i f ’)
353 p l t . rcParams [’ a x e s . l i n e w i d t h ’] =0.9
354 p l t . rcParams [’ x t i c k . l a b e l s i z e ’]=27
355 p l t . rcParams [’ y t i c k . l a b e l s i z e ’]=27
356
357 f i g , a x e s = p l t . s u b p l o t s ( 1 , 5 , f i g s i z e = ( 4 5 , 1 2 ) , s h a r e y=True )
358 x1 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
359 s 1= s c i p y . s t a t s . norm . p d f ( x1 , l o c =( a l l c i . l o c [’ carry1Y1C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1Y1C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
360 s n s . k d e p l o t ( ( all mu12M [’ carry1Y1C ’] / 1 2 ) / ( ( a l l s i g m a 1 2 M [’ carry1Y1C ’ ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 0 ] , l e g e n d
=F a l s e )
361 a x e s [ 0 ] . f i l l b e t w e e n ( x1 , s 1 , c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
362 a x e s [ 0 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
363 a x e s [ 0 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
364
365 x2 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
366 s 2= s c i p y . s t a t s . norm . p d f ( x2 , l o c =( a l l c i . l o c [’ carry1Y2C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1Y2C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
367 s n s . k d e p l o t ( ( all mu12M [’ carry1Y2C ’] / 1 2 ) / ( ( a l l s i g m a 1 2 M [’ carry1Y2C ’ ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , ax=a x e s [ 1 ] , c o l o r=”#1f 3 b 4 d ”, l e g e n d
=F a l s e )
368 a x e s [ 1 ] . f i l l b e t w e e n ( x2 , s 2 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
369 a x e s [ 1 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
370 a x e s [ 1 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
371 372
373 x3 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
374 s 3= s c i p y . s t a t s . norm . p d f ( x3 , l o c =( a l l c i . l o c [’ carry1Y3C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1Y3C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
375 s n s . k d e p l o t ( ( all mu12M [’ carry1Y3C ’] / 1 2 ) / ( ( a l l s i g m a 1 2 M [’ carry1Y3C ’ ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 2 ] , l e g e n d
=F a l s e )
376 a x e s [ 2 ] . f i l l b e t w e e n ( x3 , s 3 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
377 a x e s [ 2 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
378 a x e s [ 2 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
379 380
381 x4 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
382 s 4= s c i p y . s t a t s . norm . p d f ( x4 , l o c =( a l l c i . l o c [’ carry1Y4C params ’,’ mu ’ ] . v a l u e s [ 0 ] / 1 2 ) / ( ( a l l c i . l o c [’ carry1Y4C params ’,’ sigma ’ ] .
v a l u e s [ 0 ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , s c a l e =1)
383 s n s . k d e p l o t ( ( all mu12M [’ carry1Y4C ’] / 1 2 ) / ( ( a l l s i g m a 1 2 M [’ carry1Y4C ’ ] / np . s q r t ( 1 2 ) ) /np . s q r t ( 2 5 2 ) ) , c o l o r=”#1f 3 b 4 d ”, ax=a x e s [ 3 ] , l e g e n d
=F a l s e )
384 a x e s [ 3 ] . f i l l b e t w e e n ( x4 , s 4 , f a c e c o l o r=”#d8dcd6 ”, l a b e l=”Non b o o t s t r a p z−s c o r e ”)
385 a x e s [ 3 ] . a x v l i n e ( x = 1 . 9 6 , l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
386 a x e s [ 3 ] . a x v l i n e ( x =−1.96, l i n e s t y l e =’ dashed ’, c=” f i r e b r i c k ”)
387
388 x5 = np . l i n s p a c e (−2 , 7 , 1 0 0 0 )
389 s 5= s c i p y . s t a t s . norm . p d f ( x5 , l o c =( a l l c i . l o c [’ carry1Y5C params ’,’