\documentclass{standalone}

% Tikz
\usepackage{ifthen}
\usepackage{intcalc}
\usepackage{tikz}
\usepackage{multirow}
\usetikzlibrary{external}
\usetikzlibrary{decorations.pathreplacing,calligraphy}
\usetikzlibrary{arrows.meta, decorations.text, decorations.markings, decorations.pathreplacing, angles, quotes, calligraphy}
\definecolor{bittersweet}{rgb}{1.0, 0.44, 0.37}

% Logisig definitions
% Log-sig heights
\pgfmathsetmacro{\pathyshift}{0.8}
\global\def\rs{{0, 1, 2.1, 3.3, 4.5, 5.5, 6.5}}
\global\def\lsone{{2.15, 2.3, 2.1, 2.4, 2.1, 2.4}}
\global\def\lstwo{{2.1, 2.4, 2.2, 2.5, 2.2, 2.1}}
\global\def\lsthree{{2.3, 2.1, 2.15, 2.35, 2.5, 2.2}}
\global\def\lspathraise{0.7}
\global\def\data{{0.4, 0.7, 0.9, 0.5, 0.6, 0.9, 0.8, 0.5, 0.3, 0.3, 0.8, 1.0, 0.2, 0.4, 0.7, 0.5, 1.0, 1.0, 0.8, 0.4, 0.2, 0.6, 0.8, 0.5, 0.3, 0.5, 0.2, 0.1, 0.5, 0.2, 0.4, 0.6, 0.3, 0.2, 0.1, 0.7, 0.6, 0.2, 0.5, 0.4, 0.4, 0.1}}

% Logsig
\definecolor{colorls1}{HTML}{66C2A5}
\definecolor{colorls2}{HTML}{8DA0CB}
\definecolor{colorls3}{HTML}{FC8D62}

