Congruence properties of combinatorial sequences via Walnut and the Rowland-Yassawi-Zeilberger automaton

Abstract: Certain famous combinatorial sequences, such as the Catalan numbers and the Motzkin numbers, when taken modulo a prime power, can be computed by finite automata. Many theorems about such sequences can therefore be proved using Walnut, which is an implementation of a decision procedure for proving various properties of automatic sequences. In this paper we explore some results (old and new) that can be proved using this method.

\section{Introduction}
We study the properties of two famous combinatorial sequences.
For $n \geq 0$, let $$C_n = \frac{1}{n+1}\binom{2n}{n}$$ denote the
\emph{$n$-th Catalan number} and let $$M_n = \sum_{k=0}^{\lfloor n/2
  \rfloor}\binom{n}{2k}C_k$$ denote the \emph{$n$-th Motzkin number}.
For more about the Catalan numbers, see
\cite{Stanley}.

Many authors have studied congruence properties of these and other
sequences modulo primes $p$ or prime powers $p^\alpha$.
Notably, Alter and Kubota \cite{AK73} studied the Catalan numbers modulo $p$,
and Deutsch and Sagan \cite{DS06} studied many sequences,
including the Catalan numbers, Motzkin numbers, Central Delannoy numbers,
Ap\'ery numbers, etc., modulo certain prime powers.
Eu, Liu, and Yeh \cite{ELY08} studied the Catalan and Motzkin numbers
modulo $4$ and $8$, and Krattenthaler and M\"uller \cite{KM18} studied
the Motzkin numbers and related sequences modulo powers of $2$.
Rowland and Yassawi \cite{RY15} and Rowland and Zeilberger \cite{RZ14}
gave different methods to compute finite automata that compute the
sequences $(C_n \bmod p^\alpha)_{n \geq 0}$ and $(M_n \bmod
p^\alpha)_{n \geq 0}$ (and many other similar sequences),
where $p^\alpha$ is a prime power.  Rowland and
Zeilberger provide a number of these automata for different $p^\alpha$
at the website
\begin{center}
  \url{https://sites.math.rutgers.edu/~zeilberg/mamarim/mamarimhtml/meta.html}
\end{center}
along with the Maple code used to compute them.
We use some of these automata, along with the program Walnut
\cite{Walnut}, available at the website
\begin{center}
\url{https://cs.uwaterloo.ca/~shallit/walnut.html}
\end{center}
to study properties of these sequences.

We use the Rowland--Zeilberger algorithm (and Walnut) as a black-box,
so we do not discuss the theory behind it.  We just mention that
this algorithm applies to any sequence of numbers that can be defined
as the \emph{constant term} of $[P(x)]^nQ(x)$, where $P$ and $Q$ are
\emph{Laurent polynomials}.  In particular, the $n$-th Catalan number
is the constant term of $(1/x+2+x)^n(1-x)$ and the $n$-th Motzkin number
is the constant term of $(1/x+1+x)^n(1-x^2)$.

Note that the automata produced by this method read their input in
least-significant-digit-first format.  All of the automata in this
paper therefore also follow this convention.  We use the notation
$(n)_k$ to denote the base-$k$ representation of
$n$ in the lsd-first format.

Burns has posted several manuscripts to the arXiv
\cite{Bur_A,Bur_B,Bur_C,Bur_D,Bur_E,Bur_F} in which he investigates
various properties of the Catalan and Motzkin numbers modulo primes $p$
by analyzing structural properties of automata computed using the
Rowland--Yassawi algorithm.  This paper takes a similar approach, but
we use Walnut to simplify/automate much of the analysis.

\section{Motzkin numbers}
Deutsch and Sagan \cite{DS06} gave a characterization of ${\bf m}_2 = (M_n \bmod 2)_{n \geq
  0}$ that involves the Thue--Morse sequence $${\bf t} = (t_n)_{n \geq
  0} = (0,1,1,0,1,0,0,1,\ldots).$$  Let $${\bf c} = (c_n)_{n \geq 0} =
(1,3,4,5,7,\ldots)$$ denote the starting positions of the ``runs'' in
${\bf t}$, excluding the first run (which, of course, starts at position
$0$).

\begin{theorem}[Deutsch and Sagan] The Motzkin number $M_n$ is even if
  and only if either $n \in
  4{\bf c}-2$ or $n \in 4{\bf c}-1$.
\end{theorem}

\begin{proof}
  We can prove this result using Walnut.  The Rowland--Zeilberger algorithm
  produces the algorithm in Figure~\ref{MOT2}, which, when fed with $(n)_2$,\
  outputs $M_n \bmod 2$.

  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{MOT2.pdf}
    \caption{Automaton for $M_n \bmod 2$}\label{MOT2}
  \end{figure}

Next we use Walnut to construct an
automaton for the sequence ${\bf c}$.  The commands
\begin{verbatim}
def tm_blocks "?lsd_2 n>=1 & (At t<n => T_lsd[i+t]=T_lsd[i]) &
    T_lsd[i+n]!=T_lsd[i] & (i=0|T_lsd[i-1]!=T_lsd[i])":
def tm_block_start "?lsd_2 i>=1 & ($tm_blocks(i,1)|$tm_blocks(i,2))":
\end{verbatim}
produce the automaton \texttt{tm\_block\_start} given in
Figure~\ref{tm_block_start}, which computes ${\bf c}$.
We see that the elements of ${\bf c}$ are
$$ \{ m4^k : m \text{ is odd and } k \geq 0 \}.$$

  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{tm_block_start.pdf}
    \caption{Automaton for starting positions of ``runs'' in {\bf t}}\label{tm_block_start}
  \end{figure}

To complete the proof of the theorem, it suffices to execute the
Walnut command
\begin{verbatim}
eval even_mot "?lsd_2 An (MOT2[n]=@0 <=> Ei $tm_block_start(i) &
    (n+2=4*i | n+1=4*i))":
\end{verbatim}
which produces the output ``TRUE''.
\end{proof}

Deutsch and Sagan also characterized ${\bf m}_3 = (M_n \bmod 3)_{n \geq
  0}$:

\begin{theorem}{(Deutsch and Sagan)}
The Motzkin number $M_n$ satisfies
\[
M_n \equiv_3
\begin{cases}
1, & \text{ if either } (n)_3 = 0w, w \in \{0,1\}^* \text{ or }
(n+2)_3 = 0w, w \in \{0,1\}^*, \\
2, & \text{ if } (n+1)_3 = 0w, w \in \{0,1\}^*, \\
0, & \text{ otherwise.}
\end{cases}
\]
\end{theorem}

This can also be obtained directly from the automaton for ${\bf m}_3$.
If we examine ${\bf m}_5 = (M_n \bmod 5)_{n \geq 0}$, however, we
discover that its behaviour is very different
from that of ${\bf m}_3$.  Deutsch and Sagan determined the positions
of the $0$'s in ${\bf m}_5$.

\begin{theorem}{(Deutsch and Sagan)}
The Motzkin number $M_n$ is divisible by $5$ if and only if
$n$ is of the form
\[ (5i+1)5^{2j}-2, \quad (5i+2)5^{2j-1}-1 \quad
(5i+3)5^{2j-1}-2 \quad (5i+4)5^{2j}-1 . \]
\end{theorem}

\begin{proof}
The Rowland--Zeilberger algorithm gives the automaton in Figure~\ref{MOT5}, which fully characterizes ${\bf m}_5$.

  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.60]{MOT5.pdf}
    \caption{Automaton for $M_n \bmod 5$}\label{MOT5}
  \end{figure}

