gpt4 book ai didi

python - 使用 scipy 最大化目标(通过 kelly criterium)

转载 作者:行者123 更新时间:2023-12-05 06:12:28 26 4
gpt4 key购买 nike

我有以下两个 pandas 数据框:new & outcome

new = pd.DataFrame([[5,5,1.6],[0.22,0.22,0.56]]).T
new.index = ['Visitor','Draw','Home']
new.columns = ['Decimal odds', 'Win prob']
new['Bet amount'] = np.zeros((len(new),1))

输出:

         Decimal odds  Win prob  Bet amount
Visitor 5.0 0.22 0.0
Draw 5.0 0.22 0.0
Home 1.6 0.56 0.0

和数据框“结果”

outcome = pd.DataFrame([[0.22,0.22,0.56],[100,100,100]]).T
outcome.index = ['Visitor win','Draw','Home win']
outcome.columns = ['Prob.','Starting bankroll']
outcome['Wins'] = ((new['Decimal odds'] - 1) * new['Bet amount']).values
outcome['Losses'] = [sum(new['Bet amount'][[1,2]]) , sum(new['Bet amount'][[0,2]]), sum(new['Bet amount'][[0,1]])]
outcome['Ending bankroll'] = outcome['Starting bankroll'] + outcome['Wins'] - outcome['Losses']
outcome['Logarithm'] = np.log(outcome['Ending bankroll'])

输出:

             Prob.  Starting bankroll  Wins  Losses  Ending bankroll  Logarithm
Visitor win 0.22 100.0 0.0 0.0 100.0 4.60517
Draw 0.22 100.0 0.0 0.0 100.0 4.60517
Home win 0.56 100.0 0.0 0.0 100.0 4.60517

特此,目标通过以下公式计算:

objective = sum(outcome['Prob.'] * outcome['Logarithm'])

现在我想通过 `new['Bet amount'] 列中包含的值最大化 objective。约束条件是 a、b 和 c 介于 0 和 100 之间。此外,a、b 和 c 的总和必须低于 100。原因是 a、b、c 类似于用于下注的资金比率体育投注。

想使用 scipy 库来实现。到目前为止,我的代码如下所示:

from scipy.optimize import minimize

prob = new['Win prob']
decimal = new['Decimal odds']
bank = outcome['Starting bankroll'][0]

def constraint1(bet):
a,b,c = bet

return 100 - a + b + c

con1 = {'type': 'ineq', 'fun': constraint1}
cons = [con1]

b0, b1, b2 = (0,100), (0,100), (0,100)
bnds = (b0, b1, b2)

def f(bet, sign = -1):
global prob, decimal, bank
p0,p1,p2 = prob
d0,d1,d2 = decimal
a,b,c = bet

wins0 = a * (d0-1)
wins1 = b * (d1-1)
wins2 = c * (d2-1)

loss0 = b + c
loss1 = a + c
loss2 = a + b

log0 = np.log(bank + wins0 - loss0)
log1 = np.log(bank + wins1 - loss1)
log2 = np.log(bank + wins2 - loss2)

objective = (log0 * p0 + log1 * p1 + log2 * p2)

return sign * objective


bet = [5,8,7]

result = minimize(f, bet, method = 'SLSQP', bounds = bnds, constraints = cons)

然而,这并没有产生预期的结果。期望的结果是:

a = 3.33
b = 3.33
c = 0

我的问题也是如何设置methodinitial值?通过为赌注分配不同的方法和初始值,结果似乎有很大差异。

如有任何帮助,我们将不胜感激!

(这是pinnacle网站上发布的例子:https://www.pinnacle.com/en/betting-articles/Betting-Strategy/the-real-kelly-criterion/HZKJTFCB3KNYN9CJ)

最佳答案

如果您在函数中打印出“下注”值,您可以看到哪里出错了。

[5. 8. 7.]
[5.00000001 8. 7. ]
[5. 8.00000001 7. ]
[5. 8. 7.00000001]
[5.00040728 7.9990977 6.99975556]
[5.00040729 7.9990977 6.99975556]
[5.00040728 7.99909772 6.99975556]
[5.00040728 7.9990977 6.99975558]
[5.00244218 7.99458802 6.99853367]
[5.0024422 7.99458802 6.99853367]

该算法尝试通过相对于您的初始值进行非常小的调整来优化公式,并且它永远不会进行足够的调整以获得您正在寻找的值。

如果你查看 scipy 网页,你会发现 https://docs.scipy.org/doc/scipy/reference/optimize.minimize-slsqp.html#optimize-minimize-slsqp

eps float
Step size used for numerical approximation of the Jacobian.

result = minimize(f, bet, method='SLSQP', bounds=bnds, constraints=cons,
options={'maxiter': 100, 'ftol': 1e-06, 'iprint': 1, 'disp': True,
'eps': 1.4901161193847656e-08, 'finite_diff_rel_step': None})

因此您从 1.0e-08 的步长开始,因此您的初始估计在算法将要寻找的范围之外偏离了许多数量级。

我建议将您的赌注标准化为 0 到 1 之间的值。所以与其说我在 0 到 100 之间下注,不如说您在 0 到 1 之间下注您净财富的一小部分。A许多算法旨在处理标准化输入(介于 0 和 1 之间)或归一化输入(与平均值的标准差)。

此外,它看起来像:

def constraint1(bet):
a,b,c = bet

return 100 - a + b + c

应该是:

def constraint1(bet):
a,b,c = bet

return 100 - (a + b + c)

但我认为这不会影响您的结果

关于python - 使用 scipy 最大化目标(通过 kelly criterium),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63617933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com