# 时间序列的单调性

### 连续函数的单调性

$f'(x_{0}) = \lim_{x\rightarrow x_{0}}\frac{f(x)-f(x_{0})}{x-x_{0}}.$

### 时间序列的单调性

$\sum_{m=k}^{j}w_{m}x_{m} > \sum_{m=i}^{k-1} w_{m}x_{m},$ 其中$\sum_{m=k}^{j}w_{m} = \sum_{m=i}^{k-1}w_{m}.$

$\sum_{m=k}^{j}w_{m}x_{m} < \sum_{m=i}^{k-1} w_{m}x_{m},$ 其中 $\sum_{m=k}^{j}w_{m} = \sum_{m=i}^{k-1}w_{m}.$

### 时间序列的单调性 — 均线方法

$(x_{i+k}-x_{i})/((i+k)-i) = (x_{i+k}-x_{i})/k,$
$(x_{i} - x_{i-k})/(i-(i-k)) = (x_{i} -x_{i-k})/k.$

#### 简单的移动平均算法

$M_{w}(n) = \frac{x_{n-w+1}+\cdots+x_{n}}{w} = \frac{\sum_{j=n-w+1}^{n}x_{j}}{w},$

$M_{s}(n) > M_{\ell}(n)$
$\Leftrightarrow \frac{\sum_{j=n-s+1}^{n}x_{j}}{s} > \frac{\sum_{j=n-\ell+1}^{n}x_{j}}{\ell} = \frac{\sum_{j=n-\ell+1}^{n-s}x_{j} + \sum_{j=n-s+1}^{n}x_{j}}{\ell}$
$\Leftrightarrow M_{s}(n)=\frac{\sum_{j=n-s+1}^{n}x_{j}}{s} > \frac{\sum_{j=n-\ell+1}^{n-s}x_{j}}{\ell-s} = M_{\ell-s}(n-s),$

#### 带权重的移动平均算法

$WMA_{w}(n) = \frac{x_{n-w+1}+2\cdot x_{n-w+2}+\cdots + w\cdot x_{n}}{1+2+\cdots+w} = \frac{\sum_{j=1}^{w}j \cdot x_{n-w+j}}{w\ \cdot (w+1)/2}.$

$WMA_{s}(n) > WMA_{\ell}(n)$
$\Leftrightarrow \frac{\sum_{j=1}^{s} j \cdot x_{n-s+j}}{s\cdot(s+1)/2} > \frac{\sum_{j=1}^{\ell}j\cdot x_{n-\ell +j}}{\ell\cdot(\ell+1)/2} = \frac{\sum_{j=1}^{\ell-s}j\cdot x_{n-\ell+s} + \sum_{j=\ell -s + 1}^{\ell}j\cdot x_{n-\ell + j}}{\ell\cdot(\ell+1)/2}$
$\Leftrightarrow \frac{\sum_{j=1}^{s} j \cdot x_{n-s+j}}{s\cdot(s+1)/2} > \frac{\sum_{j=1}^{\ell-s}j\cdot x_{n-\ell+s} + \sum_{j=1}^{s}(j+\ell-s)\cdot x_{n- s + j}}{\ell\cdot(\ell+1)/2}$
$\Leftrightarrow \sum_{j=1}^{s}\bigg(\frac{j}{s\cdot(s+1)/2} - \frac{j+\ell -s}{\ell\cdot(\ell+1)/2} \bigg) \cdot x_{n-s+j} > \frac{\sum_{j=1}^{\ell-s}j\cdot x_{n-\ell+j}}{\ell\cdot(\ell+1)/2}$
$\Leftrightarrow \sum_{j=j_{0}}^{s}\bigg(\frac{j}{s\cdot(s+1)/2} - \frac{j+\ell -s}{\ell\cdot(\ell+1)/2} \bigg) \cdot x_{n-s+j} > \frac{\sum_{j=1}^{\ell-s}j\cdot x_{n-\ell+j}}{\ell\cdot(\ell+1)/2}$
$+ \sum_{j=1}^{j_{0}-1} \bigg(\frac{j+\ell -s}{\ell\cdot(\ell+1)/2}- \frac{j}{s\cdot(s+1)/2}\bigg) \cdot x_{n-s+j},$

$\frac{j}{s\cdot(s+1)/2} - \frac{j+\ell -s}{\ell\cdot(\ell+1)/2} \geq 0 \Leftrightarrow j \geq \frac{s\cdot(s+1)}{\ell+s-1},$

