繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

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

作者/我是一个土豆 2019-07-28 22:00 0 来源: FX168财经网人物频道
  • 凯利公式来源于赌博,是数学意义上的最佳下注仓位,简约而不简单。

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

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、结论:¶

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

分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2144文章4145粉丝45

    暂无个人简介信息

  • 梁孟梵

    人气2152文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 指导老师

    人气1856文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2296文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 刘钥钥1

    人气2016文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2320文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4728文章8329粉丝118

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...