\documentclass[a4paper,12pt,fleqn]{scrartcl}
\usepackage[l2tabu,orthodox]{nag}% Old habits die hard. All the same, there are commands, classes and packages which are outdated and superseded. nag provides routines to warn the user about the use of those.

\usepackage[all,error]{onlyamsmath}% Error on deprecated math commands like $$ $$.
\usepackage[strict=true]{csquotes}

%\usepackage{color}

\usepackage{listings}
\lstset{frame=single,framerule=0pt,language={C},showstringspaces=false,numbers=left,columns=fullflexible}

% COMP2111-specific macros. See
% http://www.cse.unsw.edu.au/~cs2111/18s1/LaTeX/primer.html
\usepackage{2111defs2}
\usepackage[colorlinks=true]{hyperref}

\newcommand{\assn}[1]{{\color{red}\left\{#1\right\}}}
\newcommand{\remark}[1]{{\sffamily\color{blue}{#1}}}

% define some convenience macros specific to this task
\newcommand{\perm}{\mathsf{perm}}
%title variable
\title{Assignment 2}
\author{Ruofei HUANG(z5141448)\and
Anqi ZHU(z5141541)
}

\newcommand{\variant}[3]{(#1:#2\mapsto #3)}
\newcommand{\oldwhatever}[4]{\variant{#1}{#2}{\variant{#1[#2]}{#3}{#4}}}
\newcommand{\whatever}[4]{\variant{#1}{#2\mapsto #3}{#4}}
\newcommand{\ah}{\mathsf{a}}
\newcommand{\be}{\mathsf{b}}
\newcommand{\length}[1]{\left|#1\right|}
\newcommand{\noof}[2]{\left\|#1\right\|_{#2}}

\newcommand{\pre}{\mathit{pre}}
\newcommand{\post}{\mathit{post}}
\def\L{\mathcal{L}}
\begin{document}
\maketitle
\section{Task 1}
\subsection{Prime }
We define a number $n$ to be a prime if it is a natural number greater than 1 and
cannot be formed by mutiplying two natural numbers (bigger than 1) smaller than
itself\footnote{
Reference from Wikipedia:
\url{https://en.wikipedia.org/wiki/Prime_number}}.
Hence we can describe the set containing all primes as:
\begin{gather*}
  Prime = \{ n\in \nat | \neg (\Exi{x\in (1..n-1)}{x|n}) \And n > 1 \}
\end{gather*}
GMP provides a function called ISPRIME() to check if a certain number $n$ is a
prime or not. The procedure of this function can be expressed as:
\begin{gather*}
  \PROC \ ISPRIME(\VALUE \ n, \RESULT \ p) \cdot \\
  n, p: [TRUE, (n_0 \in Prime \And p > 0) \Or (n_0 \notin Prime \And p \leq 0)]
\end{gather*}
\subsubsection{Procedure Call Simplification}
We shall simplify its procedure call sugar, $\RESULT \ p = ISPRIME(\VALUE \ n)$, to
verify primes later in our refinement. \\
According to its original procedure call sugar, we can explain the verification in
toy language as:
\begin{gather*}
  \VAR \ c;\\
  ISPRIME(n,c);\\
  \IF ~c >0~ \THEN\\
  \qquad  \assert{n \in Prime}\\
  \qquad ...\\
  \ELSE \\
  \qquad  \assert{n \notin Prime}\\
  \qquad ...\\
  \FI
\end{gather*}
This can be replaced by our homemade procedure call sugar \~:
\begin{gather*}
  \IF ~ISPRIME(n) >0~ \THEN\\
  \qquad  \assert{n \in Prime}\\
  \qquad ...\\
  \ELSE \\
  \qquad  \assert{n \notin Prime}\\
  \qquad ...\\
  \FI
\end{gather*}
to simplify our proof. We can also use this simplified sugar in while rules as:
\begin{gather*}
  \WHILE ~ISPRIME(n) >0~ \DO\\
  \qquad  \assert{n \in Prime}\\
  \qquad ... \\
  \OD
\end{gather*}

\subsection{Reverse}
By the spec in verifying a number $v$ which is the reverse of the number $n$
\footnote{A proof provided by Lecturer in Control of this course on
\url{https://www.cse.unsw.edu.au/~cs2111/18s1/lec/reverse.pdf} },
we can have the following mathematical definition:
\begin{gather*}
  v = rev(n) = \sum_{i= 0}^{c(n)}(S_i 10^i)
\end{gather*}
where:
\begin{gather*}
  c(n) = \lfloor log_{10}(n)\rfloor ,\\
  S = [10]^* ,\\
  n \in \nat \And n = \sum_{i=0}^{c(n)}(S_i 10 ^{(c(n)-i)})
\end{gather*}
Then we can simplify the spec of
$\PROC \ reversen(\VALUE \ n: \nat, \RESULT \ v: \nat )$
given by the lecturer, as
\begin{gather*}
  \PROC \ reversen(\VALUE \ n: \nat, \RESULT \ v: \nat ) \cdot \\
  r, v:[\True, v = rev(r_0)]
\end{gather*}

\subsection{Emirp}
An emirp $n$ is a prime number that results in a different prime when its
decimal digits are reversed\footnote{
  Another reference from Wikipedia :\url{https://en.wikipedia.org/wiki/Emirp}
}. The definition of emirp can be construct in mathematical semantics as follows:
\begin{gather*}
  n \in Emirp \iff n \in Prime \And rev(n) \in Prime \And n \neq rev(n)
\end{gather*}
Then we can define a function to check if the specified number $n$ is an emirp.
The function is such as:
\begin{gather*}
  isEmirp(n)= \begin{cases}
    1 & \text{if } n \in Prime \And rev(n) \in Prime \And n \neq rev(n)\\
    0 & \text{else }
  \end{cases}
\end{gather*}
We use 0 and 1 as our returning value of the function, so that we can find out
how many emirps are found in the range of 2 .. $n$ according to the following
mathematics semantics:
\begin{gather*}
  \textbf{the number of emirps found} = \sum_{i=0}^{n}isEmirp(i)
\end{gather*}
where:
\begin{gather*}
  n\in \nat_{>1}
\end{gather*}

\subsubsection{Derivation of ISEMIRP() Procedure Call}
We want to transfer the isEmirp() function into a procedure so that we can use it in
our later refinement. We start with a spec of the procedure.
\begin{gather*}
  \PROC \ ISEMIRP(\VALUE \ n: \nat, \RESULT \ w)\cdot \\
  \nt{
  n,w:
  [
    \True,
    \left(
      \begin{array}{l}
        (w= 1\And rev(n_0)\neq n_0 \And n_0 \in Prime \And rev(n_0)\in Prime) \Or \\
        (w= 0\And \neg (rev(n_0)\neq n_0 \And n_0 \in Prime \And rev(n_0)\in Prime))
      \end{array}
    \right)
  ]}{(A)}
\end{gather*}

\begin{align*}
  \lrefstep{(A)}{\textbf{c-frame}}{
    w:
    [
      \True ,
      \left(
        \begin{array}{l}
          (w= 1\And rev(n)\neq n \And n \in Prime \And rev(n)\in Prime) \Or \\
          (w= 0\And \neg (rev(n)\neq n \And n \in Prime \And rev(n)\in Prime))
        \end{array}
      \right)
    ]
  }
  \\
  \refstep{\textbf{i-loc}}{
    \textbf{var}\ r\cdot r,w:
    [
      \True ,
      \left(
        \begin{array}{l}
          (w= 1\And rev(n)\neq n \And n \in Prime \And rev(n)\in Prime) \Or \\
          (w= 0\And \neg (rev(n)\neq n \And n \in Prime \And rev(n)\in Prime))
        \end{array}
      \right)
    ]
  }
  \\
  \refstep{\textbf{seq2}}{
    \nt{r:
    [
      \True ,
      r= rev(n)
    ];}{(B)}\\
    &
    \nt{r, w:
    [
      r= rev(n) ,
      \left(
        \begin{array}{l}
          (w= 1\And rev(n)\neq n \And n \in Prime \And \\
          rev(n)\in Prime)\Or (w= 0\And \neg (rev(n)\neq n\\
          \And n \in Prime \And rev(n)\in Prime))
        \end{array}
      \right)
    ]}{(C)}
  }
  \\
  \lrefstep{(B)}{\textbf{proc}}{
    reversen(n, r);
  }
  %
  %  a c-frame is require
  \\
  \lrefstep{(C)}{\textbf{c-frame}}{
    \nt {w:
    [
      r= rev(n) ,
      \left(
        \begin{array}{l}
          (w= 1\And rev(n)\neq n \And n \in Prime \And \\
          rev(n)\in Prime)\Or (w= 0\And \neg (rev(n)\neq n\\
          \And n \in Prime \And rev(n)\in Prime))
        \end{array}
      \right)
    ]}{(C*)}
  }
  %
  \\
  \lrefstep{(C*)}{\textbf{s-post}, justified below in 1.3.2}{
    \nt{
    w:
    [
      r= rev(n) ,
      \left(
        \begin{array}{l}
          (w= 1\And r\neq n \And n \in Prime \And r\in Prime) \Or \\
          (w= 0\And \neg (r\neq n \And n \in Prime \And r\in Prime))
        \end{array}
      \right)
    ]}{(C')}
  }
  %
  \\
  \lrefstep{(C')}{\textbf{if}}{
    \IF ~r \neq n ~ \\
    &\THEN\
      \nt{ w:
      [
        r \neq n \And \pre{(C')},
        \post{(C')}
      ]}{(D)}\\
    &\ELSE\
      \nt{ w:
      [
        r = n \And \pre{(C')},
        \post{(C')}
      ]}{(E)}
    \\
    &\FI \\
  }
  \\
  \lrefstep{(D)}{\textbf{if}}{
    % TODO: \in Prime \neq isPrimem (n)
    \IF ~ ISPRIME(n) > 0 ~ \\
    &\THEN\
      \nt{ w:
      [
        \pre{(D)} \And n \in Prime,
        \post{(C')}
      ]}{(F)}\\
    &\ELSE\
      \nt{ w:
      [
        \pre{(D)} \And n \notin Prime,
        \post{(C')}
      ]}{(G)}
    \\
    &\FI \\
  }
  \\
  \lrefstep{(F)}{\textbf{if}}{
    % TODO: \in Prime \neq isPrimem (n)
    \IF ~ISPRIME(r) > 0  ~ \\
    &\THEN
      \nt{ w:
      [
        \pre{(F)} \And r \in Prime,
        \post{(C')}
      ]}{(H)}\\
    &\ELSE
      \nt{ w:
      [
        \pre{(F)} \And r \notin Prime,
        \post{(C')}
      ]}{(I)}
    \\
    &\FI
    \displaybreak\\
  }
  % TODO: Need to be justify
  \lrefstep{(E)(G)(I)}{\textbf{ass}, justfied below in 1.3.3}{
    w \Ass 0
  }
  \\
  % TODO: Need to be justify
  \lrefstep{(H)}{\textbf{ass}, justified below in 1.3.4}{
    w \Ass 1
  }
\end{align*}
We gather the code for the procedure body of ISEMIRP:
\begin{gather*}
  \textbf{var}\ r;\\
  reversen(n, r);\\
  \IF~ r\neq n\ \THEN~ \\
  \qquad \IF~ ISPRIME(n)>0\ \THEN~ \\
  \qquad\qquad \IF~ ISPRIME(r)>0\\
  \qquad\qquad\THEN~ w:=1;\\
  \qquad\qquad \ELSE~ w:=0; \\
  \qquad\qquad \FI~\\
  \qquad \ELSE~ w:=0;\\
  \qquad \FI~\\
  \ELSE~ w:=0;\\
  \FI~
\end{gather*}

\subsubsection{Proof of $r= rev(n)\subst{w_0}{w}\And \post{(C*)}$}
\begin{align*}
  &r= rev(n)\subst{w_0}{w}\And \post{(C')}\\
  \justification{Subutitue and expand $\post{(C')}$}
  &
  r= rev(n) \And
  \left(
    \begin{array}{l}
      (w= 1\And rev(n)\neq n \And n \in Prime \And \\
      rev(n)\in Prime)\Or (w= 0\And \neg (rev(n)\neq n\\
      \And n \in Prime \And rev(n)\in Prime))
    \end{array}
  \right)\\
  \justification{Substitute $r = rev(n)$}
  &
  \left(
    \begin{array}{l}
      (w= 1\And r\neq n \And n \in Prime \And r\in Prime)\Or \\
      (w= 0\And \neg (r\neq n \And n \in Prime \And r\in Prime))
    \end{array}
  \right)\\
  \justification{definition of $\post{(C)}$}
  &\post{(C)}
\end{align*}

\subsubsection{Proof of $(E)(G)(I)\sqsubseteq w \Ass 0$}
Before proving this assertion, we oberserve that in the $\post{(C')}$, we have
\begin{align*}
  &\neg (r\neq n \And n \in Prime \And r\in Prime) \\
  \Equiv& r = n \Or n \notin Prime \Or r\notin Prime\\
  \Implies & w= 0
\end{align*}
Then this assertion is valid:
\begin{gather*}
  r = n \Or n \notin Prime \Or r\notin Prime \Implies \post{(C')}\subst{0}{w}
\end{gather*}
By observe the $ \pre{(E)}$ ,$ \pre{(G)}$ ,$ \pre{(I)}$, we have:
\begin{gather*}
  \pre{(E)} \Implies r = n          \Implies \post{(C')}\subst{0}{w}\\
  \pre{(G)} \Implies n \notin Prime \Implies \post{(C')}\subst{0}{w}\\
  \pre{(I)} \Implies r \notin Prime \Implies \post{(C')}\subst{0}{w}
\end{gather*}
Which all prove the validity.

\subsubsection{Proof of $(H)\sqsubseteq w \Ass 1$}
Similar to 1.3.3 we have:
\begin{gather*}
  \pre{(H)}\Equiv (r\neq n \And n \in Prime \And r\in Prime)
  \Implies \post{(C')}\subst{1}{w}
\end{gather*}

\subsection{Specification of the Main Procedure}
The job of our main procedure, $\PROC \ EMIRP(\VALUE \ n: \nat, \RESULT \ r)$,
is to find and return the $n^{th}$ emirp, where $n$ is a given positive parameter.
Using the function $isEmirp()$ which is defined and proved above, we can specify
our main procedure as:
\begin{gather*}
  \PROC \ EMIRP(\VALUE \ n: \nat, \RESULT \ r)\cdot \\
  \nt{n, r:
  [
    n>0 ,
    \sum_{i=2}^{r}isEmirp(i)= n_0 \And r \in Emirp
  ]}{(1)}
\end{gather*}

\section{Task 2}
\begin{align*}
  \lrefstep{(1)}{\textbf{c-frame}}{
    r:
    [
      n>0 ,
      \sum_{i=2}^{r}isEmirp(i)= n \And r \in Emirp
    ]
  }
  \\
  \refstep{\textbf{i-loc}}{
    \textbf{var}\ count \cdot count, r:
    [
      n>0 ,
      \sum_{i=2}^{r}isEmirp(i)= n \And r \in Emirp
    ]
  }
  \\
  \refstep{\textbf{seq}}{
    \nt{
      count, r:
      [
        n>0 ,
        Inv
      ]
    }{(2)};\\
    &\nt{
      count , r:
      [
        Inv,
        \sum_{i=2}^{r}isEmirp(i)= n \And r \in Emirp
      ]
    }{(3)}
  }
\end{align*}
Where the loop invariant is defined by:
% TODO: Modify Inv
\begin{gather*}
  Inv =
  \left( \begin{array}{l}
    count = \sum_{i=2}^{r}isEmirp(i) \And n > 0\And 0 \leq count \leq n\And r \geq count
  \end{array}\right)
\end{gather*}
\begin{align*}
  \lrefstep{(2)}{Routine work: initialize the variables in the loop}{
    r \Ass 1; count \Ass 0;
  }
  \\
  % TODO: Check whether it's correct s-post
  \lrefstep{(3)}{\textbf{s-post}, justified below in 2.1}{
    count , r:
    [
      Inv,
      Inv \And (count = n \And r \in Emirp)
    ]
  }
  \\
  \refstep {\textbf{while}, this simplification proof is above in 1.1.1}{
    \WHILE ~count \neq n \Or ISEMIRP(r)\neq 1 ~\DO \\
    & \qquad
    \nt{
      count , r:
      [
        Inv \And (count \neq n \Or r \notin Emirp),
        Inv
      ]
    }{(4)}
    \\
    &\OD
  }
  \\
  \lrefstep{(4)}{\textbf{s-post}, justified below in 2.2}{
    \nt{
    count, r:
    [
      Inv \And (count \neq n \Or r \notin Emirp),
      Inv\subst{count+isEmirp(r)}{count}\subst{r+1}{r}
    ];
    }{(5)}
  }
  \\
  \lrefstep{(5)}{\textbf{seq2}}{
    \nt{
    r:
    [
      Inv \And (count \neq n \Or r \notin Emirp),
      Inv\subst{r+1}{r}
    ];
    }{(6)}
    \\
    & \nt{
    count,r:
    [
      Inv\subst{r+1}{r},
      Inv\subst{count+isEmirp(r)}{count}\subst{r+1}{r}
    ]
    }{(7)}
  }
  \\
  \lrefstep{(6)}{\textbf{ass}}{
    r\Ass r+1;
  }
  \\
  \lrefstep{(7)}{\textbf{ass}}{
    count \Ass count+ISEMIRP(r);
  }
  % TODO: Need to justify
  % \\
  % \lrefstep{(5)}{\textbf{c-frame, ass}, justified below}{
  %   r \Ass r+1;
  % }
\end{align*}
We gather the code for the procedure body of EMIRP:
\begin{gather*}
  \textbf{var}\ count;\\
  r \Ass 1; \\
  count \Ass 0;\\
  \WHILE ~count \neq n \Or  ISEMIRP(r) \neq 1~ \DO \\
  \qquad r \Ass r+1;\\
  \qquad count \Ass count + ISEMIRP(r);\\
  \OD
\end{gather*}

\subsection{Proof of
$\pre(3)\subst{count_0}{count}\subst{r_0}{r} \And (Inv \And (count = n \And r \in Emirp))
\Implies \post{(3)}$}
\begin{align*}
  & \pre(3) \And Inv \And (count = n \And r \in Emirp)\\
  \justification{Expand $\pre(3)$ }
  & Inv\subst{count_0}{count}\subst{r_0}{r}
    \And (Inv \And (count = n \And r \in Emirp))
  \\
  \justification{Substitute $Inv$ and
    $Inv\subst{count_0}{count}\subst{r_0}{r}$ }
  & \left( \begin{array}{l}
    (count_0 = \sum_{i=2}^{r_0}isEmirp(i)
    \And n > 0\And 0 \leq count_0 \leq n\And r_0 \geq count_0) \And\\
    (count = \sum_{i=2}^{r}isEmirp(i) \And
    n > 0\And 0 \leq count \leq n\And r \geq count \And\\
    (count = n \And r \in Emirp))
  \end{array}\right)
  \\
  \justification[\Implies]{Simplify and remove $count$.}
  & \left( \begin{array}{l}
    (count_0 = \sum_{i=2}^{r_0}isEmirp(i)
    \And n > 0\And 0 \leq count_0 \leq n\And r_0 \geq count_0) \And\\
    (n= \sum_{i=2}^{r}isEmirp(i) \And r \geq n \And  r \in Emirp)
  \end{array}\right)
  \\
  \justification[\Implies]{Directly imply from second line.}
  & \sum_{i=2}^{r}isEmirp(i)= n \And r \in Emirp \\
  \justification{Definition of $\post{(3)} $}
  & \post{(3)}
\end{align*}

% \subsection{Proof of $(5) \isrefinedby r\Ass r+1  $}
% We need to prove the validity of:
% \begin{gather*}
%   ss
% \end{gather*}

\subsection{Proof of $\pre{(4)}\subst{count_0}{count}\subst{r_0}{r}\And
 Inv\subst{count+isEmirp(r)}{count}\subst{r+1}{r}\Implies \post{(4)}$}
\begin{align*}
  & pre{(4)}\subst{count_0}{count}\subst{r_0}{r}\And
  Inv\subst{count+isEmirp(r)}{count}\subst{r+1}{r}\\
  \justification{Expand $\pre{(4)}$ and substitute}
  & (Inv \And (count_0 \neq n \Or r_0 \notin Emirp))\And
  Inv\subst{count+isEmirp(r)}{count}\subst{r+1}{r}\\
  \justification{Expand Inv and substitute}
  & \left( \begin{array}{l}
    ((count_0 = \sum_{i=2}^{r_0}isEmirp(i)
    \And n > 0\And 0 \leq count_0 \leq n\And r_0 \geq count_0) \\
    (count_0 \neq n \Or r_0 \notin Emirp))\And \\
    %
    (count+isEmirp(r+1) = \sum_{i=2}^{r+1}isEmirp(i)
    \And n > 0\And\\
     0 \leq count+isEmirp(r+1) \leq n\And r+1 \geq count+isEmirp(r+1))
    %
  \end{array}\right)
  \\
  \justification[\Implies]{Separate $\sum_{i=2}^{r+1}isEmirp(i)$}
  & \left( \begin{array}{l}
    ((count_0 = \sum_{i=2}^{r_0}isEmirp(i)
    \And n > 0\And 0 \leq count_0 \leq n\And r_0 \geq count_0) \\
    (count_0 \neq n \Or r_0 \notin Emirp))\And \\
    %
    (count+isEmirp(r+1) = \sum_{i=2}^{r}isEmirp(i)+isEmirp(r+1)
    \And n > 0\And\\
     0 \leq count+isEmirp(r+1) \leq n\And r+1 \geq count+isEmirp(r+1))
    %
  \end{array}\right)
  \\
  \justification[\Implies]{Merge $count_0$ and $count$, $r_0$ and $r$}
  & \left( \begin{array}{l}
    count = \sum_{i=2}^{r}isEmirp(i) \And n > 0\And 0 \leq count \leq n\And r \geq count
    \end{array}\right)
  \\
  \justification{Definition of Inv}
  & Inv
  \\
  \justification{Definition of $\post{(4)}$}
  & \post{(4)}
\end{align*}
\section{Task 3}
\lstinputlisting{emirp.c}

\section{Task 4}
We have implemented the procedure calls $ISEMIRP$ and $EMIRP$ to our C functions.
For the $ISPRIME$ function we used a libarary function in GMP
(\url{mpz_probab_prime_p}). We pretended that the libarary function will always
give back an accurate check for whether the number is a prime or not (We tested that
the function could be accurate enough until at least finding the 1000th emirp.)\\
We set $r$ to have an initial value of 1, then it would be start
searching $Emirp$ from 2 because $ r \Ass r +1 $ is before
$ count \Ass count +1 $. Also we found it difficult to prove when the
loop is break then it will implies $r \in Prime$, so we have add a check
in the while loop.
\end{document}


\chapter{Analyzing Synthetic Data}
\label{analysis_chapter}
\thispagestyle{empty}

In this chapter we focus on our research goals \textbf{RG1.1} and \textbf{RG2} as defined in section \ref{research_goals}. This chapter is divided into two sections. We name the first section as \textbf{Static networks}, to signify working with pre-generated synthetic networks as per \textbf{RG1.1}. The next section is named as \textbf{Growing networks} to signify the growth of synthetic networks with the aid of recommendation engine ($R$) as per the requirement of \textbf{RG2}.

We start each section by defining the experimental setup for the given goal and how we strive to fulfill it. We define the algorithm we follow for the generation of synthetic networks which we would use for our analysis and also state the various static and tuneable parameters for our experiments. Once we have clearly defined our setup, we look at the results obtained through our experiments in the following subsections and analyze them in detail. We finish each section with the important findings from our observations.

\section{Static Networks}
\label{static_networks}
In this section we deal with pre-generated synthetic networks. In the experimental setup subsection we first talk about how we generate these pre-generated synthetic networks. In the results subsection we look at the different properties of these pre-generated synthetic networks and analyze the visibility bias we observe in the recommendation results.

\subsection{Experimental Setup}
We need to generate synthetic networks which can be used for our bias analysis. For the generation of synthetic networks we use the steps designed by Karimi et. al. \cite{karimi2018homophily}. Algorithm \ref{algo_karimi_model} shows the detailed steps we use to output a synthetic network $G_{t}(V_{t},E_{t})$ where $\delta(x)$ denotes the current degree of the node $x$ in the network. We then use these generated synthetic network as an input to generate a recommendations list ($L_{v}, |L_{v}|=k$) for each node $v \in V_t$. The recommendation list $L_{v}$ is given by the various recommender engines $R$ (the working of which we have discussed previously in detail in chapter \ref{recommender_methods}) we use.

\begin{algorithm}
	\SetAlgoLined
	\DontPrintSemicolon
	\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
	\Input{Total iterations : $t$\\Initial number of nodes : $m_{0}$\\Edges added per iteration: $m \leq m_{0}$\\Symmetric homophily value : $h$\\Minority Fraction: $f$}
	\Output{Network : $G_{t}(V_{t},E_{t})$ where $|V_{t}| = t+m_{0}, |E_{t}| \leq mt$}
	\BlankLine
	$n \leftarrow t+m_{0}$\;
	$N = \{v_{1},...,v_{n}\}$\;
	Assign $(f \times n)$ nodes in $N$ randomly to the minority group. Assign rest of the nodes in $N$ to the majority group.\;
	$V_{0} = \{v_{1},...,v_{m_{0}}\}$\;
	\For{$i \leftarrow 1$ \KwTo $t$}{
		$j \leftarrow (i+m_{0})$\;
		$V_{i} = V_{i-1} \cup \{v_{j}\}$\;
		\ForEach{vertex $u \in V_{i-1}$}{
			$\Pi_{u} = (\delta(u) \times h_{uv_{j}}) / \sum_{w \in V_{i-1}}^{} (\delta(w) \times h_{wv_{j}}) $\;
		}
		Choose destination set $D \subseteq V_{i-1}, |D| \leq m$ based on probabilities $\Pi_{u \in V_{i-1}}$\;
		$E_{i} = E_{i-1} \cup \{(v_{j}, u) | u \in D\}$\;
	}
	\caption{Barabási-Albert network generation model with homophily \cite{karimi2018homophily}}\label{algo_karimi_model}
\end{algorithm}

In table \ref{table_karimi_model} we list down the various tuneable and static parameters in our experimental setup for algorithm \ref{algo_karimi_model}.

\begin{table}[h]
	\centering
	\begin{tabular}{ |c|c| }
		\hline
		\textbf{Parameter} & \textbf{Value} \\
		\hline
		Total Iterations ($t$) & 998 \\
		Initial Number of Nodes ($m_{0}$) & 2 \\
		Edges added per iteration ($m$) & 2 \\
		Symmetric homophily value ($h$) & [0.0, 0.2, 0.5, 0.8, 1.0] \\
		Minority Fraction ($f$) & [0.1, 0.2, 0.3, 0.4, 0.5] \\
		\hline
	\end{tabular}
	\caption{Parameters for generation of synthetic networks as outlined by algorithm \ref{algo_karimi_model}}
	\label{table_karimi_model}
\end{table}

To obtain the recommendation list $L_{v}$ for a node $v \in V_{t}$ from the recommendation engine $R$ we provide as input $G_{t}(V_{t}, E_{t})$ and $v$ to $R$ and receive as output the list $L_{v}$. In table \ref{table_recommendation} we list down the various parameters used by the Recommender system $R$.

\begin{table}[h]
	\centering
	\begin{tabular}{ |c|c| }
		\hline
		\textbf{Parameter} & \textbf{Value} \\
		\hline
		Training Iterations ($T$) & 10000 \\
		Length of recommendation list ($k$) & 5 \\
		Ranking Factor ($r$) & [0.0, 0.5, 1.0] \\
		\hline
	\end{tabular}
	\caption{Parameters for recommendation engine $R$}
	\label{table_recommendation}
\end{table}

\subsection{Experimental Results}
Given the experimental setup as discussed above, we generate synthetic networks by tuning the different tuneable parameters generating 5 simulations for each variation. Below we look at the plots for degree distribution, degree growth, fraction of minorities holding total degree and top D\% degree rank to gain insights about these generated network. Our findings are similar to that of Karimi et. al. in \cite{karimi2018homophily}, and the differences can be attributed to our use of less iterations ($t$) to generate smaller networks and having less number of simulations (5) for each variation. Upon understanding the network structure we next look at the Disparate Visibility \cite{fabbri2020effect} measure for the recommendations we gather for all nodes in the network.

\subsubsection{Degree Distribution}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_static.png}
	\caption{Degree distribution for static networks. The minority fractions are provided at the right-side of each row while the homophily is stated at the top of each column. Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_static_fig}
\end{figure}

Figure \ref{dd_static_fig} shows the plot for degree distribution of our generated synthetic networks. At the bottom row where both group sizes are same, the distribution is similar for all homophily values. As we go upwards towards networks with smaller minority group size, we see the disparity in distribution grow. Minority nodes are at an advantage in the heterophilic regime since they garner a lot of attention from the majority group. This changes as the homophily value increases putting majorities in favour as they prefer nodes from their own larger group. In a completely homophilic network, there exists in-group competition which decreases the difference in degree distribution values among groups.

\subsubsection{Degree Growth}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_static.png}
	\caption{Degree growth for static networks. The minority fractions are provided at the right-side of each row while the homophily is stated at the top of each column. Degree growth for minority and majority node is visualized using the red and the blue color plot lines respectively.}
	\label{dg_static_fig}
\end{figure}

Figure \ref{dg_static_fig} shows how the degree of a majority and a minority node grows over time in the network generation process. We have realized before in our analysis of degree distribution, when the minority group size is small they tend to have higher degrees in heterophilic regime. In the homophilic regime, the majority groups have a slight advantage but due to competition amongst themselves this growth factor is small. We see in our plot however that when both group sizes are same, the minority has a higher growth over majority for $h=1.0$ and also the majority has a higher growth over minority at $h=0.8$. This is probably due to the choice of the nodes which contribute to the growth plot. We conducted similar network generation experiments for higher iteration values($t=5000$) and there we did not perceive this effect. We choose to keep these results though since these are the same networks we use in our later experiments.

\subsubsection{Fraction of total degree held by minorities}

\begin{figure}[h!]
	\centering
	\includegraphics[width=0.8\textwidth]{images/mf_static.png}
	\caption{Fraction of total degree held by minority nodes in static networks at different minority fractions and different homophily values.}
	\label{mf_static_fig}
\end{figure}

Figure \ref{mf_static_fig} shows the fraction of total degrees held by the minority nodes in the network. In the heterophilic regime, the minority nodes hold a higher degree due to being favored by the majority nodes (which causes the \textit{``majority illusion''} problem). At $h=0.5$, where homophily doesn't play a role the degree fraction is nearly the same as minority group size. As we move towards the homophilic regime, the majority nodes do hold greater degree share at $h=0.8$ but this equalizes again as we near the complete homophily stage where the in-group support keeps both groups at appropriate degree share.

\subsubsection{Top D\% degree rank}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/top_static.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in static networks. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_static_fig}
\end{figure}

Figure \ref{top_static_fig} shows the fraction of minority nodes which are present in the top D\% of node rankings (ranked according to degree). In heterophilic regime, the minority is highly over-represented than its group size, when the minority group size is small. This effect can be seen to reduce as the minority group size increases and the ranking at each percentage is closer to the original group size. At an equal group size, we can see that the degree rankings at each percentage adheres to the original group size.

\subsubsection{Disparate Visibility}
\label{static_dv}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dv_static.png}
	\caption{Disparate Visibility measure for static networks of different minority group sizes. The black line at each plot shows the equal visibility point and the black dotted lines represent the upper and lower limit for the disparate visibility measure.}
	\label{dv_static_fig}
\end{figure}

Figure \ref{dv_static_fig} shows the disparate visibility measure for the various recommendation methods. In the heterophilic regime we can see that minority nodes gain much higher visibility in recommendations for the \textbf{reinforcement methods} and \textbf{PA-Homophily} method. This we can expect as in the heterophilic regime the minorities are highly over-represented since they are likely to connect with the large number of majorities. As the minority fraction increases, this effect is seen to reduce. In the homophilic regime, although we see slight visibility gain for majority nodes at $h=0.8$ this is not that significant. The values lie quite close to equal visibility in recommendations at the homophilic regime.

For the \textbf{Adamic-Adar} and \textbf{Twitter-Rank} methods we observe a close stable measure with almost equal visibility for both groups at all homophily cases.

For Adamic-Adar at $h=0$ we observe that the disparate visibility is always 0. This happens because in the case of complete heterophily, for Adamic-Adar, minority nodes are always suggested minority nodes and majority nodes are always suggested majority nodes. This happens owing to how the Adamic-Adar method works internally. To elaborate our point, we can consider the bipartite graph $G_t$ at $h=0$ as input to our recommender $R$ driven by Adamic-Adar. We know that Adamic-Adar suggests nodes based on common neighbours. Two minority nodes in $G_t$ would only have majority nodes in common at $h=0$ and thus a minority node would only be recommended to minorities. This same logic also holds in the case of a majority node recommendations. Thus, the disparate visibility is always at 0 in the case of complete heterophily for Adamic-Adar.

In the case of Twitter-Rank also we notice balanced visibility for both groups. While for Adamic-Adar we see from the plots that although there is equal visibility at $h=0$, the visibility for minorities is quite high at $h=0.2$. For Twitter-Rank however we can see that the visibility measure is quite near to the equal visibility line at all homophilies.

\subsection{Summarized Findings}

We list down our observations from the experiments on Static Networks below.

\begin{enumerate}
	\item In the heterophilic regime, minority group nodes are at an advantage. In the homophilic regime, majority group nodes do tend to gain some advantage, but due to fierce competition amongst themselves this is very slight.

	\item As the minority group size increases and it gets closer to the equal group-size ($f=0.5$), the minorities start losing advantage over the majority in heterophilic regime. This phenomenon is also true for the majorities in the homophilic regime.

	\item The bias in recommendations received with different recommendation systems are highest in the case of \textbf{reinforcement methods} and \textbf{PA-Homophily} at the heterophilic regime. In the homophilic regime the visibility is close to equal for both groups. \textbf{Twitter-Rank} provides best equality in visibility for both groups in recommendations, also closely followed by \textbf{Adamic-Adar}.

\end{enumerate}

\section{Growing Networks}
\label{growing_networks}
In this section we propagate growth of synthetic networks, in which the growth is influenced by the use of different recommender systems $R$. In the experimental setup sub-section we lay down the detailed algorithm which we use for growing these networks. In the results sub-section we look in detail at the different properties of these recommendation influenced networks.

\subsection{Experimental Setup}
We generate our network by using algorithm \ref{growing_network_model}. We take influence from the \textit{recommendation algorithm model} by Stoica et. al. \cite{stoica2018algorithmic} while developing our growth algorithm. The key difference between the two approaches is the usage of a generic recommendation engine $R$ in our case and the usage of the karimi model \cite{karimi2018homophily} to make edge-formation decisions.

\begin{algorithm}
	\SetAlgoLined
	\DontPrintSemicolon
	\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
	\Input{Total iterations : $t$\\Symmetric homophily value : $h$\\Probability of node being marked as minority node: $f$\\Recommender Engine : $R$\\Organic Growth Probability : $\alpha$\\Randomness : $\mu$\\Number of recommendations : $k$\\}
	\Output{Network : $G_{t}(V_{t},E_{t})$}
	\BlankLine
	With probability $f$ mark node $v_{0}$ as minority, else mark as majority.\;
	$V_{0} = \{v_{0}\}$\;
	\For{$i \leftarrow 1$ \KwTo $t$}{
		With $\alpha$ probability, choose $growth\_type$ as $organic$, else choose $algorithmic$.\;
		\uIf{$growth\_type$ is $organic$}{
			$V_{i} = V_{i-1} \cup \{v_{i}\}$\;
			With probability $f$ mark node $v_{i}$ as minority, else mark as majority.\;
			With probability $\mu$ choose $random\_connect$ as true, else choose false.\;
			\eIf{$random\_connect$ is true}{
				Choose node $u_{i}$ from $V_{i-1}$ at random.\;
			}{
				\ForEach{vertex $u \in V_{i-1}$}{
					$\Pi_{u} = (\delta(u) \times h_{uv_{i}}) / \sum_{w \in V_{i-1}}^{} (\delta(w) \times h_{wv_{i}}) $\;
				}
				Choose $u_{i}$ based on probability $\Pi_{u \in V_{i-1}}$\;
			}
		}
		\ElseIf{$growth\_type$ is $algorithmic$}{
			$V_{i} = V_{i-1}$\;
			Choose $v_{i}$ from $V_{i}$ at random.\;
			Get recommendation list $L_{v_{i}} (|L_{v_{i}}|=k)$ for $v_{i}$ from recommender engine $R$.\;
			\ForEach{vertex $u \in L_{v_{i}}$}{
				$\Pi_{u} = (\delta(u) \times h_{uv_{i}}) / \sum_{w \in L_{v_{i}}}^{} (\delta(w) \times h_{wv_{i}}) $\;
			}
			Choose $u_{i}$ based on probability $\Pi_{u \in L_{v_{i}}}$.\;
		}
		\eIf{$u_{i}$}{
			$E_{i} = E_{i-1} \cup \{(u_{i},v_{i})\}$\;
		}{
			$E_{i} = E_{i-1}$\;
		}
	}
	\caption{Growing network model}\label{growing_network_model}
\end{algorithm}

In table \ref{table_growth_model} we list down the various tuneable and static parameters in our experimental setup for algorithm \ref{growing_network_model}. As the recommendation engine $R$, we use the following methods: PA-Homophily, Adamic-Adar, Twitter-Rank, Ranked-Bandit, Top-Rank (which we have discussed in detail in chapter \ref{recommender_methods}). The additional parameters used by the reinforcement methods are the same as listed in table \ref{table_recommendation}.

\begin{table}[h]
	\centering
	\begin{tabular}{ |c|c| }
		\hline
		\textbf{Parameter} & \textbf{Value} \\
		\hline
		Total iterations ($t$) & 10000 \\
		Randomness ($\mu$) & 0.1 \\
		Organic growth probability ($\alpha$) & 0.1 \\
		Symmetric homophily value ($h$) & [0.0, 0.2, 0.5, 0.8, 1.0] \\
		Probability of minority node ($f$) & [0.1, 0.2, 0.3, 0.4, 0.5] \\
		Number of recommendations ($k$) & 5 \\
		Recommender training iterations ($T$) & 10000 \\
		Ranking factor ($r$) & [0.0, 0.5, 1.0] \\
		\hline
	\end{tabular}
	\caption{Parameters for generation of synthetic networks as outlined by algorithm \ref{growing_network_model}}
	\label{table_growth_model}
\end{table}

What we can observe from our algorithm given the experimental setup is that the synthetic networks which we receive as output would be having more number of edges than the previous synthetic networks used in section \ref{static_networks} owing to \textit{algorithmic growth}. The total number of iterations for the generation of $G_{t}$ is $t=10000$. Given the organic growth probability $\alpha=0.1$, we expect the total number of nodes $|V_{t}| \simeq 1000$. Since the number of edges are quite high, owing to \textit{algorithmic growth}, the average degree of nodes also increases, which can be seen very specifically in some plots (specially that of \textit{Adamic-Adar}). Also another notable difference with the networks generated in section \ref{static_networks} is that $h=0$ may not mean a completely bipartite graph in the case of \textit{growing networks} since there is a randomness factor $(\mu)$ at play here. Also by similar logic, for a value of $h=1$ the synthetic network might not be completely disjoint. How all these factors come together and influence the network growth we will be seeing in the next few sections.

Now that we have detailed the experimental setup, we generate synthetic networks by tuning the different parameters in table \ref{table_growth_model}, having 5 simulations for each variation. In the subsequent sections we list the various results for our experiments and also our analysis for them.

\subsection{Experimental Results : PA-Homophily}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_growth_pa.png}
	\caption{Degree distribution for growing networks with \textbf{PA-Homophily}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_growth_pa_fig}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_growth_pa.png}
	\caption{Degree growth for growing networks with \textbf{PA-Homophily}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree growth for minority and majority node is visualized using red and blue color plot lines respectively.}
	\label{dg_growth_pa_fig}
\end{figure}

\begin{SCfigure}[1][h!]
	\centering
	\includegraphics[trim=0 5 0 10, clip, width=0.75\textwidth]{images/mf_growth_pa.png}
	\caption{The fraction of total degree held by minority nodes for growing networks with \textbf{PA-Homophily}.}
	\label{mf_growth_pa_fig}
\end{SCfigure}

\begin{figure}[h!]
	\centering
	\includegraphics[trim=0 10 0 5, clip, width=1.0\textwidth]{images/top_growth_pa.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in growing networks with \textbf{PA-Homophily}. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_growth_pa_fig}
\end{figure}

In the case of \textit{PA-Homophily}, the recommender method calculates probability of candidate nodes to be chosen for connection with seeking node by considering parameters like degree of the candidate nodes and homophily between the candidate and the seeking node.

In the \textbf{heterophilic} regime, we can see that the minority nodes have a clear edge over majority nodes and this is higher in case of lower minority group sizes. The large number of majority nodes in this regime choose to give attention to the fewer minority nodes which lead to these minorities ultimately holding higher degrees. This can be seen from the degree distribution plot (figure \ref{dd_growth_pa_fig}).

From the degree growth plot (\ref{dg_growth_pa_fig}) we can see that minorities would have higher degree growth than majorities due to this attention from majorities. They also hold very high amount of the total degree in the networks as can be seen in figure \ref{mf_growth_pa_fig}. This clearly shows that the minorities are having a high over-representation in this regime. Ranking the nodes according to their degrees confirms this for us as we can see in figure \ref{top_growth_pa_fig} that the top rank holders are mostly minority nodes. The recommendation lists for minorities and majorities have nodes from the other groups highly and thus driven by the algorithmic growth this gives us a network similar to what we get from the models generated by algorithm \ref{algo_karimi_model} in the case of \textit{static networks}.

In the \textbf{homophilic} regime, from the degree distribution plot (figure \ref{dd_growth_pa_fig}) we see that majorities tend to hold higher degrees than minorities which is expected since the groups tends tend to connect amongst themselves and the majorities are larger in number.

We also see that the minority nodes are recommended a lot more majority nodes in their recommendation lists, specially when the minority size is very low and homophily is $h=0.8$. At $h=0.8$, the majority has higher growth but the difference with growth of minorities is not very high.

At complete homophily only in-group support is present so then the reversed effect of $h=0$ can be seen for lower minority fractions. Looking at the plot in figure \ref{mf_growth_pa_fig} we can see that the minorities share of total degrees is only slightly lower than its group size at $h=0.8$. Ranking these nodes according to degree as done in figure \ref{top_growth_pa_fig} shows that minorities are underrepresented. At $h=1$ we see that the minorities tend to be over-represented at about the $D=40\%$ mark. We saw this effect slightly before in our pre-generated synthetic networks in figure \ref{top_static_fig}. We have confirmed that this effect is not seen for networks with about 5000 nodes and we believe that this is only seen due to the less number of nodes we use in our networks. Also, the randomness factor ($\mu$) adds an extra exaggeration to this effect which might be another factor to cause this kind of over-representation for our plots in the \textit{growing networks} perspective.

\subsection{Experimental Results : Adamic-Adar}

In the case of \textit{Adamic-Adar}, the growth is completely based on the network structure that develops over time. The recommendation system is agnostic of the type of nodes it is dealing with when recommending and is solely dependent on the neighbours of the seeker and candidate nodes used to calculate the Adamic-Adar index.

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_growth_aa.png}
	\caption{Degree distribution for growing networks with \textbf{Adamic-Adar}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column.  Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_growth_aa_fig}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_growth_aa.png}
	\caption{Degree growth for growing networks with \textbf{Adamic-Adar}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree growth for minority and majority node is visualized using red and blue color plot lines respectively.}
	\label{dg_growth_aa_fig}
\end{figure}

\begin{SCfigure}[1][h!]
	\centering
	\includegraphics[trim=0 5 0 10, clip, width=0.75\textwidth]{images/mf_growth_aa.png}
	\caption{The fraction of total degree held by minority nodes for growing networks with \textbf{Adamic-Adar}.}
	\label{mf_growth_aa_fig}
\end{SCfigure}

\begin{figure}[h!]
	\centering
	\includegraphics[trim=0 10 0 5, clip, width=1.0\textwidth]{images/top_growth_aa.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in growing networks with \textbf{Adamic-Adar}. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_growth_aa_fig}
\end{figure}

Let us consider the \textbf{heterophilic} regime, where nodes prefer to get connected with nodes from the opposite group. In the extreme heterophilic case, nodes simply reject connection with nodes from the other group. If we consider homophily value of $h=0$, the nodes from same group which are suggested would be completely rejected for connections thus most of the recommendations would not result in any fruitful connections. However at $h=0.2$, even with weak probability they would connect which helps evolve the structure of the network unbiased. This difference between behavior at the two homophily values causes a stark difference in the degree distribution plot (figure \ref{dd_growth_aa_fig}) for Adamic-Adar.

Also in the degree distribution plot (figure \ref{dd_growth_aa_fig}) we see that while at the extreme heterophily case ($h=0$), minority nodes do hold higher degrees in most cases, but with the change in minority group size there is not much change. It can be seen that the probability of having a lower degree minority node seems to be increasing with the increase in minority group size.

The degree growth plot (figure \ref{dg_growth_aa_fig}) shows that at $h=0$ homophily there is a slightly higher degree growth for the minorities which mostly happens due to the organic growth of the network. As the strict heterophily is relaxed and minorities start connecting with majorities even with weaker probabilities, we can see that both the minorities and majorities have high degree growth with time.

This can be further verified by checking the fraction of total degrees held by minority nodes (figure \ref{mf_growth_aa_fig}) where at $h=0.2$ we see that the minorities hold degree close to its original size. Also with the degree ranking plot (figure \ref{top_growth_aa_fig}) it is seen that the plot lines lie close to the fraction of minorities in the network at $h=0.2$ which signifies a balanced network. At $h=0$ we do see over-representation but that is only due to the strict heterophily measure and with even the slightest lax network would ease up towards the unbiased structure.

In the \textbf{homophilic} regime, at $h=0.8$ there is high recommendation of majorities for both the minority and majority seeker nodes. The lesser the minority size the more majority nodes are recommended by the recommender. This is what aides the majorities to gain very high degrees compared to minorities in the case of $h=0.8$.

At $h=1$, majorities start getting recommended majorities and minorities start getting recommended minorities. This causes the minority nodes to also gain higher degrees like majorities and thus have similar distribution as can be seen in figure \ref{dd_growth_aa_fig}. Looking at the degree growth plot (figure \ref{dg_growth_aa_fig}) we see that both type of nodes have high degree growth with the majorities maybe slightly leading in the case of lower minority fractions. Since the degree of both type of nodes have similar growth we can also see that the fraction of total degrees held by both type of nodes is almost balanced according to the minority fraction of the network. This signifies that the minorities have almost the same representation as their group-size in degree ranking which can be seen in figure \ref{top_growth_aa_fig}.

The observation which we can draw from the Adamic-Adar recommendations is that the type of recommended nodes which the seeker nodes receive is almost similar at the extreme cases of homophily and heterophily. This harms in edge-connection in $h=0$ but helps in $h=1$. For the in-between cases there is always higher number of majority nodes recommended for both type of nodes which causes the visibility of the majorities to be at par with its group size.

\subsection{Experimental Results : Twitter-Rank}

\textbf{Twitter-Rank} bases ranking on the SALSA scoring mechanism. The nodes in hubs and authorities are formed by collecting nodes to form a ``circle of trust'' (hub side) and the nodes connecting  to the hubs (authorities side) which are then ranked according to the SALSA scores and provided as recommendations.

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_growth_tr.png}
	\caption{Degree distribution for growing networks with \textbf{Twitter-Rank}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_growth_tr_fig}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_growth_tr.png}
	\caption{Degree growth for growing networks with \textbf{Twitter-Rank}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree growth for minority and majority node is visualized using red and blue color plot lines respectively.}
	\label{dg_growth_tr_fig}
\end{figure}

\begin{SCfigure}[1][h!]
	\centering
	\includegraphics[trim=0 5 0 10, clip, width=0.75\textwidth]{images/mf_growth_tr.png}
	\caption{The fraction of total degree held by minority nodes for growing networks with \textbf{Twitter-Rank}.}
	\label{mf_growth_tr_fig}
\end{SCfigure}

\begin{figure}[h!]
	\centering
	\includegraphics[trim=0 10 0 5, clip, width=1.0\textwidth]{images/top_growth_tr.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in growing networks with \textbf{Twitter-Rank}. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_growth_tr_fig}
\end{figure}

First we would be discussing about behavior in the \textbf{heterophilic} regime. At $h=0$, majorities are recommended an incredibly high amount of majority nodes and minorities are recommended minority nodes (this increases with the group size). For a smaller minority group size at $h=0$, it can be seen that there is more majorities which are recommended for minorities but with time this also drops. This explains why at $h=0$, the nodes do not attain very high degrees as can be seen in the degree distribution plot (figure \ref{dd_growth_tr_fig}). The degree growth is quite high for minorities because over time they still receive a healthy number of majority node connections. The majorities however do not receive that much of minority connections so their degree growth is weak.

At $h=0.2$, the distribution completely changes. At this phase majorities start getting recommended highly for both type of nodes. This provides the unbiased networks which have the fraction of total degrees close to that of the original group size (as seen in figure \ref{mf_growth_tr_fig}). We can see that although at the extreme case the minorities are overrepresented, as the homophily increases the network reaches a equal visibility relative to the group size.

In the \textbf{homophilic} regime, at $h=0.8$ there is high number of majority node recommendations for both the majority and the minority nodes. Thus even in this case the degree is closer to the group size of the individual nodes. If we look at the fraction of total degrees held by minorities plot (figure \ref{mf_growth_tr_fig}) we see that minorities seem to be holding slightly lesser fraction of degree than its group size. This can be understood by the fact that while there is high number of recommendations of majorities for both groups, the count of majorities recommended for majorities is slightly less in the homophilic regime. At the complete homophily case since most of the network is possibly disconnected, nodes start getting recommendations from its own group as we had also observed previously in the \textit{PA-Homophily} case.

An observation which we can make specifically from the degree growth plot (\ref{dg_growth_tr_fig}) is that in the range of $h=[0.2, 0.8]$ the degree growth is quite dampened. We believe that since in this method the user's `circle of trust' is formed based on the personalized PageRank mechanism the recommendation of nodes have a local effect, as in the recommended nodes belong in the locality of the seeker nodes. Thus, the degree of specific nodes might not grow as fast as seen in other methods. This also signifies that we do not reach very high degrees for nodes in this method as can be seen from the degree distribution plot (figure \ref{dd_growth_tr_fig}) compared to let's say \textit{Adamic-Adar} which causes the network nodes to have really high degrees.

\subsection{Experimental Results : Ranked-Bandit}

In our experiments we modified the value of ranking factor ($r$) with values $\{0.0, 0.5, 1.0\}$ to influence the effect of ranking in the recommendation list $L_{v_{i}}$ for node $v_{i} \in V_{i}$. We however observed that this ranking factor $r$ provides very little difference to our plots. If we check the count of the recommendations which nodes get for different ranking values (check additional plots in appendices), we can see that at higher $r$ values there is a slight difference in the homophilic region for the number of nodes which are recommended for each type. This is however very minimal and we do not believe that it influences the choice of network nodes connection. In this section we display only the plots for the $r$ value of 1.0. The plots for other $r$ values can be found in the additional plots in appendices.

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_growth_rb10.png}
	\caption{Degree distribution for growing networks with \textbf{Ranked-Bandit ($r = 1.0$)}.The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_growth_rb10_fig}
\end{figure}

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_growth_rb10.png}
	\caption{Degree growth for growing networks with \textbf{Ranked-Bandit ($r = 1.0$)}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree growth for minority and majority node is visualized using red and blue color plot lines respectively.}
	\label{dg_growth_rb10_fig}
\end{figure}

\begin{SCfigure}[1][h!]
	\centering
	\includegraphics[trim=0 5 0 10, clip, width=0.75\textwidth]{images/mf_growth_rb10.png}
	\caption{The fraction of total degree held by minority nodes for growing networks with \textbf{Ranked-Bandit ($r = 1.0$)}.}
	\label{mf_growth_rb10_fig}
\end{SCfigure}

\begin{figure}[h!]
	\centering
	\includegraphics[trim=0 10 0 5, clip, width=1.0\textwidth]{images/top_growth_rb10.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in growing networks with \textbf{Ranked-Bandit ($r = 1.0$)}. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_growth_rb10_fig}
\end{figure}

If we observe the degree distribution while using the \textbf{Ranked-Bandit} method in figure \ref{dd_growth_rb10_fig}, we can see that owing to the use of the click model (which in turn uses the Karimi model \cite{karimi2018homophily}) we see the patterns which could be observed in figure \ref{dd_growth_pa_fig} also observed here. In the heterophilic regime, minority nodes hold higher degrees than majority nodes and as we move towards the homophilic regime, this reverses. Also as we move down the figure towards increasing minority group sizes we can see that the degree distribution for both group of nodes can be perceived as similar. This is an indication that the reinforcement learning model could correctly learn the connection tendencies and thus push our system towards formation of a network which is similar to what we might observe with the Karimi model.

The degree growth plot given in figure \ref{dg_growth_rb10_fig} is also quite similar to the degree growth plot in figure \ref{dg_growth_pa_fig}. In the case of $h=0$, we see higher degree growth than expected for majority nodes, which might be possible because of the randomness ($\mu$) factor and also the fact that majorities are chosen more for connections in algorithmic growth.

The fraction of degree held by minorities plot (figure \ref{mf_growth_rb10_fig}) has similarities in all cases with figure \ref{mf_growth_pa_fig} other than at $h=0.2$. Comparing with the plot for \textit{PA-Homophily} we can see that at $h=0.2$ the minority nodes seem to be holding a higher fraction of the total degree than what is observed for \textit{PA-Homophily} for all minority group sizes ($f$). This would signify that minorities are much more over-represented in the heterophilic regime in \textbf{Ranked-Bandit} method.

We see a over-representation of minority nodes in heterophilic regime and under-representation in the homophilic regime when looking at the Top D\% degree ranked nodes in figure \ref{top_growth_rb10_fig}. Also as we observed before in figure \ref{mf_growth_rb10_fig}, we see that at $h=0.2$, minorities are much more over-represented in comparison to what we had observed before in the case of \textit{PA-Homophily}. This means that in the heterophilic regime, the bias for minorities is much higher in this kind of a recommendation system.

\subsection{Experimental Results : Top-Rank}

Similar to \textbf{Ranked-Bandit}, we modified the value of ranking factor ($r$) with values $\{0.0, 0.5, 1.0\}$ to influence the effect of ranking in the recommendation list $L_{v_{i}}$ for node $v_{i} \in V_{i}$. We observe slight differences in the number of minority and majority nodes which form the recommendation list, but our belief is that this does not affect the final structure of the model. Here we show the results for $r=1.0$, and the plots for the other $r$ values can be found in the appendices.

Looking at the degree distribution plots (figure \ref{dd_growth_top10_fig}), we observe similarities with \textbf{Ranked-Bandit}. In the heterophilic regime, we can see that minority nodes clearly hold higher degrees at lower minority-group sizes. As the group-size increases, majorities tend to hold as much of higher degrees as minorities, a phenomenon observed before too. As we move towards the homophilic regime, the majority nodes hold higher degrees than the minority nodes owing to the preference of same-group attachments and higher volume of majority nodes. This effect is neutralized out as we move down towards equal group-sizes.

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dd_growth_top10.png}
	\caption{Degree distribution for growing networks with \textbf{Top-Rank ($r = 1.0$)}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree distribution for the majority and minority nodes are visualized using blue and red plot points respectively.}
	\label{dd_growth_top10_fig}
\end{figure}

Upon looking at the degree growth plot (figure \ref{dg_growth_top10_fig}), we see that in the heterophilic regime the degree growth is much higher for minority nodes than majority nodes. This phenomenon has also been previously observed for synthetic networks which were influenced by the Karimi growth model. As we move towards the homophilic regime, the majority nodes gain higher degrees over minorities. We see a much faster degree growth for minorities than majority nodes in the heterophilic regime, and this can be attributed to the fact that the \textbf{Top-Rank} method is able to recommend minority nodes to majority with a much higher confidence (by having them at a greater precedence level). This confidence at placing nodes at higher precedence slots could also be the reason why at $f=0.1$ and $h=1$ we see that the minority nodes have higher degree growth than majority nodes.

\begin{figure}[h!]
	\centering
	\includegraphics[width=1.0\textwidth]{images/dg_growth_top10.png}
	\caption{Degree growth for growing networks with \textbf{Top-Rank ($r = 1.0$)}. The minority fractions are provided at the right-side of each row and the homophily values are specified at the top of each column. Degree growth for minority and majority node is visualized using red and blue color plot lines respectively.}
	\label{dg_growth_top10_fig}
\end{figure}

Looking at the total degree held by minority nodes (figure \ref{mf_growth_top10_fig}) we also see similar results to what is expected. In the heterophilic regime, minority nodes hold higher degrees than their group-size, while in the homophilic regime they hold lower degrees which eventually is balanced out at complete homophily.

\begin{SCfigure}[1][h!]
	\centering
	\includegraphics[trim=0 5 0 10, clip, width=0.75\textwidth]{images/mf_growth_top10.png}
	\caption{The fraction of total degree held by minority nodes for growing networks with \textbf{Top-Rank ($r = 1.0$)}.}
	\label{mf_growth_top10_fig}
\end{SCfigure}

The fraction of minorities in top D\% graph (figure \ref{top_growth_top10_fig}) show minorities being over-represented in the heterophilic regime, and underrepresented in the homophilic regime. This factor is also reduced as the minority group size increases. This shows the bias which is present in the click model is assimilated by the \textbf{Top-Rank} model as well, which causes the network to form in a similar fashion as the karimi model.

\begin{figure}[h!]
	\centering
	\includegraphics[trim=0 10 0 5, clip, width=1.0\textwidth]{images/top_growth_top10.png}
	\caption{The fraction of minority nodes found in top D\% nodes ranked according to degree in growing networks with \textbf{Top-Rank ($r = 1.0$)}. A black dotted line at each plot shows the actual fraction of minority nodes in the network.}
	\label{top_growth_top10_fig}
\end{figure}

\subsection{Summarized Findings}

We list down our major observations from the experiments on Growing Networks below.

\begin{enumerate}
	\item In \textbf{PA-Homophily} and the \textbf{reinforcement methods} we see similar plot patterns as observed for pre-generated static networks. This is due to the use of a \textit{click model} which is similar to the synthetic network generation method. It also shows that our reinforcement learning methods were able to correctly imbibe the traits of network and can be tuned internally to give better unbiased recommendations.

	\item The Randomness factor ($\mu$) in algorithm \ref{growing_network_model} causes the minorities to have over-representation in the complete homophily regime as there are connections established between nodes from opposite groups even in the case of complete homophily.

	\item \textbf{Adamic-Adar} is able to produce networks which are unbiased. This is possible at all cases of homophily other than in the strict heterophily case. Use of this recommendation method could lead to the evolution of a naturally balanced network with correct visibility for groups. Also it is observed that nodes tend to obtain really high degrees in this scenario.

	\item \textbf{Twitter-Rank} also provides us with correct representation of groups according to their groups sizes. The degree growth for networks using this recommendation mode is very slow owing to the use of personalized PageRank mechanism to form user's `circle of trust'. This makes node recommendations localized in nature.
\end{enumerate}


\section{Parallel Arrows}
\subsection{Introduction to Parallelism}
\begin{frame}[fragile]{Basic Parallelism (1)}
In general, Parallelism can be looked at as:
\begin{lstlisting}[frame=htrbl]
parEvalN :: [a -> b] -> [a] -> [b]
\end{lstlisting}
\begin{center}
\includegraphics[scale=0.5]{images/parEvalN}
\end{center}
\end{frame}
\begin{frame}[fragile]{Basic Parallelism (2)}
\begin{lstlisting}[frame=htrbl]
parEvalN :: [a -> b] -> [a] -> [b]
\end{lstlisting}
Roadmap:
\begin{itemize}
\item Implement using existing Haskells
\begin{itemize}
\item GpH
\item ParMonad
\item Eden
\end{itemize}
\item Generalize to Arrows
\item Profit
\end{itemize}
\end{frame}

\begin{frame}[fragile]{GpH}
\begin{lstlisting}[frame=htrbl]
parEvalN :: (NFData b) => [a -> b] -> [a] -> [b]
parEvalN fs as = zipWith ($) fs as `using` parList rdeepseq
\end{lstlisting}
\begin{center}
	\includegraphics[scale=0.5]{images/parEvalNMulticore}
\end{center}
\end{frame}

\begin{frame}[fragile]{Par Monad}
\begin{lstlisting}[frame=htrbl]
parEvalN :: (NFData b) => [a -> b] -> [a] -> [b]
parEvalN fs as = runPar $
	(sequenceA $ map (spawnP) $ zipWith ($) fs as) >>= mapM get
\end{lstlisting}
\begin{center}
\includegraphics[scale=0.4]{images/parEvalNParMonad1}
\end{center}
\end{frame}

\begin{frame}[fragile]{Par Monad}
\begin{lstlisting}[frame=htrbl]
parEvalN :: (NFData b) => [a -> b] -> [a] -> [b]
parEvalN fs as = runPar $
	(sequence $ map (spawnP) $ zipWith ($) fs as) >>= mapM get
\end{lstlisting}
\begin{center}
	\includegraphics[scale=0.4]{images/parEvalNParMonad2}
\end{center}
\end{frame}

\begin{frame}[fragile]{Par Monad}
\begin{center}
	\includegraphics[scale=0.4]{images/parEvalNParMonad}
\end{center}
\end{frame}

\begin{frame}[fragile]{Eden}
\begin{lstlisting}[frame=htrbl]
parEvalN :: (Trans a, Trans b) => [a -> b] -> [a] -> [b]
parEvalN = spawnF
\end{lstlisting}
\begin{center}
\includegraphics[scale=0.4]{images/parEvalNEden}
\end{center}
\end{frame}


\section{Bootstrapping moments of ensemble statistics}


\documentclass[a4paper,10pt]{article}

\usepackage{amsmath}
\usepackage[english]{babel}
\usepackage[style=ieee,backend=bibtex]{biblatex}
	\bibliography{ref.bib}
\usepackage{booktabs}
\usepackage[font={footnotesize}]{caption}
\usepackage[hidelinks]{hyperref}
\usepackage{fancyhdr}
\usepackage{float}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usepackage{microtype}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{pgfplots}
	\usepgfplotslibrary{colorbrewer}
	\usepgfplotslibrary{units}
	\pgfplotsset{compat=newest}
	\pgfplotsset{select coords between index/.style 2 args={
	    x filter/.code={
	        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
	        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
	    }
	}}
	\pgfplotsset{cycle list/YlOrRd-9}
\usepackage{placeins}
\usepackage{siunitx}
\usepackage{textcomp}
\usepackage{tikz}
	\usetikzlibrary{arrows}
	\usetikzlibrary{shapes.geometric}
	\usetikzlibrary{positioning}
\usepackage{titling}
\usepackage{threeparttable}
\usepackage{xcolor}
\usepackage{xspace}

\newcommand{\DesignSparkPcb}{DesignSpark
	PCB\textsuperscript{\textregistered}\xspace}
\newcommand{\LTSpice}{LTSpice\textsuperscript{\textregistered}\xspace}

\title{Amplifier Circuit Design in \LTSpice and \DesignSparkPcb}
\author{Z0996690}
\date{\today}

% Headers and footers
\pagestyle{fancy}
\fancyhf{}
\lhead{\includegraphics[width=0.1\textwidth]{img/Durham.png}}
\chead{\small\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}

\begin{document}

% Title page.
\begin{titlepage}
    \centering
    \vspace*{\fill}
    \includegraphics[width=0.5\textwidth]{img/Durham.png}\\
    \vspace*{\fill}
    \LARGE\thetitle\\
    \vskip0.2em
    \large Level 3 Electronics and Communication\\
    \vskip0.4em
    \large\theauthor\\
    \vskip0.4em
    \large\thedate\\
    \vspace*{\fill}
\end{titlepage}

\begin{abstract}
	An LM386 audio amplifier circuit was designed to drive an \SI{8}{\ohm}
	speaker, with a maximum gain of \SI{35}{\decibel} and maximum bass boost of
	\SI{6}{\decibel} without clipping a \SI{65}{\decibel}SPL input. First the
	schematic was simulated in \LTSpice to ensure the design met the
	specification. Secondly a layout was formulated using \DesignSparkPcb.
\end{abstract}

\section{Schematic design}

Figure~\ref{fig:blocks} provides an overview of the system the amplifier
circuit was designed for.

\begin{figure}[h]
	\centering
	\begin{footnotesize}
	\begin{tikzpicture}
		\node [draw,circle,thick,minimum size=0.15\textwidth]
			(mic) {\begin{tabular}{c}
				\SI{40}{\decibel}\\
				re \si{\volt\per\pascal}
			\end{tabular}};
		% Symbols
		\node [draw,right=of mic,isosceles triangle,minimum width=0.15\textwidth,
			   minimum height=0.1\textwidth,isosceles triangle apex angle=60,
			   thick] (pre) {\SI{46}{\decibel}};
		\node [draw,right=of pre,isosceles triangle,minimum width=0.15\textwidth,
			   minimum height=0.1\textwidth,isosceles triangle apex angle=60,
			   thick,fill=lightgray] (amp) {$A>20$};
		\node [draw,right=of amp,trapezium,shape border rotate=90,thick,
			   minimum size=0.08\textwidth] (speaker) {};

		% Complete mic and loudspeaker
		\draw [thick] ([yshift=0.06\textwidth]mic.west) --
			([yshift=-0.06\textwidth]mic.west);
		\draw [thick] ([yshift=0.06\textwidth]speaker.west) --
			([yshift=-0.06\textwidth]speaker.west);

	   	% System input range.
	   	\node [anchor=east] at ([yshift=0.05\textwidth]mic.west)
	   		{\SI{65}{\decibel}SPL};
	   	\node [anchor=east] at ([yshift=-0.05\textwidth]mic.west)
	   		{\SI{30}{\decibel}SPL};

   		% Amplifier input range.
	   	\node [anchor=east,text=gray] at ([yshift=0.05\textwidth]amp.west)
	   		{\SI{70.8}{\milli\volt}};
	   	\node [anchor=east,text=gray] at ([yshift=-0.05\textwidth]amp.west)
	   		{\SI{1.3}{\milli\volt}};

   		% Speaker input range.
	   	\node [anchor=east] at ([yshift=-0.05\textwidth]speaker.west)
	   		{\SI{20}{\milli\volt}};

   		% Connections
	   	\draw [thick] (mic) -- (pre);
	   	\draw [thick] (pre) -- (amp);
	   	\draw [thick] (amp) -- (speaker);
	\end{tikzpicture}
	\end{footnotesize}
	\caption{System block diagram, indicating minimum and maximum inputs to the
		system and amplifier circuit $A$.}
	\label{fig:blocks}
\end{figure}

The LM386 datasheet described how to implement much of the circuit in
Figure~\ref{fig:ltspice}~\cite{tilm386}.

Contrary to the datasheet, the schematic included no Boucherot cells as the load
had no inductive component. The DC decoupling capacitors were chosen to be
suitably large to avoid significantly filtering frequencies above
\SI{20}{\hertz}. C2 was originally \SI{2200}{\micro\farad}, but had to be
reduced due to PCB size constraints. The schematic also includes two supply
bypass capacitors to filter noise on the \SI{12}{\volt} line.

\begin{figure}[h]
	\centering
	\includegraphics[width=\textwidth]{img/spice_circuit.pdf}
	\caption{Audio amplifier \LTSpice schematic.}
	\label{fig:ltspice}
\end{figure}

A potentiometer provided volume control without distorting the gain response by
attenuating the amplifier input. This can be seen in Figure~\ref{fig:volume}.

\begin{figure}[h]
	\centering
	\begin{footnotesize}
	\begin{tikzpicture}
	    \begin{axis}[
	        xlabel={$f$~/~\si{\hertz}},
	        ylabel={$|A|$~/~\si{\decibel}},
	        xmode = log,
		    enlargelimits=false,
		    enlarge y limits={abs=1},
		    grid=both,
		    grid style={line width=.1pt, draw=gray!10},
		    major grid style={line width=.2pt,draw=gray!50},
		    minor tick num=4,
		    every axis plot/.append style={thick},
		    legend style={font=\tiny,at={(1.01,0.5)},anchor=west},
		    reverse legend,
	    ]
	   		\pgfplotsset{cycle list shift=2}
	        \addplot+[select coords between index={0000}{0300}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-6}$}
	        \addplot+[select coords between index={0301}{0601}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-5}$}
	        \addplot+[select coords between index={0602}{0902}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-4}$}
	        \addplot+[select coords between index={0903}{1203}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-3}$}
	        \addplot+[select coords between index={1204}{1504}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-2}$}
	        \addplot+[select coords between index={1505}{1805}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$2^{-1}$}
	        \addplot+[select coords between index={1806}{2106}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$1$}
	    \end{axis}
	\end{tikzpicture}
	\end{footnotesize}
	\caption{Bode plot of amplifier gain as volume potentiometer varies
		logarithmically. Set with maximum gain and minimum bass boost.}
	\label{fig:volume}
\end{figure}

In simulation, bypassing pins 1 and 8 with a purely
capacitive load resulted in clipping for the \SI{70.8}{\milli\volt} input.
The \SI{46}{\decibel} maximum gain was limited to \SI{35}{\decibel} by placing
R1 in series with POT2. Varying POT2 varied the gain loop resistance from
\SIrange{470}{10470}{\ohm}, decreasing the gain as indicated in
Figure~\ref{fig:gain}. At \SI{35}{\decibel}, the output was undistorted, as
evidenced by Figure~\ref{fig:clipping}.

\begin{figure}[h]
	\centering
	\begin{footnotesize}
	\begin{tikzpicture}
	    \begin{axis}[
	        xlabel={$f$~/~\si{\hertz}},
	        ylabel={$|A|$~/~\si{\decibel}},
	        xmode = log,
		    enlargelimits=false,
		    enlarge y limits={abs=1},
		    grid=both,
		    grid style={line width=.1pt, draw=gray!10},
		    major grid style={line width=.2pt,draw=gray!50},
		    minor tick num=4,
		    every axis plot/.append style={thick},
		    legend style={font=\tiny,at={(1.01,0.5)},anchor=west},
		    reverse legend,
	    ]
	   		\pgfplotsset{cycle list shift=1}
	        \addplot+[select coords between index={1806}{2106}]
	        	table [x index=0, y index=1]{data/audio_amplifier_vol.txt};
        	\addlegendentry{$0$}
	        \addplot+[select coords between index={0000}{0300}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-6}$}
	        \addplot+[select coords between index={0301}{0601}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-5}$}
	        \addplot+[select coords between index={0602}{0902}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-4}$}
	        \addplot+[select coords between index={0903}{1203}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-3}$}
	        \addplot+[select coords between index={1204}{1504}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-2}$}
	        \addplot+[select coords between index={1505}{1805}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$2^{-1}$}
	        \addplot+[select coords between index={1806}{2106}]
	        	table [x index=0, y index=1]{data/audio_amplifier_gain.txt};
        	\addlegendentry{$1$}
	    \end{axis}
	\end{tikzpicture}
	\end{footnotesize}
	\caption{Bode plot of amplifier gain as gain potentiometer varies
		logarithmically. Set with maximum volume and minimum bass boost.}
	\label{fig:gain}
\end{figure}

\begin{figure}[h]
	\centering
	\begin{footnotesize}
	\begin{tikzpicture}
	    \begin{axis}[
	        xlabel={$t$~/~\si{\milli\second}},
	        ylabel={$V_{out}$~/~\si{\volt}},
			change x base=true, x SI prefix=milli,
		    enlargelimits=false,
		    ymin=-4, ymax=4,
		    grid=both,
		    grid style={line width=.1pt, draw=gray!10},
		    major grid style={line width=.2pt,draw=gray!50},
		    minor tick num=4,
		    every axis plot/.append style={thick},
	    ]
	   		\pgfplotsset{cycle list shift=8}
	        \addplot+[select coords between index={1}{383}]
	        	table [x index=0, y index=1]{data/audio_amplifier_trans.txt};
	    \end{axis}
	\end{tikzpicture}
	\end{footnotesize}
	\caption{Maximum output voltage across speaker in response to the maximum
		input voltage---\SI{65}{\decibel}SPL, \SI{10}{\kilo\hertz}.}
	\label{fig:clipping}
\end{figure}

The datasheet describes how a \SI{10}{\kilo\ohm} resistor is required to boost
bass by \SI{6}{\decibel}. In simulation, increasing the series resistance
reduced this effect. A \SI{100}{\kilo\ohm} potentiometer was sufficient to
effectively open circuit the bass boost branch. Figure~\ref{fig:bass} illustrates
the \SIrange{0}{6}{\decibel} boost at \SI{80}{\hertz}.

\begin{figure}[h]
	\centering
	\begin{footnotesize}
	\begin{tikzpicture}
	    \begin{axis}[
	        xlabel={$f$~/~\si{\hertz}},
	        ylabel={$|A|$~/~\si{\decibel}},
	        xmode = log,
		    enlargelimits=false,
		    enlarge y limits={abs=1},
		    grid=both,
		    grid style={line width=.1pt, draw=gray!10},
		    major grid style={line width=.2pt,draw=gray!50},
		    minor tick num=4,
		    every axis plot/.append style={thick},
		    legend style={font=\tiny,at={(1.01,0.5)},anchor=west},
		    reverse legend,
	    ]
	   		\pgfplotsset{cycle list shift=1}
	        \addplot+[select coords between index={0000}{0300}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass0.txt};
        	\addlegendentry{$0$}
	        \addplot+[select coords between index={0000}{0300}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-6}$}
	        \addplot+[select coords between index={0301}{0601}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-5}$}
	        \addplot+[select coords between index={0602}{0902}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-4}$}
	        \addplot+[select coords between index={0903}{1203}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-3}$}
	        \addplot+[select coords between index={1204}{1504}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-2}$}
	        \addplot+[select coords between index={1505}{1805}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{$2^{-1}$}
	        \addplot+[select coords between index={1806}{2106}]
	        	table [x index=0, y index=1]{data/audio_amplifier_bass.txt};
        	\addlegendentry{1}
	    \end{axis}
	\end{tikzpicture}
	\end{footnotesize}
	\caption{Bode plot of amplifier gain as bass boost potentiometer varies
		logarithmically. Set with maximum volume and gain.}
	\label{fig:bass}
\end{figure}

\FloatBarrier
\section{PCB design}

Table~\ref{tab:bom} lists the components used to implement the schematic. A
large package was used for the input decoupling capacitor to mitigate the
capacitance change in X7R capacitors subjected to DC biases. A C0G capacitor was
used for the bass boost capacitor, as simulations showed the voltage across this
component varied wildly and C0G capacitors have no DC response.

\begin{table}[h]
	\centering
	\caption{Bill of materials.}
	\label{tab:bom}
	\begin{footnotesize}
	\begin{tabular}{@{}cr@{, }r@{, }lccc@{}
	}
	\toprule
	Ref &
	\multicolumn{3}{c}{Description} &
	Package &
	Manufacturer number &
	RS number \\
	\midrule
	C1 & \SI{4.7}{\micro\farad} & \SI{25}{\volt} & X7R cap & 1210 &
		KEMET C1210C475K3RACTU & 691-1256 \\
	C2 & \SI{1000}{\micro\farad} & \SI{25}{\volt} & Al cap & FK--H &
		Panasonic EEVF1KE102Q & 708-3434 \\
	C3 & \SI{10}{\micro\farad} & \SI{10}{\volt} & X7R cap & 0805 &
		KEMET C0805C106K8RACTU & 802-9854 \\
	C4 & \SI{0.033}{\micro\farad} & \SI{25}{\volt} & C0G cap & 0805 &
		KEMET C0805C333J3GACTU & (\emph{Farnell}) \\
	C5,7 & \SI{0.1}{\micro\farad} & \SI{25}{\volt} & X7R cap & 0603 &
		KEMET C0603C104J3RACTU & 801-5237 \\
	C6 & \SI{10}{\micro\farad} & \SI{25}{\volt} & X7R cap & 1206 &
		KEMET C1206C104K3RACTU & 802-9977 \\
	JACK1 & \SI{3.5}{\milli\meter} & \multicolumn{2}{@{}l}{stereo jack} & thru &
		Swithcraft 35RAPC2BV4 & (\emph{Farnell}) \\
	POT1,2 & \SI{10}{\kilo\ohm} & \multicolumn{2}{@{}l}{logarithmic pot} & thru &
		Bourns 91A1A-B28-D15L & 522-5153 \\
	POT3 & \SI{100}{\kilo\ohm} & \multicolumn{2}{@{}l}{logarithmic pot} & thru &
		Bourns 91A1A-B28-D20L & 522-5175 \\
	R1 & \SI{470}{\ohm} & \SI{50}{\volt} & resistor & 0603 &
		Panasonic ERJPA3F4700V & 826-6966 \\
	R2 & \SI{10}{\kilo\ohm} & \SI{50}{\volt} & resistor & 0603 &
		Panasonic ERJPA3F1002V & 826-6704 \\
	U1 & \multicolumn{3}{c}{LM386 audio amplifier} & SOIC &
		TI LM386M-1/NOPB & 536-1366 \\
	\bottomrule
	\end{tabular}
	\end{footnotesize}
\end{table}

Custom PCB symbols were created for the Panasonic FK-series size-H
package~\cite{panasonicfk}; the stereo jack~\cite{switchcraft35rapc} and the
potentiometers~\cite{bourns91_95}.

% \begin{figure}[h]
% 	\centering
% 	\includegraphics[width=\textwidth]{img/audio_amplifier_sch.pdf}
% 	\caption{Audio amplifier \DesignSparkPcb schematic.}
% \end{figure}

To ensure all traces were sufficiently wide, the maximum current through each
branch was simulated. The trace widths are listed in
Table~\ref{tab:width}. \SI{10}{mil} was the nominal width, whilst
\SI{15}{mil} was for the supply, ground and output traces to minimise voltage
drop. These can be seen in Figure~\ref{fig:pcb}.

\begin{table}[h]
	\centering
	\caption{Trace width calculations due to maximum current with 25 V
		     supply, \SI{1}{oz\per ft^2} trace, \SI{10}{\celsius} temperature
		     rise at \SI{25}{\celsius}.}
    \label{tab:width}
    \begin{footnotesize}
	\begin{tabular}{@{}crrc@{}}
	\toprule
	Branch &
	{$f_{I_{\text{max}}}$ / \si{\hertz}} &
	{$I_{max}$ / \si{\milli\ampere}} &
	Width / mil \\
	\midrule
	Vs   &    85 & 470 & 4.2 \\
	Vin  &    33 &   0 & 0.0 \\
	C1   &    24 &   0 & 0.0 \\
	C2   &    85 & 500 & 4.6 \\
	C3   & 20000 & 110 & 0.5 \\
	C4   & 20000 & 170 & 1.0 \\
	C5   & 20000 &   0 & 0.0 \\
	C6   & 20000 &   0 & 0.0 \\
	C7   & 20000 &   0 & 0.0 \\
	U1.1 & 20000 &  65 & 0.3 \\
	\bottomrule
	\end{tabular}
    \end{footnotesize}
\end{table}

\begin{figure}
	\centering
	\includegraphics[width=\textwidth]{img/audio_amplifier_pcb.png}
	\caption{Audio amplifier \DesignSparkPcb layout.}
	\label{fig:pcb}
\end{figure}

The larger bypass capacitor C6 was placed as close to the supply as
possible, whilst bypass capacitors C5 and C7 were placed close to the
LM386. The input, output and LM386 were each given their own ground to limit
interference. The input and output return paths follow the outward path on the
other side of the board and the gain and bass boost loops were also minimised.

The board was optimised for size and number of vias: measuring 2060 by 1280 mil,
with 0 vias.

\FloatBarrier
\printbibliography

\end{document}


\subsection{Strategies for the iterated prisoner's dilemma}

Tit-for-tat

Grim trigger


\section{Results}
\labsec{results}

\subsection{Nested Cross-Validation Package}

All the similar published works use a 5-fold cross-validation to
evaluate their models. However, since there are also some parameters to
tune, they rely on a (computationally expensive) netsed cross-validation
in order not to overestimate the predictive power. Since I needed to run
several different models for each of the 140 genes, each with its own
parameters, I decided to write an R
package\sidenote[][-2.3cm]{\url{https://github.com/fmarotta/cvtools}} to
perform the nested cross-validation with a heuristic algorithm that does
not try all the possible values for the parameters, but rather,
independently for each parameter, it starts at one value and explores
the adjacent ones; then, it moves in the direction where the error
decreases
(\reffig{cv}).

\begin{marginfigure}[-3.4cm]
  \centering
  \includegraphics{cv}
  \caption{First, the mtry is tuned while the nodesize is kept fixed;
the algorithm started at the default value of 256, then it moved up in
the range as long as the error decreased, and finally it came back to
explore the values in between. Next, the nodesize was tuned in a similar
fashion.}
  \labfig{cv}
\end{marginfigure}

This package requires the user to write a function which takes
predefined arguments and returns a predefined output, but except for
that, it can be used with any regression model. I evaluated the
performances of ridge, BART, random forest, and PCR.
\sidecite[-12.9cm]{James2013a,Hastie2009}

\subsection{Model Performance}

The measure of performance is not the MSE nor the $R^2$, but rather the
square of the correlation between true and predicted expression
($\rho^2$); indeed, we do not want to penalise errors on single
individuals, but we are interested in the general trend of expression. I
decided to compare my results with those of TIGAR, \cite{Nagpal2019}
which is currently most recent paper on this topic.

\begin{table}[b]
  \caption{Mean $\rho^2$ across 141 genes. The t-test was always
performed with respect to TIGAR.}
  \labtab{comp}
  \begin{tabular}{lcc}
  \textbf{Model} & \textbf{Mean} \boldmath$\rho^2$ & \textbf{t-test pval} \\
  \midrule
  TIGAR & 0.067 & NA\\
  Ridge & 0.076 & 0.025\\
  BART & 0.074 & 0.121\\
  Ranger & 0.069 & 0.398\\
  PCR & 0.064 & 0.737\\
  \end{tabular}
\end{table}

Even if it captures only the linear relationships, ridge gave the best
predictive performance (\reffig{rho2distr}), probably because in this
context where $p >> n$, it is a good compromise between bias, variance
and overfitting. According to a t-test, the $\rho^2$ achieved by ridge
with the TBA values are even higher than those obtained by TIGAR
(\reftab{comp}).

\begin{marginfigure}[-2cm]
  \includegraphics{rho2distr}
  \caption{Density plot of the $\rho^2$ achieved by T-REx (Ridge) and
TIGAR. The dotted lines denotes the means of the
distributions.}
  \labfig{rho2distr}
\end{marginfigure}

The performance of BART was not so different, despite the method being
completely different. However, an important advantage of BART with
respect to ridge is its ability to provide importance measures, allowing
us to find which transcription factors are important for each gene.
Additionally, BART captures the interactions between transcription
factors.

The other methods, random forest and principal component regression,
were much less powerful.

\subsection{Considering the Expression of the Transcription Factor}

As high as its affinity for the DNA may be, if the transcription factor
is present only in tiny amounts it will not bind many regulatory
regions. For this reason, we tried to enhance our predictors with
information from the expression of the transcription factors.

\marginnote{%
The Hill equation models the rate of expression of a gene:
\begin{equation*}
  \theta = w \frac{L^n}{K^n + L^n} \approx w \left(\frac{L}{K}\right)^n.
\end{equation*}
Here, $L$ is the amount of transcription factor, $K$ is the dissociation
constant (the inverse of the affinity), and $w$ is a constant. It is
reasonable to suppose that if many transcription factors regulate a
gene, the rate will be given by the following product, where we denoted
$A = 1 / K$:
\begin{equation*}
  \theta = w_1 \left( L_1 A_1 \right)^{n_1} \cdots w_p \left( L_p A_p \right)^{n_p}.
\end{equation*}
Now, if we compute the log of the rate, we get a linear combination
which in principle is a deterministic function, but in practice we can
use this linear combination as the right-hand side of a model formula
and let ridge estimate the coefficients:
\begin{align*}
  Y \sim \beta_0 &+ \beta_1 \left(\log L_1 + \log A_1\right) \\
                 &+ \cdots \\
                 &+ \beta_p \left(\log L_p + \log A_p\right).
\end{align*}
}

In the dataset, we have expression values for some 40.000 genes; of
these, about 800 are transcriptional factors. In practice we removed
those rows from the dataset, and summed the TBA and the expression of
corresponding transcription factors. The new predictors gave a
considerable improvement in the performance, and, perhaps surprisingly,
ridge outclassed BART. In the adjacent margin note I describe the
working hypothesis more in detail.

\begin{table}[H]
  \begin{tabular}{lr}
  \textbf{Model} & \textbf{Mean} \boldmath$\rho^2$ \\
  \midrule
  Ridge & 0.393\\
  BART & 0.268\\
  \end{tabular}
\end{table}


\documentclass[10pt]{res} % default is 10 pt
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage{helvet}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{setspace}

\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=blue,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
    }\setlength{\textheight}{12in} % increase text height to fit resume on 1 page
\newsectionwidth{0pt}  % So the text is not indented under section headings
\addtolength{\topmargin}{-0.5in}

\begin{document}

% \name{Julian M. Lehrer\\[12pt]} % the \\[12pt] adds a blank line after name
\moveleft.5\hoffset\centerline{\Large\bf Julian M. Lehrer}
\moveleft\hoffset\vbox{\hrule width\resumewidth height 1pt}

\vspace*{5pt}
\moveleft.5\hoffset\centerline{707-490-9354 $\mid$ julianlehrer.me $\mid$ github.com/jlehrer1 $\mid$ jmlehrer@ucsc.edu}
\vspace*{-20pt}

\begin{resume}
\setlist{topsep=3pt}

\section{EDUCATION}
    \vspace*{3pt}
    \textbf{University of California, Santa Cruz} \vspace{1mm}\\
    Applied Mathematics, M.S. (3.5 GPA) \hfill {\sl Fall 2021 - Spring 2022} \vspace{1mm} \\
    Computational Mathematics, B.A. (Honors, 3.5 GPA) \hfill {\sl Fall 2018 - Spring 2021} \\
    \vspace*{-15pt}
    % \textbf{Honors \& Awards} \vspace{1mm} Dean's Honors List, Next Generation Scholar in Applied Mathematics
    % \vspace{-15pt}
\section{EXPERIENCE}
\vspace*{3pt}
    \textbf{Graduate Student Researcher} $\mid$ \textit{UCSC Genomics Institute \hfill Sep 2021 - Present}
    \begin{itemize}
        \item Developing OpenCV computer vision pipeline for time series organoid microscopy images measuring growth and irregularity. Deployed via Kubernetes to distributed computing nodes connected to AWS S3. Visualizing results with Matplotlib and Plotly
        \item Leveraging distributed deep learning with PyTorch for subtype classification of cortical cells from gene expression. Utilizing UMAP and density-based clustering (HDBSCAN) for visualization, dimensionality reduction and cluster identification
        \item Researching new methods for outlier detection of stochastic time series and pointcloud data
        \item Writing paper on applications of functional depth (outlier detection and homogeneity testing) in biological experiments
    \end{itemize} \vspace*{-10pt}

    \textbf{Student Researcher} $\mid$ \textit{UCSC Genomics Institute \hfill Sep 2020 - Aug 2021}
    \begin{itemize}
        \item Developed first Python library (\href{https://github.com/braingeneers/statdepth}{statdepth}) for statistical depth calculations, outlier detection and homogeneity testing of time series and pointcloud data
        \item Analyzed RNA-seq data with non-parametric functional depth methods, running on a distributed compute cluster (CephFS)
    \end{itemize} \vspace*{-10pt}

    \textbf{Data Science Intern} $\mid$ \textit{Blackthorn Therapeutics \hfill Jul 2020 - Oct 2020}
    \begin{itemize}
        \item Applied stacked models (XgBoost, regularized linear regression and Facebook Prophet) to research which features most heavily impact depression and anxiety rates across the US
        \item Implemented KNN clustering with PCA to analyze which states are most heavily affected by rates of depression, unemployment and other factors caused by economic crisis
    \end{itemize} \vspace*{-10pt}

    \textbf{Data Science Intern} $\mid$ \textit{Startup Genome \hfill Mar 2020 - Jun 2020}
    \begin{itemize}
        % \item Built analytics pipeline for understanding how COVID-19 affects global startup ecosystems
        \item Created deep learning model with Pandas, Tensorflow and NLTK for text processing to classify startup sectors from funding data
        % \item Collaborated on data engineering pipeline to prepare data for clients
        \item Composed data engineering pipeline to generate and visualize funding metrics for clients
    \end{itemize}

\section{PROJECTS: \normalfont{\href{https://github.com/jlehrer1/Projects}{https://github.com/jlehrer1/Projects}}}

\setlist{topsep=3pt}
\vspace*{3pt}
    \textbf{Transparency Project (1st Place CruzHacks 2020)}
    \begin{itemize}
        \item Won CruzHacks 2020 by creating a fully interactive website that brings clarity to the political process through interactive data visualizations. Built with Python (pandas), Plotly and Dash, and hosted live on GCloud
    \end{itemize}\vspace*{-8pt}
    \textbf{SQLtoPandas}
    \begin{itemize}
        \item Developed Python package to use SQL querying on Pandas DataFrames without creating a SQL database, using Pandas, Numpy and SQLite
    \end{itemize}\vspace*{-8pt}

    % \textbf{InstantEDA}
    % \begin{itemize}
    %     \item Python package to instantly generate common exploratory data plots without cleaning your DataFrame
    %     \item Built with Python (pandas, numpy, plotly), published on PyPi
    % \end{itemize}\vspace*{-8pt}
    \textbf{DrivenData: DengueAI}
    \begin{itemize}
        % \item Used a combination of engineered lagged features and fourier models to achieve a top 11.8\% score globally on the DrivenData Dengue fever prediction contest
        \item Utilized Bayesian hyperparemeter tuning with XgBoost, Scikit-learn, and Pandas to achieve a top 11.8\% score globally on the DrivenData Dengue fever prediction contest using a combination of engineered lagged features and stacked models.
        % \item Created data visualizations using Plotly
    \end{itemize}

\section{SKILLS}
\vspace*{3pt}
    \textbf{Programming}: Python (Scikit-learn, Pandas, Numpy, Tensorflow, Keras, Plotly, Matplotlib, Scipy, XgBoost), SQL, Swift, C, Java, Matlab
    \vspace*{4pt}\\
    % \textbf{Theory}: Statistical models, machine learning, deep learning, numerical optimization, numerical methods \\
    \textbf{Software}: Docker, Kubernetes, Slurm, Jupyter, AWS S3, Ceph, Git, Bash, Google Cloud, Anaconda
    % \vspace*{-8pt}

% \section{RELEVANT COURSEWORK}
% \vspace*{3pt}
%     Data Structures, Abstract Data Types, Probability Theory, Bayesian Inference, Numerical Analysis, Machine Learning, Numerical Optimization, High Performance Computing
\end{resume}
\end{document}