\newcommand\ncdediagram[1]{
    \begin{tikzpicture}
        % Choose between the logsig and normal version
        \pgfmathsetmacro{\logsigversion}{#1}

        % For reducing the height of the hidden state and integration steps in the two cases
        \ifthenelse{\logsigversion=0}
            {\pgfmathsetmacro{\hreduce}{1.8}}
            {\pgfmathsetmacro{\hreduce}{0}}

        % Time
        \draw[black, thick, ->] (0, 0) -- (7, 0);

        \ifthenelse{\logsigversion=0}
            {
                \foreach \i in {0, 6} {
                    \node at (\rs[\i], 0)[circle,fill, inner sep=1.5pt] {};
                }
                \node at (\rs[0], -0.25) {\tiny $t_0$};
                \node at (\rs[6], -0.25) {\tiny $t_m$};
            }
            {
                \foreach \i in {0, 1, 2, 3, 4, 5, 6} {
                    \node at (\rs[\i], 0)[circle,fill, inner sep=1.5pt] {};
                }
                \node at (\rs[0], -0.25) {\tiny $r_0$};
                \node at (\rs[1], -0.25) {\tiny $r_1$};
                \node at (\rs[2], -0.25) {\tiny $r_2$};
                \node at (\rs[3], -0.25) {\tiny $r_3$};
                \node at (\rs[4], -0.25) {\tiny $r_{m-2}$};
                \node at (\rs[5], -0.25) {\tiny $r_{m-1}$};
                \node at (\rs[6], -0.25) {\tiny $r_m$};
            }
        \node at (3.85, -0.25) {\tiny $\cdots$};




        \pgfmathsetmacro{\dataix}{0}
        \pgfmathsetmacro{\midwayprev}{0}
        \pgfmathsetmacro{\xprev}{0}
        \pgfmathsetmacro{\yprev}{0}
        \foreach \i in {0, 1, 2, 3, 4, 5} {
            % Macros
            \pgfmathsetmacro{\frac}{\rs[\i + 1] - \rs[\i]}
            \pgfmathsetmacro{\midway}{\rs[\i] + (\rs[\i + 1] - \rs[\i]) * 0.5}

            % Data and path
            \foreach \d in {0, 1, 2, 3, 4, 5, 6} {
                \ifthenelse{\i>0 \AND \d=0}{\pgfmathsetmacro{\y}{\data[\dataix - 1] * 0.5 + 0.1}}{\pgfmathsetmacro{\y}{\data[\dataix] * 0.5 + 0.1}}
                \pgfmathsetmacro{\x}{\rs[\i] + \d * \frac * 0.1667}
                \pgfmathsetmacro{\dx}{\frac * 0.1667}
                \ifthenelse{\i=3}
                    {
                        % Data
                        \node at (\x, \y)[circle, draw=black, fill=green!50, inner sep=1.5pt, opacity=0.3] {};
                        % Path
                        \node at (\x, \y + \pathyshift)[circle, draw=black, fill=green!50, inner sep=0.5pt, densely dashed] {};
                        \draw[green!50, thick, cap=round, dashed] (\xprev, \yprev + \pathyshift) -- (\x, \y + \pathyshift);
                    }
                    {
                        % Time tick
                        \ifthenelse{\logsigversion=0}{\draw (\x, -0.05) -- (\x, 0.05);}{}
                        % Data
                        \node at (\x, \y)[circle, draw=black, fill=green!50, inner sep=1.5pt] {};
                        % Path
                        \node at (\x, \y + \pathyshift)[circle, draw=black, fill=green!50, inner sep=0.5pt] {};
                        \ifthenelse{\i=0 \AND \d=0}{}{\draw[green!50, thick, cap=round] (\xprev, \yprev + \pathyshift) -- (\x, \y + \pathyshift);}
                        % Integration steps
                        \ifthenelse{\logsigversion=0}
                            {
                                \ifthenelse{\d=6 \AND \i=5}
                                    {}
                                    {
                                        \node at (\x, 3.8 - \hreduce)[circle, draw=black, fill=orange!50, inner sep=1.5pt] {};
                                    }
                            }
                            {}
                    }
                % Update globals
                \pgfmathsetmacro{\dataixnew}{\dataix + 1}
                \global\let\dataix=\dataixnew
                \global\let\yprev=\y
                \global\let\xprev=\x

            }

            \ifthenelse{\logsigversion=1}{
                \ifthenelse{\i=3}
                    {
                        % Integration steps
                        \node at (\rs[\i], 3.8)[circle, draw=black, fill=orange!50, inner sep=1.5pt] {};
                        \draw[->, dashed] (\rs[\i] + 0.08, 3.8) -- (\rs[\i+1] - 0.08, 3.8);
                        % % Brace
                        % \draw[decoration={calligraphic brace, amplitude=3pt}, decorate, line width=1pt, dotted] (\rs[\i] + 0.05, 1.65) node {} -- (\rs[\i+1] - 0.05, 1.65);
                        % % Brace arrow
                        % \draw[->, dashed] (\midway, 1.8) -- (\midway, 2.0);
                        % % Log-signatures
                        % \node at (\midway, \lsone[\i])[circle, draw=black, fill=colorls1, inner sep=1pt, opacity=0.3] {};
                        % \node at (\midway, \lstwo[\i])[circle, draw=black, fill=colorls2, inner sep=1pt, opacity=0.3] {};
                        % \node at (\midway, \lsthree[\i])[circle, draw=black, fill=colorls3, inner sep=1pt, opacity=0.3] {};
                    }
                    {
                        % Brace
                        \draw[decoration={calligraphic brace, amplitude=3pt}, decorate, line width=1pt] (\rs[\i] + 0.05, 1.65) node {} -- (\rs[\i+1] - 0.05, 1.65);
                        % Brace arrow
                        \draw[->, dashed] (\midway, 1.8) -- (\midway, 2.0);
                        % Log-signatures
                        \node at (\midway, \lsone[\i])[circle, draw=black, fill=colorls1, inner sep=1pt] {};
                        \node at (\midway, \lstwo[\i])[circle, draw=black, fill=colorls2, inner sep=1pt] {};
                        \node at (\midway, \lsthree[\i])[circle, draw=black, fill=colorls3, inner sep=1pt] {};
                        % Integration steps
                        \node at (\rs[\i], 3.8)[circle, draw=black, fill=orange!50, inner sep=1.5pt] {};
                        \draw[->] (\rs[\i] + 0.08, 3.8) -- (\rs[\i+1] - 0.08, 3.8);
                    }
                % Path raise
                \ifthenelse{\i=3}
                    {

                    }
                    {
                        \node at (\midway, \lsone[\i] + \lspathraise)[circle, draw=black, fill=colorls1, inner sep=.5pt] {};
                        \node at (\midway, \lstwo[\i] + \lspathraise)[circle, draw=black, fill=colorls2, inner sep=.5pt] {};
                        \node at (\midway, \lsthree[\i] + \lspathraise)[circle, draw=black, fill=colorls3, inner sep=.5pt] {};
                    }
                % Log-sig path
                \ifthenelse{\i>0}
                    {
                        % Horrendous code to dash out the middle logsig bit
                        \ifthenelse{\i=3 \OR \i=4}
                        {
                            \ifthenelse{\i=3}
                                {
                                    \pgfmathsetmacro{\midmidway}{\midwayprev + (\midway - \midwayprev) * 0.5}
                                    \draw[colorls1, thick, cap=round] (\midwayprev, \lsone[\i-1] + \lspathraise) -- (\midmidway, \lsone[\i-1]*0.5 + \lsone[\i]*0.5 + \lspathraise);
                                    \draw[colorls1, thick, cap=round, densely dashed] (\midmidway, \lsone[\i-1]*0.5 + \lsone[\i]*0.5 + \lspathraise) -- (\midway, \lsone[\i] + \lspathraise);
                                    \draw[colorls2, thick, cap=round] (\midwayprev, \lstwo[\i-1] + \lspathraise) -- (\midmidway, \lstwo[\i-1]*0.5 + \lstwo[\i]*0.5 + \lspathraise);
                                    \draw[colorls2, thick, cap=round, densely dashed] (\midmidway, \lstwo[\i-1]*0.5 + \lstwo[\i]*0.5 + \lspathraise) -- (\midway, \lstwo[\i] + \lspathraise);
                                    \draw[colorls3, thick, cap=round] (\midwayprev, \lsthree[\i-1] + \lspathraise) -- (\midmidway, \lsthree[\i-1]*0.5 + \lsthree[\i]*0.5 + \lspathraise);
                                    \draw[colorls3, thick, cap=round, densely dashed] (\midmidway, \lsthree[\i-1]*0.5 + \lsthree[\i]*0.5 + \lspathraise) -- (\midway, \lsthree[\i] + \lspathraise);
                                }
                                {
                                    \pgfmathsetmacro{\midmidway}{\midwayprev + (\midway - \midwayprev) * 0.5}
                                    \draw[colorls1, thick, cap=round, densely dashed] (\midwayprev, \lsone[\i-1] + \lspathraise) -- (\midmidway, \lsone[\i-1]*0.5 + \lsone[\i]*0.5 + \lspathraise);
                                    \draw[colorls1, thick, cap=round] (\midmidway, \lsone[\i-1]*0.5 + \lsone[\i]*0.5 + \lspathraise) -- (\midway, \lsone[\i] + \lspathraise);
                                    \draw[colorls2, thick, cap=round, densely dashed] (\midwayprev, \lstwo[\i-1] + \lspathraise) -- (\midmidway, \lstwo[\i-1]*0.5 + \lstwo[\i]*0.5 + \lspathraise);
                                    \draw[colorls2, thick, cap=round] (\midmidway, \lstwo[\i-1]*0.5 + \lstwo[\i]*0.5 + \lspathraise) -- (\midway, \lstwo[\i] + \lspathraise);
                                    \draw[colorls3, thick, cap=round, densely dashed] (\midwayprev, \lsthree[\i-1] + \lspathraise) -- (\midmidway, \lsthree[\i-1]*0.5 + \lsthree[\i]*0.5 + \lspathraise);
                                    \draw[colorls3, thick, cap=round] (\midmidway, \lsthree[\i-1]*0.5 + \lsthree[\i]*0.5 + \lspathraise) -- (\midway, \lsthree[\i] + \lspathraise);
                                }
                        }
                        {
                            \draw[colorls1, thick, cap=round] (\midwayprev, \lsone[\i-1] + \lspathraise) -- (\midway, \lsone[\i] + \lspathraise);
                            \draw[colorls2, thick, cap=round] (\midwayprev, \lstwo[\i-1] + \lspathraise) -- (\midway, \lstwo[\i] + \lspathraise);
                            \draw[colorls3, thick, cap=round] (\midwayprev, \lsthree[\i-1] + \lspathraise) -- (\midway, \lsthree[\i] + \lspathraise);
                        }
                    }
                    {
                        \draw[colorls1, thick, cap=round] (\midwayprev, \lsone[\i] + \lspathraise) -- (\midway, \lsone[\i] + \lspathraise);
                        \draw[colorls2, thick, cap=round] (\midwayprev, \lstwo[\i] + \lspathraise) -- (\midway, \lstwo[\i] + \lspathraise);
                        \draw[colorls3, thick, cap=round] (\midwayprev, \lsthree[\i] + \lspathraise) -- (\midway, \lsthree[\i] + \lspathraise);
                    }
                \ifthenelse{\i=5}
                    {
                        \draw[colorls1, thick, cap=round] (\midway, \lsone[\i] + \lspathraise) -- (\rs[\i+1], \lsone[\i] + \lspathraise);
                        \draw[colorls2, thick, cap=round] (\midway, \lstwo[\i] + \lspathraise) -- (\rs[\i+1], \lstwo[\i] + \lspathraise);
                        \draw[colorls3, thick, cap=round] (\midway, \lsthree[\i] + \lspathraise) -- (\rs[\i+1], \lsthree[\i] + \lspathraise);
                    }
                    {}
                }
                {
                }

            % Update globals
            \global\let\midwayprev=\midway
        }

        % Integration step arrow for non logsig
        \ifthenelse{\logsigversion=1}{}{\draw[->, dashed] (\rs[3] + 0.08, 3.8 - \hreduce) -- (\rs[4] - 0.08, 3.8 - \hreduce);}

        % Hidden state
        \draw[blue!50, thick, cap=round] (\rs[0], 4.8 - \hreduce) .. controls (0.5, 3.5 - \hreduce) and (2, 5.3 - \hreduce) .. (\rs[3], 4.8 - \hreduce);
        \draw[blue!50, thick, cap=round, dashed] (\rs[3], 4.8 - \hreduce) .. controls (\rs[3] + 0.2, 4.7 - \hreduce) and (\rs[4] - 0.2, 4.3 - \hreduce) .. (\rs[4], 4.3 - \hreduce);
        \draw[blue!50, thick, cap=round] (\rs[4], 4.3 - \hreduce) .. controls (\rs[4] + 0.3, 4.2 - \hreduce) and (\rs[6] - 0.4, 4.4 - \hreduce) .. (\rs[6], 4.7 - \hreduce);

        % Middle dots
        \ifthenelse{\logsigversion=1}
            {\node at (4, 1.7) {$\cdots$};}
            {}

        % RHS naming
        \node at (8, 0) {\footnotesize Time};
        \node (data) at (8, 0.4) {\footnotesize Data $\mathbf{x}$};
        \node (path) at (8, 1.15) {\footnotesize Path $X$};
        \ifthenelse{\logsigversion=1}{
            \node (logsig) at (8, 2.1) {\footnotesize $\mathrm{LogSig}_{r_i, r_{i+1}}(X)$};
            \node (logsig_path) at (8, 2.95) {\footnotesize Logsignature path};
        }{}
        \node (hidden) at (8, 4.7 - \hreduce) {\footnotesize Hidden state $Z_t$};

        % Arrows
        \draw[->] (data) -- (path);
        \ifthenelse{\logsigversion=1}
            {
                \draw[->] (path) -- (logsig);
                \draw[->] (logsig) -- (logsig_path);
                \draw [decorate, decoration={calligraphic brace,amplitude=1.5pt, mirror}] (6.5,3.6) -- node[midway, right, xshift=-6.5pt] {\tiny \begin{tabular}{l}Integration\\steps\end{tabular}} (6.5,4 - \hreduce);
                \draw[->] (logsig_path) -- (hidden);
            }
            {
                \draw [decorate, decoration={calligraphic brace,amplitude=1.5pt, mirror}] (6.5, 3.6 - \hreduce) -- node[midway, right, xshift=-5.5pt] {\scriptsize \begin{tabular}{l}Integration\\steps\end{tabular}} (6.5, 4 - \hreduce);
                \draw[->] (path) -- (hidden);
            }

    \end{tikzpicture}
}


\begin{document}
    \ncdediagram{1}
\end{document}
