File size: 4,810 Bytes
031a2d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | from pathlib import Path
import pandas as pd
from services.feature_engineering import FeatureEngineer, FeatureEngineerConfig
def build_dataset(df_all: pd.DataFrame, columns: list[str]) -> pd.DataFrame:
cols = [col for col in columns if col in df_all.columns]
df = df_all[cols].replace([float("inf"), float("-inf")], pd.NA).dropna().copy()
return df
def print_dataset_summary(name: str, path: Path, df: pd.DataFrame) -> None:
print(f"[OK] {name} saved")
print(f"Path: {path}")
print(f"Shape: {df.shape}")
print(f"Start: {df.index.min()}")
print(f"End: {df.index.max()}")
def main() -> None:
config = FeatureEngineerConfig(
input_path="data/processed/merged_monthly.csv",
output_path_full="data/features/features_monthly.csv",
output_path_long="data/features/features_monthly_full_history.csv",
)
output_dir = Path("data/features")
output_dir.mkdir(parents=True, exist_ok=True)
engineer = FeatureEngineer(config.input_path)
engineer.run()
df_all = engineer.df.copy()
full_feature_cols = [
"spx_return",
"ndx_return",
"ftse_return",
"gold_return",
"gold_return_3m",
"gold_vol_3m",
"gold_vol_6m",
"gold_drawdown",
"gold_max_dd_6m",
"btc_return",
"eurusd_return",
"gbpusd_return",
"spx_vol_3m",
"spx_vol_6m",
"ndx_vol_3m",
"ndx_vol_6m",
"vix_level",
"us2y_yield",
"us10y_yield",
"yield_spread",
"us_cpi_yoy",
"uk_cpi_yoy",
"high_yield_spread",
"vix_spike",
"spx_drawdown",
"spx_max_dd_6m",
"ndx_drawdown",
"ndx_max_dd_6m",
# ECB
"ecb_level",
"ecb_yoy",
# New: Fed Funds, TIPS, Breakeven, DXY, QQQ
"fed_funds_level",
"fed_funds_change_1m",
"tips_10y_level",
"tips_10y_change_1m",
"breakeven_10y_level",
"breakeven_10y_change_1m",
"real_yield_tips",
"real_yield_tips_change_1m",
"dxy_level",
"dxy_return",
"dxy_return_3m",
"qqq_return",
]
long_history_cols = [
"spx_return",
"ndx_return",
"gold_return",
"gold_return_3m",
"gold_vol_3m",
"gold_vol_6m",
"gold_drawdown",
"gold_max_dd_6m",
"eurusd_return",
"gbpusd_return",
"spx_vol_3m",
"spx_vol_6m",
"ndx_vol_3m",
"ndx_vol_6m",
"vix_level",
"us2y_yield",
"us10y_yield",
"yield_spread",
"us_cpi_yoy",
"high_yield_spread",
"vix_spike",
"spx_drawdown",
"spx_max_dd_6m",
"ndx_drawdown",
"ndx_max_dd_6m",
# ECB
"ecb_level",
"ecb_yoy",
# New: Fed Funds, TIPS, Breakeven, DXY, QQQ
"fed_funds_level",
"fed_funds_change_1m",
"tips_10y_level",
"tips_10y_change_1m",
"breakeven_10y_level",
"breakeven_10y_change_1m",
"real_yield_tips",
"real_yield_tips_change_1m",
"dxy_level",
"dxy_return",
"dxy_return_3m",
"qqq_return",
]
# BTC uses a SEPARATE short-history file so it doesn't truncate the long-history
# dataset for SPX/NDX/Gold (BTC data starts 2010, would lose 7 years otherwise)
btc_cols = [
"spx_return", "ndx_return", "gold_return",
"eurusd_return", "gbpusd_return",
"spx_vol_3m", "ndx_vol_3m",
"vix_level", "us2y_yield", "us10y_yield", "yield_spread",
"us_cpi_yoy", "high_yield_spread",
"vix_spike", "spx_drawdown", "ndx_drawdown",
"ecb_level", "ecb_yoy",
"dxy_return", "qqq_return",
"btc_return", # target — NaN before 2010, dropna trims automatically
]
full_df = build_dataset(df_all, full_feature_cols)
long_df = build_dataset(df_all, long_history_cols)
btc_df = build_dataset(df_all, btc_cols)
full_output = Path(config.output_path_full)
long_output = Path(config.output_path_long)
btc_output = Path("data/features/features_monthly_btc.csv")
full_df.to_csv(full_output)
long_df.to_csv(long_output)
btc_df.to_csv(btc_output)
print_dataset_summary("Full-feature dataset", full_output, full_df)
print()
print_dataset_summary("Long-history model dataset", long_output, long_df)
print()
print_dataset_summary("BTC short-history dataset", btc_output, btc_df)
print("\nColumns in full-feature dataset:")
for col in full_df.columns:
print(f" - {col}")
print("\nColumns in long-history model dataset:")
for col in long_df.columns:
print(f" - {col}")
if __name__ == "__main__":
main()
|