$\sum_{j=j_{0}}^{s}\bigg(\frac{j}{s\cdot(s+1)/2} - \frac{j+\ell -s}{\ell\cdot(\ell+1)/2} \bigg) = \frac{\sum_{j=1}^{\ell-s}j}{\ell\cdot(\ell+1)/2} + \sum_{j=1}^{j_{0}-1} \bigg(\frac{j+\ell -s}{\ell\cdot(\ell+1)/2}- \frac{j}{s\cdot(s+1)/2}\bigg)$
$\Leftrightarrow \sum_{j=1}^{s}\bigg(\frac{j}{s\cdot(s+1)/2} - \frac{j+\ell -s}{\ell\cdot(\ell+1)/2} \bigg) = \frac{\sum_{j=1}^{\ell-s}j}{\ell\cdot(\ell+1)/2},$

#### 指数移动平均算法

$\text{EWMA}(\alpha, i) = x_{1}, \text{ when } i = 1,$
$\text{EWMA}(\alpha, i) = \alpha \cdot x_{i} + (1-\alpha) \cdot \text{EWMA}(\alpha, i-1), \text{ when } i \geq 2,$

$\text{EWMA}(\alpha, i) = \alpha x_{i} + \alpha(1-\alpha) x_{i-1} + \cdots \alpha(1-\alpha)^{k}x_{i-k} + (1-\alpha)^{k+1}\text{EWMA}(\alpha, t-(k+1)).$

$\text{EWMA}_{s}(\alpha, n) = \alpha x_{n} + \alpha(1-\alpha) x_{n-1} + \cdots + \alpha(1-\alpha)^{s-2}x_{n-s+2} + (1-\alpha)^{s-1}x_{n-s+1},$
$\text{EWMA}_{\ell}(\beta, n) = \beta x_{n} + \beta(1-\beta) x_{n-1} + \cdots + \beta(1-\beta)^{\ell-2}x_{n-\ell+2} + (1-\beta)^{\ell-1}x_{n-\ell+1}.$

$s=1$ 时，$\text{EWMA}_{s}(\alpha,n) = x_{n}$。那么

$\text{EWMA}_{s}(\alpha, n) > \text{EWMA}_{\ell}(\beta,n)$
$\Leftrightarrow x_{n} > \beta x_{n} + \beta(1-\beta) x_{n-1} + \cdots + \beta(1-\beta)^{\ell-2}x_{n-\ell+2} + (1-\beta)^{\ell-1}x_{n-\ell+1}$
$\Leftrightarrow x_{n} > \beta x_{n-1} + \cdots + \beta(1-\beta)^{\ell-3}x_{n-\ell+2}+ (1-\beta)^{\ell-2}x_{n-\ell+1}.$

$s\geq 2$ 时，根据假设有 $0<\beta<\alpha<1/(s-1)$，并且

$\text{EWMA}_{s}(\alpha, n) = \alpha x_{n} + \alpha(1-\alpha) x_{n-1} + \cdots + \alpha(1-\alpha)^{s-2}x_{n-s+2} + (1-\alpha)^{s-1}x_{n-s+1},$
$\text{EWMA}_{\ell}(\beta, n) = \beta x_{n} + \beta(1-\beta) x_{n-1} + \cdots + \beta(1-\beta)^{\ell-2}x_{n-\ell+2} + (1-\beta)^{\ell-1}x_{n-\ell+1}$
$= \beta x_{n} + \beta(1-\beta) x_{n-1} + \cdots + \beta(1-\beta)^{s-2}x_{n-s+2} + \beta(1-\beta)^{s-1}x_{n-s+1}$
$+ \beta(1-\beta)^{s}x_{n-s} + \cdots + (1-\beta)^{\ell-1}x_{n-\ell+1}.$

$\alpha > \beta,$
$\alpha(1-\alpha) > \beta(1-\beta),$
$\cdots$
$\alpha(1-\alpha)^{s-2} > \beta(1-\beta)^{s-2}.$

$(\alpha -\beta)x_{n} +\cdots + (\alpha(1-\alpha)^{s-2}-\beta(1-\beta)^{s-2})x_{n-s+2} + ((1-\alpha)^{s-1}-\beta(1-\beta)^{s-1})x_{n-s+1}$
$> \beta(1-\beta)^{s}x_{n-s} +\cdots + (1-\beta)^{\ell-1}x_{n-\ell+1},$

$(\alpha -\beta)x_{n} + \cdots + (\alpha(1-\alpha)^{s-2}-\beta(1-\beta)^{s-2})x_{n-s+2}$
$> (\beta(1-\beta)^{s-1} - (1-\alpha)^{s-1})x_{n-s+1} + \beta(1-\beta)^{s}x_{n-s} +\cdots + (1-\beta)^{\ell-1}x_{n-\ell+1},$

