[BUG] relative_strength_index seeds Wilder's smoothed average one change late
Describe the bug
relative_strength_index in gs_quant/timeseries/technicals.py seeds Wilder's smoothed average one change late. smoothed_moving_average seeds from apply_ramp(mean(x, Window(w, 0)), w), which is the mean of changes 1 to w rather than 0 to w-1. The result is exactly Wilder's RSI computed on prices[1:], so the first observation is discarded and every value is shifted from the definition.
To Reproduce
import numpy as np, pandas as pd
from gs_quant.timeseries import technicals as tc
def wilder(prices, w):
ch = prices.diff().dropna(); g = ch.clip(lower=0); l = (-ch).clip(lower=0)
ag, al = g.iloc[:w].mean(), l.iloc[:w].mean()
out = {ch.index[w - 1]: 100 - 100 / (1 + ag / al)}
for i in range(w, len(ch)):
ag = (ag * (w - 1) + g.iloc[i]) / w; al = (al * (w - 1) + l.iloc[i]) / w
out[ch.index[i]] = 100 - 100 / (1 + ag / al)
return pd.Series(out)
pr = pd.Series(100 + np.cumsum(np.random.default_rng(7).normal(0, 1, 60)), index=pd.bdate_range("2024-01-01", periods=60))
g = tc.relative_strength_index(pr, 14); ref = wilder(pr, 14); ref1 = wilder(pr.iloc[1:], 14)
print(g.iloc[0], g.index[0].date()) # 39.421877 on 2024-01-22
print(ref.loc[g.index[0]]) # 42.061514 Wilder on the same date
print(ref1.iloc[0]) # 39.421877 Wilder on prices[1:]
print((g - ref).abs().max()) # 2.639636
print((g - ref1).abs().max()) # 0.0Expected behavior The first average seeded from the first w changes, giving 42.061514 on 2024-01-22 in the example and Wilder's values thereafter.
Screenshots Not applicable.
Systems setup:
- OS: macOS 26 (Darwin 25.6.0), arm64
- Python version: 3.12
- GS-Quant version: 2.1.17, commit f2a505a; pandas 3.0.6, numpy 2.3.5
Additional context
The test in test_technicals.py pins values starting at dates[w + 1], which is the shifted seed, so the snapshot locks the shift in and would need regenerating. The gap is small in level terms, at most 2.6 RSI points on this series, but the function is not Wilder's definition as documented.
Source: goldmansachs/gs-quant