Last active
October 21, 2023 09:57
-
-
Save johnGachihi/e5a7b11596b1b5ea04c9b55e6840770e to your computer and use it in GitHub Desktop.
Forward stepwise using BIC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bic_selection(candidates: set, | |
selected: set = None, | |
prev_ic: int = np.inf, | |
prev_ics: list = None, | |
ic_change_threshold: int = 0): | |
if prev_ics is None: | |
prev_ics = [] | |
if selected is None: | |
selected = set() | |
best_candidate = {"name": "", "bic": np.inf} | |
for c in candidates: | |
feats = list(selected | {c}) | |
res = sm.OLS(y_train, sm.add_constant(X_train[feats])).fit() | |
if res.bic < best_candidate["bic"]: | |
best_candidate = {"name": c, "bic": res.bic} | |
if prev_ic - best_candidate["bic"] > ic_change_threshold: | |
return bic_selection( | |
candidates=candidates - {best_candidate["name"]}, | |
selected=selected | {best_candidate["name"]}, | |
prev_ic=best_candidate["bic"], | |
prev_ics=[*prev_ics, best_candidate["bic"]], | |
ic_change_threshold=ic_change_threshold | |
) | |
else: | |
return list(selected), np.array(prev_ics) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment