From f5973173d1442fa32e3056a4ae7b8e6bba4bebbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Morales?= Date: Wed, 6 Dec 2023 16:47:23 -0600 Subject: [PATCH] wrap tags in equation --- action_files/clean_equations | 7 +++++-- nbs/docs/models/MultipleSeasonalTrend.ipynb | 4 ++-- nbs/docs/models/OptimizedTheta.ipynb | 14 +++++++------- nbs/docs/models/StandardTheta.ipynb | 8 ++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/action_files/clean_equations b/action_files/clean_equations index 3126ef2a5..0a7a57a6b 100755 --- a/action_files/clean_equations +++ b/action_files/clean_equations @@ -4,7 +4,9 @@ from pathlib import Path from nbdev.clean import process_write -pat = re.compile(r'\$\s*([^\$]+?)\s*\$') +single_line_with_spaces = re.compile(r'\$\s*([^\$]+?)\s*\$') +with_tag_pat = re.compile(r'\$\$(.*?)\\tag\s*(\d+)(.*?)\$\$') +with_tag_repl = r'\n$$\n\\begin{equation}\n\1\\tag \2\n\\end{equation}\n$$\n' def clean_equations(nb): for cell in nb['cells']: @@ -12,7 +14,8 @@ def clean_equations(nb): continue lines = [] for i, line in enumerate(cell['source']): - line = pat.sub(r'$\1$', line) + line = single_line_with_spaces.sub(r'$\1$', line) + line = with_tag_pat.sub(with_tag_repl, line) lines.append(line) cell['source'] = lines diff --git a/nbs/docs/models/MultipleSeasonalTrend.ipynb b/nbs/docs/models/MultipleSeasonalTrend.ipynb index 731d25531..2b756caed 100644 --- a/nbs/docs/models/MultipleSeasonalTrend.ipynb +++ b/nbs/docs/models/MultipleSeasonalTrend.ipynb @@ -88,13 +88,13 @@ "Traditionally, the four variations have been assumed to be mutually independent from one another and specified by means of an additive decomposition model:\n", "\n", "\n", - "$$y_t= T_t +C_t +S_t +I_t, t=1,\\ \\cdots, n \\tag 1$$\n", + "\n$$\n\\begin{equation}\ny_t= T_t +C_t +S_t +I_t, t=1,\\ \\cdots, n \\tag 1\n\\end{equation}\n$$\n\n", "\n", "where $y_t$ denotes the observed series at time $t$, $T_t$ the long-term trend, $C_t$ the business cycle, $S_t$ seasonality, and $I_t$ the irregulars.\n", "\n", "If there is dependence among the latent components, this relationship is specified through a multiplicative model\n", "\n", - "$$y_t= T_t \\times C_t \\times S_t \\times I_t, t=1,\\ \\cdots, n \\tag 2$$\n", + "\n$$\n\\begin{equation}\ny_t= T_t \\times C_t \\times S_t \\times I_t, t=1,\\ \\cdots, n \\tag 2\n\\end{equation}\n$$\n\n", "\n", "where now $S_t$ and $I_t$ are expressed in proportion to the trend-cycle $T_t \\times C_t$ . In some cases, mixed additive-multiplicative models are used.\n", "\n", diff --git a/nbs/docs/models/OptimizedTheta.ipynb b/nbs/docs/models/OptimizedTheta.ipynb index 37a4aa376..c1368808e 100644 --- a/nbs/docs/models/OptimizedTheta.ipynb +++ b/nbs/docs/models/OptimizedTheta.ipynb @@ -97,10 +97,10 @@ "Assume that either the time series $Y_1, \\cdots Y_n$ is non-seasonal or it has been seasonally adjusted using the multiplicative classical decomposition approach. \n", "\n", "Let $X_t$ be the linear combination of two theta lines,\n", - "$$X_t=\\omega \\text{Z}_t (\\theta_1) +(1-\\omega) \\text{Z}_t (\\theta_2) \\tag 1$$\n", + "\n$$\n\\begin{equation}\nX_t=\\omega \\text{Z}_t (\\theta_1) +(1-\\omega) \\text{Z}_t (\\theta_2) \\tag 1\n\\end{equation}\n$$\n\n", "\n", "where $\\omega \\in [0,1]$ is the weight parameter. Assuming that $\\theta_1 <1$ and $\\theta_2 \\geq 1$, the weight $\\omega$ can be derived as\n", - "$$\\omega:=\\omega(\\theta_1, \\theta_2)=\\frac{\\theta_2 -1}{\\theta_2 -\\theta_1} \\tag 2$$\n", + "\n$$\n\\begin{equation}\n\\omega:=\\omega(\\theta_1, \\theta_2)=\\frac{\\theta_2 -1}{\\theta_2 -\\theta_1} \\tag 2\n\\end{equation}\n$$\n\n", "\n", "It is straightforward to see from Eqs. (1), (2) that $X_t=Y_t, \\ t=1, \\cdots n$ i.e., the weights are calculated properly in such a way that Eq. (1) reproduces the original series. \n", "\n", @@ -125,11 +125,11 @@ "\n", "The $h$ -step-ahead forecasts calculated at origin are given by\n", "\n", - "$$\\hat Y_{n+h|n} = (1-\\frac{1}{\\theta}) [\\text{A}_n+\\text{B}_n (n+h)]+ \\frac{1}{\\theta} \\tilde {\\text{Z}}_{n+h|n} (\\theta) \\tag 3$$\n", + "\n$$\n\\begin{equation}\n\\hat Y_{n+h|n} = (1-\\frac{1}{\\theta}) [\\text{A}_n+\\text{B}_n (n+h)]+ \\frac{1}{\\theta} \\tilde {\\text{Z}}_{n+h|n} (\\theta) \\tag 3\n\\end{equation}\n$$\n\n", "\n", "where $\\tilde {\\text{Z}}_{n+h|n} (\\theta)=\\tilde {\\text{Z}}_{n+1|n} (\\theta)=\\alpha \\sum_{i=0}^{n-1}(1-\\alpha)^i \\text{Z}_{n-i}(\\theta)+(1-\\alpha)^n \\ell_{0}^{*}$ is the extrapolation of $\\text{Z}_t(\\theta)$ by an SES model with $\\ell_{0}^{*} \\in \\mathbb{R}$ as the initial level parameter and $\\alpha \\in (0,1)$ as the smoothing parameter. Note that for $\\theta=2$ Eq. (3) corresponds to Step 4 of the STheta algorithm. After some algebra, we can write\n", "\n", - "$$\\tilde {\\text{Z}}_{n+1|n} (\\theta)=\\theta \\ell{n}+(1-\\theta) \\{ \\text{A}_n [1-(1-\\alpha)^n] + \\text{B}_n [n+(1-\\frac{1}{\\alpha}) [1-(1-\\alpha)^n] ] \\} \\tag 4$$\n", + "\n$$\n\\begin{equation}\n\\tilde {\\text{Z}}_{n+1|n} (\\theta)=\\theta \\ell{n}+(1-\\theta) \\{ \\text{A}_n [1-(1-\\alpha)^n] + \\text{B}_n [n+(1-\\frac{1}{\\alpha}) [1-(1-\\alpha)^n] ] \\} \\tag 4\n\\end{equation}\n$$\n\n", "\n", "where $\\ell_{t}=\\alpha Y_t +(1-\\alpha) \\ell_{t-1}$ for $t=1, \\cdots, n$ and $\\ell_{0}=\\ell_{0}^{*}/\\theta$.\n", "\n", @@ -144,11 +144,11 @@ "\n", "Let $\\text{A}_n$ and $\\text{B}_n$ be fixed coefficients for all $t=1, \\cdots, n$ so that Eqs. (3), (4) configure the state space model given by\n", "\n", - "$$Y_t=\\mu_{t}+\\varepsilon_{t} \\tag 5$$ \n", + "\n$$\n\\begin{equation}\nY_t=\\mu_{t}+\\varepsilon_{t} \\tag 5\n\\end{equation}\n$$\n \n", "\n", - "$$\\mu_{t}=\\ell_{t-1}+(1-\\frac{1}{\\theta}) \\{(1-\\alpha)^{t-1} \\text{A}_n +[\\frac{1-(1-\\alpha)^t}{\\alpha} \\text{B}_n] \\tag 6 \\}$$\n", + "\n$$\n\\begin{equation}\n\\mu_{t}=\\ell_{t-1}+(1-\\frac{1}{\\theta}) \\{(1-\\alpha)^{t-1} \\text{A}_n +[\\frac{1-(1-\\alpha)^t}{\\alpha} \\text{B}_n] \\tag 6\n\\end{equation}\n$$\n\n", "\n", - "$$\\ell_{t}=\\alpha Y_t +(1-\\alpha)\\ell_{t-1} \\tag 7$$\n", + "\n$$\n\\begin{equation}\n\\ell_{t}=\\alpha Y_t +(1-\\alpha)\\ell_{t-1} \\tag 7\n\\end{equation}\n$$\n\n", "\n", "with parameters $\\ell_{0} \\in \\mathbb{R}$, $\\alpha \\in (0,1)$ and $\\theta \\in [1,\\infty)$ . The parameter $\\theta$ is to be estimated along with $\\alpha$ and $\\ell_{0}$ We call this the optimised Theta model (OTM).\n", "\n", diff --git a/nbs/docs/models/StandardTheta.ipynb b/nbs/docs/models/StandardTheta.ipynb index 577300e80..040ba0212 100644 --- a/nbs/docs/models/StandardTheta.ipynb +++ b/nbs/docs/models/StandardTheta.ipynb @@ -79,17 +79,17 @@ "\n", "Assimakopoulos and Nikolopoulo for standard theta model proposed the Theta line as the solution of the equation\n", "\n", - "$$D^2 \\zeta_t(\\theta) = \\theta D^2 Y_t, t = 1,\\cdots,T \\tag 1$$\n", + "\n$$\n\\begin{equation}\nD^2 \\zeta_t(\\theta) = \\theta D^2 Y_t, t = 1,\\cdots,T \\tag 1\n\\end{equation}\n$$\n\n", "\n", "where $Y_1, \\cdots , Y_T$ represent the original time series data and $DX_t = (X_t − X_{t−1})$. The initial values $\\zeta_1$ and $\\zeta_2$ are obtained by minimizing $\\sum_{i=1}^{T} [Y_t - \\zeta_t (\\theta) ]^2$. However, the analytical solution of (1) is given by \n", "\n", - "$$\\zeta_t(\\theta)=\\theta Y_t +(1−\\theta)(A_T +B_T t),\\ t=1, \\cdots, T, \\tag 2$$\n", + "\n$$\n\\begin{equation}\n\\zeta_t(\\theta)=\\theta Y_t +(1−\\theta)(A_T +B_T t),\\ t=1, \\cdots, T, \\tag 2\n\\end{equation}\n$$\n\n", "\n", "where $A_T$ and $B_T$ are the minimum square coefficients of a simple linear regression over $Y_1, \\cdots,Y_T$ against $1, \\cdots , T$ which are only dependent on the original data and given as follow\n", "\n", - "$$A_T=\\frac{1}{T} \\sum_{i=1}^{T} Y_t - \\frac{T+1}{2} B_T \\tag 3$$\n", + "\n$$\n\\begin{equation}\nA_T=\\frac{1}{T} \\sum_{i=1}^{T} Y_t - \\frac{T+1}{2} B_T \\tag 3\n\\end{equation}\n$$\n\n", "\n", - "$$B_T=\\frac{6}{T^2 - 1} (\\frac{2}{T} \\sum_{t=1}^{T} tY_t - \\frac{T+1}{T} \\sum_{t=1}^{T} Y_t \\tag 4)$$\n", + "\n$$\n\\begin{equation}\nB_T=\\frac{6}{T^2 - 1} (\\frac{2}{T} \\sum_{t=1}^{T} tY_t - \\frac{T+1}{T} \\sum_{t=1}^{T} Y_t \\tag 4\n\\end{equation}\n$$\n\n", "\n", "Theta lines can be understood as functions of the linear regression model directly applied to the data from this perspective. Indeed, the Theta method’s projections for h steps ahead are an ad hoc combination (50 percent - 50 percent) of the linear extrapolations of $\\zeta(0)$ and $\\zeta(2)$.\n", "\n",