\chapter{Probability Problem set Solutions}
\begin{enumerate}
	\item An unbiased dice is thrown three times successively. The probability that the numbers of dots on the uppermost surface add up to 16 is
	{\exyear{NET/JRF(DEC-2011)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{1}{16}$
		\task[\textbf{B.}] $\frac{1}{36}$
		\task[\textbf{C.}] $\frac{1}{108}$
		\task[\textbf{D.}] $\frac{1}{216}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\intertext{ We can get sum of dice as 16 in total six ways i.e. three ways $(6,5,5)$ and three ways $(6,6,4)$}
		\text{Total number of ways for 3 dice having six faces }&=6 \times 6 \times 6\\
		&=\frac{6}{6 \times 6 \times 6}=\frac{1}{36}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item A ball is picked at random from one of two boxes that contain 2 black and 3 white and 3 black and 4 white balls respectively. What is the probability that it is white?
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $34 / 70$
		\task[\textbf{B.}] $41 / 70$
		\task[\textbf{C.}] $36 / 70$
		\task[\textbf{D.}] $29 / 70$
	\end{tasks}
	\begin{answer}$\left. \right. $
		\begin{figure}[H]
			\centering
			\includegraphics[height=1.6cm,width=4.55cm]{NET-19-2012}
		\end{figure}
		\begin{align*}
		\intertext{Probability of picking white ball}
		\text{From box }I=\frac{3}{5} \text{ and from box }I I&=\frac{4}{7}
		\intertext{Probability of picking a white ball from either of the two boxes is $=\frac{1}{2}\left[\frac{3}{5}+\frac{4}{7}\right]=\frac{41}{70}$}
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item  A bag contains many balls, each with a number painted on it. There are exactly $n$ balls which have the number $n$ (namely one ball with 1 , two balls with 2, and so on until $N$ on them). An experiment consists of choosing a ball at random, noting the number on it and returning it to the bag. If the experiment is repeated a large number of times, the average value the number will tend to
	{\exyear{NET/JRF(JUNE-2012)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{2 N+1}{3}$
		\task[\textbf{B.}] $\frac{N}{2}$
		\task[\textbf{C.}] $\frac{N+1}{2}$
		\task[\textbf{D.}] $\frac{N(N+1)}{2}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Total number of balls }1+2+3+4+\ldots . .+N&=\frac{N(N+1)}{2}\\
		\text{The probability for choosing a $k^{\text {th }}$ ball at random } &=\frac{k}{\frac{N(N+1)}{2}}\\
		\text{Average of it is given by }\langle k\rangle&=\Sigma k \cdot P=\frac{2 \Sigma k^{2}}{N(N+1)}\\&=\frac{2}{N(N+1)} \cdot \frac{N(N+1)(2 N+1)}{6}\\
		&=\frac{2 N+1}{3} \quad\text{ where }\Sigma k^{2}=\frac{N(N+1)(2 N+1)}{6}
		\end{align*}
		So the correct answer is \textbf{Option (A)}
	\end{answer}
	\item  In a series of five Cricket matches, one of the captains calls "Heads" every time when the toss is taken. The probability that he will win 3 times and lose 2 times is
	{\exyear{NET/JRF(DEC-2012)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $1 / 8$
		\task[\textbf{B.}]  $5 / 8$
		\task[\textbf{C.}] $3 / 16$
		\task[\textbf{D.}] $5 / 16$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		P&=\left(\frac{1}{2}\right)^{3}\left(1-\frac{1}{2}\right)^{5-3} \frac{5 !}{3 !(5-3) !}\\&=\frac{1}{8} \times\left(\frac{1}{2}\right)^{2} \cdot \frac{5 !}{3 !(5-3) !}\\
		&=\frac{1}{32} \cdot \frac{5 \times 4 \times 3 !}{3 ! \times 2 !}=\frac{20}{32 \times 2}\\&=\frac{5}{8 \times 2}=\frac{5}{16}
		\intertext{The probability of getting exactly $k$ successes in $n$ trials is given by probability mass function $=\frac{n !}{k !(n-k) !} p^{k} \cdot(1-p)^{n-k}, k=$ successes, $n=$ trials.}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item  Two independent random variables $m$ and $n$, which can take the integer values $0,1,2, \ldots, \infty$, follow the Poisson distribution, with distinct mean values $\mu$ and $v$ respectively. Then
	{\exyear{NET/JRF(DEC-2014)}}
	\begin{tasks}(1)
		\task[\textbf{A.}]  The probability distribution of the random variable $l=m+n$ is a binomial distribution.
		\task[\textbf{B.}] The probability distribution of the random variable $r=m-n$ is also a Poisson distribution.
		\task[\textbf{C.}] The variance of the random variable $l=m+n$ is equal to $\mu+v$
		\task[\textbf{D.}] The mean value of the random variable $r=m-n$ is equal to 0
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\sigma_{l}^{2}&=\sigma_{m}^{2}+\sigma_{n}^{2}=\mu+v
		\end{align*}
		So the correct answer is \textbf{Option (C)}
	\end{answer}
	\item  Consider a random walker on a square lattice. At each step the walker moves to a nearest neighbour site with equal probability for each of the four sites. The walker starts at the origin and takes 3 steps. The probability that during this walk no site is visited more than one is
	{\exyear{NET/JRF(DEC-2015)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $12 / 27$
		\task[\textbf{B.}] $27 / 64$
		\task[\textbf{C.}] $3 / 8$
		\task[\textbf{D.}] $9 / 16$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Total number of ways }&=4 \times 4 \times 4\\
		\text{Number of preferred outcome }&=4 \times 3 \times 3
		\intertext{$(\because$ Any four option in step-1 and only 3 option in step $2 \& 3$ because he can not go to previous position)}\\
		\text{probability }&=\frac{4 \times 3 \times 3}{4 \times 4 \times 4}=\frac{9}{16}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item  Let $X$ and $Y$ be two independent random variables, each of which follow a normal distribution with the same standard deviation $\sigma$, but with means $+\mu$ and $-\mu$, respectively. Then the sum $X+Y$ follows a
	{\exyear{NET/JRF(JUNE-2016)}}
	\begin{tasks}(1)
		\task[\textbf{A.}] Distribution with two peaks at $\pm \mu$ and mean 0 and standard deviation $\sigma \sqrt{2}$
		\task[\textbf{B.}]  Normal distribution with mean 0 and standard deviation $2 \sigma$
		\task[\textbf{C.}] Distribution with two peaks at $\pm \mu$ and mean 0 and standard deviation $2 \sigma$
		\task[\textbf{D.}] Normal distribution with mean 0 and standard deviation $\sigma \sqrt{2}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\mu^{\prime}&=\mu_{x}+\mu_{y}=\mu-\mu=0\\
		\sigma^{12}&=\sigma_{x}^{2}+\sigma_{y}^{2}=\sigma^{2}+\sigma^{2}\\
		\sigma^{\prime}&=\sqrt{2} \sigma
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item  A random variable $n$ obeys Poisson statistics. The probability of finding $n=0$ is $10^{-6}$. The expectation value of $n$ is nearest to
	{\exyear{NET/JRF(JUNE-2017)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] 14
		\task[\textbf{B.}] $10^{6}$
		\task[\textbf{C.}] $e$
		\task[\textbf{D.}] $10^{2}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\intertext{In Poisson's statistics the probability of finding the value $n$ is given by $P(n)=\frac{\mu^{n}}{n !} e^{-\mu}$}
		\intertext{The mean of Poisson's statistics is $\mu$. From the question}
		P(0)&=10^{-6} \Rightarrow 10^{-6}=\frac{\mu^{0}}{0 !} e^{-\mu} \Rightarrow e^{-\mu}=10^{-6}\\
		\text{	Talking Log of both sides, }-\mu&=-6 \ln 10 \Rightarrow \mu=6 \ln 10\\
		\text{Hence the expectation value of $n$ is }\mu&=6 \times 2.30=13.8 \approx 14
		\end{align*}
		So the correct answer is \textbf{Option (A)}
	\end{answer}
\item At each time step, a random walker in one dimension either remains at the same point with probability $\frac{1}{4}$, or moves by a distance $\Delta$ to the right or left with probabilities $\frac{3}{8}$ each. After $N$ time steps, its root mean squared displacement is
{\exyear{NET/JRF(JUNE-2019)}}
\begin{tasks}(4)
	\task[\textbf{A.}] $\Delta \sqrt{N}$
	\task[\textbf{B.}] $\Delta \sqrt{\frac{9 N}{16}}$
	\task[\textbf{C.}] $\Delta \sqrt{\frac{3 N}{4}}$
	\task[\textbf{D.}] $\Delta \sqrt{\frac{3 N}{8}}$
\end{tasks}
\begin{answer}
	\begin{align*}
	\intertext{For this problem it is evident from problem and given options}
	x_{\mathrm{rms}}&=k \sqrt{N}\\
	\text{If }N&=1 \text{ (Special case) (Let)}
	\intertext{Outcomes $1,0,-1$
		(times $\Delta$ ) with probability $\frac{3}{8}, \frac{1}{4}, \frac{3}{8}$}
	\left\langle x^{2}\right\rangle&=\sum P_{i} X_{i}^{2}=\frac{3}{8} \cdot 1+\frac{1}{4} \cdot 0+\frac{3}{8} \cdot 1\\&=\frac{3}{4} \Rightarrow x_{\text {rms }}=\sqrt{\frac{3}{4} \cdot 1}\\
	\text{So, option }&\Delta \sqrt{\frac{3 N}{4}}\text{ is correct.}
	\end{align*}
	So the correct answer is \textbf{Option (C)}
\end{answer}
	\item A box contains 5 white and 4 black balls. Two balls are picked together at random from the box. What is the probability that these two balls are of different colours?
	{\exyear{NET/JRF(DEC-2019)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{1}{2}$
		\task[\textbf{B.}] $\frac{5}{18}$
		\task[\textbf{C.}] $\frac{1}{3}$
		\task[\textbf{D.}] $\frac{5}{9}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\intertext{Probability that the two balls are of different colors}
		&5 W, 4 B\\
		&=\frac{5 C_{1} \times 4 C_{1}}{9 C_{2}}=\frac{\frac{5 !}{4 ! \times 1 !} \times \frac{4 !}{3 ! \times 1 !}}{\frac{9 !}{7 ! \times 2 !}}=\frac{5 \times 4}{\frac{9 \times 8}{2}}=\frac{5}{9}
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}
	\item  A particle hops randomly from a site to its nearest neighbour in each step on a square lattice of unit lattice constant. The probability of hopping to the positive $x$-direction is $0.3$, to the negative $x$-direction is $0.2$, to the positive $y$-direction is $0.2$ and to the negative $y$-direction is $0.3 .$ If a particle starts from the origin, its mean position after $N$ steps is
	{\exyear{NET/JRF(DEC-2019)}}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\frac{1}{10} N(-\hat{i}+\hat{j})$
		\task[\textbf{B.}] $\frac{1}{10} N(\hat{i}-\hat{j})$
		\task[\textbf{C.}] $N(0.3 \hat{i}-0.2 \hat{j})$
		\task[\textbf{D.}] $N(0.2 \hat{i}-0.3 \hat{j})$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		&\left\langle r_{i}\right\rangle \sum_{i} p_{i} r_{i}\\
		&=0.3 \hat{i}-0.2 \hat{i}+0.2 j-0.3 \hat{j}=0.1 \hat{i}-0.1 \hat{j}\\
		\text{For $N$ steps, }&=\frac{N}{10}[\hat{i}-\hat{j}]
		\end{align*}
		So the correct answer is \textbf{Option (B)}
	\end{answer}
	\item A basket consists of an infinite number of red and black balls in the proportion $p:(1-p)$. Three balls are drawn at random without replacement. The probability of their being two red and one black is a maximum for
	{\exyear{NET/JRF(JUNE-2020)}}
	\begin{tasks}(4)
		\task[\textbf{A.}]  $p=\frac{3}{4}$
		\task[\textbf{B.}] $p=\frac{3}{5}$
		\task[\textbf{C.}] $p=\frac{1}{2}$
		\task[\textbf{D.}] $p=\frac{2}{3}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		P&=p^{2}(1-p) \quad \\&\Rightarrow \frac{d P}{d p}=\frac{d}{d p} p^{2}(1-p)=0 \\&\Rightarrow p^{2}(-1)+(1-p) 2 p=0\\
		&\Rightarrow-p^{2}+2 p-2 p^{2}\\&=0 \Rightarrow 3 p^{2}=2 p \Rightarrow p=2 / 3
		\end{align*}
		So the correct answer is \textbf{Option (D)}
	\end{answer}

	\item An unbiased die is cast twice. The probability that the positive difference (bigger smaller) between the two numbers is 2 is
	{\exyear{ JEST 2012}}
	 \begin{tasks}(2)
		\task[\textbf{a.}]$\frac{1}{9}$
		\task[\textbf{b.}]$\frac{2}{9}$
		\task[\textbf{c.}] $\frac{1}{6}$
		\task[\textbf{d.}] $\frac{1}{3}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		p(2)&=\frac{n(E)}{n(S)}
		\intertext{The number of ways to come positive difference}
		&[(3,1),(4,2),(5,3),(6,4),(1,3)(2,4),(3,5)(4,6)]\\
		p(2)&=\frac{8}{36}=\frac{2}{9}
		\end{align*}
		So the correct answer is \textbf{Option (b)}
	\end{answer}
	\item A box contains 100 coins out of which 99 are fair coins and 1 is a double-headed coin. Suppose you choose a coin at random and toss it 3 times. It turns out that the results of all 3 tosses are heads. What is the probability that the coin you have drawn is the doubleheaded one?
	{\exyear{ JEST 2013}}
	 \begin{tasks}(2)
		\task[\textbf{a.}] $0.99$
		\task[\textbf{b.}]$0.925$
		\task[\textbf{c.}] $0.75$
		\task[\textbf{d.}] $0.01$
	\end{tasks}
	\begin{answer}
		So the correct answer is \textbf{Option (c)}
	\end{answer}
	\item There are on average 20 buses per hour at a point, but at random times. The probability that there are no buses in five minutes is closest to
	{\exyear{ JEST 2013}}
	 \begin{tasks}(2)
		\task[\textbf{a.}]$0.07$
		\task[\textbf{b.}] $0.60$
		\task[\textbf{c.}]$0.36$
		\task[\textbf{d.}] $0.19$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\intertext{From Poision's distribution function,}
		P(n)&=\frac{e^{-\lambda} \lambda^{n}}{\lfloor n}
		\text{here, }\lambda&=20\text{ buses per hour }\\
		\Rightarrow \lambda&=\frac{5}{3}\text{ buses in five minutes}
		\intertext{Therefore, the probability that there are no buses in five minutes,}
		P(n=0)&=\frac{e^{-\frac{5}{3}}\left(\frac{5}{3}\right)^{0}}{\lfloor 0}=e^{-5 / 3}=0.1886 \approx 0.19
		\end{align*}
		So the correct answer is \textbf{Option (d)}
	\end{answer}
	\item Two drunks start out together at the origin, each having equal probability of making a step simultaneously to the left or right along the $x$ axis. The probability that they meet after $n$ steps is
	{\exyear{ JEST 2013}}
	 \begin{tasks}(2)
		\task[\textbf{a.}]$\frac{1}{4^{n}} \frac{2 n !}{n !^{2}}$
		\task[\textbf{b.}] $\frac{1}{2^{n}} \frac{2 n !}{n !^{2}}$
		\task[\textbf{c.}] $\frac{1}{2^{n}} 2 n !$
		\task[\textbf{d.}]  $\frac{1}{4^{n}} n !$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{The probability of taking ' $r$ '}&\text{ steps out of $N$ steps }={ }^{N} C_{r}\left(\frac{1}{2}\right)^{r}\left(\frac{1}{2}\right)^{N-r}\\
		\text{Total steps }&=N=n+n=2 n
	\intertext{	For taking probability of $n$ steps out of $N$}
	P={ }^{N} C_{n}\left(\frac{1}{2}\right)^{n}\left(\frac{1}{2}\right)^{N-n}&=\frac{N !}{(N-n) ! n !}\left(\frac{1}{2}\right)^{n}\left(\frac{1}{2}\right)^{N-n}=\frac{2 n !}{n ! n !}\left(\frac{1}{2}\right)^{2 n}=\frac{2 n !}{(n !)^{2} 4^{n}}
		\end{align*}
		So the correct answer is \textbf{Option (a)}
	\end{answer}
	\item If two ideal dice are rolled once, what is the probability of getting atleast one '6'?
	{\exyear{ JEST 2015}}
	 \begin{tasks}(2)
		\task[\textbf{a.}]$\frac{11}{36}$
		\task[\textbf{b.}]$\frac{1}{36}$
		\task[\textbf{c.}]$\frac{10}{36}$
		\task[\textbf{d.}]  $\frac{5}{36}$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\text{Number of point}&\text{ in sample space } n(S)=11\\
		[(1,6),(2,6),(3,6),&(4,6),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6)]\\
	\text{	Number of point }&\text{in population }n(P)=6^{2}=36
		\intertext{Probability of getting atleast one '6' on face of dice} &=\frac{n(S)}{n(P)}=\frac{11}{36}
		\end{align*}
			So the correct answer is \textbf{Option (a)}
	\end{answer}
	\item The mean value of random variable $x$ with probability density $p(x)=\frac{1}{\sigma \sqrt{2 \pi}} . \exp \left[-\frac{\left(x^{2}+\mu x\right)}{\left(2 \sigma^{2}\right)}\right]$ is:
	{\exyear{ JEST 2016}}
	 \begin{tasks}(2)
		\task[\textbf{a.}]0
		\task[\textbf{b.}]$\frac{\mu}{2}$
		\task[\textbf{c.}] $\frac{-\mu}{2}$
		\task[\textbf{d.}]  $\sigma$
	\end{tasks}
	\begin{answer}
		\begin{align*}
		\langle x\rangle=\frac{1}{\sigma \sqrt{2 \pi}} \int_{-\infty}^{\infty} x \exp \left(-\frac{x^{2}}{2 \sigma^{2}}\right) d x \int_{-\infty}^{\infty} \exp \left(-\frac{\mu x}{2 \sigma^{2}}\right) d x=0 \quad\text{ (due to odd function)}
		\end{align*}
		So the correct answer is \textbf{Option (a)}
	\end{answer}
\item Suppose that we toss two fair coins hundred times each. The probability that the same number of heads occur for both coins at the end of the experiment is
{\exyear{ JEST 2017}}
 \begin{tasks}(2)
	\task[\textbf{a.}]$\left(\frac{1}{4}\right)^{100} \sum_{n=0}^{100}\left(\begin{array}{c}100 \\ n\end{array}\right)$
	\task[\textbf{b.}] $2\left(\frac{1}{4}\right)^{100} \sum_{n=0}^{100}\left(\begin{array}{c}100 \\ n\end{array}\right)^{2}$
	\task[\textbf{c.}]$\frac{1}{2}\left(\frac{1}{4}\right)^{100} \sum_{n=0}^{100}\left(\begin{array}{c}100 \\ n\end{array}\right)^{2}$
	\task[\textbf{d.}] $\left(\frac{1}{4}\right)^{100} \sum_{n=0}^{100}\left(\begin{array}{c}100 \\ n\end{array}\right)^{2}$
\end{tasks}
\begin{answer}
	\begin{align*}
	\intertext{ If we toss one fair coins hundred times, then probability of $n$ number of head occurs at the end of 100 times is}
	&{ }^{100} C_{n}\left(\frac{1}{2}\right)^{n}\left(\frac{1}{2}\right)^{100-n}
\intertext{	Hence, the probability that same number of heads occur for both coins at the end of experiment is}
&\sum_{n=0}^{100}\left({ }^{100} C_{n}\left(\frac{1}{2}\right)^{100}\right) \cdot\left({ }^{100} C_{n}\left(\frac{1}{2}\right)^{100}\right)=\sum_{n=1}^{100}\left({ }^{100} C_{n}\right)^{2}\left(\frac{1}{2}\right)^{200}=\left(\frac{1}{4}\right)^{100} \sum_{n=1}^{100}\left({ }^{100} C_{n}\right)^{2}
	\end{align*}
		So the correct answer is \textbf{Option (d)}
\end{answer}
\item An electronic circuit with 10000 components performs its intended function success fully with a probability $0.99$ if there are no faulty components in the circuit. The probability that there are faulty components is $0.05$. if there are faulty components, the circuit perform successfully with a probability $0.3$. The probability that the circuit performs successfully is $\frac{x}{10000}$. What is $x$ ?
{\exyear{ JEST 2018}}
\begin{answer}
So the correct answer is  \textbf{9555}
\end{answer}
\item A person plans to go from town $A$ to town $B$ by taking either the route $(R 1+R 2)$ with probability $\frac{1}{2}$ or the route $(R 1+R 3)$ with probability $\frac{1}{2}$ (see figure). Further, there is a probability $\frac{1}{3}$ that $R 1$ is blocked, a probability $\frac{1}{3}$ that $R 2$ is blocked, and a probability $\frac{1}{3}$ that $R 3$ is blocked. What is the probability that he/she would reach town $B$ ?
{\exyear{ JEST 2019}}
\begin{figure}[H]
	\centering
	\includegraphics[height=3cm,width=5.5cm]{JEST-58-2019}
\end{figure}
 \begin{tasks}(2)
	\task[\textbf{a.}]$\frac{8}{9}$
	\task[\textbf{b.}]$\frac{1}{3}$
	\task[\textbf{c.}]$\frac{4}{9}$
	\task[\textbf{d.}]$\frac{2}{3}$
\end{tasks}
\begin{answer}
	\begin{align*}
	\text{Given that probability of}&\text{ $R 1$ blocked }=1 / 3\\
	\text{Probability of $R 1$ }&\text{not blocked }=1-\frac{1}{3}=\frac{2}{3}\\
	\text{Probability from $A$ to $B$ }&\text{without restriction }=\frac{1}{2}\\
	\text{Route $R 2$ probability }&=\frac{1}{2} \times \frac{2}{3}\text{ not blocked}\\
	\text{Route }R 3&=\frac{1}{2} \times \frac{2}{3}\\
\text{	Total probability }(A \rightarrow B)&=\frac{2}{3}\left[\frac{1}{2} \times \frac{2}{3}+\frac{1}{2} \times \frac{2}{3}\right]=\frac{4}{9}
	\end{align*}
	So the correct answer is \textbf{Option (c)}
\end{answer}

\end{enumerate}

CSIR- Mathematical Physics/chapter/Probability Problem set Solutions.tex


\section{Background on DSP-PBE}

We give a short introduction on Digital Signal Processing Programming-By-Example (DSP-PBE).
The idea of DSP-PBE was introduced in~\cite{SantolucitoFARM}.

\subsection{Aural Distance as a Metric Space}

One of the key components in machine learning systems is having a metric that to quantifies how close the candidate solution is to the desired solution.
In the case of programming-by-example, the desired solution is defined by the user-provided input-output example pairs.
At a high-level, our goal is to minimize the distance, $dist$, between the output of the candidate function, $F$, on the provided input, $I$, and the provided output $O$.
Put mathematically, we have a minimization problem: $min (dist ( F(I)), O)$.

Previous work proposed using a distance metric between two audio files based on constellation plots of multiple FFT plots over the samples~\cite{SantolucitoFARM}.
We adopt the same distance metric for this work.

\url{https://en.wikipedia.org/wiki/Short-time_Fourier_transform#Sliding_DFT}

\subsection{Gradient Descent}

We use a modified version of gradient descent to find good parameters for the filter.
Have to decide how much to go into this - probably too technical for ICMC, but there are few key points worth highlight (e.g. distance metric needs to be convex-ish).
Should we include a similar convexity graph as in FARM?

\section{DSP-PBE for analog effects}

In order to use DSP-PBE there are a number of new challenges that we must overcome.
First, we need a more efficient way of choosing an initial point for gradient descent.
Second, we must have a way to automatically explore different structures of DSP programs.
Finally, a priority for this system is usability by novice DSP programmers, so we needed our tool to have the ability to generate program code that could be immediately run.

\markk{system image here specialized on analog effects}

\subsection{Choosing an initial point for Gradient Descent}

As a very rough estimate, if the max freq peak of the output is less than the max freq peak of input
  we need a lpf, and it should have a value a bit less than the max peak of output

\subsection{Searching for DSP structure}

\markk{need an image of a DSP program structures (demonstrating both sequential and parallel composition) along with the program code to describe this structure. Ideally made in tikz, but if we run out of time, a screenshot of Max/PD/google slides will work too}

Just brute force for now - maybe guided by user?
Something smarter can come later.

\subsection{Generating Program Code}

\markk{how to we turn the solution into a PD/MaxMSP/SuperCollider program (see TODO on github, still need to actually code this). Need to be sure to mention that this was not possible in previous FARM paper.}

\samm{added some words on the conversion to supercollider code}

In order to make the filter generated by the DSP-PBE program usable by computer musicians we have implemented a translation scheme to convert the in-program representation of the filter to raw SuperCollider code.


\documentclass{article}

\usepackage{fullpage}
\usepackage{textcomp}
\usepackage{graphicx}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\cfoot{\sc Page \thepage\ of \pageref{end}}

\begin{document}

{\large \noindent{}University of Toronto at Scarborough\\
\textbf{CSC A67/MAT A67 - Discrete Mathematics, Fall 2015}}

\section*{\huge Exercise \#5: Logic}

{\large Due: November 6, 2015 at 11:59 p.m.\\
This exercise is worth 3\% of your final grade.}\\[1em]
\textbf{Warning:} Your electronic submission on MarkUs affirms that this exercise is your own work and no
one else's, and is in accordance with the University of Toronto Code of Behaviour on Academic Matters,
the Code of Student Conduct, and the guidelines for avoiding plagiarism in CSC A67/MAT A67.\\[1ex]
This exercise is due by 11:59 p.m. November 6. Late exercises will not be accepted.\\[1ex]
\renewcommand{\labelenumi}{\arabic{enumi}.}
\renewcommand{\labelenumii}{(\alph{enumii})}
\begin{enumerate}
\item If\marginpar{[3]} $k$: ``A bell has been rung", $m$: ``There is meat in the room", and $n$: ``The dogs are salivating",\\
which of the following statements are equivalent to $\neg n\to\neg(m\vee k)$?
	\renewcommand{\labelenumii}{\roman{enumii})}\begin{enumerate}
	\item $\neg n\to\neg m\vee k$
	\item ``If the dogs are not salivating, then there is no meat in the room, or a bell has not been rung."
	\item ``If the dogs are not salivating, then there is no meat in the room and a bell has not been rung."
	\item $n\to (m\vee k)$
	\item $\neg n\to \neg m\vee\neg k$
	\item $\neg n\to \neg m\wedge\neg k$
	\item ``If the dogs are not salivating, then a bell has not been rung, nor is there meat in the room."
	\item $n\vee\neg m\wedge\neg k$
	\item $\neg(m\vee k)\vee n$
	\item ``There is no meat in the room, nor has a bell been rung, if the dogs are not salivating."
	\item ``A bell has not been rung, or there is no meat in the room, if the dogs are not salivating."
	\item ``If a bell has not been rung and there is no meat in the room, then the dogs are not salivating."
	\item ``For the dogs to be salivating, it is sufficent and necessary that a bell has been rung and that there is meat in the room."
	\item $\neg (m\vee k)\to\neg n$
	\item ``For the dogs to be salivating, it is necessary that a bell has been rung and that there is meat in the room."
	\item ``If the dogs are salivating, then a bell has been rung or there is meat in the room."
	\item $n\to m\wedge k$
	\item $\neg n\to m\wedge k$
	\item $n\vee\neg(m\vee k)$
	\item $\neg(m\vee k)\to\neg(\neg n)$
	\end{enumerate}\pagebreak
\item Construct\marginpar{[8]} truth tables for the following statements:
	\renewcommand{\labelenumii}{(\alph{enumii})}\begin{enumerate}
	\item $a\vee b\to\neg b$
	\item $a\wedge b\wedge c$
	\item $\neg\neg a\vee a$
	\item $a\vee b\to a\vee b$
	\end{enumerate}
\item Shade\marginpar{[8]} the regions of a Venn diagram where each of the following statements is true:
	\begin{enumerate}
	\item $\neg p\to q$
	\item $p\leftrightarrow q\vee\neg q$
	\item $\neg p\vee \neg q\wedge p$
	\item $(p\wedge q)\vee(r\wedge q)$
	\end{enumerate}
\item \begin{enumerate}
	\item Negate\marginpar{[4]} the statement ``Sarah has a spaceship and has three fingers and is from Venus" and convert it to formal logic.
	\item Simplify the statement from \textbf{(a)} as much as possible using equivalence rules.
	\end{enumerate}
\item Using\marginpar{[3]} the statements\\
	\begin{tabular}{p{0.47\textwidth}p{0.5\textwidth}}
	\begin{description}
	\item[A:] ``Your vehicle has a District No. 64 permit"
	\item[B:] ``It is between 8am and 9am"
	\item[C:] ``It is a school day (Monday to Friday)"
	\item[D:] ``You have been parked for less than 2 hours during legal hours"
	\item[E:] ``It is between 8am and 6pm"
	\item[F:] ``You have been parked for less than 5 minutes during legal hours"
	\item[G:] ``It is Monday"
	\item[H:] ``You are left of the sign"
	\item[I:] ``It is between 1:30pm and 4pm"
	\item[J:] ``It is between 9am and 10am"
	\item[K:] ``It is after 4pm"
	\item[L:] ``It is after 1pm"
	\end{description}
	create a formal logic statement that is true any time you are allowed to park near the following parking signs, and false any time it is illegal to park there:&
	\begin{center}
	\includegraphics[width=0.4\textwidth,clip,trim=5cm 5cm 5cm 6cm]{Formal-Logic-Exercise-parkingSigns.JPG}
	\end{center}
	\end{tabular}
\end{enumerate}
\hrulefill\\
\noindent[Total: 26 marks]\label{end}

\end{document}


The quasi-linear forms written above enable the particularization of the theory of first order quasi-linear systems developed in section \ref{sec:PDEs} to solid mechanics problems. %Once the characteristic analysis of systems \eqref{eq:HE_quasilinear}, \eqref{eq:HPP_quasi-linear} and \eqref{eq:EP_quasilinear} will be carried out, a particular type of IVP which is of particular interest in this manuscript will be introduced.

%The theory of first order quasi-linear system is now applied to solid dynamics through the characteristic analysis of systems \eqref{eq:HE_quasilinear}, \eqref{eq:HPP_quasi-linear} and \eqref{eq:EP_quasilinear} is performed in this section. Second, a particular type of IVP that will be considered in the rest of the manuscript is introduced.
%\subsection{Characteristic structure of solutions}
For the sake of simplicity, studies of finite deformations and linearized geometrical frameworks will be condensed in this part by using the generic notation of stress $\tens{S}$ and vectors written in the reference configuration.
Furthermore, instead of considering multi-dimensional systems of conservation laws, we will focus without loss of generality on the quasi-linear form \eqref{eq:HE_quasilinear} projected on an arbitrary direction $\vect{N}=\[\vect{E}_1,\vect{E}_2,\vect{E}_3\]$ \cite[p.425-426]{Leveque}. In this direction, one has:
\begin{equation}
  \label{eq:normal_quasi}
  \Qcb_t + \Jbsf \drond{\Qcb}{X_N} = \Scb
\end{equation}
in which $X_N=\vect{X}\cdot\vect{N}$ and the \textit{Jacobian matrix} $\Jbsf = \Absf^\alpha N_\alpha$ of dimension $m$ arise.
In three dimensions, the non-symmetrical PK1 tensor in quasi-linear form \eqref{eq:HE_quasilinear} yields a Jacobian matrix of dimension $m=3+9$ while systems \eqref{eq:HPP_quasi-linear} and \eqref{eq:EP_quasilinear} involving the Cauchy tensor, lead to $m=3+6$.
The characteristic analysis of system \eqref{eq:normal_quasi} is therefore equivalent to that of linear combinations of matrices $\Absf^\alpha$. With the previous developments, the Jacobian matrix reads:
\begin{equation}
  \label{eq:jacobian_generic}
  \Jbsf=-\matrice{\tens{0}^2 & \frac{1}{\rho_0}\tens{I}\otimes \vect{N} \\  \tilde{\Hbb}\cdot\vect{N} & \tens{0}^4 }
\end{equation}
in which $\tilde{\Hbb}$ is either the hyperelastic or elastoplastic tangent modulus, or the elastic stiffness tensor depending on the case considered. The characteristic structure of the problem is given by the $m$ eigenvalues $c_K$ and associated left eigenvectors $\Lcb^K= \[ \vect{v}^K \: , \: \tens{S}^K \]$ of the Jacobian matrix satisfying:
\begin{equation}
  \label{eq:eigen_system}
  \vect{\Lc}^K \(\Jbsf - c_K \Ibsf\) = \vect{0}
\end{equation}
where $\Ibsf$ is the $m\times m$ identity matrix. Thus, for non-zero eigenvalues one gets:
\begin{subequations}
  \begin{alignat}{1}
    \label{eq:eigen_left_stress}
    & -\tens{S}^K:\(\tilde{\Hbb}\cdot  \vect{N}\) - c_K  \vect{v}^K =\vect{0} \\
    \label{eq:eigen_left_velo}
    & -\frac{1}{\rho_0}\vect{v}^K\otimes\vect{N} - c_K \tens{S}^K = \tens{0}
  \end{alignat}
\end{subequations}
Substitution of $\tens{S}^K$ obtained from \eqref{eq:eigen_left_velo} in \eqref{eq:eigen_left_stress} leads to:
\begin{equation}
  \label{eq:acoustic_eigen}
 (\vect{v}^K\otimes\vect{N}):\(\tilde{\Hbb}\cdot  \vect{N}\) - \rho_0c_K^2 \vect{v}^K = \tens{0}
\end{equation}
which is the left eigensystem of the acoustic tensor $A_{ij}=N_\alpha \tilde{H}_{i\alpha j \beta}  N_\beta$. Due to the symmetry of $\tens{A}$, system \eqref{eq:acoustic_eigen} is equivalent to the right eigensystem:
\begin{equation}
  \label{eq:acoustic_eigen_system_lambda}
  \(  N_\alpha \tilde{H}_{i\alpha j \beta}  N_\beta - \rho_0 c_K^2 \delta_{ij} \) v_j^K =0
\end{equation}
or alternatively, with the eigenvalues $\omega_p$ and associated eigenvectors of the acoustic tensor $\vect{l}^p\: \: (p=1,2,3)$:
\begin{equation}
  \label{eq:acoustic_eigen_system}
   \( \tens{A} - \omega_p \tens{I} \) \cdot \vect{l}^p  = \vect{0}
\end{equation}
The condition for system \eqref{eq:normal_quasi} to be hyperbolic (real eigenvalues and independent eigenvectors) is thus ensured by the positive definiteness of the acoustic tensor, also known as the \textit{strong ellipticity} condition \cite{Foundation_of_elasticity}:
\begin{equation}
  \label{eq:strong_ellipticity}
  (\vect{n}\otimes \vect{N}): \tilde{\Hbb}: (\vect{n}\otimes \vect{N}) > 0 \quad \forall \vect{N},\vect{n} \in \Rbb^3 \: ; \: \vect{N},\vect{n} \ne \vect{0}
\end{equation}
If the condition holds, the acoustic tensor admits $3$ couples of eigenvalue--eigenvector $\{\omega_p,\vect{l}^p\}$ leading to $6$ couples $\{c_K,\Lcb^K\}$ for the Jacobian matrix, the $6$ other eigenvalues being null \cite{Kluth}.
The couples $\{c_K,\Lcb^K\}$ are referred to as \textit{left characteristic fields}.
The left eigenvectors associated with non-zero eigenvalues of the Jacobian matrix are obtained by using equation \eqref{eq:eigen_left_velo} so that the following $6$ eigenfields of the quasi-linear form \eqref{eq:normal_quasi} can be defined:
\begin{equation}
  \label{eq:left_eigenfields}
    \left\lbrace \pm \sqrt{\frac{\omega_p}{\rho_0}} ; \quad \[\: \pm \rho_0\sqrt{\frac{\omega_p}{\rho_0}} \vect{l}^p , -\vect{l}^p\otimes \vect{N} \:\]  \right\rbrace ,\quad p=1,2,3
\end{equation}
At last, one has to find six independent left eigenvectors associated with the null eigenvalue of multiplicity $6$ by solving equation \eqref{eq:eigen_left_stress} for the null eigenvalue:
\begin{equation}
  \label{eq:left_null_eigenvectors}
  \tens{S}^K:\(\tilde{\Hbb}\cdot  \vect{N}\) =\vect{0},\quad K=1,...,6
\end{equation}
Following the same procedure for right eigenvectors $\Rcb^K=\matrice{\vect{v}^K \\ \tens{S}^K}$, the Jacobian matrix right eigensystem reads:
\begin{subequations}
  \begin{alignat}{1}
    \label{eq:eigen_right_stress}
    & -\frac{1}{\rho_0}\tens{S}^K\cdot  \vect{N} - c_K  \vect{v}^K =\vect{0} \\
    \label{eq:eigen_right_velo}
    & -\tilde{\Hbb}:\(\vect{v}^K\otimes\vect{N}\) - c_K \tens{S}^K = \tens{0}
  \end{alignat}
\end{subequations}
which leads to the \textit{right characteristic fields} associated with the non-null eigenvalues:
\begin{equation}
  \label{eq:right_eigenfields}
  \left\lbrace \pm \sqrt{\frac{\omega_p}{\rho_0}} ; \quad \[\: \pm \sqrt{\frac{\omega_p}{\rho_0}} \vect{l}^p , -\tilde{\Hbb}:\( \vect{l}^p\otimes \vect{N}\) \:\]  \right\rbrace ,\quad p=1,2,3
\end{equation}
In equation \eqref{eq:right_eigenfields}, $\{\omega_p,\vect{l}^p\}$ still denotes the eigenfields of the acoustic tensor. Moreover, the $6$ independent right eigenvectors associated with the zero eigenvalue required to complete the set of right characteristic fields must satisfy:
\begin{equation}
  \label{eq:right_null_eigenvectors}
  \tens{S}^K \cdot  \vect{N} =\vect{0},\quad K=1,...,6
\end{equation}

\begin{remark}
  Since the right-hand side of equation \eqref{eq:normal_quasi} is not involved in the characteristic analysis, linear elasticity and elasto-viscoplasticity in small strains share the same characteristic structure.
\end{remark}

\begin{remark}
  \label{rq:similarity_solution}
  In the case of a vanishing source term $\Scb$, the specialization of characteristic equations \eqref{eq:PDEs_ODEs} to system \eqref{eq:normal_quasi} leads to:
\begin{equation}
  \label{eq:characteristic_equations_homogeneous}
  \Lcb^K \cdot d\Qcb = \vect{0},\quad K=1,...,6
\end{equation}
meaning that the solution is constant along each characteristic straight line with slope $\xi = c_K$. Such solutions $\Qcb(\xi)$ that only depend on the ray $\xi$ are called \textit{self-similar solutions}.
\end{remark}

%%% Local Variables:
%%% mode: latex
%%% ispell-local-dictionary: "american"
%%% TeX-master: "../mainManuscript"
%%% End:


\chapter{Algorithms}
\label{chap:Algorithm}

This chapter will cover the various algorithms, including interpolation, that are required for either preparing data or in the evaluation of the equations outlined in \Cref{chap:2ndOrdCalc}.

The reason for the interpolation is that the number of frequencies given in the WAMIT output files (on the order of tens of frequencies) is not likely going to correspond to the number of wave frequencies actually used by HydroDyn (on the order of hundreds to thousands), nor does the WAMIT output necessarily have to be equally discretized or even complete.  The WAMIT output files may be very sparcely populated.  So, it is necessary to interpolate in order to find the missing ones.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{FFT and IFFT}
\label{sec:Algorithm:FFT}

The FFT (or discrete Fourier transform -- DFT) and inverse FFT used in \HD are found in the FFTPACK version 4.1 from UCAR/NCAR.  For a given discretized function, for example the complex wave form $Z[k]$ in the frequency domain, the inverse fourier transform to the time domain can be written as:
\begin{equation}
   z(t_n) = \frac{1}{N} \sum\limits_{k=-\frac{N}{2}+1}^{\frac{N}{2}} Z[k] e^{i\omega_k t_n} = \Re\left\{\sum\limits_{k=1}^{N'}a_k e^{iw_k t_n}\right\},
\label{eq:IFFTofZ}
\end{equation}
where $N$ is given in \Cref{eq:N} as
\begin{equation}
   N=\frac{2 \pi}{\Delta t \Delta \omega} = \frac{t_\text{max}}{\Delta t} = \frac{2 \omega_\text{max}}{\Delta \omega} = 2(N'+1).
\label{eq:IFFT_N}
\end{equation}
In \Cref{eq:IFFTofZ}, the expression for the first summation is what is used within \HD and the second summation expression is used in some of Tiago's writings.  The relationship between $Z[k]$ and $a_k$ can be written as
\begin{equation}
   Z[k] =
         \begin{dcases*}
            \frac{N a_k}{2}            & $k=1 ~\ldots~ N/2-1$\\
            0                          & $k=0$ and $k=N/2$\\
            \frac{N a^*_{|k|}}{2}      & $k=-N/2+1 ~\ldots~ -1$\\
         \end{dcases*}
\label{eq:IFFT_Zk_ak}
\end{equation}
where $a^*$ is the complex conjugate of $a$.

\subsection{Numerical Evaluation of IFFT}
In the evaulation of \Cref{eq:IFFTofZ} in \HD to yield the wave height as a function of time, the IFFT is evaluated as
\begin{equation}
   z(t_n) = \frac{1}{N} \sum\limits_{k=-\frac{N}{2}+1}^{\frac{N}{2}} Z[k] e^{i\omega_k t_n} = \operatorname{IFFT}\left(Z[k]\right)
\label{eq:IFFTofZ:eval}
\end{equation}
where the IFFT is only evaluated over $k = 0 \ldots N/2$ (the negative frequencies are evaluated internally following the relationships in \Cref{eq:IFFT_Zk_ak}).  The normalization constant of $1/N$ is also handled by the IFFT subroutines and is set by the initialization of the IFFT.

There are some constraints imposed on what $N$ can be because of the IFFT solver used.  $N$ must be even, and preferably a product of small prime numbers for speed.  Additionally, $Z[k=0] = 0$ and $Z[k=N/2] = 0$ must be specified.

\subsection{$Z[k]$ in \HD}
In \HD the complex wave form in frequency space, $Z[k]$, is given as
\begin{equation}
   Z[k] = W[k]\sqrt{\frac{2\pi}{\Delta t} S^\text{2-sided}_\zeta (\omega_k)} = W[k]\sqrt{N \Delta \omega S^\text{2-sided}_\zeta (\omega_k)}
\label{eq:Zk}
\end{equation}
where
\begin{equation}
   W[k]  = \sqrt{\frac{N}{2}}\sqrt{-2 \ln \left( U_1[k]\right)}e^{i 2\pi U_2[k]},
\label{eq:Wk}
\end{equation}
the DFT of gaussian white noise using the Box-Muller method, and $S^\text{2-sided}_\zeta(\omega_k)$ is the two sided power spectral density (PSD) of the wave elevation per unit time.\footnote{Note that Tiago uses $a_k = \sqrt{2\Delta \omega S^\text{1-sided}_\zeta(\omega_k)}e^{i 2\pi U_k}$ which is a simplification where $U_k = U_2[k]$ and $\sqrt{-2 \ln\left(U_1[k]\right)} = \sqrt{2}$.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Interpolation}
\label{sec:Algorithm:Interp}
Four interpolation algorithms are required for the \modname{WAMIT2} module: two three-dimensional and two four-dimensional interpolations.
For each set of 3D and 4D interpolation algorithms, a linear interpolation for full arrays and a linear interpolation for sparse arrays are needed.  Due to the complexity of implimenting an interpolation over sparse arrays and time constraints in the development schedule, a placeholder will be created for it with an error message stating that an interpolation scheme for sparse arrays is not implimented at this time.  The WAMIT output file reading algorithm is developed in such a way that an unordered sparse array can be read in and stored (see \Cref{sec:WamitOuput:Read}).

The first order wave forces calculated within the \fname{WAMIT} module is interpolated with a linear method.  In light of this and considering time constraints on the development, we will use linear interpolation algorithms for now.  If time permits, we might investigate possibilities for other interpolation algorithms that will produce smooth surfaces is cubic interpolation (and smoothly continuous derivatives) or better allow for sparse data.
%The issue with this interpolation method is that it tends to produce overshoot which may be undesirable.  Algorithms which could minimize the effects of overshoot such as cosine fitting, radial basis function weighting, and hermite polynomial among others.  In the ideal world, one would choose an algorithm that will best fit the data.  In this case, this is not obvious considering that the general shape of the data will depend on what floating platform is used.

\subsection{3D Interpolation}
\label{sec:interp:3d}
\subsubsection{Full array interpolation}
A three-dimensional linear interpolation routine is available in the \modname{InflowWind} module.  This routine was written specifically for the full field wind files, so it will require some modification to generalize it.

\subsubsection{Sparse array interpolation}
\label{sec:interp:3d:sparse}
Due to time constraints in the development schedule, a placeholder subroutine will be created that tells the user that this is a currently unavailable feature.  The user can then use an external data manipulation program to do the interpolation on their WAMIT output to create a full array (that can be unordered) that can be read in.  The WAMIT output file reading algorithm will accomodate the reading either a full array (both the upper and lower triangle of the QTF) or partial array (upper half only, or a mix of upper and lower).  This routine will expect a mask array (boolean?) of identical size to the sparse array that indicates which elements of the data array are missing.

By creating this placeholder subroutine, we give ourselves the option of creating this interpolation scheme as time permits with the ability to handle a limited sparseness of the QTF array (\emph{i.e.} no more than a two step gap in any dimension).

\subsection{4D Interpolation}
\label{sec:interp:4d}
\subsubsection{Full array interpolation}
At present we do not have any four-dimensional interpolation routines.  A four-dimensional linear interpolation should be fairly simple to extend from the three-dimensional one.

\subsubsection{Sparse array interpolation}
\label{sec:interp:4d:sparse}
See \Cref{sec:interp:3d:sparse}.


\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{bm} %for bold lower greek symbols

\title{Feed Forward Neural Network}
\author{Masahiro Ogawa}

\begin{document}
\maketitle

\section{Notation}
\begin{itemize}
\item $\bm{x}$ : vector
\item $\bm{A}$ : matrix
\item ${}^t\bm{A}$ : transpose of $\bm{A}$
\item $f.(\bm{A}) =
 \begin{pmatrix}
  f(a_{1,1}) & f(a_{1,2}) & \cdots & f(a_{1,n}) \\
  f(a_{2,1}) & f(a_{2,2}) & \cdots & f(a_{2,n}) \\
  \vdots  & \vdots  & \ddots & \vdots  \\

  f(a_{m,1}) & f(a_{m,2}) & \cdots & f(a_{m,n})
 \end{pmatrix}$ : for $m \times n$ matrix $\bm{A}$
\item $(\bm{A}.*\bm{B})_{i,j} = a_{i,j}b_{i,j}$
\item $\bm{1}_{m\times n} =
  \begin{pmatrix}
    1 & \cdots & 1 \\
    \vdots & \ddots & \vdots \\
    1 & \cdots & 1
  \end{pmatrix}$ : $m \times n$ dim
\item $\bm{\tilde{y}} =
  \begin{pmatrix}
    1 \\
    \bm{y}
  \end{pmatrix}$ : homogeneous vector

\subsection{variables}
\item $n$ : data number
\item $N$ : total number of data
\item $l$ : layer number
\item $L$ : output layer number. thus, the total number of layer is $L+1$
\item $k$ : learning iteration number
\item $K$ : final number of the learning iteration
\item $J$ : cost
\item $f(x)$ : activation function
\item $\bm{X}_0 = (\bm{x}_{0,1},\dots , \bm{x}_{0,N}) =
  \begin{pmatrix}
    x_{0,1,1} & \cdots & x_{0,1,N} \\
    \vdots & \ddots & \vdots\\
    x_{0,d_0,1} & \cdots & x_{0,d_0,N}
  \end{pmatrix}$
  : $d_0 \times N$ dim : input vectors
\item $\bm{B} = (\bm{b}_1,\dots , \bm{b}_N) =
  \begin{pmatrix}
    b_{1,1} & \cdots & b_{1,N}\\
    \vdots & \ddots & \vdots\\
    b_{d_L,1} & \cdots & b_{d_L,N}
  \end{pmatrix}
  $ : $d_L \times N$ dim : instruction signals
\item $\bm{\tilde{W}}_l(k) =
  (\bm{\tilde{w}}_{l,1}(k),\dots , \bm{\tilde{w}}_{l,d_{l+1}}(k)) =
  \begin{pmatrix}
    \tilde{w}_{l,0,1}(k) & \cdots & \tilde{w}_{l,0,d_{l+1}}(k)\\
    \vdots & \ddots & \vdots\\
    \tilde{w}_{l,d_l,1}(k) & \cdots & \tilde{w}_{l,d_l,d_{l+1}}(k)
  \end{pmatrix}
  $ :
  weights : $(d_l+1) \times d_{l+1}$ dim
\end{itemize}

\section{Input}
parameters
\begin{itemize}
\item $L$ : output layer number.
\item $\{ d_l \}_{l=0}^L$ : neuron numbers in each layer $l$
\item $\{\bm{\tilde{W}}_l(0)\}_{l=0}^L \mid \bm{\tilde{W}}_l(0) =
  (\bm{\tilde{w}}_{l,1}(0),\dots , \bm{\tilde{w}}_{l,d_{l+1}}(0))$ :
  $(d_l+1) \times d_{l+1}$ dim : initial weights
\item $\rho$ : learning rate parameter
\item $T_e$ : error threshold
\item $T_K$ : threshold of the maximum number of iteration
\end{itemize}

data
\begin{itemize}
\item $\bm{X}_0 = (\bm{x}_{0,1},\dots , \bm{x}_{0,N})$ : $d_0 \times N$ dim : input vectors
\item $\bm{B} = (\bm{b}_1,\dots , \bm{b}_N)$ : $d_L \times N$ dim : instruction signals
\end{itemize}

\section{Output}
\begin{itemize}
\item $\{\bm{\tilde{W}}_l(K)\}_{l=0}^L \mid \bm{\tilde{W}}_l(K) = (\bm{\tilde{w}}_{l,1}(K),\dots ,
  \bm{\tilde{w}}_{l,d_{l+1}}(K))$ : $(d_l+1) \times d_{l+1}$ dim :
  final (after $K$th iteration) learned weights
\item $\bm{Y}_L = (\bm{y}_{L,1},\dots , \bm{y}_{L,N})$ :
  $dL \times N$ dim : output vectors
\item $J = \sum_{n=1}^N J_n$
  : cost, final error
\end{itemize}

\section{Algorithm(Iterative version, Stochastic Gradient descent)}
\begin{figure}
  \centering
  \includegraphics[width=1.0\textwidth]{neuralnet_architecture}
  \caption{Neural network architecture}
  \label{fig:nn}
\end{figure}
\subsection{Iteration}
Iterate feed forward and back propagation algorithm for each input
datum until the error become smaller than the threshold or the
iteration number exceeds the
maximum iteration number.
That is,
\begin{equation}
  J < T_e \mbox{, or } k < T_K.
\end{equation}

The iteration means update of the weights as,
\begin{eqnarray}
  \tilde{w}_{lij}(k+1)
  = \tilde{w}_{lij}(k) - \rho \frac{\partial J_n}{\partial
    \tilde{w}_{lij}(k)} \\
 \iff \bm{\tilde{W}}_l(k+1)
  = \bm{\tilde{W}}_{l}(k) - \rho \frac{\partial J_n}{\partial
    \bm{\tilde{W}}_{l}(k)} \label{eq:weight_update}
\end{eqnarray}
From this equation, once we could compute $\frac{\partial J_n}{\partial
  \bm{\tilde{W}}_{l}(k)}$, we can update the weights.
Therefore we concentrate on computing $\frac{\partial J_n}{\partial
  \bm{\tilde{W}}_{l}(k)}$ below.

\subsection{Feed Forward}
The below are true for all data and all iteration,
so we omit the data number $n$ and iteration number $k$ as
e.g.\ $\bm{x}_{l,n} = \bm{x}_l, \bm{\tilde{W}}_l(k) = \bm{\tilde{W}}_l$.
\begin{eqnarray}
  \bm{x}_l = {}^t\bm{\tilde{W}}_{l-1} \bm{\tilde{y}}_{l-1} \\
  \bm{y}_l = f.(\bm{x}_l) \\
\end{eqnarray}

\subsubsection{activation function for hidden layers}
We can choose below 2 types of function as activation function.
These 2 activation functions will not produce any difference.
\begin{eqnarray}
  f(x) =
  \begin{cases}
    (1+e^{-x})^{-1} \mbox{ : logistic function}\\
    \tanh(x)
  \end{cases} \label{eq:activation hidden}
\end{eqnarray}

\subsubsection{activation function for an output layer}
To fit output value as desired, we have to choose appropriate activation
function for an output layer.
\begin{eqnarray}
  f(x) =
  \begin{cases}
    x &\mbox{: identity function for regression}\\
    (1+e^{-x})^{-1} &\mbox{: logistic function for [0,1] output binary
    classification}\\
  \frac{\exp(x)}{\sum_j \exp(x_{Lj})} &\mbox{: soft max function for multiclass
  classification}\\
    \tanh(x) &\mbox{: for [-1,1] output}
  \end{cases} \label{eq:activation out}
\end{eqnarray}
Note that we divide binary and multiclass case, because if we use soft
max function for binary class classification, $f(x)$ always become 1.

\subsection{Back Propagation}
In this section, we compute $\frac{\partial J_n}{\partial
  \bm{\tilde{W}}_{l-1}(k)}$ instead of $\frac{\partial J_n}{\partial
  \bm{\tilde{W}}_{l}(k)}$ for just index convenience.
And we omit $n$ as $J_n = J$ in this section.

\begin{eqnarray}
  \frac{\partial J}{\partial \tilde{w}_{l-1,ij}}
  &=& \frac{\partial J}{\partial x_{lj}} \frac{\partial x_{lj}}{\partial
    \tilde{w}_{l-1,ij}} \\
  &=& \epsilon_{lj} \frac{\partial (\sum_m{\tilde{w}_{l-1,mj}
      \tilde{y}_{l-1,m}})}{\partial \tilde{w}_{l-1,ij}}
  \hspace{5mm} \mid \epsilon_{lj} :=  \frac{\partial J}{\partial x_{lj}}\\
  &=& \epsilon_{lj} \tilde{y}_{l-1,i}\\
  \iff \frac{\partial J}{\partial \bm{\tilde{W}}_{l-1}}
  &=& \bm{\tilde{y}}_{l-1} {}^t\bm{\epsilon_{l}}\\
  \Rightarrow \bm{\tilde{W}}_{l-1}(k+1) &=& \bm{\tilde{W}}_{l-1}(k) - \rho \bm{\tilde{y}}_{l-1}{}^t\bm{\epsilon}_l\\
  \epsilon_{lj} &=&  \frac{\partial J}{\partial x_{lj}}\\
  &=& \frac{\partial J}{\partial y_{lj}} \frac{\partial
    y_{lj}}{\partial x_{lj}}\\
  &=& \frac{\partial J}{\partial y_{lj}} \frac{\partial
    f(x_{lj})}{\partial x_{lj}}\\
\end{eqnarray}

\subsubsection{back propagation for hidden layers}
\begin{eqnarray}
  \epsilon_{lj}
  &=& \frac{\partial J}{\partial y_{lj}} \frac{\partial
    f(x_{lj})}{\partial x_{lj}}\\
  &=& \sum_h \frac{\partial J}{\partial x_{l+1,h}} \frac{\partial
      x_{l+1,h}}{\partial y_{lj}} f'({x}_{lj})\\
  &=& \sum_h \epsilon_{l+1,h}
    \frac{(\sum_m w_{lmh}y_{lm} + w_{l0h})}{\partial y_{lj}}  f'({x}_{lj})\\
  &=& \sum_h \epsilon_{l+1,h} w_{ljh}  f'({x}_{lj})\\
  \Rightarrow
  \bm{\epsilon}_l &=& (\bm{W}_l\bm{\epsilon}_{l+1}) .* f'.(\bm{x}_l)\\
  &=&
  \begin{cases}
    (\bm{W}_l \bm{\epsilon}_{l+1}).*(\bm{y}_l-\bm{y}_l.*\bm{y}_l)
    &\mbox{if } f(x)=(1+e^{-x})^{-1}\\
    (\bm{W}_l \bm{\epsilon}_{l+1}).*(\bm{1}_{dl}-\bm{y}_l.*\bm{y}_l)
    &\mbox{if } f(x)=\tanh(x)
  \end{cases}
\end{eqnarray}

\subsubsection{back propagation for an output layer}
To compute a back propagation for an output layer, we need a concrete cost function form.
Because of this situation, we have to choose appropriate cost function form for each
problem as below at this time.
\begin{eqnarray}
  J =
  \begin{cases}
    \frac{1}{2} \lVert \bm{y}_L-\bm{b} \rVert ^2  &\mbox{regression }\\
    -(b \ln y_L + (1-b)\ln(1-y_L)) &\mbox{binary classification} \\
    -{}^t\bm{b} \ln \bm{y}_L &\mbox{multiclass classification}
  \end{cases} \label{eq:J}
\end{eqnarray}

Using cost(\ref{eq:J}) and activation function(\ref{eq:activation out}),
$\epsilon_L$ can be computed as below.
Note that $f(x_h)$ is related to $x_j$, so we have to think about the term
$\frac{\partial f(x_{Lh})}{\partial x_{Lj}}$.
\begin{eqnarray}
  \epsilon_{Lj}
  &=& \sum_h \frac{\partial J}{\partial y_{Lh}} \frac{\partial
    f(x_{Lh})}{\partial x_{Lj}}\\
  &=&
  \begin{cases}
    \sum_h (\frac{\partial (1/2 \lVert \bm{y}_L-\bm{b} \rVert ^2)}{\partial
      y_{Lh}}) \frac{\partial x_{Lh}}{\partial x_{Lj}}
    &\mbox{regression }\\
    -(\frac{b}{y_L} - \frac{1-b}{1-y_L}) \frac{\partial}{\partial
      x_{L}}(1+e^{-x_L})^{-1}   &\mbox{binary classification}\\
    -\sum_h \frac{b_h}{y_{L,h}} \frac{\partial}{\partial
      x_{Lj}}(\frac{\exp(x_{Lh})}{\sum_i \exp(x_{Li})})
    &\mbox{multiclass classification}
  \end{cases} \\
  &=&
  \begin{cases}
    \sum_h (y_{Lh}-b_h) \delta_{h,j}\\
    -(\frac{b}{y_L} - \frac{1-b}{1-y_L})(y_L(1-y_L))\\
    -\sum_h \frac{b_h}{y_{L,h}} (\delta_{h,j}y_{Lj} - y_{Lh}y_{Lj})
  \end{cases}\\
  &=&  y_{Lj}-b_j
\end{eqnarray}
We use the softmax property $\sum_h b_h = 1$ in the last equation of multi-class
classification case.

In both case the result becomes $y-b$, so there is no gradient
diminishing problem. That's why we use these combination of the cost and
final layers activation function.

\subsubsection{final result}
Finally, we get below results.
\begin{eqnarray}
  \bm{\epsilon}_l &=&
  \begin{cases}
    \bm{y}_L - \bm{b}  &\mbox{if } l=L \\
    (\bm{W}_l\bm{\epsilon}_{l+1}) .* f'.(\bm{x}_l) &\mbox{otherwise}
  \end{cases} \\
  &=&
  \begin{cases}
    \bm{y}_L - \bm{b}\\
    (\bm{W}_l \bm{\epsilon}_{l+1}).*(\bm{1}_{dl}-\bm{y}_l.*\bm{y}_l)  &\mbox{if } f(x)=\tanh(x)
  \end{cases} \\
  \bm{\tilde{W}}_{l-1}(k+1) &=& \bm{\tilde{W}}_{l-1}(k) - \rho\ \bm{\tilde{y}}_{l-1}{}^t\bm{\epsilon}_l
\end{eqnarray}

\section{Algorithm(Matrix version)}
\subsection{Iteration}
Iterate feed forward algorithm for all input
data and back propagation for all data until the error become smaller than the threshold or the
iteration number exceeds the
maximum iteration number.
That is,
\begin{equation}
  J < T_e \mbox{, or } k < T_K.
\end{equation}

\subsection{Feed Forward}
The below are true for all iteration.
So we omit the iteration number as
e.g.\ $\bm{\tilde{W}}_l(i) = \bm{\tilde{W}}_l$.
\begin{eqnarray}
  \bm{X}_l = {}^t\bm{\tilde{W}}_{l-1} \bm{\tilde{Y}}_{l-1} \\
  \bm{Y}_l = f.(\bm{X}_l)
\end{eqnarray}
$f(x)$ is defined by (\ref{eq:activation hidden}) and (\ref{eq:activation
out}).

\subsection{Back Propagation}
\begin{eqnarray}
J &=&
\begin{cases}
  \frac{1}{2} \mathrm{Tr} ({}^t(\bm{Y}_L - \bm{B})(\bm{Y}_L - \bm{B}))
  &\mbox{regression} \\
  -\mathrm{Tr} \{
  \begin{pmatrix}
    {}^t\bm{B} \\
    \bm{1}_{1 \times N} - {}^t\bm{B}
  \end{pmatrix}
  \ln. ( \bm{Y}_L \hspace{3mm}  \bm{1}_{N \times 1} - \bm{Y}_L) \} &\mbox{binary classification}\\
  -\mathrm{Tr} ({}^t\bm{B} \ln.(\bm{Y}_L)) &\mbox{multiclass classification}
  \end{cases}\\
  \bm{E}_l = (\bm{\epsilon}_{l,1}, \cdots, \bm{\epsilon}_{l,N}) &=&
  \begin{cases}
    \bm{Y}_L - \bm{B}  &\mbox{if } l=L \\
    (\bm{W}_l\bm{E}_{l+1}) .* f'.(\bm{X}_l) &\mbox{otherwise}
  \end{cases} \\
  &=&
  \begin{cases}
    \bm{Y}_L - \bm{B} \\
    (\bm{W}_l\bm{E}_{l+1}).*(\bm{1}_{dl\times N}-\bm{Y}_l.*\bm{Y}_l) &\mbox{if } f(x)=\tanh(x)
  \end{cases} \\
  \bm{\tilde{W}}_{l-1}(k+1) &=& \bm{\tilde{W}}_{l-1}(k) - \rho
  \sum_{n=1}^N \bm{\tilde{y}}_{l-1,n} {}^t\bm{\epsilon}_{l,n}\\
  &=& \bm{\tilde{W}}_{l-1}(k) - \rho\ \bm{\tilde{Y}}_{l-1} {}^t\bm{E}_l
\end{eqnarray}

\end{document}


\chapter{\texorpdfstring{Relation of $T/{\cal E}$}{T/E} to \texorpdfstring{$D(z)$}{D(z)}} %  From Self-consistent Theory of Anderson Localization
\label{sec:appendix_TE_Dz_relation}

% see also lab notebook SVN 20091230_ben_transmission_energy_derivations
% Note: a significant amount of content was barrowed from /svn/research/oned/TE\_paper

This is an expansion of Appendix section~\ref{app:Dz_derivation}. As in that section we assume a slab geometry. The $z$ coordinate normal to the slab is separated from the perpendicular component ${\bf \rho}$ as ${\bf r}=({\bf \rho},z)$. Again assuming no dependence on ${\bf \rho}$ allows us to give the ensemble-averaged diffusive flux $\langle\vec{J}(\vec{r},t)\rangle$ and the energy density $\langle {\cal W}(\vec{r},t)\rangle$ are related via \cite{1953_Morse}
\begin{equation}
\langle\vec{J}(\vec{r},t)\rangle=-D(\vec{r})\vec{\nabla}\langle {\cal W}(\vec{r},t)\rangle
\label{eq:Jflux_relation}
\end{equation}
The diffusion approximation amounts to $D(\vec{r})\equiv D_0=c\ell_{tmfp}/3$, where $c$ is the speed of light and $\ell_{tmfp}$ is the transport mean free path.

We consider a 3D random medium in a shape of a slab of thickness $L$, where we explicitly  separate the coordinate $z$ normal to the slab from the perpendicular component ${\bf \rho}$ as ${\bf r}=({\bf \rho},z)$. Under a CW plane-wave illumination at normal incidence, the dependence on ${\bf \rho}$ and $t$ can be neglected.
\begin{equation}
\langle\vec{J}_z(z)\rangle=-D(z)\frac{d}{dz}\langle {\cal W}(z)\rangle
\end{equation}

Integration over $z$ gives
\begin{equation}
\int_z^L\frac{\langle J_z(z^\prime)\rangle dz^\prime}{D(z^\prime)}=-\langle {\cal W}(L)\rangle + \langle {\cal W}(z)\rangle
\label{eq:E1_relation}
\end{equation}
where the energy stored inside the random medium ${\cal E}$ is formally defined as
\begin{equation}
\langle {\cal E} \rangle =\int_0^L\langle {\cal W}(z)\rangle dz.
\label{eq:Energy_definition_relation}
\end{equation}
thus
\begin{equation}
\langle {\cal E} \rangle = \int_0^L \left( \langle {\cal W}(L)\rangle + \int_z^L\frac{\langle J_z(z^\prime)\rangle }{D(z^\prime)}dz^\prime\right) dz
\end{equation}
The remaining work is to factor out transmission $T$ in order to find the relation between $T/{\cal E}$ and $D(z)$. The energy density $\langle {\cal W}(L)\rangle$ at the right boundary can be expressed in terms of right- and left-propagating fluxes. From the definition of diffusive flux \cite{1953_Morse}
\begin{equation}
\langle J_{\pm}(z)\rangle = \frac{c}{4} \langle {\cal W}(z)\rangle \mp \frac{D_0}{2} \frac{d\langle {\cal W}(z)\rangle }{dz}
\label{eq:diffusive_flux_relation}
\end{equation}
where $ \langle J_{-}\rangle$ and $ \langle J_{+}\rangle $ are the fluxes propagating along negative and positive $z$-directions respectively. Since $\langle J_+(L)\rangle=J_0T$ and $\langle J_-(L)\rangle=0$, using Eqs.~\ref{eq:diffusive_flux_relation} to eliminate $D_0$ yields
\begin{equation}
\langle J_+(L)\rangle + \langle J_-(L)\rangle = 2 \frac{c}{4}\langle {\cal W}(L)\rangle
\end{equation}
Therefore $\langle {\cal W}(L)\rangle=2J_0T/c$ and the energy can be re-written as
\begin{equation}
\langle {\cal E} \rangle = \int_0^L \left( 2J_0T/c + \int_z^L\frac{\langle J_z(z^\prime)\rangle}{D(z^\prime)}dz^\prime\right) dz
\end{equation}
Next, we reduce $\langle J_z(z^\prime)\rangle$ to find an approximately equivalent transmission.

%the boundary conditions are from Eq.~\ref{eq:Jflux_conserv}
%\begin{equation}
%J_z(z=0)=-J_0R,\ \, J_z(z=L)=J_0T
%\label{eq:Jflux_bc}
%\end{equation}

In the CW regime when the energy density ${\cal W}(z)$ is stationary, $\partial \langle {\cal W}(z)\rangle/\partial t=0$, it follows from energy conservation condition for flux $\vec{J}$ and energy ${\cal W}$
\begin{equation}
\frac{\partial \langle {\cal W}(\vec{r},t)\rangle }{\partial t}+\vec{\nabla} \cdot \langle\vec{J}(\vec{r},t)\rangle=
\frac{c}{\ell_g}\langle {\cal W}(\vec{r},t)\rangle+J_0 \delta(z-z_p)
\label{eq:Jflux_conserv_relation}
\end{equation}
 that the $z$ component of flux is constant for $z>z_p\sim\ell$. The value of the constant can be obtained from the boundary condition at $z=L$ as
\begin{equation}
\langle J_z(z)\rangle=\left\{
\begin{array}{l l}
\langle J_z(L)\rangle\equiv J_0 \langle T \rangle ,&\quad z_p<z<L\\
\langle J_z(0)\rangle\equiv -J_0 \langle R \rangle,&\quad 0<z<z_p\\
\end{array} \right.
\label{eq:Jfluxz_const_relation}
\end{equation}
where $T$ ($R$) is the transmission (reflection) coefficient. As a check, by integrating Eq.~(\ref{eq:Jflux_conserv_relation}) over the entire system we obtain the standard (passive) flux conservation $\langle J_z(L)\rangle -\langle J_z(0)\rangle =J_0 \langle T \rangle-(-J_0 \langle R \rangle)=J_0(\langle T \rangle+\langle R \rangle)=J_0$. To take advantage of the fact that $\langle J_z(z)\rangle$ is piecewise constant, c.f. Eq.~(\ref{eq:Jfluxz_const_relation}), we have to neglect by $0<z<z_p$ contribution. Then a constant can be substituted for $J_z(z')$,
\begin{equation}
\langle {\cal E} \rangle = \int_0^L \left( 2J_0\langle T \rangle/c + \int_z^L\frac{J_0 \langle T \rangle }{D(z^\prime)}dz^\prime\right) dz
\end{equation}
This introduces an error $\propto z_p/L\sim\ell/L\ll 1$. Factoring $T$ from the integrands,
\begin{equation}
\langle {\cal E} \rangle =J_0\langle T \rangle\int_0^L \left( \int_z^L\frac{dz^\prime}{D(z^\prime)}+2/c\right) dz
\label{eq:E1a_relation}
\end{equation}
Note that the second term is of the same order $\sim \ell/L$ as the term omitted in arriving to the above expression. Hence, $2/c$ contribution has to be dropped as well.
\begin{equation}
\langle {\cal E} \rangle =J_0T\int_0^L \int_z^L \frac{1}{D(z^\prime)}dz^\prime dz
\end{equation}

Taking advantage of the system symmetry, $D(z)=D(L-z)$, the double integration can be further simplified as
\begin{eqnarray}
\displaystyle\int_{0}^{L}\int_{z}^{L}\displaystyle\frac{1}{D(z^\prime)}dz^\prime dz &=&\frac{1}{2}\displaystyle\int_{0}^{L}\int_{0}^{L}\displaystyle\frac{1}{D(z^\prime)}dz^\prime dz \nonumber\\
&=&\frac{L}{2}\int_{0}^{L}\displaystyle\frac{1}{D(z)}dz.
\label{eq:E3_relation}
\end{eqnarray}
After normalizing the integral so that it yields unity in the case when the wave interference effects are neglected, $D(z)=D_0\equiv c\ell/3$, for passive media
\begin{equation}
\frac{\langle T \rangle}{\langle {\cal E} \rangle}\simeq
\frac{1}{J_0}
\frac{2D_0}{L^2}
\left(
\displaystyle\frac{1}{L}\displaystyle\int_{0}^{L}\displaystyle\frac{D_0}{D(z)}dz
\right)^{-1},
\label{eq:TE_vs_D_relation}
\end{equation}
We note that in process of deriving Eq.~(\ref{eq:TE_vs_D_relation}), we dropped the terms on the order of $\sim\ell/L\ll 1$.

Dropping the localization corrections leaves
\begin{equation}
\frac{\langle T \rangle}{\langle {\cal E} \rangle}\simeq \frac{1}{J_0} \frac{2D_0}{L^2}
\label{eq:diffusion_te_only}
\end{equation}
Any deviation from Eq.~\ref{eq:diffusion_te_only} in passive diffusive media can be attributed to localization corrections.


%          -------------------------------------------------------------------------------------------------------------
%          ------------ Two General Purpose Algorithms for Counting Permutations with Consecutive Sequences ------------
%          -------------------------------------------------------------------------------------------------------------
%          ---------------------------------------------------- by -----------------------------------------------------
%          -------------------------------------------------------------------------------------------------------------
%          ----------------------------------------------- Thomas Becker -----------------------------------------------
%          -------------------------------------------------------------------------------------------------------------
%          ------------------------------------------------- APR 2017 --------------------------------------------------

\documentclass{article}
\usepackage{amsfonts}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{multirow}
\usepackage{hyperref}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}
\usepackage{theorem}
\usepackage{enumerate}

% ---------------------------------------------------------------------
%
% Amstex math italic bold
%
% ---------------------------------------------------------------------
%
\font\tencmmib=cmmib10
\font\ninecmmib=cmmib9
\font\sevencmmib=cmmib7
\font\fivecmmib=cmmib5
%
\newfam\cmmibfam
\def\mib{\fam\cmmibfam\tencmmib}
%
% NOTE: If these fonts are not available, use the following alternate
% definition of \mib:
%\def\mib{\mathbf}
%
\textfont\cmmibfam=\tencmmib
\scriptfont\cmmibfam=\sevencmmib
\scriptscriptfont\cmmibfam=\fivecmmib

{\theoremstyle{plain}
  \theorembodyfont{\rm}
  \newtheorem{theorem}{Theorem}[section]
  \newtheorem{proposition}[theorem]{Proposition}
  \newtheorem{lemma}[theorem]{Lemma}
  \newtheorem{corollary}[theorem]{Corollary}
  \newtheorem{definition}[theorem]{Definition}
  \newtheorem{example}[theorem]{Example}
  \newtheorem{notation}[theorem]{Notation}
  \newtheorem{purpose}{Purpose of this Section}
}
\renewcommand{\thepurpose}{\kern -5pt}
\renewcommand\refname{References and Notes}

\def\proof{\par \noindent              %--- PROOF ----------------------
\mbox{\bf Proof}\ \,}                  %
\def\endproof{\mbox{$\Box$} \par }     %--- END OF PROOF ---------------

% Use vertical space instead of indent to start a paragraph
%
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}

\begin{document}
\author{Thomas Becker\\ {\small \it thomas@greaterthanzero.com}}
\title{Two General Purpose Algorithms for Counting Permutations with Consecutive Sequences}
\date{\small April 22, 2017}
\maketitle

\begin{abstract}
  We state, prove the correctness of, and discuss the complexity of two general
  purpose algorithms, one building upon the other, for counting permutations with
  specified configurations of consecutive sequences. The algorithms are based on
  a theorem that describes how counting permutations with consecutive sequences can
  be reduced to counting permutations with no consecutive sequences at all.
\end{abstract}

\section{Preface}
Combinatorial mathematics is not my specialty as a mathematician. However, I recently wrote
a rather lightweight blog post \cite{BlogPost} on the subject of random shuffle mode on today's
music players. In the process, I needed to know the number of permutations with certain
configurations of consecutive sequences. The answers to many of my questions were readily
available on the Web (see e.g. \cite{Oeis}),
but there were also some questions to which I did not find the answer. Therefore, I
wrote some general-purpose algorithms---based on one core mathematical theorem---to conveniently
calculate the numbers that I needed. The algorithms are available on github \cite{Algos}.
I checked the results against brute force calculations, and, to the extent possible, against
the Online Encyclopedia of Integer Sequences \cite{Oeis} for small numbers of elements.
But since the correctness of the algorithms
is far from obvious, I also felt that formal mathematical correctness proofs should be available.
That is the reason why I wrote this paper. It is quite possible that there is nothing here that
combinatorial mathematicians don't already know, but again, I was not able to find everything I
needed online. Any feedback, particularly regarding references to
the literature, would be much appreciated.

\section{Introduction}

\begin{notation}
  If $n$ is an integer with $n \geq 1$, we write ${\mathcal P}_n$ for the set of all permutations of the integers
  $1, 2, \ldots, n$.
\end{notation}

\begin{definition}
  Let $P\in {\mathcal P}_n$, and let $k \geq 2$.
  A {\bf consecutive sequence} of length $k$ in $P$ is a contiguous subsequence of $k$ consecutive
  integers in $P$, that is, a contiguous subsequence of the form $(i, i+1, \ldots, i+k-1)$.
  A consecutive sequence is called {\bf maximal} if it is not a subsequence of a consecutive sequence
  of greater length.
\end{definition}

There is a vast body of work regarding the count of permutations that have a specified configuration of
consecutive sequences, such as permutations having a certain number of consecutive pairs or triples
\cite{OeisRefs}. In this paper, we state, prove the correctness of, and discuss the complexity of two general
purpose algorithms, one based upon the other, for
counting permutations whose maximal consecutive sequences are described in certain ways. The need
for these algorithms, which are available under the MIT license \cite{Algos}, arose from the author's curiosity
about the behavior of random shuffle mode on today's music players \cite{BlogPost}. The algorithms do not
use the (more generally applicable) inclusion-exclusion principle that is often employed for counting
permutations with certain properties. Instead, they rely on a technique of reducing the problem of counting
permutations with consecutive sequences to the problem of counting permutations with no consecutive sequences
at all. This technique is a generalized form of an argument that the author encountered in a Quora post by Jed Yang
\cite{JedYang}.

Our core algorithms deal with the number of permutations having certain configurations of
{\it maximal} consecutive sequences. However, as we will show, they can be employed to answer questions
regarding just consecutive sequences as well.

The organization of the paper and the key results are as follows. In Theorem \ref{theorem_count_by_length_and_initial_element},
we give an auxiliary algorithm on which the two main algorithms are based. It calculates the number of
permutations that meet a specification of maximal consecutive sequences by initial elements and lengths.
The theorem reduces that number of permutations to the number of permutations of fewer elements that
have no consecutive sequences at all.

Building on Theorem \ref{theorem_count_by_length_and_initial_element}, Theorem \ref{theorem_count_by_length_and_count}
provides an algorithm to count the permutations whose maximal consecutive sequences have been specified by
stating how many exactly of each length there should be. Using that algorithm, one may, for example,
calculate the number of permutations that have exactly three consecutive pairs, none of which are linked
to form a consecutive triple (specify ``three maximal consecutive sequences of length two, zero maximal
consecutive sequences of any other length''). The complexity of the algorithm is $O(n\cdot m)$, where
$m$ is the number of lengths for which the number of maximal consecutive sequences has been specified
as greater than zero.

Building on top of that, Theorem \ref{theorem_iterating_over_specs_by_count_by_length_and_count}
describes a generic, customizable algorithm
that iterates over specifications of maximal consecutive sequences by lengths and counts.
A user-supplied function decides if permutations with a given specification should be included in the count or not.
For example, the user-supplied function could accept only configurations that specify a non-zero count for maximal
sequences of length two and a zero count for all other lengths.
The result would be the number of permutations that have any number of consecutive pairs, but no linked pairs
that form larger consecutive sequences. This algorithm is of course no more than a ``glorified brute force
algorithm'': instead of iterating over permutations, we iterate over specifications of maximal consecutive sequences
by lengths and counts. While this is a dramatic improvement (see Section \ref{sec_iterating} for details), it
is a brute force approach nonetheless. However, our implementation lets the user exploit the fact that oftentimes,
not all specifications of maximal consecutive sequences by lengths and counts need to be looked at.
This can lead to vastly improved complexity in many cases. In the example that we just mentioned, the iteration
becomes linear in $n$.

Finally, the last section of this paper discusses how to use our core algorithms, which deal with
maximal consecutive sequences, to answer questions regarding just plain consecutive sequences.

\section{Specifying Maximal Consecutive Sequences by Lengths And Initial Elements}

\begin{definition} \label{def_mcs_spec_by_length_and_initial_element}
  Let $n$ be an integer with $n\geq 1$. An {\bf MCS-specification by lengths and initial elements} for $n$ is a
  set of pairs of integers $$\{\,(a_1, k_1), (a_2, k_2), \ldots,(a_m, k_m)\,\}$$ with the following properties:
  \begin{enumerate}[(i)]
  \item
    $a_i \geq 1$ and $k_i \geq 2$ for $1\leq i \leq m$,
  \item
    $a_i + k_i \leq a_{i+1}$ for $1\leq i \leq m-1$,
  \item
    $a_m + (k_m-1) \leq n$.
  \end{enumerate}
\end{definition}

\begin{notation}\label{notation_mcs_spec_by_length_and_initial_element}
  If S is an MCS-specification by lenghts and initial elements for $n$ as in
  Definition \ref{def_mcs_spec_by_length_and_initial_element}
  above, we write ${\mathcal Q}_{(n,S)}$ for the set of all permutations
  $P \in {\mathcal P}_n$ with the following property: for each $i$ with $1\leq i \leq m$, $P$ has a maximal
  consecutive sequence of length $k_i$ that starts with the integer $a_i$, and $P$ has no other maximal
  consecutive sequences.
\end{notation}

\begin{purpose}
Present an auxiliary algorithm, to be used in later sections,
for calculating $|{\mathcal Q}_{(n,S)}|$ from $n$ and $S$.
\end{purpose}

The following technical lemma will be needed when we use induction on $m$ in connection with
MCS-specifications by lengths and initial elements.

\begin{lemma}\label{lemma_ind_on_m}
  Let $S$ be an MCS-specification by lengths and initial elements for $n$ as in
  Definition \ref{def_mcs_spec_by_length_and_initial_element}
  above, and assume that $m\geq 1$. Then $n-(k_m-1) \geq 1$, and
  $$T = \{\,(a_1, k_1), (a_2, k_2), \ldots,(a_{m-1}, k_{m-1})\,\}$$
  is an MCS-specification by lengths and initial elements for $n-(k_m-1)$.
\end{lemma}

\proof
From (i) and (iii) of Definition \ref{def_mcs_spec_by_length_and_initial_element}, we may conclude that
$$1 \leq a_m \leq n - (k_m - 1),$$ which proves the first claim of the lemma.
If $m=1$, the second claim is trivial since the empty set is an MCS-specifications by lengths
and initial elements for any positive integer. Now let $m > 1$. It is clear that $T$ has properties
(i) and (ii) of Definition \ref{def_mcs_spec_by_length_and_initial_element}. Moreover, we have
\begin{eqnarray*}
  a_{m-1} + (k_{m-1} - 1) & \leq & a_m - 1 \\
                          & \leq & n - k_m \\
                          & \leq & n - (k_m-1),
\end{eqnarray*}
and thus $T$ satisfies (iii) of Definition \ref{def_mcs_spec_by_length_and_initial_element} as well.
\endproof

\begin{notation}
  We let ${\mathcal U}_n$ denote the subset of ${\mathcal P}_n$ that consists of all permutations with no consecutive
  sequences.
\end{notation}

It is well-known (see e.g. \cite{JedYang}) that the cardinality of ${\mathcal U}_n$ satisfies the recurrence relation
$$
|{\mathcal U}_n| = (n-1) \cdot |{\mathcal U}_{n-1}| + (n-2) \cdot |{\mathcal U}_{n-2}|.
$$
The theorem below provides the desired algorithm for calculating $|{\mathcal Q}_{(n,S)}|$ by reducing the
problem to the calculation of $|{\mathcal U}_r|$ for a certain $r$.

\begin{theorem}\label{theorem_count_by_length_and_initial_element}
  Let $n \geq 1$, let $S = \{\,(a_1, k_1), (a_2, k_2), \ldots,(a_m, k_m)\,\}$ be an
  MCS-specification by lengths and initial elements for $n$, and let $k = \sum_{i=1}^m k_i$.
  Then $|{\mathcal Q}_{(n,S)}| = |{\mathcal U}_{n - (k - m)}|$.
\end{theorem}

\proof
We will prove the theorem by showing that there is a bijection between ${\mathcal Q}_{(n,S)}$
and ${\mathcal U}_{n - (k - m)}$. For this, it suffices to show that there are maps
$$
f: {\mathcal Q}_{(n,S)}\rightarrow {\mathcal U}_{n - (k - m)}
\quad\text{and}\quad
g: {\mathcal U}_{n - (k - m)} \rightarrow {\mathcal Q}_{(n,S)}
$$
such that $g\circ f$ is the identity on ${\mathcal Q}_{(n,S)}$ and $f\circ g$ is the identity on
${\mathcal U}_{n - (k - m)}$. Intuitively speaking, $f$ is obtained by throwing out
all elements of maximal consecutive sequences except for the initial ones, then adjusting greater
elements of the permutation downward to close the gaps. The map $g$ is the reverse operation of that.
For a formal proof of the existence of these maps, we proceed by induction on $m$. For $m=0$,
the claim is trivial as $${\mathcal U}_{n - (k - m)} = {\mathcal U}_n = {\mathcal Q}_{(n,S)}$$
in that case. Now let $m>0$, and let
$$
T = \{\,(a_1, k_1), (a_2, k_2), \ldots,(a_{m-1}, k_{m-1})\,\}.
$$
By Lemma \ref{lemma_ind_on_m}, $T$ is an MCS-specification by lengths and initial elements
for $n - (k_m - 1)$. This together with the induction hypothesis implies
that there is a bijection between
$$
{\mathcal Q}_{(n-(k_m-1),T)}
\quad\text{and}\quad
{\mathcal U}_{(n-(k_m-1)) - ((k-k_m) - (m-1))} = {\mathcal U}_{n - (k - m)}.
$$
Therefore, it suffices to construct maps
$$
f: {\mathcal Q}_{(n,S)}\rightarrow {\mathcal Q}_{(n-(k_m-1),T)}
\quad\text{and}\quad
g: {\mathcal Q}_{(n-(k_m-1),T)} \rightarrow {\mathcal Q}_{(n,S)}
$$
such that $g\circ f$ is the identity on ${\mathcal Q}_{(n,S)}$ and $f\circ g$ is the identity on
${\mathcal Q}_{(n-(k_m-1),T)}$.
For $P\in {\mathcal Q}_{(n,S)}$, let $f(P)$ be the integer sequence that is obtained from $P$ as follows:
\begin{enumerate}
\item
  Strike the elements $a_m + 1, a_m+2, \ldots, a_m+(k_m-1)$ from $P$.
\item
  In the remaining sequence, replace every element $a$ that is greater than $a_m$ with $a-(k_m-1)$.
\end{enumerate}
For $Q\in {\mathcal Q}_{(n-(k_m-1),T)}$, first note that the integer $a_m$ occurs in the sequence $Q$
because $a_m \leq n-(k_m-1)$ by Definition \ref{def_mcs_spec_by_length_and_initial_element} (iii).
Now let $g(Q)$ be the integer sequence that
is obtained from $Q$ by reversing the procedure that defines $f$:
\begin{enumerate}
\item
 Replace every element $a$ in $Q$ that is greater than $a_m$ with $a+(k_m-1)$.
\item
  Augment the resulting sequence by inserting the sequence $(a_m + 1, a_m+2, \ldots, a_m+(k_m-1))$
  following the element $a_m$.
\end{enumerate}
It is easy to see that $f(P)$ contains exactly the integers $1, 2, \ldots, n-(k_m-1)$, and $g(Q)$ contains
exactly the integers $1, 2, \ldots, n$, and therefore,
$$
f(P)\in {\mathcal P}_{n-(k_m-1)}
\quad\text{and}\quad
g(Q)\in {\mathcal P}_n.
$$
Also, it is immediate from the definition of $f$ and $g$ that $g\circ f$ is the
identity on ${\mathcal Q}_{(n,S)}$ and $f\circ g$ is the identity on ${\mathcal Q}_{(n-(k_m-1),T)}$.
It remains to show that
$$
f({\mathcal Q}_{(n,S)}) \subseteq {\mathcal Q}_{(n-(k_m-1),T)}
\quad \text{and}\quad
g({\mathcal Q}_{(n-(k_m-1),T)}) \subseteq {\mathcal Q}_{(n,S)}.
$$
So let $P\in {\mathcal Q}_{(n,S)}$. To show that $f(P)\in {\mathcal Q}_{(n-(k_m-1),T)}$, we must prove
that $f(P)$ has precisely the maximal consecutive sequences that $T$ specifies. Before delving into that argument,
it may be helpful to visualize how $f(P)$ is obtained from $P$. Under the action of $f$, an element of the
sequence $P$ may be removed, change its position, change its value, change both position and value,
or change neither position nor value. The elements $a_m + 1, a_m + 2, \ldots, a_m + (k -1)$, which we know
are positioned consecutively, get removed. The elements that are positioned to the right of that subsequence,
all the way to the end of $P$, move $k_m - 1$ positions to the left. Finally, those elements are greater than
$a_m$---and the only ones that are left are actually greater than $a_m + (k -1)$---are decremented by $k-1$.
You may also want to remind yourself that the subscript $m$ on $a_m$ is not indicative of position in
$P$ or $f(P)$. It stems from the MCS-specification $S$.

Now imagine the sequence $f(P)$ being split in two, with the cut being after the element
$a_m$. Let's call these two pieces $P_1$ and $P_2$. All the integers that are members of the $m-1$ maximal
consecutive sequences in $P$ starting with $a_1, a_2, \ldots, a_{m-1}$ are less than $a_m$. Therefore,
their values are not changed under the action of $f$, and neither are their relative positions.
Therefore, each of these sequences is present as a consecutive sequence in either $P_1$ or $P_2$.
As for the elements in between and around those sequences, in $P_1$ or $P_2$,
they are either less than or equal to $a_m$,
in which case their value is unchanged under $f$, or they are greater than $a_m$, in which case they are
the result of decrementing in lockstep, by the same amount, namely, $k_m -1$. Moreover, no relative positions
have changed among any of these under the action of $f$. It follows that none of these elements
have joined any of the maximal consecutive sequences of $P$, and the only new consecutive pair that
could have formed among them would be $(a_m, a_m+1)$, but that's impossible since $a_m$ sits at the
end of $P_1$. We see that the maximal consecutive sequences that we find in $P_1$ and $P_2$ are precisely those
that are specified by $T$.

It remains to show that no consecutive pair forms between the last element of $P_1$ and the first element of
$P_2$ as we join the two to form $f(P)$. The last element of $P_1$ is $a_m$. The first element of $P_2$ is
the result of the effect that $f$ had on the first element following the maximal consecutive sequence
$a_m, a_m + 1, \ldots, a_m + (k -1)$ in $P$. That element was either less than $a_m$, in which
case its value is unchanged, or it was greater than $a_m + (k_m-1)$ and unequal to $a_m + k$, in which case its value
was changed to something not equal to $a_m + 1$. In either case, no consecutive pair forms at the juncture
of $P_1$ and $P_2$. This concludes the proof that $f(P)\in {\mathcal Q}_{(n-(k_m-1),T)}$ and thus
$f({\mathcal Q}_{(n,S)}) \subseteq {\mathcal Q}_{(n-(k_m-1),T)}$. We leave the proof of
$g({\mathcal Q}_{(n-(k_m-1),T)}) \subseteq {\mathcal Q}_{(n,S)}$ to the reader, as it is little more than
the argument that we just made in reverse.
\endproof

Since we know how to calculate the cardinality of ${\mathcal U}_n$ for any $n$, the theorem above gives
us an algorithm to calculate the number of permutations of $n$ integers that have maximal consecutive
sequences of specified lengths with specified initial elements. However, judging from experience, that algorithm
isn't very interesting. The description of consecutive sequences is just too specific. What one wants
is being able to count the permutations with consecutive sequences or maximal consecutive sequences that
are specified by lengths and counts, as in, ``exactly $x$ number of consecutive triples,'' or, ``exactly
$x$ number of consecutive triples and no longer consecutive sequences,'' or some such thing. This will
be achieved in the next two sections.

As for the complexity of the algorithm of Theorem \ref{theorem_count_by_length_and_initial_element},
it is clear that the classical recurrence relation for ${\mathcal U}_n$ that we stated preceding the
theorem can be rewritten as a bottom-up multiplication that calculates ${\mathcal U}_n$ in constant
space and linear time. Therefore, the complexity of the algorithm of
Theorem \ref{theorem_count_by_length_and_initial_element} is $O(n)$.

As an aside, let us mention that Theorem \ref{theorem_count_by_length_and_initial_element} continues
to hold if instead of specifying maximal consecutive sequences by initial element and count, we specify
them by initial position and count. This follows from the fact that for $n\geq 1$,
the map that exchances value and position is a bijection on ${\mathcal P}_n$. Here, the permutation
$(a_1, a_2, \ldots, a_m)$ maps to the permutation where $i$ is the element at position $a_i$ for $1 \leq i\leq n$.
Under this map, maximal consecutive sequences of length $k$ with initial element $a$ map to maximal
consecutive sequences of length $k$ that start at position $a$ and vise versa.

\section{Specifying Maximal Consecutive Sequences by Lengths And Counts}

\begin{definition} \label{def_mcs_spec_by_length_and_count}
  Let $n$ be an integer with $n\geq 1$. An {\bf MCS-specification by lengths and counts} for $n$ is a
  set of pairs of integers $$\{\,(k_1, c_1), (k_2, c_2), \ldots,(k_m, c_m)\,\}$$ with the following properties:
  \begin{enumerate}[(i)]
  \item
    $k_i \geq 2$ and $c_i \geq 1$ for $1\leq i \leq m$,
  \item
    $\sum_{i=1}^m c_i \cdot k_i \leq n$, and
  \item
    $k_i \neq k_j$ for $1\leq i, j \leq m$.
  \end{enumerate}
\end{definition}

\begin{notation}\label{notation_mcs_spec_by_length_and_count}
  If $T$ is an MCS-specification by lenghts and counts for $n$ as in
  Definition \ref{def_mcs_spec_by_length_and_count}
  above, we write ${\mathcal R}_{(n,T)}$ for the set of all permutations
  $P \in {\mathcal P}_n$ with the following property: for each $i$ with $1\leq i \leq m$, $P$ has exactly $c_i$
  maximal consecutive sequence of length $k_i$, and $P$ has no other maximal
  consecutive sequences.
\end{notation}

\begin{purpose}
Present an algorithm for calculating $|{\mathcal R}_{(n,T)}|$
from $n$ and $T$.
\end{purpose}

It is clear from Definitions \ref{def_mcs_spec_by_length_and_initial_element}
and \ref{def_mcs_spec_by_length_and_count} and the corresponding Notations
\ref{notation_mcs_spec_by_length_and_initial_element} and \ref{notation_mcs_spec_by_length_and_count} that
${\mathcal R}_{(n,T)}$ is the disjoint union of certain ${\mathcal Q}_{(n,S)}$, namely, those
where $S$ ranges over all those
MCS-specifications by lengths and initial elements that are of the form
$$S = \{\,(a_1, l_1), (a_2, l_2), \ldots,(a_p, l_p)\,\}$$
with the properties
\begin{enumerate}[(i)]
\item
  $p = \sum_{i=1}^m c_i$, and
\item
  for $1\leq i \leq m$, there are exactly $c_i$ many $j$ with $1\leq j \leq p$ and $l_j = k_i$.
\end{enumerate}

So if we denote the set of all MCS-specifications by lengths and initial elements that satisfy (i) and (ii)
above by ${\mathcal S}_T$, then we have, as a first step towards our algorithm for calculating
$|{\mathcal R}_{(n,T)}|$,
$$
|{\mathcal R}_{(n,T)}| = \sum_{S\in {\mathcal S}_T} |{\mathcal Q}_{(n,S)}|.\eqno(1)
$$

Theorem \ref{theorem_count_by_length_and_initial_element} tells us how to calculate $|{\mathcal Q}_{(n,S)}|$,
and moreover, the algorithm for doing so uses only $n$, $p$, and $\sum_{j=1}^p l_j$.
It is immediate from properties (i) and (ii) above that
$$
p = \sum_{i=1}^m c_i
\quad \text{and}\quad
\sum_{j=1}^p l_j = \sum_{i=1}^m c_i \cdot k_i.
$$
So if we let $c = \sum_{i=1}^m c_i$ and $k = \sum_{i=1}^m c_i \cdot k_i$, we can extend equation (1) above
to the following second step towards our algorithm for calculating $|{\mathcal R}_{(n,T)}|$:
$$
|{\mathcal R}_{(n,T)}| = |{\mathcal S}_T| \cdot |{\mathcal U}_{n - (k - c)}|.\eqno(2)
$$

Therefore, all that remains to do is to figure out what $|{\mathcal S}_T|$ is:
how many MCS-specifications by lengths and initial elements
are there that satisfy (i) and (ii) above? That number is fairly easy to describe: it is the number of ways
in which one can choose subsets
$A_1, A_2, \ldots, A_{m}\subset \{\,1, 2, \ldots, n\,\}$ such that

\begin{enumerate}[(a)]
\item
  $|A_i| = c_i$ for $1\leq i\leq m$, and
\item
  the elements of the $A_i$ are far enough apart so that each $a \in A_i$ can
  be the initial value of a maximal consecutive sequence of length $k_i$.
\end{enumerate}

At first glance, it may seem difficult to figure out the number of ways in which the $A_i$ can be chosen.
The key to making it easy lies in going back the proof of the equality
$|{\mathcal Q}_{(n,S)}| = |{\mathcal U}_{n - (k - c)}|$ of
Theorem \ref{theorem_count_by_length_and_initial_element}, which we just used to pass from equation (1) to
equation (2). This equality was proved
by exhibiting a bijection between ${\mathcal Q}_{(n,S)}$ and ${\mathcal U}_{n - (k - c)}$.
We mapped permutations with maximal consecutive sequences to shorter permutations without any consecutive
sequences by striking from all maximal consecutive sequences all elements except for the
first one, then renumbering the remaining elements to close the resulting gaps. The inverse operation consisted of
starting with a permutation with no consecutive sequences, then
blowing up the specified initial elements to consecutive sequences by inserting and renumbering elements. At the risk
of being accused of a hand-waving argument, we'll say that it is now clear that picking the subsets
$A_1, A_2, \ldots, A_{m}\subset \{\,1, 2, \ldots, n\,\}$ with properties (a) and (b) above is equivalent to picking
subsets $B_1, B_2, \ldots, B_{m}\subset \{\,1, 2, \ldots, n - (k - c)\,\}$ with just property (a).
The formal proof by induction parallels the proof of Theorem \ref{theorem_count_by_length_and_initial_element} and
is simpler than the latter.
Counting the ways in which the $B_i$ can be selected is elementary. The answer is
$$
\prod_{i=1}^m\binom{n-(k-c) - \sum_{j=1}^{i-1}c_j}{c_i}\,,
$$
or, equivalently,
$$
\frac{(n-(k-c))!}{c_1! \cdot c_2! \cdot \ldots \cdot c_m! \cdot (n-k)!}\,,
$$
or, equivalently,
$$
\frac{(n-(k-c)) \cdot (n-(k-c) - 1)\cdot \ldots \cdot (n-(k-c) - c + 1)}{c_1! \cdot c_2! \cdot \ldots \cdot c_m!}\,.
$$

We have thus proved the following theorem, which provides the desired algorithm for calculating
$|{\mathcal R}_{(n,T)}|$ from $n$ and $T$.

\begin{theorem}\label{theorem_count_by_length_and_count}
  Let $n \geq 1$, let $T = \{\,(k_1, c_1), (k_2, c_2), \ldots,(k_m, c_m)\,\}$ be an
  MCS-specification by lengths and counts for $n$, let $k = \sum_{i=1}^m c_i \cdot k_i$,
  and let $c = \sum_{i=1}^m c_i$.
  Then
  $$
  |{\mathcal R}_{(n,T)}| = |{\mathcal U}_{n - (k - c)}| \cdot
  \prod_{i=1}^m\binom{n-(k-c) - \sum_{j=1}^{i-1}c_j}{c_i},
  $$
  or, equivalently,
  $$
  |{\mathcal R}_{(n,T)}| = |{\mathcal U}_{n - (k - c)}| \cdot
  \frac{(n-(k-c))!}{c_1! \cdot c_2! \cdot \ldots \cdot c_m! \cdot (n-k)!}\,,
  $$
  or, equivalently,
  \begin{eqnarray*}
    |{\mathcal R}_{(n,T)}| & = & |{\mathcal U}_{n - (k - c)}| \cdot \\
                           &   &
    \frac{(n-(k-c)) \cdot (n-(k-c) - 1)\cdot \ldots \cdot (n-(k-c) - c + 1)}{c_1! \cdot c_2! \cdot \ldots \cdot c_m!}\,.
    \quad \Box
  \end{eqnarray*}

\end{theorem}

It is clear that the complexity of the algorithm of \ref{theorem_count_by_length_and_count}
is $O(m\cdot n)$, which, depending on how the $k_i$ are defined, can be anything between
$O(n)$ and $O(n^2)$.

\section{Iterating over Specifications by Lengths And Counts}
\label{sec_iterating}
\begin{purpose}
  Present a generic algorithm for counting the permutations that meet certain specifications
  by lengths and counts, where a client-supplied function performs the selection of
  specifications to be included in the count.
\end{purpose}

Now that we know how to calculate $|{\mathcal R}_{(n,T)}|$, that is, the number of permutations that
meet a given specification by lengths and counts, it is an obvious and rather trivial thing to write
an algorithm that performs an in-place creation of every specification by lengths and counts for a
given $n$ and lets a user-provided function decide which ones should be included in the count.
Therefore, the following theorem requires no further proof.

\begin{theorem}\label{theorem_iterating_over_specs_by_count_by_length_and_count}
  Let $n \geq 1$, let ${\mathcal T}$ be the set of all MCS-specifications by
  lengths and counts for $n$, and let $f$ be a function from ${\mathcal T}$ to the set $\{\, 0, 1\,\}$.
  Then the expression
  $$
  \sum_{\{\,T \in{\mathcal T}\,\mid\, f(T) = 1\,\}}|{\mathcal R}_{(n,T)}|\eqno(3)
  $$
  amounts to an algorithm for calculating the number of permutations that meet exactly
  those MCS-specifications by lengths and counts for $n$ on which the function $f$ returns $1$.
  \endproof
\end{theorem}

The problem with this algorithm is that the number of MCS-specifications by lengths and counts
for $n$ grows faster with $n$ than one would wish. By definition, the number of these specifications is
\begin{eqnarray*}
  \big|\{\,(k_1, c_1), (k_2, c_2), \ldots,(k_m, c_m) & \mid & k_i \geq 2,\ c_i \geq 1 \text{ for } 1\leq i \leq m,\\
                                                 &      & \sum_{i=1}^m c_i \cdot k_i \leq n,\
                                                          k_i\neq k_j\text{ for } 1\leq i,j \leq m \,\}\big|\,.
\end{eqnarray*}
Determining what this is looks like a non-trivial combinatorial problem unto itself.
At this point, the best we know how to do is to look at some numbers. The brute force
approach to counting permutations begins to encounter performance issues at $n=12$, as
$12!=4.79001600\times 10^8$, and performance degrades quickly after that.
A comparable number of MCS-specifications by lengths and counts,
namely, $4.83502844\times 10^8$, is reached for $n=108$. Indeed, the algorithm of Theorem
\ref{theorem_iterating_over_specs_by_count_by_length_and_count} starts to noticeably slow
down for $n$ around $100$ \cite{Performance}. Considering that $60!$ is roughly equal to the number of atoms
in the known, observable universe, being able to count permutations for $n=100$ must be
considered an achievement. On the other hand, $100$ is not exactly, shall we say, a large number.

Luckily, many common questions regarding the number of permutations with certain consecutive
sequences allow an optimization that can cut down dramatially on the number of MCS-specifications
by lengths and counts that need to be considered in the sum in (3) above. Typically, when counting
permutations with certain configurations of consecutive pairs, triples, quadruples, etc.
(maximal or not necessarily maximal), one knows in advance that the selection function $f$ of
Theorem \ref{theorem_iterating_over_specs_by_count_by_length_and_count} will reject every
MCS-specification by lengths and counts that specifies a non-zero count for maximal consecutive
sequences of length greater than some bound and/or less than some bound. To exploit that,
our implementation of the algorithm \cite{Algos} lets the user specify a
lower and/or upper bound for non-zero lengths
of maximal consecutive sequences. If $l$ and $u$ are the specified bounds, then the algorithm
will include only those MCS-specifications by lengths and counts in the sum in (3) above that
specify 0 for any length less than $l$ and greater than $u$.

For example, when
calculating the number of permutations that have maximal consecutive pairs but no other maximal
consecutive sequences, that is, permutations that have any number of consecutive pairs none of
which are linked to form longer consecutive sequences, one would tell the algorithm
to only generate those MCS-specifications by lengths and counts that specify a count greater than zero
for length 2 and a zero count for all lengths greater than 2. This cuts the length of the sum in (3)
above down to $\lfloor\frac{n}{2}\rfloor$.

Sometimes, it takes a bit of creativity to avail oneself of the lower bound/upper bound optimization. Suppose,
for example, that you wish to calculate the number of permutations that have at least one maximal consecutive
sequence of length greater than or equal to $k$ for some $k$.
As it stands, this condition does not allow you to use the lower bound/upper bound optimization.
But you could also calculate the number of permutations that have no maximal consecutive sequences
greater than or equal to $k$ and then subtract that from $n!$. Now the optimization is applicable.

\section{Permutations with Consecutive Sequences}
The algorithms we have discussed so far deal with the number of permutations having certain configurations
of maximal consecutive sequences. Oftentimes, one is interested in the number of permutations having certain
kinds of---not necessarily maximal---consecutive sequences. Adapting our algorithms for that purpose is
rather straightforward, and in some cases trivial. For a trivial case, consider the question, ``How many
permutations are there in ${\mathcal P}_n$ that have consecutive sequences of length $k$?'' Having a
consecutive sequence of length $k$ is obviously equivalent to having a maximal consecutive sequence of
length greater than or equal to $k$. Therefore, this is the application of Theorem
\ref{theorem_iterating_over_specs_by_count_by_length_and_count} that we mentioned at the end of the
previous section.

Perhaps the most commonly asked question about consecutive sequences is, ``How many
permutations are there in ${\mathcal P}_n$ that have $c$ many consecutive sequences of length $k$?''
To use our algorithms for answering this question, we need the following lemma whose proof is trivial.

\begin{lemma}
  Let $n \geq 1$, let $T = \{\,(k_1, c_1), (k_2, c_2), \ldots,(k_m, c_m)\,\}$ be an
  MCS-specification by lengths and counts for $n$. Furthermore, let $P\in {\mathcal R}_{(n,T)}$,
  that is, $P$ is a permutation that meets the specification $T$, and let $k \geq 2$.
  Then the number of consecutive
  sequences of length $k$ in $P$ equals
  $$
  \sum_{\shortstack{$\scriptstyle i=1$\\$\scriptstyle k_i\geq k$}}^{m}c_i\cdot (k_i - k + 1)\,.
  $$
\end{lemma}

It is now a straightforward task to count the permutations that have exactly $c$ consecutive sequences
of length $k$: apply Theorem \ref{theorem_iterating_over_specs_by_count_by_length_and_count} with a
selection function that employs the lemma above to accept exactly those MCS-specifications by lengths
and counts that result in $c$ consecutive sequences. Note also that the upper bound optimization that
we mentioned following Theorem \ref{theorem_iterating_over_specs_by_count_by_length_and_count} is
applicable here. That's because we know that any MCS-specification by lengths and count that specifies
a non-zero count for a length $l$ with $l > k + c - 1$ will be rejected. Therefore, we only need to
look at MCS-specifications by lengths and counts that specify a zero count for lengths greater than
$k + c - 1$. Our algorithm package on github \cite{Algos} has a ready-to-use implementation.

\bibliographystyle{alpha}
\begin{thebibliography}{0}

\bibitem{Oeis}
  \href{http://oeis.org/}{Online Encyclopedia of Integer Sequences}

\bibitem{OeisRefs}

  Even if the author's mathematical specialty were combinatorics, which it is not,
  it would be foolish to attempt an overview or an even remotely complete set of references
  in a short article like this. A good place to learn about existing results and start finding
  references is the \href{http://oeis.org/}{Online Encyclopedia of Integer Sequences}, specifically
 the entries \href{http://oeis.org/A010027}{A010027}, \href{http://oeis.org/A002628}{A002628}, and
  \href{http://oeis.org/A000255}{A0000255}.

\bibitem{Algos}
    \href{https://github.com/walkswiththebear/ConsecutiveSequences}{ConsecutiveSequences} on github.

\bibitem{BlogPost}
    \href{http://blog.greaterthanzero.com/post/159874910652/some-mathematics-algorithms-and-probabilities}{Some Mathematics, Algorithms, and Probabilities Concerning Your Music Player’s Random Shuffle Mode} at the GreaterThanZero company blog.

\bibitem{JedYang}
 This \href{https://www.quora.com/What-is-the-probability-that-a-shuffled-music-album-will-have-at-least-two-songs-in-their-original-relative-consecutive-order}{proof by Jed Yang}
  on Quora is short, elegant, and self-contained.

\bibitem{Performance}
  This is assuming that only a small number of MCS-specifications are accepted for the count. If many are accepted,
  the slowdown can begin as early as $n=75$.

\end{thebibliography}

\end{document}