The Walnut command
\begin{verbatim}
eval mot5mod0 "?lsd_5 MOT5[n]=@0":
\end{verbatim}
produces the automaton in Figure~\ref{mot5mod0}, from which
one easily derives the result.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{mot5mod0.pdf}
    \caption{Automaton for the positions of the $0$'s in ${\bf m}_5$}\label{mot5mod0}
  \end{figure}
\end{proof}

One notices that ${\bf m}_3$ contains
arbitrarily large runs of $0$'s, whereas ${\bf m}_5$ does
not have this property.  We can use Walnut to determine the types of repetitions
that are present in ${\bf m}_5$, but we first need to introduce some
definitions.

Let $w = w_1w_2\cdots w_n$ be a word of length $n$ and
\emph{period} $p$; i.e., $w_i = w_{i+p}$  for $i = 0, \ldots, n-p$.
If $p$ is the smallest period of $w$, we say that the \emph{exponent}
of $w$ is $n/p$.  We also say that $w$ is an \emph{$(n/p)$-power} of
\emph{order $p$}.  Words of exponent $2$ (resp.~$3$) are called
\emph{squares} (resp.~\emph{cubes}).  If ${\bf x}$ is an infinite
sequence, we define the \emph{critical exponent} of ${\bf x}$ as
$$ \sup \{ e \in \mathbb{Q}: \text{ there is a factor of } {\bf x}
   \text{ with exponent } e \}. $$

\begin{theorem}
The sequence ${\bf m}_5$ has critical exponent $3$.  Furthermore,
the only cubes in ${\bf m}_5$ are $111$, $222$, $333$, and $444$.
\end{theorem}

\begin{proof}
We execute the Walnut commands
\begin{verbatim}
eval tmp "?lsd_5 Ei,n (n>=1) & At (t<=2*n) => MOT5[i+t]=MOT5[i+t+n]":
eval tmp "?lsd_5 Ei (n>=1) & At (t<2*n) => MOT5[i+t]=MOT5[i+t+n]":
\end{verbatim}
and note that the first outputs ``FALSE'', indicating that ${\bf m}_5$
has no factors of exponent larger than $3$, and the second produces
an automaton that only accepts $n=1$, indicating that the only cubes
in ${\bf m}_5$ have order $1$.  By inspecting a prefix of ${\bf m}_5$,
one sees that $111$, $222$, $333$, and $444$ all occur.
\end{proof}

We can also prove that every pattern of residues that appears in
${\bf m}_5$ appears infinitely often, and furthermore, we can
give a bound on when the next occurrence of a pattern will appear
in ${\bf m}_5$.  We say that a sequence ${\bf x}$ is
\emph{uniformly recurrent} if for every factor $w$ of ${\bf x}$,
there is a constant $c$ such that every occurrence of $w$ in ${\bf x}$
is followed by another occurrence of $w$ at distance at most $c$.

Note that ${\bf m}_3$ is \emph{not} uniformly recurrent.  This is
due to the presence of arbitrarily large runs of $0$'s in ${\bf m}_3$.
On the other hand, the sequence ${\bf m}_5$ exhibits rather different
behaviour.

\begin{theorem}
The sequence ${\bf m}_5$ is uniformly recurrent.  Furthermore,
if $w$ has length $n$ and occurs at position $i$ in ${\bf m}_5$,
then there is another occurrence of $w$ at some position $j$,
where $i < j \leq i+200n$.  The bound $200n$ cannot be replaced by
$200n-1$.
\end{theorem}

\begin{proof}
This is proved with the Walnut commands
\begin{verbatim}
def mot5faceq "?lsd_5 At (t<n) => (MOT5[i+t]=MOT5[j+t])":
eval tmp "?lsd_5 An (n>=1) => Ai Ej (j>i) & (j<i+200*n+1) &
    $mot5faceq(i,j,n)":
eval tmp "?lsd_5 An (n>=1) => Ai Ej (j>i) & (j<i+200*n) &
    $mot5faceq(i,j,n)":
\end{verbatim}
noting that the first \texttt{eval} command returns ``TRUE'' and
the second returns ``FALSE''.
\end{proof}

Burns \cite{Bur_E} studied ${\bf m}_p$ for $p$ between $7$
and $29$ using automata computed using the Rowland--Yassawi algorithm.
Among other things, his work suggests that depending on the value of $p$, the sequence
${\bf m}_p$ either behaves like ${\bf m}_3$, where $0$ has density $1$ (i.e.,
$p = 7, 17, 19$), or ${\bf m}_p$ behaves like ${\bf m}_5$, where $0$ has
density $<1$ (i.e., $p = 11, 13, 23, 29$).  Many of Burns' results could also be
obtained using Walnut.

\begin{problem}\label{mot_rec}Characterize the primes $p$ for which ${\bf m}_p$ is
uniformly recurrent.
\end{problem}

Indeed, based on Burns' results and the discussion in the next section,
we guess that the answer to this problem is given by the sequence
$$2, 5, 11, 13, 23, 29, 31, 37, 53, \ldots$$
of primes that do not divide any central trinomial number.  This is
sequence
\seqnum{A113305} of \cite{OEIS}.

\section{Central trinomial coefficients}
The Motzkin numbers are closely related to the \emph{central trinomial
coefficients} $T_n$.  The usual definition of $T_n$ is as the coefficient of $x^n$
in $(1+x+x^2)^n$, but the definition
$$T_n = \sum_{k \geq 0}\binom{n}{2k}\binom{2k}{k}$$ better illustrates the connection between
these numbers and the Motzkin numbers.  The number $T_n$ is also
the constant term of $(1/x+1+x)^n$, which is the form needed for the Rowland--Zeilberger
algorithm.  Deutsch and Sagan
studied the divisibility of $T_n$ modulo primes and Noe \cite{Noe06} did the same
for generalized central trinomial numbers.

\begin{theorem}[Deutsch and Sagan]
The central trinomial coefficient $T_n$ satisfies
\[
T_n \equiv_3
\begin{cases}
1, & \text{ if } (n)_3  \text{ does not contain a } 2;\\
0, & \text{ otherwise.}
\end{cases}
\]
\end{theorem}

Deutsch and Sagan proved this by an application of Lucas' Theorem;
it is also immediate from the automaton produced by the Rowland--Zeilberger
algorithm.  As with the Motzkin numbers, the behaviour of $T_n$ modulo $5$ is
rather different from that modulo $3$.  We collect some properties below
(compare with those of ${\bf m}_5$ from the previous section).

\begin{theorem}
Let ${\bf t}_5 = (T_n \bmod 5)_{n \geq 0}$.  Then
\begin{enumerate}
\item ${\bf t}_5$ does not contain $0$ (i.e., $T_n$ is never divisible by $5$);
\item ${\bf t}_5$ has critical exponent $3$; furthermore,
the only cubes in ${\bf t}_5$ are $111$, $222$, $333$, and $444$;
\item ${\bf t}_5$ is uniformly recurrent;  Furthermore,
if $w$ has length $n$ and occurs at position $i$ in ${\bf t}_5$,
then there is another occurrence of $w$ at some position $j$,
where $i < j \leq i+200n-192$.  The constant $192$ cannot be replaced with $193$.
\item If $w$ has length $n$ and appears in ${\bf t}_5$, then
$w$ appears in the prefix of ${\bf t}_5$ of length $121n$.  The
quantity $121n$ cannot be replaced with $121n-1$.
\end{enumerate}
\end{theorem}

\begin{proof}
Properties 1)--3) can all be obtained by similar Walnut commands to those used in the previous
section for the Motzkin numbers.  We just need the automaton for ${\bf t}_5$.
The Rowland--Zeilberger algorithm gives the pleasantly symmetric automaton
in Figure~\ref{TRI5}.

  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.60]{TRI5.pdf}
    \caption{Automaton for $T_n \bmod 5$}\label{TRI5}
  \end{figure}

