Skip to content

add CombinatorialGapKFold - #41

Open
aldder wants to merge 9 commits into
WenjieZ:masterfrom
aldder:combinatorialgapkfold
Open

add CombinatorialGapKFold#41
aldder wants to merge 9 commits into
WenjieZ:masterfrom
aldder:combinatorialgapkfold

Conversation

@aldder

@aldder aldder commented Feb 1, 2022

Copy link
Copy Markdown

From "Advances in Financial Machine Learning" book by Marcos López de Prado
the implemented version of Combinatorial Cross Validation with Purging and Embargoing

image

explaining video: https://www.youtube.com/watch?v=hDQssGntmFA

@pep8speaks

pep8speaks commented Feb 1, 2022

Copy link
Copy Markdown

Hello @aldder! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 451:9: F841 local variable 'gap_before' is assigned to but never used
Line 451:21: F841 local variable 'gap_after' is assigned to but never used

Line 672:80: E501 line too long (102 > 79 characters)
Line 678:80: E501 line too long (103 > 79 characters)
Line 684:80: E501 line too long (115 > 79 characters)
Line 690:80: E501 line too long (119 > 79 characters)
Line 698:80: E501 line too long (106 > 79 characters)

Comment last updated at 2022-02-11 12:19:56 UTC

@WenjieZ

WenjieZ commented Feb 5, 2022

Copy link
Copy Markdown
Owner

Hi @aldder , please try to add some test cases in the test_split.py file.

@codecov

codecov Bot commented Feb 7, 2022

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.88%. Comparing base (c05265a) to head (c7b2bed).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
+ Coverage   97.51%   97.88%   +0.37%     
==========================================
  Files           3        3              
  Lines         643      756     +113     
==========================================
+ Hits          627      740     +113     
  Misses         16       16              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@WenjieZ WenjieZ left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comments and make changes accordingly.

Comment thread tscv/_split.py Outdated
self.n_groups = N
self.test_splits = k

def split(self, X, y=None, groups=None):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The canonical way of doing this is to redefine _iter_test_indices(self, X, y=None, groups=None) from the base class and generate only the test indices. The base class will take care of the rest. Please refer to the other derived classes and make modification accordingly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that in order to check if the training set size is > 0 we need to compute the complement of test indices after their generation.
And if we already do this, there is no point in discarding this information to recalculate it after

@WenjieZ WenjieZ Feb 11, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check of the training set size (and the test set size in some cases) can be delayed and implemented in GapCrossValidator._iter_train_indices() and its 3 siblings. You can also implement it in GapCrossValidator.split() if you find it a hustle to implement it four times.

@WenjieZ WenjieZ Feb 11, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I don't find it necessary to check the non-emptiness of the training set. Some models/algorithms can output a default estimator/strategy given an empty training set (e.g., equal weight portfolio). If your model/algorithm requires a non-empty training set, it's probably a good idea to check it in the model/algorithm rather than in the cross-validator. That's why I didn't implement this check when I released the package.

That said, I probably forgot to check the non-emptiness of the test set.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok then, I will replace the implementation of the split method with _iter_test_indices

About the non-emptiness of the test set I think you prefer to implement it in the base class with a specific PR, so I won't touch it, ok?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your judgement is correct.

Comment thread tscv/_split.py Outdated
n_splits : int
Returns the number of splitting iterations in the cross-validator.
"""
return len(list(combinations(range(self.n_groups), self.test_splits)))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the combination number to generate the result directly rather than instantiating all combinations.

Comment thread tscv/_split.py
from itertools import chain
from itertools import chain, combinations
from inspect import signature
from scipy.special import comb

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add scipy in

TSCV/setup.py

Line 55 in 2abbc3d

install_requires=['numpy>=1.13.3', 'scikit-learn>=0.22']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants