<jupyter_start><jupyter_text>Data<jupyter_code>dataset = StockDataset('^GSPC')
df = dataset.get_hist(start_date='1950-01-01', end_date='2021-10-23', time_interval='daily')
x = pd.concat(
    [df, 
     dataset.lookback_agg(lookback_len=30), 
     dataset.lookback_agg(lookback_len=60), 
     dataset.lookback_agg(lookback_len=120)],
    axis=1)

y = dataset.get_change_forecast_label(forecast_len=30, is_up=False, method='past_all')
# TRAIN_START = '1951-01-01'
TRAIN_START = '1988-01-10'
TEST_START = '2016-01-01'
TEST_END = '2019-12-01'

train_x = x[TRAIN_START:TEST_START]
train_y = y[TRAIN_START:TEST_START]
test_x = x[TEST_START:TEST_END]
test_y = y[TEST_START:TEST_END]

split = TimeSeriesSplit(n_splits=10, test_size=120)
# split = StratifiedTimeSeriesSplit(n_splits=10, test_size=120, min_positive_ratio=0.25)<jupyter_output><empty_output><jupyter_text>LogisticRegression<jupyter_code>pipeline = Pipeline(steps=[
    ('scaler', HybridScaler()), 
    ('model', LogisticRegression(penalty='elasticnet', l1_ratio=0.5, solver='saga'))
])
results = cross_validate(pipeline, train_x, train_y, cv=split, scoring='accuracy')
results['test_score'].mean()
pipeline.fit(train_x, train_y)
pred_y = pipeline.predict(test_x)

confusion_matrix(test_y, pred_y)
from sklearn.metrics import fbeta_score
print(f'Accuracy: {(test_y == pred_y.reshape(-1)).mean()}')
print(f'F1:{fbeta_score(test_y, pred_y, beta=1)}')
print(f'F2:{fbeta_score(test_y, pred_y, beta=2)}')
# test_df = moving_average(test_y, pipeline.predict_proba(test_x)[:, 1])
test_df = pd.DataFrame({'true': test_y, 'pred': pipeline.predict_proba(test_x)[:, 1]})


ax1 = test_df.plot(figsize=(18, 10), color=['g', 'orange'])
ax1.set_ylabel('bubble')
ax1.axhline(y=0.5, color='r', linestyle='--')

ax2 = ax1.twinx() 
ax2.set_ylabel('SP500')
ax2 = test_x['close'].plot()<jupyter_output><empty_output><jupyter_text>RandomForecast<jupyter_code>from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline(steps=[
    ('scaler', HybridScaler()), 
    ('model', RandomForestClassifier(n_estimators=200, random_state=1))
])
results = cross_validate(pipeline, train_x, train_y, cv=split, scoring='accuracy')
results['test_score'].mean()
pipeline.fit(train_x, train_y)
pred_y = pipeline.predict(test_x)

confusion_matrix(test_y, pred_y)
print(f'Accuracy: {(test_y == pred_y.reshape(-1)).mean()}')
print(f'F1:{fbeta_score(test_y, pred_y, beta=1)}')
print(f'F2:{fbeta_score(test_y, pred_y, beta=2)}')
# test_df = moving_average(test_y, pipeline.predict_proba(test_x)[:, 1])
test_df = pd.DataFrame({'true': test_y, 'pred': pipeline.predict_proba(test_x)[:, 1]})


ax1 = test_df.plot(figsize=(18, 10), color=['g', 'orange'])
ax1.set_ylabel('bubble')
ax1.axhline(y=0.5, color='r', linestyle='--')

ax2 = ax1.twinx() 
ax2.set_ylabel('SP500')
ax2 = test_x['close'].plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17872191011235955, 'fraction_numerical': 0.04353932584269663, 'mean_word_length': 8.31045751633987, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '171360', 'license': 'MIT', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1167, 'n_words': 153, 'path': 'ML_models.ipynb', 'repo_name': 'garyzccisme/bubble-detection', 'size': '2848'}