cvxgrp / cvxportfolio

Portfolio optimization and back-testing.
https://www.cvxportfolio.com
GNU General Public License v3.0
993 stars 258 forks source link

NotImplementedError: only the default get_loc method is currently supported for MultiIndex #3

Closed leolle closed 6 years ago

leolle commented 7 years ago

I have this error when I run the HelloWorld example. Can anybody tell me how to fix it? Thank you.

market_sim=cp.MarketSimulator(returns, [tcost_model, hcost_model], cash_key='USDOLLAR') 
init_portfolio = pd.Series(index=returns.columns, data=0.)
init_portfolio.USDOLLAR = 100
result = market_sim.run_backtest(init_portfolio,
                               start_time='2013-01-03',  end_time='2016-12-31',  
                               policy=spo_policy)

result.summary()
--------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-9-b229f1c1f6b4> in <module>()
      4 result = market_sim.run_backtest(init_portfolio,
      5                                start_time='2013-01-03',  end_time='2016-12-31',
----> 6                                policy=spo_policy)
      7 
      8 result.summary()

/home/weiwu/projects/cvxportfolio/cvxportfolio/simulator.py in run_backtest(self, initial_portfolio, start_time, end_time, policy, loglevel)
    114             start = time.time()
    115             try:
--> 116                 u = policy.get_trades(h, t)
    117             except cvx.SolverError:
    118                 logging.warning(

/home/weiwu/projects/cvxportfolio/cvxportfolio/policies.py in get_trades(self, portfolio, t)
    222 
    223         for cost in self.costs:
--> 224             cost_expr, const_expr = cost.weight_expr(t, wplus, z, value)
    225             costs.append(cost_expr)
    226             constraints += const_expr

/home/weiwu/projects/cvxportfolio/cvxportfolio/risks.py in weight_expr(self, t, w_plus, z, value)
     43 
     44     def weight_expr(self, t, w_plus, z, value):
---> 45         self.expression = self._estimate(t, w_plus - self.w_bench, z, value)
     46         return self.gamma * self.expression, []
     47 

/home/weiwu/projects/cvxportfolio/cvxportfolio/risks.py in _estimate(self, t, wplus, z, value)
     81     def _estimate(self, t, wplus, z, value):
     82         try:
---> 83             self.expression = cvx.quad_form(wplus, locator(self.Sigma, t))
     84         except TypeError:
     85             self.expression = cvx.quad_form(

/home/weiwu/projects/cvxportfolio/cvxportfolio/risks.py in locator(obj, t)
     30     """Picks last element before t."""
     31     try:
---> 32         return obj.iloc[obj.axes[0].get_loc(t, method='pad')]
     33     except AttributeError:  # obj not pandas
     34         return obj

/home/weiwu/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/indexes/multi.py in get_loc(self, key, method)
   1946         """
   1947         if method is not None:
-> 1948             raise NotImplementedError('only the default get_loc method is '
   1949                                       'currently supported for MultiIndex')
   1950 

NotImplementedError: only the default get_loc method is currently supported for MultiIndex
jonathanng commented 7 years ago

I fixed this issue on my branch. I've already made a pull request, but there has been no response. You can download my fixed version here:

https://github.com/jonathanng/cvxportfolio

leolle commented 7 years ago

@jonathanng thank you, I cloned your branch, but new error came up. I am using anaconda3-4.4.0.

/home/weiwu/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/cvxpy/atoms/quad_form.py:81: UserWarning: Forming a nonconvex expression quad_form(x, indefinite).
  warnings.warn("Forming a nonconvex expression quad_form(x, indefinite).")

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-b229f1c1f6b4> in <module>()
      4 result = market_sim.run_backtest(init_portfolio,
      5                                start_time='2013-01-03',  end_time='2016-12-31',
----> 6                                policy=spo_policy)
      7 
      8 result.summary()

/home/weiwu/projects/cvxportfolio/cvxportfolio/simulator.py in run_backtest(self, initial_portfolio, start_time, end_time, policy, loglevel)
    114             start = time.time()
    115             try:
--> 116                 u = policy.get_trades(h, t)
    117             except cvx.SolverError:
    118                 logging.warning(

/home/weiwu/projects/cvxportfolio/cvxportfolio/policies.py in get_trades(self, portfolio, t)
    236 
    237         for el in costs:
--> 238             assert (el.is_convex())
    239 
    240         for el in constraints:

AssertionError: 
jonathanng commented 7 years ago

Try updating your copy of cvxopt:

pip install cvxopt -U

Also in my branch, I cleaned up the HelloWorld example code. Try to see if that works.

leolle commented 7 years ago

@jonathanng My cvxopt versions is up to date. Here's another error:

home/weiwu/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/cvxpy/atoms/quad_form.py:81: UserWarning: Forming a nonconvex expression quad_form(x, indefinite).
  warnings.warn("Forming a nonconvex expression quad_form(x, indefinite).")

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-7-22d34144d184> in <module>()
     12     start_time        = '2013-01-03',
     13     end_time          = '2016-12-31',
---> 14     policy            = spo_policy
     15 )
     16 

/home/weiwu/projects/tmp/test/cvxportfolio/cvxportfolio/simulator.py in run_backtest(self, initial_portfolio, start_time, end_time, policy, loglevel)
    114             start = time.time()
    115             try:
--> 116                 u = policy.get_trades(h, t)
    117             except cvx.SolverError:
    118                 logging.warning(

/home/weiwu/projects/tmp/test/cvxportfolio/cvxportfolio/policies.py in get_trades(self, portfolio, t)
    236 
    237         for el in costs:
--> 238             assert (el.is_convex())
    239 
    240         for el in constraints:

AssertionError: 
jonathanng commented 7 years ago

Which notebook is this? HelloWorld?

leolle commented 7 years ago

yes, hello world. I have no problem with preload data optimization and simulation.

On Tue, Aug 8, 2017 at 9:32 PM, JPN notifications@github.com wrote:

Which notebook is this? HelloWorld?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cvxgrp/cvxportfolio/issues/3#issuecomment-320956854, or mute the thread https://github.com/notifications/unsubscribe-auth/AMSS0rOjQRYBac4gqmz_CtHPePvco3wWks5sWGN0gaJpZM4OsBc1 .

jonathanng commented 7 years ago

Which cell/line is giving you this error?

leolle commented 7 years ago

this one:

market_sim = cp.MarketSimulator(
    market_returns = returns,
    costs          = [tcost_model, hcost_model],
    cash_key       = 'USDOLLAR'
) 

init_portfolio = pd.Series(index=returns.columns, data=0.)
init_portfolio.USDOLLAR = 100

result = market_sim.run_backtest(
    initial_portfolio = init_portfolio,
    start_time        = '2013-05-21',
    end_time          = '2016-12-31',  
    policy            = spo_policy
)

result.summary()
jonathanng commented 7 years ago

I'm not sure what the issue is. It's hard to say without have the same environment. Have you tried a fresh install of Python & cvxportfolio?

jonathanng commented 6 years ago

I encountered a similar error and I found that if you have negative borrow rates, it won't be able to determine if the problem is convex.

One easy fix would be to do something like:

borrow_costs[borrow_costs < 0.0] = 0.0
ghost commented 6 years ago

This should be fixed now.

kenuku commented 4 years ago

You need to sort the columns alphabetically in 'returns' DataFrame. Probably 'cvxpy' not fully compatible with pandas. I spent three days searching for this glitch.

Add to your code: returns.sort_index(axis=1, inplace=True)

kenuku commented 4 years ago

And the cash asset should be in last place, i.e. in the last column as described in paper:

https://web.stanford.edu/~boyd/papers/pdf/cvx_portfolio.pdf

This column can be called '_USDOLLAR' instead 'USDOLLAR', then the columns will be sorted correctly.