For Property 4), we use the Walnut commands
\begin{verbatim}
def pr_tri5 "?lsd_5 Aj Ei i+n<=s & At t<n => TRI5[i+t]=TRI5[j+t]":
eval tmp "?lsd_5 An $pr_tri5(n,121*n)":
eval tmp "?lsd_5 An $pr_tri5(n,121*n-1)":
\end{verbatim}
and note that the last two commands return "TRUE" and "FALSE",
respectively.
\end{proof}

We should note that in the special case of the central trinomial coefficients,
it is not necessary to resort to either the Rowland--Zeilberger or Rowland--Yassawi algorithms to compute the automaton for $T_n \bmod p$.  Using the following result of Deutsch and Sagan,
one can directly define the automaton for $T_n \bmod p$.

\begin{theorem}{(Deutsch and Sagan)}\label{tri_lucas}
Let $(n)_p = n_0n_1\cdots n_r$. Then $$T_n \equiv_p \prod_{i=0}^r T_{n_i}.$$
\end{theorem}

An immediate consequence is that $T_n$ is divisible by $p$ if and only if one of the $T_{n_i}$
is divisible by $p$.
This criterion allows one to determine the primes that do not divide any central trinomial
coefficient; i.e., those in \seqnum{A113305} of \cite{OEIS}, which we conjectured in the
previous section to be the ones that answer the question of Problem~\ref{mot_rec}.

We can also give the following sufficient condition for ${\bf t}_p = (T_n \bmod p)_{n \geq 0}$
to be uniformly recurrent.  For $i =0,\ldots,p-1$, let $\tau_i = T_i \bmod p$.

\begin{theorem}
Let $p$ be prime and let $\Sigma = \{\tau_i : i = 0,\ldots,p-1\}$.  If $\Sigma$ does not
contain $0$ but does contain a primitive root modulo $p$, then ${\bf t}_p$ is uniformly
recurrent.
\end{theorem}

\begin{proof}
Clearly the order of the product in Theorem~\ref{tri_lucas} does not matter; it follows then
that Theorem~\ref{tri_lucas} holds for the most-significant-digit-first representation
of $n$, as well as the least-significant-digit-first representation.  If we consider
Theorem~\ref{tri_lucas} with $n$ written in msd-first notation, we see that ${\bf t}_p$
is generated by iterating the morphism $f : \Sigma^* \to \Sigma^*$ defined by
$$f(\tau_i) = (\tau_i\tau_0 \bmod p)(\tau_i\tau_1 \bmod p)\cdots (\tau_i\tau_{p-1} \bmod p)$$
for $i = 0,\ldots,p-1$; i.e., ${\bf t}_p = f^\omega(1)$.

Recall that if there exists $t$ such that for every $a,b \in \Sigma$ the word $f^t(a)$
contains $b$, we say that $f$ is a \emph{primitive morphism}.  Now $\tau_0=1$, so for
$i=0,\ldots,p-1$, we can write $f(\tau_i)=\tau_i x_i$ for some word $x_i$.  It follows
that $f^p(\tau_i) = \tau_i f(x_i) f^2(x_i)\cdots f^{p-1}(x_i)$.  Furthermore,
if $0 \notin \Sigma$ and $x_0$ contains a primitive root modulo $p$, then for every $i$,
each non-zero residue modulo $p$ appears in one of $\tau_i, f(x_i), f^2(x_i), \ldots,
f^{p-1}(x_i)$.  This proves that the morphism $f$ is primitive.  A standard result
from the theory of morphic sequences states that any fixed point of a primitive morphism
is uniformly recurrent \cite[Theorem~10.9.5]{AS03}.
\end{proof}