### 时间序列的单调性 — 带状方法

#### $3-\sigma$ 控制图

$\mu = \frac{x_{1}+\cdots+x_{n}}{n},$
$\sigma^{2} = \frac{(x_{1}-\mu)^{2}+\cdots+(x_{n}-\mu)^{2}}{n}.$

$\text{UCL} = \mu + L \cdot \sigma,$
$\text{Center Line} = \mu,$
$\text{LCL} = \mu - L \cdot \sigma,$

#### Moving Average 控制图

$M_{w}(n) = \frac{x_{n-w+1}+\cdots + x_{n}}{w} = \frac{\sum_{j=n-w+1}^{n}x_{j}}{w}.$

$V(M_{w}) = \frac{1}{w^{2}}\sum_{j=n-w+1}^{n} V(x_{j}) = \frac{1}{w^{2}}\sum_{j=n-w+1}^{n}\sigma^{2} = \frac{\sigma^{2}}{w}.$

$\text{UCL} = \mu + L\cdot \frac{\sigma}{\sqrt{w}},$
$\text{Center Line} = \mu,$
$\text{LCL} = \mu - L \cdot \frac{\sigma}{\sqrt{w}},$

#### EWMA 控制图

$z_{i} = x_{1}, \text{ when } i=1,$
$z_{i} = \lambda x_{i} + (1-\lambda) z_{i-1}, \text{ when } i\geq 2.$

$\sigma_{z_{i}}^{2}= \lambda^{2} \sigma^{2} + (1-\lambda)^{2} \sigma_{z_{i-1}}^{2},$

$\sigma_{z_{i}}^{2} = \frac{\lambda^{2}}{1-(1-\lambda)^{2}} \sigma^{2} \Rightarrow \sigma_{z_{i}} = \sqrt{\frac{\lambda}{2-\lambda}}\sigma.$

$\text{UCL} = \mu + L\sigma\sqrt{\frac{\lambda}{2-\lambda}},$
$\text{Center Line} = \mu,$
$\text{LCL} = \mu - L\sigma\sqrt{\frac{\lambda}{2-\lambda}},$

### 时间序列的单调性 — 柱状方法

#### MACD 方法

MACD 算法是比较常见的用于判断时间序列单调性的方法，它的大致思路分成以下几步：

• 根据长短窗口分别计算两条指数移动平均线(EWMA short, EWMA long)；
• 计算两条指数移动平均线之间的距离，作为离差值(DIF)；
• 计算离差值(DIF)的指数移动平均线，作为DEA；
• 将 (DIF-DEA) * 2 作为 MACD 柱状图。

$\text{EWMA}_{s}(\alpha, n) = (1-\alpha) \cdot \text{EWMA}_{s}(\alpha, n-1) + \alpha \cdot x_{n},$
$\text{EWMA}_{\ell}(\beta,n) = (1-\beta) \cdot \text{EWMA}_{\ell}(\beta, n-1) + \beta \cdot x_{n},$

$\alpha = \frac{2}{s+1} = \frac{2}{13},$
$\beta = \frac{2}{\ell+1} = \frac{2}{27}.$

$\text{DIF}(n) = \text{EWMA}_{s}(\alpha, n) - \text{EWMA}_{\ell}(\beta,n).$

$\gamma = 2 / (signal + 1)$，计算 DEA 如下：

$\text{DEA}(\gamma, n) = \gamma * \text{DIF}(n) + (1-\gamma) * \text{DEA}(\gamma, n).$

$\text{MACD}(n) = (\text{DIF}(n) - \text{DEA}(\gamma, n)) * 2.$

• 当 DIF(n) 与 DEA(n) 都大于零时，表示时间序列有上涨的趋势；
• 当 DIF(n) 与 DEA(n) 都小于零时，表示时间序列有递减的趋势；
• 当 DIF(n) 下穿 DEA(n) 时，此时 MACD(n) 小于零，表示时间序列有下跌的趋势；
• 当 DIF(n) 上穿 DEA(n) 时，此时 MACD(n) 大于零，表示时间序列有上涨的趋势；
• MACD(n) 附近的向上或者向下的面积，可以作为时间序列上涨或者下跌幅度的标志。

PS：算法可以从指数移动平均算法换成移动平均算法或者带权重的移动平均算法，长短线的周期可以不局限于 26 和 12，信号线的周期也不局限于 9。

### 参考资料

1. Moving Average：https://en.wikipedia.org/wiki/Moving_average