Coverage for features/alt_data_features.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Shared, pure alternative-data feature implementations (development/ 

2Problems.md #71, Phase 4.12) - additive sibling to features/bond_features.py 

3and features/macro_features.py, backed by data_pipeline/fred_backfill.py's 

4implied-volatility and financial-conditions series rather than price data. 

5 

6Broadcast identically to EVERY asset's model input (date-only, not 

7asset-specific) - same "compute once per date/bar, every asset sees it" 

8shape as bond_features.py/macro_features.py. 

9 

10Imported by BOTH train.py (offline, per-date FRED-series lookup via 

11data_pipeline.fred_backfill.series_value_asof()/series_change_asof()) and 

12main.py (runtime, once per bar) for parity by construction - same 

13convention as features/bond_features.py. 

14 

15Every function neutral-defaults to 0.0 on missing input - never raises. 

16The 0.0 default is chosen so it means something PLAUSIBLE, not merely 

17"absent": implied_volatility_level() is a log-ratio to a reference VIX 

18level (see VIX_REFERENCE_LEVEL below), so its neutral default corresponds 

19to "VIX = 20," an unremarkable reading, not "VIX = 0," which cannot 

20happen. This mirrors every other neutral-default in this codebase 

21(bond_features.py, macro_features.py) using 0.0 to mean "no signal." 

22 

23These three features were chosen from a wider candidate list (VIX, VXV, 

24NFCI, STLFSI4, UMCSENT, TEDRATE, SOFR, BAMLH0A0HYM2, DTWEXBGS) after 

25live-verifying each one's real 2014-2021 coverage and checking pairwise 

26collinearity - see data_pipeline/fred_backfill.py's 

27DEFAULT_ALT_DATA_REFERENCE_SERIES docstring for the rejected candidates 

28and why. The surviving three are deliberately NOT redundant with each 

29other or with the existing 8 bond_* features: implied_volatility_level 

30alone cannot distinguish the 2019-12-27..2020-03-25 COVID-crash era (mean 

31VIX 28.54) from the immediately-following recovery era (mean VIX 29.24) - 

32term structure and financial-conditions CHANGE can (measured: term 

33structure mean +0.013 during the crash vs +0.105 after; NFCI 4-week 

34change mean +0.154/max +0.608 during the crash vs a total 2014-2019 range 

35of [-0.091, +0.091]). 

36""" 

37 

38from __future__ import annotations 

39 

40import math 

41 

42IMPLIED_VOLATILITY_LEVEL_NEUTRAL = 0.0 

43IMPLIED_VOL_TERM_STRUCTURE_NEUTRAL = 0.0 

44FINANCIAL_CONDITIONS_CHANGE_NEUTRAL = 0.0 

45REAL_RATE_CHANGE_NEUTRAL = 0.0 

46BREAKEVEN_INFLATION_CHANGE_NEUTRAL = 0.0 

47DOLLAR_INDEX_CHANGE_NEUTRAL = 0.0 

48 

49# Divisor for implied_volatility_level()'s log-ratio - an unremarkable, 

50# roughly-median VIX reading (measured p50 15.1 over the 2014-12..2021-03 

51# common_window), chosen so the neutral default (log(1.0) = 0.0) maps back 

52# to a plausible market state rather than an impossible one (a raw VIX 

53# feature's 0.0 default would mean "VIX = 0", which never happens). 

54VIX_REFERENCE_LEVEL = 20.0 

55 

56ALT_DATA_FEATURE_NAMES = [ 

57 "alt_implied_volatility_level", 

58 "alt_implied_vol_term_structure", 

59 "alt_financial_conditions_change", 

60] 

61 

62 

63def implied_volatility_level(vix_close: float | None) -> float: 

64 """log(VIX / VIX_REFERENCE_LEVEL). VIX is right-skewed (measured p50 

65 15.1 / p90 26.7 / p99 53.9 over the common_window) - a raw level would 

66 let extreme prints (82.69 in March 2020) dominate the feature scaler's 

67 clip/winsorize range; the log compresses that skew the same way 

68 close_to_close_return features already use log-scale-friendly 

69 transforms elsewhere in this codebase. Returns 

70 IMPLIED_VOLATILITY_LEVEL_NEUTRAL (0.0) when vix_close is missing or 

71 non-positive (a non-positive VIX reading is not physically meaningful 

72 - never log() a non-positive number).""" 

73 if vix_close is None or vix_close <= 0.0: 

74 return IMPLIED_VOLATILITY_LEVEL_NEUTRAL 

75 return math.log(float(vix_close) / VIX_REFERENCE_LEVEL) 

76 

77 

78def implied_vol_term_structure(vix_close: float | None, vix_3m_close: float | None) -> float: 

79 """(VIX 3-month - VIX) / VIX - positive means contango (the market is 

80 calm: longer-dated implied vol priced higher than spot), negative 

81 means backwardation (stress RIGHT NOW: spot implied vol priced above 

82 the 3-month tenor, since a acute near-term shock is expected to fade). 

83 Normalized by the spot VIX level so the ratio is comparable across 

84 vol regimes - the whole reason this feature exists alongside 

85 implied_volatility_level() rather than instead of it: VIX level alone 

86 cannot separate the COVID-crash era from the immediately-following 

87 recovery era (both ~28-29 mean VIX), while term structure can (+0.013 

88 during the crash vs +0.105 after, with a -0.256 backwardation minimum 

89 during the crash). Returns IMPLIED_VOL_TERM_STRUCTURE_NEUTRAL (0.0) 

90 when either leg is missing or vix_close is non-positive.""" 

91 if vix_close is None or vix_3m_close is None or vix_close <= 0.0: 

92 return IMPLIED_VOL_TERM_STRUCTURE_NEUTRAL 

93 return (float(vix_3m_close) - float(vix_close)) / float(vix_close) 

94 

95 

96def financial_conditions_change(nfci_now: float | None, nfci_prior: float | None) -> float: 

97 """NFCI(t) - NFCI(t - 4 weeks) - the Chicago Fed National Financial 

98 Conditions Index's own 4-week CHANGE, not its level. The level is 

99 deliberately not a feature here: measured correlation of rho=0.73 

100 against the existing bond_credit_spread_level feature (BAA10Y), i.e. 

101 the model already has substantially the same information via that 

102 channel. The 4-week CHANGE is a genuinely different signal (rho=0.23 

103 against bond_credit_spread_level) - a near-binary "conditions are 

104 tightening violently right now" indicator: measured mean +0.154 (max 

105 +0.608) during the 2019-12-27..2020-03-25 COVID-crash era, versus a 

106 total 2014-12..2019-12 pre-COVID range of only [-0.091, +0.091] - the 

107 crash reading sits roughly 6.7x outside five years of prior variation. 

108 Returns FINANCIAL_CONDITIONS_CHANGE_NEUTRAL (0.0) when either endpoint 

109 is missing (e.g. fewer than 4 weeks of history available yet, or the 

110 series cache is absent).""" 

111 if nfci_now is None or nfci_prior is None: 

112 return FINANCIAL_CONDITIONS_CHANGE_NEUTRAL 

113 return float(nfci_now) - float(nfci_prior) 

114 

115 

116def real_rate_change(real_rate_now: float | None, real_rate_prior: float | None) -> float: 

117 """DFII10(t) - DFII10(t - N) - the 10-year TIPS (real) yield's own 

118 CHANGE, not its level. V5.1 Phase 2 (item 8 / F2, development/ 

119 Problems.md): a stationary driver for 

120 features/cross_asset_sensitivity.py's per-asset real-rate sensitivity 

121 beta - a level would be non-stationary and comparing betas fit against 

122 a trending regressor across different market eras is not meaningful, 

123 the same reasoning financial_conditions_change() above already 

124 documents for NFCI. Returns REAL_RATE_CHANGE_NEUTRAL (0.0) when either 

125 endpoint is missing.""" 

126 if real_rate_now is None or real_rate_prior is None: 

127 return REAL_RATE_CHANGE_NEUTRAL 

128 return float(real_rate_now) - float(real_rate_prior) 

129 

130 

131def breakeven_inflation_change(breakeven_now: float | None, breakeven_prior: float | None) -> float: 

132 """T10YIE(t) - T10YIE(t - N) - the 10-year breakeven inflation rate's 

133 own CHANGE. Same stationarity reasoning as real_rate_change() above - 

134 a driver for features/cross_asset_sensitivity.py, not a broadcast 

135 level feature. Returns BREAKEVEN_INFLATION_CHANGE_NEUTRAL (0.0) when 

136 either endpoint is missing.""" 

137 if breakeven_now is None or breakeven_prior is None: 

138 return BREAKEVEN_INFLATION_CHANGE_NEUTRAL 

139 return float(breakeven_now) - float(breakeven_prior) 

140 

141 

142def dollar_index_change(dollar_index_now: float | None, dollar_index_prior: float | None) -> float: 

143 """DTWEXBGS(t) - DTWEXBGS(t - N) - the trade-weighted dollar index's 

144 own CHANGE. Same stationarity reasoning as real_rate_change() above - 

145 a driver for features/cross_asset_sensitivity.py, not a broadcast 

146 level feature (see data_pipeline/fred_backfill.py's 

147 DEFAULT_ALT_DATA_REFERENCE_SERIES docstring for why DTWEXBGS is being 

148 re-added as a sensitivity driver rather than a level, having 

149 previously been evaluated and left out of the original alt-data 

150 trio). Returns DOLLAR_INDEX_CHANGE_NEUTRAL (0.0) when either endpoint 

151 is missing.""" 

152 if dollar_index_now is None or dollar_index_prior is None: 

153 return DOLLAR_INDEX_CHANGE_NEUTRAL 

154 return float(dollar_index_now) - float(dollar_index_prior)