\begin{example}
For $p=5$, we have $(T_0,T_1,T_2,T_3,T_4) = (1,1,3,7,19)$, so $(\tau_0,\tau_1,\tau_2,\tau_3,\tau_4) =
(1,1,3,2,4)$ contains the primitive root $2$.  The word
$${\bf t}_5 = 113241132433412221434423111324\cdots$$ is
uniformly recurrent and is equal to
$f^\omega(1)$, where $f$ is the morphism defined by
\begin{align*}
1 &\to 11324\\
2 &\to 22143\\
3 &\to 33412\\
4 &\to 44231.
\end{align*}
\end{example}

A computer calculation shows that for each prime $p$ appearing in the list of
initial values $2,5,11,13,\ldots,479$ of \seqnum{A113305}, the first $p$
terms of ${\bf t}_p$ always contain a primitive root modulo $p$.
Hence, each of these ${\bf t}_p$'s are uniformly recurrent.

\section{Catalan numbers}
Alter and Kubota \cite{AK73} studied the sequences ${\bf c}_p = (C_n \bmod p)_{n
  \geq 0}$, where $p$ is prime.  They proved that the runs of $0$'s in
${\bf c}_p$ have lengths
\begin{equation}\label{cp_0runs}
  \frac{p^{m+1+\delta_{3p}}-3}{2},
\end{equation}
where $\delta_{3p}$ is $1$ when $p=3$ and $0$ otherwise.  This implies, of
course, that for every prime $p$, the sequence ${\bf c}_p$ is not
uniformly recurrent.  Alter and Kubota also
proved that the blocks of non-zero values in ${\bf c}_p$ have length
$$\frac{p+3(1+2\delta_{3p})}{2}.$$
For $p=3$, Deutsch and Sagan~\cite[Theorem~5.2]{DS06} gave a complete
characterization of ${\bf c}_3$.  We can obtain a similar
characterization using Walnut.

\begin{theorem}[Deutsch and Sagan]\label{c3_0runs}
  The runs of $0$'s in ${\bf c}_3$ begin at positions $n$, where either
  $$(n)_3 \in 211^* \text{ or } (n)_3 \in 211^*0\{0,1\}^*,$$
  and have length $(3^{i+2}-3)/2$, where $i$ is the length of the
  leftmost block of $1$'s in $(n)_3$.  The blocks of non-zero values
  in ${\bf c}_3$ are given by the following:
  \begin{itemize}
  \item The block $11222$ occurs at position $0$.
  \item The block $111222$ occurs at all positions $n$ where $(n)_3 \in 222^*0w$
  for some $w \in \{0,1\}^*$ that contains an odd number of $1$'s.
  \item The block $222111$ occurs at all positions $n$ where $(n)_3 \in 222^*0w$
  for some $w \in \{0,1\}^*$ that contains an even number of $1$'s.
  \end{itemize}
\end{theorem}

\begin{proof}
  We use the automaton for ${\bf c}_3$ given in Figure~\ref{CAT3}.

  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{CAT3.pdf}
    \caption{Automaton for $C_n \bmod 3$}\label{CAT3}
  \end{figure}

  The Walnut command
\begin{verbatim}
eval cat3max0 "?lsd_3 n>=1 & (At t<n => CAT3[i+t]=@0) &
    CAT3[i+n]!=@0 & (i=0|CAT3[i-1]!=@0)":
