请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  数理科学 帖子:3351425 新帖:43

凯利公式和仓位管理(一)

我是一个土豆发表于:7 月 28 日 22:00回复(1)
  • 凯利公式来源于赌博,是数学意义上的最佳下注仓位,简约而不简单。

  • 但是,在股票市场是否有应用的机会?本系列文旨在给出凯利公式的正确理解,并试着在股市中有效运用,实现仓位的合理配置,欢迎大家交流和讨论。

1、引言¶

研究目的: 本文以扔硬币为例,在给定概率和盈亏比情况下,通过模拟验证了根据凯利公式确定的下注比率为最优。

研究内容: 基于凯利公式的最优解下,分别模拟多次,验证其收益情况。

研究结论: 凯利公式在确定赢率概率和赔率情况下,凯利公式拥有最优的收益。将在下章研究凯利公式在股市的应用。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.mlab as mlab
import datetime
from collections import defaultdict
import scipy.stats as stats
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
%matplotlib inline

2、公式¶

凯利公式 f=(b·p−q)/b ; 其中p表示胜率,q=1-p,b表示赌赢了的赔率(扣除本金后的收益/本金)。f表示单次下注占总资金的比例。

3、参数设置¶

# b为赔率
b = 1.001
# p为猜对的概率
p = 0.52
# q为猜错的概率
q = 1-p
# N为总模拟次数
N = 100
# Nround为每次模拟掷硬币的次数
Nround = 10000
# 初始资金
init_balance = 10000

4、模拟验证¶

这里模拟凯利公式最优解,以及最优解的1/4倍、1/2倍、2倍、4倍的运行情况

# 根据凯利公式计算最优下注比率
optimalRatio = (b * p - q)/b

ratio_list = [optimalRatio * 0.25, optimalRatio* 0.5, optimalRatio*2, optimalRatio*4]
#ratio_list = np.delete(ratio_list, [int(3.0-1)])

optimal_balance = np.zeros(Nround + 1)
ratio_balance = {}
for tmpratio in ratio_list:
    ratio_balance[tmpratio] = np.zeros(Nround + 1)

optimalbalance = []
for i in range(N):
#     print i
    # 生成随机数
    draws = np.random.uniform(0, 1, Nround)

    # 初始资金
    cur_balance = init_balance
    balance_list = [init_balance]

    # balance_dic保存不同ratio下的资金
    balance_dic = defaultdict(list)
    for tmpratio in ratio_list:
        balance_dic[tmpratio].append(init_balance)

    for draw in draws:
        if draw <= p:
            # 猜对
            cur_balance += cur_balance * optimalRatio * b
            balance_list.append(cur_balance)
            for tmpratio in ratio_list:
                tmpbalance = balance_dic[tmpratio][-1]
                tmpbalance += tmpbalance * tmpratio * b
                balance_dic[tmpratio].append(tmpbalance)
        else:
            # 猜错
            cur_balance -= cur_balance * optimalRatio
            balance_list.append(cur_balance)
            for tmpratio in ratio_list:
                tmpbalance = balance_dic[tmpratio][-1]
                tmpbalance -= tmpbalance * tmpratio
                balance_dic[tmpratio].append(tmpbalance)
    
    
    
    optimal_balance += np.array(balance_list)
    
    for tmpratio in ratio_list:
        ratio_balance[tmpratio] += np.array(balance_dic[tmpratio])

optimal_balance /= N
for tmpratio in ratio_list:
    ratio_balance[tmpratio] /= N
    
# 设置画图颜色
colormap = [(31, 119, 180) , (174, 199, 232) , (255, 127, 14)  , (255, 187, 120), 
            (44, 160, 44)  , (152, 223, 138) , (148, 103, 189) , (197, 176, 213), 
            (214, 39, 40)  , (255, 152, 150) , (140, 86, 75)   , (196, 156, 148),  
            (227, 119, 194), (247, 182, 210) , (127, 127, 127) , (199, 199, 199),  
            (188, 189, 34) , (219, 219, 141) , (23, 190, 207)  , (158, 218, 229)]  

for i in range(len(colormap)):  
    r, g, bb = colormap[i]  
    colormap[i] = (r / 255., g / 255., bb / 255.)  
# 画图
fig, ax1 = plt.subplots(figsize=(18, 9))
ax1.plot(balance_list, color = 'blue', label = 'Optimal : %.3f' % optimalRatio, linewidth = 0.8, alpha = 0.8)
for i in range(len(ratio_list)):
    ax1.plot(balance_dic[ratio_list[i]], color = colormap[i], label = '%.3f' % ratio_list[i], linewidth = 0.8, alpha = 0.8)
ax1.axhline(y = init_balance, color = 'red', linestyle = '--', linewidth = 0.8, alpha = 0.8)
ax1.set_ylabel('Size', fontsize = 12)
plt.legend(fontsize = 12)
plt.title('Balance under different ratio', fontsize=15)
plt.show()

5、结论:¶

凯利公式为上图的蓝色,其模拟收益远高于其它几种下注方式。 下期将在股票市场上运用凯利公式的尝试。

全部回复

0/140

量化课程

    移动端课程