最近讀到股市投資 "超級績效" 系列, 裡面有一段關於勝率與獲利/虧損組合的計算, 提到虧損造成的傷害具有幾何累績性, 其實獲利也是幾何累積的, 為釐清其中的比例關係, 確定作者的觀點是如何計算出來的, 因此寫了一段程式來驗證看看.
def stockgain(win_gain, lost_gain, win_count, lost_count): #totalcount = wincount + lostcount gain = 1 for i in range(win_count): gain = gain * (1+win_gain) for i in range(lost_count): gain = gain * (1-lost_gain) return gain-1
print("win_gain lost_gain win:20% win:30% win:40% win:50% win:60% win:70%") print("-------- --------- ------- ------- ------- ------- ------- -------")
for gain in range(4, 101, 2): win_gain = gain/100 lost_gain = win_gain/2 g2 = stockgain(win_gain, lost_gain, 2, 8)*100 g3 = stockgain(win_gain, lost_gain, 3, 7)*100 g4 = stockgain(win_gain, lost_gain, 4, 6)*100 g5 = stockgain(win_gain, lost_gain, 5, 5)*100 g6 = stockgain(win_gain, lost_gain, 6, 4)*100 g7 = stockgain(win_gain, lost_gain, 7, 3)*100 print("{:6.2f}% {:6.2f}% {:7.2f}% {:7.2f}% {:7.2f}% {:7.2f}% {:7.2f}% {:7.2f}%".format(win_gain*100, lost_gain*100, g2, g3, g4, g5, g6, g7)) |
結果如下:
win_gain lost_gain win:20% win:30% win:40% win:50% win:60% win:70% -------- --------- ------- ------- ------- ------- ------- ------- 4.00% 2.00% -7.98% -2.35% 3.63% 9.98% 16.71% 23.85% 6.00% 3.00% -11.94% -3.77% 5.16% 14.92% 25.58% 37.23% 8.00% 4.00% -15.86% -5.34% 6.49% 19.80% 34.78% 51.63% 10.00% 5.00% -19.73% -7.05% 7.62% 24.62% 44.29% 67.08% 12.00% 6.00% -23.54% -8.89% 8.55% 29.34% 54.11% 83.62% 14.00% 7.00% -27.28% -10.86% 9.27% 33.95% 64.20% 101.27% 16.00% 8.00% -30.94% -12.93% 9.79% 38.43% 74.54% 120.07% 18.00% 9.00% -34.52% -15.09% 10.10% 42.76% 85.12% 140.05% 20.00% 10.00% -38.01% -17.35% 10.20% 46.93% 95.91% 161.21% 22.00% 11.00% -41.41% -19.68% 10.10% 50.92% 106.88% 183.59% 24.00% 12.00% -44.70% -22.08% 9.80% 54.71% 118.00% 207.18% 26.00% 13.00% -47.89% -24.53% 9.29% 58.29% 129.25% 232.01% 28.00% 14.00% -50.98% -27.03% 8.60% 61.64% 140.58% 258.07% 30.00% 15.00% -53.95% -29.57% 7.72% 64.74% 151.96% 285.35% 32.00% 16.00% -56.81% -32.13% 6.65% 67.60% 163.37% 313.86% 34.00% 17.00% -59.56% -34.71% 5.41% 70.18% 174.75% 343.58% 36.00% 18.00% -62.19% -37.29% 4.00% 72.49% 186.08% 374.48% 38.00% 19.00% -64.71% -39.88% 2.43% 74.51% 197.31% 406.53% 40.00% 20.00% -67.12% -42.45% 0.71% 76.23% 208.41% 439.72% 42.00% 21.00% -69.41% -45.01% -1.16% 77.65% 219.33% 473.98% 44.00% 22.00% -71.59% -47.55% -3.17% 78.77% 230.03% 509.29% 46.00% 23.00% -73.66% -50.05% -5.30% 79.56% 240.47% 545.57% 48.00% 24.00% -75.62% -52.52% -7.55% 80.04% 250.61% 582.77% 50.00% 25.00% -77.47% -54.95% -9.90% 80.20% 260.41% 620.81% 52.00% 26.00% -79.23% -57.33% -12.35% 80.04% 269.82% 659.63% 54.00% 27.00% -80.87% -59.65% -14.88% 79.56% 278.81% 699.12% 56.00% 28.00% -82.42% -61.92% -17.49% 78.77% 287.33% 739.21% 58.00% 29.00% -83.88% -64.13% -20.17% 77.65% 295.34% 779.78% 60.00% 30.00% -85.24% -66.27% -22.90% 76.23% 302.82% 820.73% 62.00% 31.00% -86.52% -68.34% -25.67% 74.51% 309.72% 861.95% 64.00% 32.00% -87.70% -70.35% -28.48% 72.49% 316.01% 903.31% 66.00% 33.00% -88.81% -72.28% -31.31% 70.18% 321.65% 944.67% 68.00% 34.00% -89.84% -74.13% -34.16% 67.60% 326.61% 985.92% 70.00% 35.00% -90.79% -75.92% -37.01% 64.74% 330.87% 1026.89% 72.00% 36.00% -91.67% -77.62% -39.86% 61.64% 334.40% 1067.45% 74.00% 37.00% -92.49% -79.25% -42.69% 58.29% 337.18% 1107.44% 76.00% 38.00% -93.24% -80.80% -45.50% 54.71% 339.18% 1146.71% 78.00% 39.00% -93.93% -82.28% -48.28% 50.92% 340.39% 1185.08% 80.00% 40.00% -94.56% -83.67% -51.02% 46.93% 340.80% 1222.40% 82.00% 41.00% -95.14% -85.00% -53.72% 42.76% 340.39% 1258.49% 84.00% 42.00% -95.66% -86.25% -56.36% 38.43% 339.16% 1293.18% 86.00% 43.00% -96.14% -87.42% -58.95% 33.95% 337.10% 1326.31% 88.00% 44.00% -96.58% -88.52% -61.47% 29.34% 334.21% 1357.70% 90.00% 45.00% -96.98% -89.56% -63.93% 24.62% 330.50% 1387.18% 92.00% 46.00% -97.33% -90.52% -66.30% 19.80% 325.97% 1414.57% 94.00% 47.00% -97.66% -91.42% -68.60% 14.92% 320.64% 1439.71% 96.00% 48.00% -97.95% -92.26% -70.82% 9.98% 314.52% 1462.44% 98.00% 49.00% -98.21% -93.03% -72.96% 5.00% 307.64% 1482.58% 100.00% 50.00% -98.44% -93.75% -75.00% 0.00% 300.00% 1500.00% |
留言列表