\end{verbatim}
  produces the automaton in Figure~\ref{cat3max0}.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{cat3max0.pdf}
    \caption{Automaton for runs of $0$'s in ${\bf c}_3$}\label{cat3max0}
  \end{figure}
  Examining the transition
  labels of the first component of the input gives the claimed representation for
  the starting positions of the runs of $0$'s and examining the transition
  labels of the second component gives the claimed length.

  For the blocks of non-zero values, we execute the Walnut commands
\begin{verbatim}
eval cat3max12 "?lsd_3 n>=1 & (At t<n => CAT3[i+t]!=@0) &
    CAT3[i+n]=@0 & (i=0|CAT3[i-1]=@0)":
eval cat3_111222 "?lsd_3 $cat3max12(i,6) & CAT3[i]=@1 &
    CAT3[i+1]=@1 & CAT3[i+2]=@1 & CAT3[i+3]=@2 &
    CAT3[i+4]=@2 & CAT3[i+5]=@2":
eval cat3_222111 "?lsd_3 $cat3max12(i,6) & CAT3[i]=@2 &
    CAT3[i+1]=@2 & CAT3[i+2]=@2 & CAT3[i+3]=@1 &
    CAT3[i+4]=@1 & CAT3[i+5]=@1":
eval cat3all12 "?lsd_3 Ai,n $cat3max12(i,n) =>
    (i=0 | $cat3_111222(i) | $cat3_222111(i))":
\end{verbatim}
to obtain the automata in Figures~\ref{cat3_111222} and \ref{cat3_222111}.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{cat3_111222.pdf}
    \caption{Automaton for blocks $111222$ in ${\bf c}_3$}\label{cat3_111222}
  \end{figure}
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{cat3_222111.pdf}
    \caption{Automaton for blocks $222111$ in ${\bf c}_3$}\label{cat3_222111}
  \end{figure}
\end{proof}

Note that the length of the runs given in Theorem~\ref{c3_0runs} is
exactly what is given by the result of Alter and Kubota stated above
in Eq.~\eqref{cp_0runs}.

We can also perform the same calculation for $p=5$ to obtain

\begin{theorem}
 The runs of $0$'s in ${\bf c}_5$ begin at positions $n$, where either
  $$(n)_5 \in 32^* \text{ or } (n)_5 \in 32^*\{0,1\}\{0,1,2\}^*,$$
  and have length $(5^{i+2}-3)/2$, where $i$ is the length of the leftmost
  block of $2$'s in $(n)_5$.
\end{theorem}

\begin{proof}
  We use the automaton for ${\bf c}_5$ given in Figure~\ref{CAT5}.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.65]{CAT5.pdf}
    \caption{Automaton for $C_n \bmod 5$}\label{CAT5}
  \end{figure}
The Walnut command
\begin{verbatim}
eval cat5max0 "?lsd_5 n>=1 & (At t<n => CAT5[i+t]=@0) &
    CAT5[i+n]!=@0 & (i=0|CAT5[i-1]!=@0)":
\end{verbatim}
  produces the automaton in Figure~\ref{cat5max0}.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{cat5max0.pdf}
    \caption{Automaton for runs of $0$'s in ${\bf c}_5$}\label{cat5max0}
  \end{figure}
  Examining the transition
  labels, as in the proof of Theorem~\ref{c3_0runs}, gives the result.
\end{proof}

Again, note that the lengths of the runs match what is given by
Eq.~\eqref{cp_0runs}.

\begin{theorem}
The sequence $c_5$ begins with the non-zero block $112$.  The other
non-zero blocks in $c_5$ are $1331$, $2112$, $3443$, and $4224$.
\end{theorem}

\begin{proof}
The Walnut command
\begin{verbatim}
eval cat5max1234 "?lsd_5 n>=1 & (At t<n => CAT5[i+t]!=@0) &
    CAT5[i+n]=@0 & (i=0|CAT5[i-1]=@0)":
\end{verbatim}
  produces the automaton in Figure~\ref{cat5max1234}.
  \begin{figure}[htb]
    \centering
    \includegraphics[scale=0.75]{cat5max1234.pdf}
    \caption{Automaton for non-zero blocks in ${\bf c}_5$}\label{cat5max1234}
  \end{figure}
We see that the initial non-zero block has length $3$ and all others
have length $4$.  We omit the Walnut command to verify
the values of these length $4$ blocks, but it is easy to
formulate.
\end{proof}

\section{Conclusion}
We have shown how to use Walnut to obtain automated proofs of certain
results in the literature concerning the Catalan and Motzkin numbers modulo $p$,
as well as the central trinomial coefficients modulo $p$.
We were also able to use Walnut to examine other properties of these
sequences that have not previously been explored, such as the presence (or
absence) of certain repetitive patterns and the property of being
uniformly recurrent.  We hope these results encourage other researchers to
continue to further explore these properties for other sequences.

Combinatorics (math.CO); Formal Languages and Automata Theory (cs.FL); Number Theory (math.NT)


A closed-form approximation for the median of the beta distribution

Abstract: A simple closed-form approximation for the median of the beta distribution Beta(a, b) is introduced: (a-1/3)/(a+b-2/3) for (a,b) both larger than 1 has a relative error of less than 4%, rapidly decreasing to zero as both shape parameters increase.

\section{Introduction}
\label{s1}

Consider
the the beta distribution
$\mathrm{Beta}(a,b)$, with the density function,
$$
  \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}
  \theta^{a-1}(1-\theta)^{b-1}.
$$
The mean of $\mathrm{Beta}(a, b)$ is readily obtained
by the formula $a/(a+b)$, but
there is no general closed formula for the median.
The median function, here denoted by $m(a,b)$,
is the function that satisfies,
$$
  \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}
  \int_0^{m(a,b)}\theta^{a-1}(1-\theta)^{b-1} \mathrm{d}\theta
  =
  \frac{1}{2}.
