In high-frequency quantitative options market-making at Optiver and Jump Trading, estimating the rolling volatility of asset returns is critical for delta hedging and quoting spreads.
Given a stream of real-valued log-returns , we maintain the unbiased sample standard deviation over a sliding window of size : The textbook algebraic shortcut suffers from catastrophic cancellation in floating-point arithmetic when the variance is small relative to the mean squared, frequently producing negative radicands or precision loss.
Your Task
Implement the RollingVolatility class using an numerically stable updating formula:
__init__(window_size: int): Initializes the calculator with capacity .update(val: float) -> float | None: Adds return val to the stream. Once exactly items are in the buffer, returns the sample standard deviation (rounded to 6 decimal places). If fewer than items have been received, returns None.Constraints
Examples
rv = RollingVolatility(window_size=3)
rv.update(0.01) # returns None
rv.update(0.02) # returns None
rv.update(0.03) # returns 0.01
rv.update(0.04) # window becomes [0.02, 0.03, 0.04], returns 0.01