The Wayback Machine - https://web.archive.org/web/20201103043636/https://github.com/kernc/backtesting.py/issues/69
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for Faster way to optimize vs GridSearch #69

Open
mitrymoe opened this issue May 13, 2020 · 10 comments
Open

Suggestion for Faster way to optimize vs GridSearch #69

mitrymoe opened this issue May 13, 2020 · 10 comments

Comments

@mitrymoe
Copy link

@mitrymoe mitrymoe commented May 13, 2020

Hey,

First of all thank you for the hard work put into this, it's a really awesome project that I still learn how to use.
I'm not that proficient in using python so that is why I'll put my suggestion here. I have used python more for machine learning stuff and there the option of searching hyperparameters was also between grid and random search. However, I found that using bayesian optimization, code runs way faster because it add probabilities to the mix by building a surrogate probability model of the objective function. That being said, you can have a look maybe over this to find out more here

My guess is that this can be applied to your maximize_func in some way and computations should, in theory, run faster.
I'll spend the next few days trying and playing around with this and if I come up with something, I'll let you know.

Keep it up!

@kernc
Copy link
Owner

@kernc kernc commented May 14, 2020

Thanks for the kind words. ❤️

bayesian optimization

Sounds useful. We should certainly look into using that package as a library:

https://github.com/fmfn/BayesianOptimization#1-specifying-the-function-to-be-optimized

In most cases probably even something as simple as random grid search should work much faster, while still giving sufficient ballpark results:

heatmap = pd.Series(np.nan,
index=pd.MultiIndex.from_tuples([p.values() for p in param_combos],
names=next(iter(param_combos)).keys()))
# TODO: add parameter `max_tries:Union[int, float]=None` which switches
# exhaustive grid search to random search. This might need to avoid
# returning NaNs in stats on runs with no trades to differentiate those
# from non-tested parameter combos in heatmap.

@ttfreeman
Copy link

@ttfreeman ttfreeman commented Sep 27, 2020

I would suggest using scikit-optimize? The random search is not guaranteed to converge faster to the optimum point because it does not learn from previous trial points.

@kernc kernc added the Hacktoberfest label Oct 1, 2020
@kernc
Copy link
Owner

@kernc kernc commented Oct 1, 2020

We could use skopt as our constraints do match. 👍 The question is, will you implement it? Random search should be roughly a 5-line change.

@ttfreeman
Copy link

@ttfreeman ttfreeman commented Oct 1, 2020

Sure, if nothing comes up I'll work on this in the next few days. I'll also look into skopt. So, we can just add a parameter method to optimize() method, and that way, different techniques of optimization can be chosen based on the input for method param.

@kernc
Copy link
Owner

@kernc kernc commented Oct 1, 2020

Exactly. For random search I also envisioned an argument: max_tries: Union[int, float] = 200. Integer for absolute; float for relative to exhaustive search. This should then match n_calls= for skopt.

@ttfreeman
Copy link

@ttfreeman ttfreeman commented Oct 4, 2020

I have implemented skopt's forest_minimize() method with encouraging results. Just need to validate the things and I also think it would be helpful to add the partial dependency plots. I am looking at how you've used bokeh and will add a function to plot this after the optimization. Hopefully will have something for your review next weekend.
image

@kernc
Copy link
Owner

@kernc kernc commented Oct 8, 2020

it would be helpful to add the partial dependency plots

Think you can just return the raw scipy.optimize.OptimizeResult that skopt.plots.plot_objective() (and possibly other utils) already knows how to work with? No need to reinvent the wheel. Instead, we should document the workflow.

We should, however, look into adopting the lower-triangular subplot layout for the backtesting.lib.plot_heatmaps(). That makes it all much clearer!

@ttfreeman
Copy link

@ttfreeman ttfreeman commented Oct 9, 2020

Yes, plotting the optimization results in a jupyter notebook is straightforward with plot_objective (and that's how I plotted the charts above for the Quick Start example). Though I am not sure if it would be as straightforward to integrate that with bokeh. The only thing that might be possible if we can export the plot_objective plots as PNG and bokeh can use them as image tag in their html file? It just won't be interactive like native bokeh charts.

I quickly looked at the heatmap plots from backtesting.lib.plot_heatmaps() and they are not as nice and clear with contour lines and test points etc.

@kernc
Copy link
Owner

@kernc kernc commented Oct 9, 2020

Though I am not sure if it would be as straightforward to integrate that with bokeh.

I don't fully understand why we'd want it to integrate it with Bokeh. plot_objective() will output into notebooks just as well, and the returned matplotlib.Axes can be savefig'd for an image. Sure, the plot won't be interactive, but that's a miniature price to pay for not needing to write/maintain any extra code? What I'm suggesting, therefore, is that we just return the OptimizeResult and mark a note in the docs that the user might want to run skopt.plots.plot_objective() upon it. It's perfectly ok to mix best of breed utilities from the ecosystem. 😁

I quickly looked at the heatmap plots from backtesting.lib.plot_heatmaps() and they are not as nice and clear with contour lines and test points etc.

They are not. It's just an unrelated note-to-self proposal that we lay them out sorted in a more immediately clear lower-triangular instead of the simple grid layout.

@guzuomuse
Copy link

@guzuomuse guzuomuse commented Nov 2, 2020

this should be on the top priority! big win if we have this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.