$$

The relationship $m(a,b)=1-m(b, a)$ holds.
Only for the special cases $a=1$ or $b=1$ we may obtain
an exact formula: $m(a, 1)=2^{-1/a}$
and $m(1, b)=1-2^{-1/b}$.
Moreover, when $a=b$, the median is exactly $1/2$.

There has been much literature about the
incomplete beta function and
its inverse
(see e.g. \citet{Dutka:1981} for a review).
The focus in literature has been
on finding accurate numerical results,
but a simple and practical
approximation that is easy to compute
has not been found.

\begin{figure}[t]
\begin{center}
\scalebox{0.80}{\includegraphics{fig-betaerr}}
\caption{\label{fig-betaerrors}
Relative errors of the approximation
$(a-1/3)/(a+b-2/3)$ of the
median of the $\mathrm{Beta}(a, b)$ distribution,
compared with the numerically computed value
for several fixed $p=a/(a+b)<1/2$.
The horizontal axis shows the shape parameter $a$
on logarithmic scale.
From left to right,
$p=0.499$, 0.49, 0.45, 0.35, 0.25, and 0.001.
}
\end{center}
\end{figure}
\section{A new closed-form approximation for the median}

Trivial bounds for the median can be derived
\citep{Payton:1989}, which are
a consequence of the more general
mode-median-mean inequality
\citep{Groeneveld:Meeden:1977}.
In the case of the beta distribution with
$1<a<b$,
the median is bounded by the
mode $(a-1)/(a+b-2)$ and the mean $a/(a+b)$:
$$
  \frac{a-1}{a+b-2}
  \le
  m(a,b)
  \le
  \frac{a}{a+b}.
$$
For $a\le1$ the formula for the mode does not hold
as there is no mode.
If $1<b<a$, the order of the inequality is reversed.
Equality holds if and only if $a=b$;
in this case the mean, median, and mode are all equal to $1/2$.

This inequality shows that if the mean is kept fixed
at some $p$,
and one of the shape parameters is increased, say $a$,
then the median is sandwiched between
$p(a-1)/(a-2p)$ and $p$,
hence the median tends to $p$.

From the formulas for the mode and mean,
it can be conjectured that
the median $m(a,b)$ could be approximated by
$m(a,b;d)=(a-d)/(a+b-2d)$ for some $d\in(0,1)$,
as this form would satisfy the above inequality
while agreeing with the symmetry requirement,
that is, $m(a,b;d)=1-m(b,a;d)$.

\begin{figure}[t]
\begin{center}
\scalebox{0.80}{\includegraphics{fig-betaerrp}}
\caption{\label{fig-betaerrp}
Relative errors of the approximation
$(a-1/3)/(a+b-2/3)$ of the
median of the $\mathrm{Beta}(a, b)$ distribution
over the whole range of possible distribution means
$p=a/(a+b)$.
The smaller of the shape parameters is fixed,
i.e. for $p\le 0.5$,
the median is computed for $\mathrm{Beta}(a, a(1-p)/p)$
and for $p>0.5$,
the median is computed for $\mathrm{Beta}(bp/(1-p), b)$.
}
\end{center}
\end{figure}

Since a $\mathrm{Beta}(a,b)$ variate can be expressed as
the ratio $\gamma_1/(\gamma_1+\gamma_2)$ where
$\gamma_1\sim\mathrm{Gamma}(a)$ and
$\gamma_2\sim\mathrm{Gamma}(b)$ (both with unit scale),
it is useful to have a look at the median
of the gamma distribution.
\citet{Berg:Pedersen:2006} studied the median
function of the unit-scale
gamma distribution median function, denoted here by $M(a)$,
for any shape parameter $a>0$,
and obtained
$M(a) = a - 1/3 + o(1)$,
rapidly approaching $a-1/3$ as $a$ increases.
It can therefore be conjectured that
the distribution median may be approximated by,
\begin{equation}\label{eq-beta}
 m(a, b) \approx
 m(a, b; 1/3)
 =
 \frac{a-1/3}{(a-1/3)+(b-1/3)}
 =
 \frac{a-1/3}{a+b-2/3}.
\end{equation}

Figure (\ref{fig-betaerrors})
shows that
this approximation indeed appears to approach
the numerically computed median asymptotically
for all distribution means $p=a/(a+b)$ as the
(smaller) shape parameter $a\to\infty$.
For $a\ge1$, the relative error is less than 4\%,
and for $a\ge2$ this is already less than 1\%.

