Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用scipy最大化目标(通过kelly准则)_Python_Pandas_Scipy_Constraints_Scipy Optimize Minimize - Fatal编程技术网

Python 使用scipy最大化目标(通过kelly准则)

Python 使用scipy最大化目标(通过kelly准则),python,pandas,scipy,constraints,scipy-optimize-minimize,Python,Pandas,Scipy,Constraints,Scipy Optimize Minimize,我有以下两个数据帧:新建和结果 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

我有以下两个数据帧:新建和结果

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
             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
和数据帧“结果”

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'])
输出:

         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
             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['betamount']中包含的值最大化
目标。约束条件是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
我的问题也是如何设置
方法
初始值
值?通过为下注指定不同的方法和初始值,结果似乎差异很大

任何帮助都将不胜感激


(这是pinnacle网站上发布的一个示例:)

当初始下注值设置为零时,代码似乎已经起作用。