Scipy或bayesian优化函数与Python中的约束、边界和数据框

使用下面的数据框,我想优化总回报,同时满足某些界限。

d = {'Win':[0,0,1, 0, 0, 1, 0],'Men':[0,1,0, 1, 1, 0, 0], 'Women':[1,0,1, 0, 0, 1,1],'Matches' :[0,5,4, 7, 4, 10,13],
     'Odds':[1.58,3.8,1.95, 1.95, 1.62, 1.8, 2.1], 'investment':[0,0,6, 10, 5, 25,0],}

data = pd.DataFrame(d)

我想最大化以下等式:

totalreturn = np.sum(data['Odds'] * data['investment'] * (data['Win'] == 1))

函数应该最大化满足以下边界:

for i in range(len(data)):
    
    investment = data['investment'][i]
    
    C = alpha0 + alpha1*data['Men'] + alpha2 * data['Women'] + alpha3 * data['Matches']
    
    if (lb < investment ) & (investment < ub) & (investment > C) == False:
        data['investment'][i] = 0

因此lbub对于数据帧中的每一行都是常量。C但是,每行的阈值都不同。因此有 6 个参数需要优化:lb, ub, alph0, alpha1, alpha2, alpha3.

谁能告诉我如何在python中做到这一点?我的诉讼至今已与SciPy的(Approach1)和贝叶斯(Approach2)优化和仅lbub正在试图进行优化。方法一:

import pandas as pd
from scipy.optimize import minimize

def objective(val, data):
    
    # Approach 1
    # Lowerbound and upperbound
    lb, ub = val
    
    # investments
    # These matches/bets are selected to put wager on
    tf1 = (data['investment'] > lb) & (data['investment'] < ub) 
    data.loc[~tf1, 'investment'] = 0
    
        
    # Total investment
    totalinvestment = sum(data['investment'])
    
    # Good placed bets 
    data['reward'] = data['Odds'] * data['investment'] * (data['Win'] == 1)
    totalreward = sum(data['reward'])

    # Return and cumalative return
    data['return'] = data['reward'] - data['investment']
    totalreturn = sum(data['return'])
    data['Cum return'] = data['return'].cumsum()
    
    # Return on investment
    print('n',)
    print('lb, ub:', lb, ub)
    print('TotalReturn: ',totalreturn)
    print('TotalInvestment: ', totalinvestment)
    print('TotalReward: ', totalreward)
    print('# of bets', (data['investment'] != 0).sum())
          
    return totalreturn
          

# Bounds and contraints
b = (0,100)
bnds = (b,b,)
x0 = [0,100]

sol = minimize(objective, x0, args = (data,), method = 'Nelder-Mead', bounds = bnds)

和方法2:

import pandas as pd
import time
import pickle
from hyperopt import fmin, tpe, Trials
from hyperopt import STATUS_OK
from hyperopt import  hp

def objective(args):
    # Approach2

    # Lowerbound and upperbound
    lb, ub = args
    
    # investments
    # These matches/bets are selected to put wager on
    tf1 = (data['investment'] > lb) & (data['investment'] < ub) 
    data.loc[~tf1, 'investment'] = 0
    
        
    # Total investment
    totalinvestment = sum(data['investment'])
    
    # Good placed bets 
    data['reward'] = data['Odds'] * data['investment'] * (data['Win'] == 1)
    totalreward = sum(data['reward'])

    # Return and cumalative return
    data['return'] = data['reward'] - data['investment']
    totalreturn = sum(data['return'])
    data['Cum return'] = data['return'].cumsum()
    
    # store results
    d = {'loss': - totalreturn, 'status': STATUS_OK, 'eval time': time.time(),
    'other stuff': {'type': None, 'value': [0, 1, 2]},
    'attachments': {'time_module': pickle.dumps(time.time)}}
    
    return d

          

trials = Trials()

parameter_space  = [hp.uniform('lb', 0, 100), hp.uniform('ub', 0, 100)]

best = fmin(objective,
    space= parameter_space,
    algo=tpe.suggest,
    max_evals=500,
    trials = trials)


print('n', trials.best_trial)

有谁知道我应该如何进行?Scipy 不会生成所需的结果。Hyperopt 优化确实会产生所需的结果。在任何一种方法中,我都不知道如何合并一个依赖于行的边界 ( C(i))。

什么都会有帮助!(任何有关优化类型的相关文章、练习或有用的解释也非常受欢迎)

以上是Scipy或bayesian优化函数与Python中的约束、边界和数据框的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>