Please apologize my ingnorance of tips to use for asking questions. I would like to realise the plots and the diagram like those shown on page 158,
160
and 161
on this document Chaine de Markov.
Any answers are welcome.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityPlease apologize my ingnorance of tips to use for asking questions. I would like to realise the plots and the diagram like those shown on page 158,
160
and 161
on this document Chaine de Markov.
Any answers are welcome.
My solutions adopts a command \samplepath
able to create a sample path with the following syntax:
\samplepath{+|0.5,+|0.25,-|1.5,+|1,+|0.5,+|0.75}
where
+/-
represent the vertical changes of the path;Similarly, there is another command \discretesamplepath
which uses the same syntax, but it represents samples in discrete time.
There are actually two methods available:
xstring
with the syntax described above;|
with /
.The code with xstring
:
\documentclass[png,border=10pt,tikz]{standalone}
\usepackage{xstring}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfkeys{/tikz/.cd,
vertical factor/.initial=0.5,
vertical factor/.get=\vertfactor,
vertical factor/.store in=\vertfactor,
start coordinate/.initial={0,\vertfactor},
start coordinate/.get=\startcoord,
start coordinate/.store in=\startcoord,
sample color/.initial=black,
sample color/.get=\samplecol,
sample color/.store in=\samplecol,
sample size/.initial=1pt,
sample size/.get=\samplesize,
sample size/.store in=\samplesize,
sample line width/.initial=very thick,
sample line width/.get=\samplelinewidth,
sample line width/.store in=\samplelinewidth,
}
\newcommand{\samplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \samples[count=\xi from 1] in {#1}{%
\StrCut{\samples}{|}{\vertdir}{\hordir}
\ifnum\xi=1
\draw[\samplelinewidth,\samplecol](start)
--++(\hordir,0) coordinate (start);
\else
\IfStrEq{\vertdir}{+}{%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,\vertfactor)$)
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\IfStrEq{\vertdir}{-}{%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,-\vertfactor)$)
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\fi
}
}
\tikzset{sample/.style={
circle,
inner sep=\samplesize,
fill=\samplecol,
}
}
\newcommand{\discretesamplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \samples[count=\xi from 1] in {#1}{%
\StrCut{\samples}{|}{\vertdir}{\hordir}
\ifnum\xi=1
\path(start)node[sample]{}
--++(\hordir,0) coordinate (start);
\else
\IfStrEq{\vertdir}{+}{%true
\path($(start)+(0,\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\IfStrEq{\vertdir}{-}{%true
\path($(start)+(0,-\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\fi
}
}
\begin{document}
\begin{tikzpicture}
% axis
\draw[-stealth] (0,-1)--(0,4) node[left]{$X(t)$};
\draw[-stealth] (-1,0)--(5,0) node[below]{$t$};
\samplepath{+|0.5,+|0.25,-|1.5,+|1,+|0.5,+|0.75}
\end{tikzpicture}
\end{document}
The code without xstring
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\up{+}
\def\down{-}
\pgfkeys{/tikz/.cd,
vertical factor/.initial=0.5,
vertical factor/.get=\vertfactor,
vertical factor/.store in=\vertfactor,
start coordinate/.initial={0,\vertfactor},
start coordinate/.get=\startcoord,
start coordinate/.store in=\startcoord,
sample color/.initial=black,
sample color/.get=\samplecol,
sample color/.store in=\samplecol,
sample size/.initial=1pt,
sample size/.get=\samplesize,
sample size/.store in=\samplesize,
sample line width/.initial=very thick,
sample line width/.get=\samplelinewidth,
sample line width/.store in=\samplelinewidth,
}
\newcommand{\samplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \vertdir/\hordir[count=\xi from 1] in {#1}{%
\ifnum\xi=1
\draw[\samplelinewidth,\samplecol](start)
--++(\hordir,0) coordinate (start);
\else
\ifx\vertdir\up%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,\vertfactor)$)
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\ifx\vertdir\down%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,-\vertfactor)$)
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\fi
}
}
\tikzset{sample/.style={
circle,
inner sep=\samplesize,
fill=\samplecol,
}
}
\newcommand{\discretesamplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \vertdir/\hordir[count=\xi from 1] in {#1}{%
\ifnum\xi=1
\path(start)node[sample]{}
--++(\hordir,0) coordinate (start);
\else
\ifx\vertdir\up%true
\path($(start)+(0,\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\ifx\vertdir\down%true
\path($(start)+(0,-\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
\relax
\relax
\fi
\fi
}
}
\begin{document}
\begin{tikzpicture}
% axis
\draw[-stealth] (0,-1)--(0,4) node[left]{$X(t)$};
\draw[-stealth] (-1,0)--(5,0) node[below]{$t$};
\samplepath{+/0.5,+/0.25,-/1.5,+/1,+/0.5,+/0.75}
\end{tikzpicture}
\end{document}
The result for both cases:
Some keys have been defined in order to customize the aspect: it is possible to use them via \tikzset
or as option of the tikzpicture
. In the following examples some of them will be used. Please, notice that these examples are with the method exploiting xstring
.
First example:
\begin{tikzpicture}[start coordinate={0,0},font=\footnotesize]
\begin{scope}
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.5,+|0.25,-|1,+|0.15,-|1.5,+|0.3,-|0.75}
\node at (4.5,1) {$0<\lambda \ll \mu$};
\end{scope}
\begin{scope}[yshift=-2cm]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.55,+|0.75,-|0.65,+|1.5,-|1}
\node at (4.5,1) {$0<\lambda = \mu$};
\end{scope}
\begin{scope}[yshift=-4cm]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.2,+|0.4,-|0.15,+|0.4,-|0.3,+|0.2,
-|0.2,+|0.2,-|0.4,+|0.2,-|0.3,+|0.5,-|0.2,+|0.4,-|0.4}
\node at (4.5,1) {$0\ll \lambda = \mu$};
\end{scope}
\end{tikzpicture}
Result:
Second example:
\begin{tikzpicture}[font=\footnotesize]
\begin{scope}[start coordinate={0,0}]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[above]{$t$};
\node at (0,2) {$X(t)$};
\samplepath{+|0.5,+|1,+|0.75,-|0.5,-|0.25,+|0.35,+|0.4,+|0.55}
\end{scope}
\begin{scope}[yshift=-3cm,start coordinate={0.25,0},sample color=blue,sample size=1.25pt]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[above]{$n$};
\node at (0,2) {$\hat{X}(n)$};
\discretesamplepath{+|0.5,+|1,+|0.75,-|0.5,-|0.25,+|0.35,+|0.4,+|0.55}
\end{scope}
\end{tikzpicture}
Result:
Another piece of the puzzle:
Which was produced by the macro \brokenline
I wrote. This macro draws one of those graphs, and takes four arguments. The first one is the vertical distance among the "steps" of the graph. The second one is a comma delimited list of the lengths of each segment. The third and fourth are the labels to appear at the left and right of the graph.
This is the complete code:
\documentclass{article}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc}
\def\brokenline#1#2#3#4{
\coordinate (prev) at (0,0);
\foreach \x [count=\i] in {#2} {
\draw[very thick] (prev) -- +(\x, 0) coordinate (cur);
\ifodd\i\def\gap{#1}\else\def\gap{-#1}\fi
\coordinate (prev) at ($(cur)+(0,\gap)$);
}
\draw[thin] (0,0) -- (prev|-0,0) node[right] (t) {$t$};
\draw[very thick,->] (prev|-0,0) +(-1pt,0) -- +(1pt,0);
\draw (0,#1) node[above] {#3}
(t|-0,#1) node[above left, inner xsep=0pt] {#4};
}
\begin{document}
\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.5, 0.3, 1, 0.2, 2, 0.4, 1.6}
{$X(t)$}{$0<\lambda\ll\mu$}
\end{tikzpicture}
\vskip5mm
\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.5, 1.2, 0.8, 2, 1.5}
{$X(t)$}{$0<\lambda=\mu$}
\end{tikzpicture}
\vskip5mm
\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.3, 0.4, 0.1, 0.4, 0.25, 0.2, 0.45, 0.2, 0.25, 0.45, 0.05, 0.1, 0.1, 0.5, 0.3, 0.1, 0.2, 0.2, 0.3, 0.3, 0.85}
{$X(t)$}{$0\ll\lambda=\mu$}
\end{tikzpicture}
\end{document}