0

I am trying to optimize a function. But this function takes a pandas data frame as an argument. So when I run the code, I get this error:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

My evaluation function:

def calc_sr(prices, allocs):
    cum_ret_prices = prices / prices.iloc[0]
    all_adj_prices = allocs * cum_ret_prices 
    daily_value = all_adj_prices.sum(1)
    sr = daily_value.mean()/daily_value.std()
    return(sr)

def eval_fn(allocs, prices):
    return 1/calc_sr(prices,allocs)
allocs = optimize.minimize(eval_fn,allocs,args={prices},method = 'Nelder-Mead', options={'maxiter':10000})

Prices is a pandas data frame.

How do I optimize this? Also one more question is that my optimization function needs the array of initial guesses to be passed in as well. SO how do I account for that when I set the args parameter?

Parth
  • 2,682
  • 1
  • 20
  • 39
  • The `args` parameter is supposed to be tuple, not a set. e.g. `args=(prices,)`. – hpaulj Sep 03 '19 at 01:19
  • Thanks! That helpes. If I wanted to give a condition that the optimized array should all add to 1 and each of them should be greater than 0. Then can I pass that as a condition to optimize or I will have to set a penalty in my evaluation function when I get improper values in? – Parth Sep 03 '19 at 01:24
  • There are optimization methods that take constraints - read the docs. – hpaulj Sep 03 '19 at 01:26
  • Yes found that part of the docs. Going though it. – Parth Sep 03 '19 at 01:27

0 Answers0