Skip to content

Commit

Permalink
wrap tags in equation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoralez committed Dec 6, 2023
1 parent ac08a8f commit f597317
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
7 changes: 5 additions & 2 deletions action_files/clean_equations
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ 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']:
if cell['cell_type'] != 'markdown':
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

Expand Down
4 changes: 2 additions & 2 deletions nbs/docs/models/MultipleSeasonalTrend.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions nbs/docs/models/OptimizedTheta.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions nbs/docs/models/StandardTheta.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f597317

Please sign in to comment.