Time Series¶
Each stock's own history, one stock at a time. period counts rows of the data (trading days), not calendar days.
Window rules¶
Every rolling operator follows one of three rules for how much history it needs and what a missing day does. The toy data shows all three on C, which has no price on Jan 04.
| Rule | Operators | A value once the window holds | A missing day in the window |
|---|---|---|---|
| Part of the window | ts_mean, ts_std, ts_sum, ts_max, ts_min, ts_var, ts_quantile, the pair operators, volatility, volume, adv |
min(20, period) values |
is skipped, as long as enough values remain |
| The whole window | ts_rank, ts_argmax, ts_argmin, ts_skew, ts_kurt, decay_linear |
period values |
hides the result until it has left the window |
| Exponential | ewma, ewma_var, ewma_cov, ts_beta_ewma |
one value | is skipped; the average carries on |
With a window of 20 days or less, part of the window needs every day too, as in the 3-day panels below. The difference shows on longer windows: a 60-day mean needs 20 values and skips the gaps between them. How qex Evaluates explains why each rule is what it is.
Rolling statistics¶
ts_mean¶
ts_mean(x, period)
The mean of each stock's last period values. Part of the window.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
ts_mean(close(), 3)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | · | · | · | · |
| Jan 04 | 11.00 | 19.00 | · | 41.00 |
| Jan 05 | 11.33 | 18.50 | · | 42.00 |
| Jan 08 | 12.00 | 17.83 | · | 42.67 |
| Jan 09 | 12.67 | 17.17 | 31.50 | 44.33 |
ts_mean(returns(1), 20) # average daily return over a month
close() / ts_mean(close(), 200) - 1 # distance from the 200-day average
ts_std¶
ts_std(x, period)
The standard deviation of the window (sample, n − 1). Part of the window.
ts_var¶
ts_var(x, period)
The variance of the window, ts_std squared. Part of the window.
ts_sum¶
ts_sum(x, period)
The sum of the window. Part of the window.
ts_max¶
ts_max(x, period)
The largest value in the window. Part of the window.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
ts_max(close(), 3)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | · | · | · | · |
| Jan 04 | 12.0 | 20.0 | · | 42.0 |
| Jan 05 | 12.0 | 19.0 | · | 43.0 |
| Jan 08 | 13.0 | 18.5 | · | 44.0 |
| Jan 09 | 14.0 | 18.5 | 32.0 | 46.0 |
ts_min¶
ts_min(x, period)
The smallest value in the window. Part of the window.
ts_quantile¶
ts_quantile(x, period, q[, min_periods])
The q-quantile of the window, q from 0 to 1, interpolating between the two nearest values: 0.5 is the median, 0
and 1 are ts_min and ts_max. Part of the window; min_periods changes the minimum.
-ts_quantile(returns(1), 252, 0.05) # 5% value-at-risk, as a loss
ts_quantile(returns(1), 60, 0.75) - ts_quantile(returns(1), 60, 0.25) # interquartile range
Lag and difference¶
delay¶
delay(x, period)
The value period rows ago; period ≥ 0. There is no negative delay: a value can only come from the past.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
delay(close(), 1)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 04 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 05 | 12.0 | 18.0 | · | 41.0 |
| Jan 08 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 09 | 13.0 | 17.0 | 32.0 | 44.0 |
delta¶
delta(x, period)
Today's value minus the value period rows ago: x - delay(x, period). Missing when either is.
Ranking and extremes¶
ts_rank¶
ts_rank(x, period)
Where today's value ranks among the stock's last period values, from 1/period (the lowest) to 1 (the highest);
ties share their average rank. The whole window: after C's missing day there is no rank until Jan 09.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
ts_rank(close(), 3)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | · | · | · | · |
| Jan 04 | 1.00 | 0.33 | · | 0.67 |
| Jan 05 | 0.50 | 0.67 | · | 1.00 |
| Jan 08 | 1.00 | 0.33 | · | 1.00 |
| Jan 09 | 1.00 | 0.33 | 0.67 | 1.00 |
ts_argmax¶
ts_argmax(x, period)
How many days ago the window's largest value was, 0 if it is today; on a tie, the oldest. The whole window. For "days since the latest event", use days_since.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
ts_argmax(close(), 3)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | · | · | · | · |
| Jan 04 | 0 | 2 | · | 1 |
| Jan 05 | 1 | 2 | · | 0 |
| Jan 08 | 0 | 1 | · | 0 |
| Jan 09 | 0 | 2 | 1 | 0 |
ts_argmin¶
ts_argmin(x, period)
How many days ago the window's smallest value was. The whole window.
Pairs of series¶
Regressions through time, one stock at a time
The pair operators work down each stock's own history: ts_beta(y, x, 252) fits one regression per stock, on
that stock's last 252 days, so every stock gets its own beta, alpha and residual. x is usually one series for
every stock, such as the market's return. For a regression across the stocks on one day, use
regression_neut;
Two kinds of regression sets them side by side.
The pair operators use only the days on which both series have a value. min_periods (default min(20, period)) is
the fewest such days a window needs: with the default, a stock 20 days after listing gets a "252-day" beta from 20
days. Ask for more when it matters.
ts_corr¶
ts_corr(x, y, period[, min_periods])
The correlation of x and y over the window. Part of the window. Over the window's \(n\) paired days,
ts_cov¶
ts_cov(x, y, period[, min_periods])
The covariance of x and y over the window. Part of the window.
ts_beta¶
ts_beta(y, x, period[, min_periods])
The least-squares slope of y on x over the window: the \(\beta\) of \(y_t = \alpha + \beta x_t + \varepsilon_t\), fitted
on the paired days. Part of the window.
ts_beta(returns(1), returns(1, 'market'), 252) # market beta
ts_beta(returns(1), returns(1, 'market'), 252, 126) # no value until 126 days are paired
ts_beta_shrunk¶
ts_beta_shrunk(y, x, period[, min_periods][, groups])
ts_beta, pulled toward the day's average beta in proportion to its own noise (Vasicek 1973). For stock \(i\), with
\(\beta_i\) its ts_beta from \(n_i\) paired days:
- \(\bar\beta\) and \(\tau^2\) are the mean and variance of the day's betas across the stocks in the universe: the prior.
- \(s_i^2\) is the sampling variance of stock \(i\)'s own estimate, from its residual variance \(\hat\sigma_i^2\):
This is the posterior mean of \(\beta_i\) under a normal prior \(N(\bar\beta, \tau^2)\). A precise estimate (\(s_i^2\) small against \(\tau^2\)) keeps its own beta; a beta from a short or noisy history moves most of the way to the average. Market Beta discusses when it helps.
A prior for each group. With groups (a group's name or any labels expression), \(\bar\beta\) and \(\tau^2\) are the
mean and variance of the betas in the stock's own group that day: a utility's beta is pulled toward the utilities',
not toward the market's average. A stock with no group has no value. A group with fewer than two betas that day has
no spread to shrink by, so its stocks keep their own betas.
How qex tells the two optional arguments apart: a number written as a number is min_periods, and anything else
(a quoted name, a labels expression) is groups. So groups comes last, after min_periods when there is one, and
min_periods is written out (126, not 252 / 2); qex refuses either mistake with a message that says which.
ts_beta_shrunk(returns(1), returns(1, 'market'), 252) # toward the day's average beta
ts_beta_shrunk(returns(1), returns(1, 'market'), 252, 'sector') # toward each sector's
ts_beta_shrunk(returns(1), returns(1, 'market'), 252, 126, 'sector') # and a value only from 126 paired days
ts_alpha¶
ts_alpha(y, x1, ..., xk, period[, min_periods])
The intercept of the same regression: the \(\alpha\) of \(y_t = \alpha + \beta x_t + \varepsilon_t\) over the window. A least-squares line goes through the two means, so over the window's paired days
Both means are over the days both series have a value, the same days as the beta. (Writing it by hand with two
ts_means gets this wrong whenever y has a gap in the window that x doesn't.) Part of the window, with
ts_beta's minimum. For a stock's daily return on the market's it is the daily alpha, Jensen's alpha without the
risk-free rate; times 252 for a year.
Several regressors. Give more than one x and it is one joint least-squares fit over the window's days where y
and every x have a value:
with the \(\beta_j\) solving the window's normal equations, \(\mathrm{cov}(x, x)\,\beta = \mathrm{cov}(x, y)\).
The alpha is net of all the regressors at once, which is not the same as taking them out one at a time when they are
correlated. A window needs at least \(k + 2\) such days (and min_periods); regressors that duplicate each other get
the minimum-norm fit. A multi-factor alpha, with factor returns supplied once per day for every stock
(Data Format):
The shrunk versions take one regressor: shrinking several betas at once needs a prior across them, which is a model of its own.
ts_alpha_shrunk¶
ts_alpha_shrunk(y, x, period[, min_periods][, groups])
The intercept that goes with ts_beta_shrunk's slope. For a fixed slope, the least-squares intercept is still the
line through the means:
It differs from ts_alpha by exactly what the shrinkage took off the beta, times the average of x:
In a rising market, a low beta shrunk up toward the average lowers the stock's alpha: part of what looked like alpha
was low beta. groups is as in ts_beta_shrunk.
ts_resid¶
ts_resid(y, x1, ..., xk, period[, min_periods])
Today's residual from the window's fit, with \(\alpha\) and the \(\beta_j\) from the window ending today:
The part of today's y that x doesn't explain: for a stock's return on the market's, its idiosyncratic return. No
value on a day y or x is missing. The fit includes today, as any rolling regression's last residual does; it
uses nothing after today.
ts_sum(ts_resid(returns(1), returns(1, 'market'), 252), 20) # last month, beyond beta
ts_resid(returns(1), field('mkt'), field('smb'), field('hml'), 252) # beyond three factors
ts_resid_shrunk¶
ts_resid_shrunk(y, x, period[, min_periods][, groups])
Today's residual from ts_beta_shrunk's fit: \(y_t - \alpha^{\text{shrunk}} - \beta^{\text{shrunk}} x_t\). groups is
as in ts_beta_shrunk.
-ts_sum(ts_resid_shrunk(returns(1), returns(1, 'market'), 252, 'sector'), 5) # idiosyncratic reversal
Exponentially weighted¶
A day's weight halves every halflife days, so the effective look-back is about three half-lives. There is no warm-up:
a value from the first observation, carried through missing days. With half-life \(h\), a value \(k\) days old has weight
ewma¶
ewma(x, halflife)
The exponentially weighted mean, over the days \(x\) has a value:
Below, C keeps its average through Jan 04.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
ewma(close(), 2)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.00 | 20.00 | 30.00 | 40.00 |
| Jan 03 | 10.59 | 19.41 | 30.29 | 41.17 |
| Jan 04 | 11.23 | 18.77 | 30.29 | 41.09 |
| Jan 05 | 11.14 | 18.67 | 30.67 | 41.84 |
| Jan 08 | 11.80 | 18.07 | 31.25 | 42.61 |
| Jan 09 | 12.54 | 17.38 | 31.34 | 43.74 |
ewma_var¶
ewma_var(x, halflife)
The exponentially weighted variance.
ewma_cov¶
ewma_cov(x, y, halflife)
The exponentially weighted covariance, over the days both have a value.
ts_beta_ewma¶
ts_beta_ewma(y, x, halflife)
The exponentially weighted version of ts_beta, over the days both have a value:
Shape of the window¶
decay_linear¶
decay_linear(x, period)
A weighted mean with weights period, period − 1, …, 1, the newest heaviest. The whole window. With period \(p\):
On Jan 04, A is (3 × 12 + 2 × 11 + 1 × 10) / 6.
close()| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | 10.0 | 20.0 | 30.0 | 40.0 |
| Jan 03 | 11.0 | 19.0 | 30.5 | 42.0 |
| Jan 04 | 12.0 | 18.0 | · | 41.0 |
| Jan 05 | 11.0 | 18.5 | 31.0 | 43.0 |
| Jan 08 | 13.0 | 17.0 | 32.0 | 44.0 |
| Jan 09 | 14.0 | 16.0 | 31.5 | 46.0 |
decay_linear(close(), 3)| A | B | C | D | |
|---|---|---|---|---|
| Jan 02 | · | · | · | · |
| Jan 03 | · | · | · | · |
| Jan 04 | 11.33 | 18.67 | · | 41.17 |
| Jan 05 | 11.33 | 18.42 | · | 42.17 |
| Jan 08 | 12.17 | 17.67 | · | 43.17 |
| Jan 09 | 13.17 | 16.75 | 31.58 | 44.83 |
ts_skew¶
ts_skew(x, period)
The skewness of the window. The whole window.
ts_kurt¶
ts_kurt(x, period)
The excess kurtosis of the window (0 for a normal distribution). The whole window.