\begin{figure}[t]
\begin{center}
\scalebox{0.80}{\includegraphics{fig-betadisterr}}
\caption{\label{fig-betadisterr}
Logarithm of the scaled absolute error (distance)
$\log(|m(a,b;d)-m(a,b)|/p)$,
computed for a fixed distribution mean $p=0.01$ and various
$d$. The approximate median of the
$\mathrm{Beta}(a,b)$ distribution
is defined as $m(a,b;d)=(a-d)/(a+b-2d)$.
Due to scaling of the error,
the graph and its scale will not essentially change even if
the error is computed for other values of $p<0.5$.
The approximation $m(a,b;1/3)$ performs the
most consistently,
attaining the lowest absolute error eventually as the
precision of the distribution increases.
}
\end{center}
\end{figure}
Figure (\ref{fig-betaerrp}) shows the relative
error over all possible distribution means $p=a/(a+b)$,
as the smallest of the two shape parameters varies from
$1$ to $4$. This illustrates how the relative error
tends uniformly to zero over all $p$ as the shape parameters
increase.
The figure also shows that
the formula consistently either underestimates
or overestimates the median depending on whether
$p<0.5$ or $p>0.5$.

However, the function
$m(a,b;d)$
approximates the median fairly accurately
if some other $d$ close to $1/3$ (say $d=0.3$) is chosen.
Figure (\ref{fig-betadisterr}) displays
curves of the logarithm of the absolute
difference from the numerically computed
median for a fixed $p=0.01$, as the shape parameter
$a$ increases.
The absolute difference
has been scaled by $p$ before taking the logarithm:
due to this scaling,
the error stays approximately constant as $p$ decreases
so the picture and its scale will not essentially change even if
the error is computed for other values of $p<0.5$.
The figure shows that although some approximations such as
$d=0.3$ has a lower absolute error for some $a$,
the error of $m(a, b; 1/3)$ tends to be lower in the long run,
and moreover performs more consistently
by decreasing at the same rate on the logarithmic scale.
In practical applications, $d=0.333$ should be a sufficiently
good approximation of $d=1/3$.

\begin{figure}[t]
\begin{center}
\scalebox{0.7}{\includegraphics{fig-betatail}}
\caption{\label{fig-betatail}
Tail probabilities $\Pr(\theta<m)$
of the $\mathrm{Beta}(a, b)$ distribution
when $m=(a-1/3)/(a+b-2/3)$.
As the smaller of the two shape
parameters increases, the tail probability
tends rapidly and uniformly to $0.5$.
}
\end{center}
\end{figure}
Another measure of the accuracy is the
tail probability
$\Pr(\theta \le m(a,b;1/3))$ of a $\mathrm{Beta}(a, b)$ variate $\theta$:
good approximators of the
median should yield probabilities close to $1/2$.
Figure (\ref{fig-betatail}) shows that
as long as the smallest of the shape parameters
is at least 1,
the tail probability is bound between $0.4865$ and $0.5135$.
As the shape parameters increase, the
probability tends
rapidly and uniformly to $0.5$.

Finally, let us have a look at a
well-known paper that provides further
support for the uniqueness of $m(a,b;1/3)$.
\citet{Peizer:Pratt:1968} and \citet{Pratt:1968}
provide approximations for
the probability function $\Pr(\theta\le x)$
of a $\mathrm{Beta}(a,b)$ variate $\theta$.
Although they do not provide a formula
for the inverse, it is
the probability function at the approximate median.
According to \citet{Peizer:Pratt:1968},
$\Pr(\theta\le x)$
is well approximated by
$\Phi(z(a,b; x))$ where $\Phi$ is the
standard normal probability function,
and $z$ is a function of the shape parameters and
the quantile $x$.
Consider $m=m(a,b;d)$:
$z(a,b;m)$ should be close to zero and at least
tend to zero fast as $a$ and $b$ increase.
Now assume that $p$ is fixed, $a$ varies and $b=a(1-p)/p$.
The function $z(a, b; m)$ equals, rewritten with
the notation in this paper,
\begin{equation}\label{eq-peizer-beta}
   \sqrt{p}\frac{1-2m}{(a-p)^{1/2}}\left(
    1/3-d
    -
   \frac{0.02p}{a}\left[
   \frac{1}{2} + \frac{1-dp/a}{p(1-p)}
   \right]
   \right)
   \left(\frac{1+f(a,p;d)}{m(1-m)}\right)^{1/2},
\end{equation}
where the function $f(a,p;d)$ tends to zero
as $a$ increases,
being exactly zero only when $d=1/2$ or $m=1/2$.
It is evident that for the fastest convergence
rate to zero, one should choose $d=1/3$.
This is of the order $O(a^{-3/2})$;
if $d\ne 1/3$,
for example if we choose the mean $p$
as the approximation of the median ($d=0$),
the rate is at most $O(a^{-1/2})$.

Statistics Theory (math.ST); Computation (stat.CO)