使用两个变量对cumprod()进行优化

时间:2018-12-10 19:26:16

标签: python pandas numpy mathematical-optimization

我正在尝试优化两个变量的函数。问题是我的函数有一个熊猫数据框'df_main',其中有3列作为param_1,param_2并返回,因此我想最大化以下定义的输出,

def func(p1, p2):
    return df_main[(df_main['param_1'] >= p1) & (df_main['param_2'] >= p2)]['returns'].add(1).cumprod().iloc[-1]

在对param_1和param_2列应用过滤器后,定义返回returns列的累积乘积

enter image description here

我正在尝试类似

import scipy.optimize as spo
spo.brute(func, ((0, 1, 0.1), (0, 1, 0.1)), finish=None)

原因

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-322-4fb6b5111a14> in <module>
----> 1 spo.brute(func, ((0,1,0.1), (0,1,0.1)), finish=None)

e:\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in brute(func, ranges, args, Ns, full_output, finish, disp)
   2829     if (N == 1):
   2830         grid = (grid,)
-> 2831     Jout = vecfunc(*grid)
   2832     Nshape = shape(Jout)
   2833     indx = argmin(Jout.ravel(), axis=-1)

e:\Anaconda3\lib\site-packages\numpy\lib\function_base.py in __call__(self, *args, **kwargs)
   1970             vargs.extend([kwargs[_n] for _n in names])
   1971 
-> 1972         return self._vectorize_call(func=func, args=vargs)
   1973 
   1974     def _get_ufunc_and_otypes(self, func, args):

e:\Anaconda3\lib\site-packages\numpy\lib\function_base.py in _vectorize_call(self, func, args)
   2040             res = func()
   2041         else:
-> 2042             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2043 
   2044             # Convert args to object arrays first

e:\Anaconda3\lib\site-packages\numpy\lib\function_base.py in _get_ufunc_and_otypes(self, func, args)
   2000 
   2001             inputs = [arg.flat[0] for arg in args]
-> 2002             outputs = func(*inputs)
   2003 
   2004             # Performance note: profiling indicates that -- for simple

e:\Anaconda3\lib\site-packages\scipy\optimize\optimize.py in _scalarfunc(*params)
   2823     def _scalarfunc(*params):
   2824         params = asarray(params).flatten()
-> 2825         return func(params, *args)
   2826 
   2827     vecfunc = vectorize(_scalarfunc)

TypeError: func() missing 1 required positional argument: 'p2'

在使用cumprod()将两个参数作为过滤器应用于数据框时,如何强行使用它们?在3列numpy数组而不是数据框本身上的应用程序也应足够。

>

1 个答案:

答案 0 :(得分:1)

scipy.optimize.brute可能会将参数作为数组(形式为np.array([p1,p2]))提供给您的函数。因此,如果您更改功能以适应此要求,是否可行?例如

def func(p_arr):
    p1, p2 = p_arr
    return df_main[(df_main['param_1'] >= p1) & (df_main['param_2'] >= p2)]['returns'].add(1).cumprod().iloc[-1]