\documentclass[mathserif, 8pt, handout]{beamer}
\usetheme{metropolis}
\usepackage{minted}
\usepackage{mathtools}
\usepackage{graphicx}

\setminted{fontsize=\small,baselinestretch=1}

\newfontfamily\mfont[Ligatures=TeX]{Fira Sans UltraLight}
\newcommand{\muted}[1]{{\mfont#1}}
\newcommand{\remark}[1]{{\footnotesize#1}}
\newcommand{\Reals}{\mathbb{R}}
\def\fs{\vskip0ex}

\title[Randomness in Haskell]{Randomness in Haskell}

\author{Tomasz Tylec} % Your name
\institute[IFR/M-ZF]
{
IF Research Polska (Gdańsk) / Mazars-Zettafox (Paris)
\medskip\hspace{1ex}$\vert$\hspace{1ex}
\textit{tomasz.tylec@ifresearch.pl}
}
\date{November 8, 2018} % Date, can be changed to a custom date

\begin{document}

\begin{frame}
\titlepage % Print the title page as the first slide
\end{frame}

\begin{frame}
\frametitle{Overview}
\tableofcontents
\vfill
\begin{flushright}
  \includegraphics[width=2cm]{qrcode.png}
\end{flushright}
\end{frame}

\section{Motivation}

\begin{frame}[fragile]
  \frametitle{What's the problem?}

  \begin{itemize}
  \item we like pure functions -- same input produce same output, no side
    effects:

\begin{minted}{haskell}
sq :: Num a => a -> a
sq x = x*x
\end{minted}
\pause

  \item function returning random value \alert{cannot be pure}\\
    \remark{it is supposed to be random, not deterministic ...}

\begin{minted}{haskell}
data Coin = Head | Tail

-- Flip coin k times
filpCoinK :: Int -> [Coin]  -- can't be random
flipCoinK k = ...
\end{minted}

    \pause

\begin{minted}{haskell}
flipCoinK' :: Int -> IO [Coin]  -- this could
flipCoinK' k = ...
\end{minted}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Yes, but ...}
  \begin{itemize}
  \item ... but random number generators are deterministic!

\begin{minted}{haskell}
Coin = Head | Tail

filpCoinK :: Seed -> Int -> (Seed, [Coin])
\end{minted}

    \pause
  \item this requires to pass random generator state everywhere around ...
    \pause
  \item ... we could hide it \verb|State| monad ...
  \item ... but still, not so elegant ...
  \item and would not work with true random source.
  \end{itemize}

    \pause

  On the other hand, \textbf{probability theory} is basically
  all about randomness.

  \alert{How did mathematicians get rid of ``random outcome of an experiment''?}
\end{frame}

\section{Mathematical background}

\begin{frame}
  \frametitle{Mathematical view -- probability theory}

  \begin{block}{Probability space}
    \fs
    Let:
    \begin{itemize}
    \item $\Omega$ be a set -- \emph{sample space} -- set of outcomes of some
      random experiment.
      \pause
    \item $\mathcal{E}$ a ``nice'' family of subsets of $\Omega$ -- \emph{set of
        events} (outcome may not be ``sharp'').
      \pause
    \item $P\dblcolon \mathcal E \to \Reals$ a \muted{measurable} function -- \emph{probability
        measure} -- such that:
    \end{itemize}
    \begin{enumerate}
    \item $P(a) \ge 0$ for all $a\in \mathcal E$
    \item $P(\Omega) = 1$
    \item $P(a + b) = P(a) + (b)$ whenever $a \cap b = \emptyset$
    \end{enumerate}
  \end{block}
  \centering
  \includegraphics[width=5cm]{prob.jpg}

\end{frame}

\begin{frame}
  \frametitle{Random variables}

  \begin{block}{Random variable}\fs
    A (measurable) function $\Omega \to X$ from sample space to some set $X$.
    It maps ``random experiment outcomes'' to some mathematically tractable objects.\\
    \remark{e.g. real numbers, but can be other things too.}

    $\mathcal R(\Omega)$ -- the set of all random variables on $(\Omega,
    \mathcal{E}, P)$.
  \end{block}

  \pause

  \begin{block}{Expected value}\fs
    If $r \in \mathcal R(\Omega)$ is a random variable, then
    \begin{equation*}
      \mathbb E(r) = \int_\Omega r(x) d\alert<5->{p(x)}
                   \uncover<3->{ = \int_\Omega r(x) \alert<5->{p(x)} dx}
                   \uncover<4->{ = \sum_{x \in \Omega} r_x \alert<5->{p_x}}
    \end{equation*}
    is called \alert{expected value} of random variable $r$.
  \end{block}

  \onslide<5->{Denote by $\mathcal R^*(\Omega)$ set of all \emph{expectations}.}

\end{frame}

\begin{frame}
  \frametitle{Getting rid of sample space}
  \begin{center}
    \includegraphics[width=5cm]{prob2.jpg}
  \end{center}

  $(\mathcal R, \mathcal R^*)$ the pair of (set of random variables, set of
  expectations) is totally sufficient to formulate all probability theory.

  Note that this does not mention $\Omega$!

\end{frame}

\section{Expressing idea in pseudo-Haskell}

\begin{frame}[fragile]
  \frametitle{Random variable type}

\begin{minted}{haskell}
type RVar a = Ω -> a -- random variable of type a
\end{minted}
  \pause
\begin{minted}{haskell}
instance Functor RVar where
    fmap :: (a -> b) -> RVar a -> RVar b
    fmap f rv = f . rv
\end{minted}

\pause
  Let's just substitute for \verb|RVar|:

  \mint{haskell}|fmap :: (a -> b) -> (Ω -> a) -> (Ω -> b)|

  \pause
  We can easily verify functor laws:

\begin{minted}{haskell}
fmap id rx = id . rx = rx
\end{minted}
  \pause
\begin{minted}{haskell}
fmap (g . f) rx = (g . f) . rx
(fmap g . fmap f) rx = fmap g (fmap f rx) = fmap g (f . rx) = g . (f . rx)
\end{minted}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Random variable type}
  It's a monad too, in pseudo-code:

\begin{minted}{haskell}
type RVar a = Ω -> a | (Ω,Ω) -> a | (Ω,Ω,Ω) -> a | ...

instance Monad RVar where
    return :: a -> RVar a
    --        a -> (Ω -> a)
    return x = const x
\end{minted}
  \pause
\begin{minted}{haskell}
    (>>=) :: RVar a -> (a -> RVar b)   -> RVar b
    --     (Ω -> a) -> (a -> (Ω -> b)) -> ((Ω,Ω) -> b)
    rv >>= f = f . rv
    -- because Ω -> Ω -> b is isomorphic to (Ω,Ω) -> b
\end{minted}
\pause
\alert{We got rid of $\Omega$!}

\end{frame}

\section{The \texttt{random-fu} package}

\begin{frame}[fragile]
  \frametitle{Basic blocks}

  \emph{Random variable} is a basic type:
\begin{minted}{haskell}
data RVar a  -- random variable of type a

instance Monad RVar
\end{minted}

  and \emph{probability distribution} is a type-class:
\begin{minted}{haskell}
class Distribution d t where
    rvar :: d t -> RVar t
\end{minted}

  It helps to construct random variables, e.g.:
  \mint{haskell}|stdUniform :: Distribution StdUniform a => RVar a|
  \vskip-1ex
  \remark{standard uniform distribution of type \texttt{a};\\
    e.g. uniform on $[0, 1]$ interval for \texttt{Double},
    random int for \texttt{Int}, random \texttt{Bool}}
  \mint{haskell}|normal :: Distribution Normal a => a -> a -> RVar a|
  \vskip-1ex
  \remark{normal (Gaussian) random variable of type \texttt{a} with given
    mean and standard deviation;\\
    defined only for \texttt{Double} and \texttt{Float}.}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Sampling random variables}

  At some point, we want to get the ``random value''.
  Random variable can be \emph{sampled:}
  \remark{types are simplified for clarity}
\begin{minted}{haskell}
  sample     :: MonadRandom  m   => RVar a -> m a
  sampleFrom :: RandomSource m s => s -> RVar a -> m a
\end{minted}

  \mintinline{haskell}{MonadRandom} is a monad that has default source of
  entropy. Instance is defined for \mintinline{haskell}{IO}.

  \mintinline{haskell}{RandomSource} is a specific source of entropy in monad
  \texttt{m}. Instances exist for standard haskell packages providing
  randomness (\texttt{mersenne-random-pure64, mwc-random, random}).
\end{frame}

\begin{frame}[standout]
  A bit of live coding
\end{frame}

\section{Stochastic process}

\begin{frame}[fragile]
  \frametitle{Random walk}

  \begin{itemize}
  \item we have set of states, represented by some type, e.g.
    \mint{haskell}{type State = (Int, Int)  -- rectangular grid}
    \pause
  \item our ``system'' jumps from one state to the other, randomly:
    \mint{haskell}{jump :: State -> RVar State}
    Next state depend on current state.
    \pause
  \item we iterate process $k$-times starting from some point \mintinline{haskell}|s0|:
\begin{minted}{haskell}
run :: Int -> (State -> RVar State) -> State -> RVar State
run n t s0 = t s0 >>= run (n-1) t
\end{minted}
  \end{itemize}

  Let's put it together.
\end{frame}

\begin{frame}
  \frametitle{Snakes \& Ladders}
  \includegraphics[width=10cm]{snakes_ladders.jpg}
\end{frame}

\section{Applications}

\begin{frame}[fragile]
  \frametitle{Classification rule mining}

  \begin{itemize}
  \item \mintinline{haskell}{Rule}
    is a predicate on a database \mintinline{haskell}{Database} (it selects rows)
    \pause
  \item we have function that transforms \mintinline{haskell}{Database} instance
    to rule selecting that particular instance.
\begin{minted}{haskell}
toRule :: Database -> Int -> Rule
\end{minted}
    \pause
  \item rules can be combined:
\begin{minted}{haskell}
(\/) :: Rule -> Rule -> Rule
\end{minted}
    \pause
  \item and scored:
\begin{minted}{haskell}
score :: Database -> Rule -> Double
\end{minted}
  \end{itemize}

    \pause

  \begin{block}{Algorithm}
    \fs
    We iteratively build result, starting with $r_0 = \bot$ being a null rule.
    Build a pool of rules $\mathrm{pool}$ by transforming each Database instance
    with positive target into rule.

    \begin{enumerate}
    \item take out randomly rule $q$ from the $\mathrm{pool}$
    \item combine it with previous rule $q' = q \vee r_{i-1}$
    \item if the score is smaller than given threshold then $r_i = r_{i-1}$
      otherwise $r_i = q'$
    \end{enumerate}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Classification rule mining -- implementation}

  \begin{block}{Algorithm}
    \fs
    We iteratively build result, starting with $r_0 = \bot$ being a null rule.
    Build a pool of rules $\mathrm{pool}$ by transforming each Database instance
    with positive target into rule.

    \begin{enumerate}
    \item take out randomly rule $q$ from the $\mathrm{pool}$
    \item combine it with previous rule $q' = q \vee r_{i-1}$
    \item if the score is smaller than given threshold then $r_i = r_{i-1}$
      otherwise $r_i = q'$
    \end{enumerate}
  \end{block}

\begin{minted}{haskell}
pickFromSet :: Set a -> RVar (a, Set a)

step :: Database -> Double -> (Rule, Set Rule) -> RVar (Rule, Set Rule)
step db thr (r, pool) = do
  (q, pool') <- pickFormSet pool
  return $ (update thr r q, pool')
\end{minted}
  \pause
\begin{minted}{haskell}
update :: Double -> Rule -> Rule -> Rule
update thr r q | score q' >= thr = q'
               | otherwise       = r
  where
    q' = r \/ q
\end{minted}
  \pause
\begin{minted}{haskell}
mine = Database -> Double -> RVar Rule
mine db thr = runUntil emptyPool (step db thr) (pool, nullRule)
  where
    pool = toSet . fmap toRule . filter positiveTarget $ db
\end{minted}
\end{frame}

\begin{frame}
  \frametitle{Comparision to older implementation (Python + C)}
  \begin{itemize}
  \item much simpler code base
    \begin{itemize}
    \item algorithm in Haskell version is well isolated in about 50 lines of code;
    \item previous implementation mix core algorithm with manipulation and has
      total 1715 lines of code.
    \end{itemize}
  \item much faster execution time (about 100$\times$ of real time speed-up)
  \item much easier development (took around 1 week)
  \end{itemize}
\end{frame}

\begin{frame}[standout]
\end{frame}

\begin{frame}[fragile]
  \frametitle{Benchmark MWC vs random-fu}
Task: compute mean value 10000 random double drawn from uniform distribution on
unit interval

\begin{verbatim}
|           | non-optimized [ms] | optimized [ms] |
|-----------|--------------------|----------------|
| random-fu |              1.800 |          0.890 |
| MWC       |              7.112 |          0.391 |
\end{verbatim}

MWC code is improves very much when optimization is
enabled. This suggest that both libs may on on-par
in less trivial programs.
\end{frame}

\end{document}


\documentclass[11pt]{beamer}
%\hypersetup{hidelinks}
\usetheme{Montpellier}
%\usetheme{Pittsburgh}

\usepackage[utf8]{inputenc}
\usepackage{scala2}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{listings}
% --------------------------------------------------------------------------

% --------------------------------------------------------------------------

\author{Stefan}
%\title{\textless:\textless}
\title{Generalized type constraints}
%\setbeamercovered{transparent}
%\setbeamertemplate{navigation symbols}{}
%\logo{}
%\institute{}
\date{}
\subject{sad-with-a-hat}
\begin{document}

%% Title
\begin{frame}
\titlepage
Based on \href{http://blog.bruchez.name/2015/11/generalized-type-constraints-in-scala.html}{Generalized type constraints in Scala}
\end{frame}

%% ToC
\begin{frame}
	\frametitle{Table of content}
	\tableofcontents
\end{frame}

\section{Introduction}
\subsection{Usage}

\begin{frame}[fragile]
\frametitle{How is it useful?}
\begin{itemize}
\item on Option
\begin{lstlisting}
def flatten[B](implicit ev: A <:< Option[B]): Option[B]
\end{lstlisting} %\pause

\item on Traversable
\begin{lstlisting}
def toMap[K, V](implicit ev: A <:< (K, V)): Map[K, V]
\end{lstlisting} %\pause

\item on Try
\begin{lstlisting}
def flatten[U](implicit ev: T <:< Try[U]): Try[U]
\end{lstlisting}
\end{itemize}
\end{frame}

\begin{frame}{What do they have in common?}
\begin{itemize}
\item implicit parameter list, with a single parameter called ev
\item type of this parameter is of the form \lstinline{Type1 <:< Type2}
\end{itemize}
\end{frame}

\subsection{Meaning}
\begin{frame}[fragile]{Meaning}
\lstinline {Type1 <:< Type2} \newline\newline
\begin{block}{Meaning}
\textit{Make sure that Type1 is a subtype of Type2, or else report an error.}
\end{block}
\end{frame}

\begin{frame}[fragile]
\begin{block}{Example 1}
\begin{lstlisting}
scala> val oo: Option[Option[Int]] = Some(Some(42))
oo: Option[Option[Int]] = Some(Some(42))

scala> oo.flatten
res1: Option[Int] = Some(42)
\end{lstlisting}
\end{block}

\begin{block}{Example 2}
\begin{lstlisting}
scala> val oi: Option[Int] = Some(42)
oi: Option[Int] = Some(42)

scala> oi.flatten
<console>:21: error: Cannot prove that Int <:< Option[B].
       oi.flatten
\end{lstlisting}
\end{block}
\end{frame}

\section{Type bound usage}
\subsection{Can’t we just use type bounds?}
\begin{frame}[fragile] {Plain example}
\begin{lstlisting}
scala> def tuple[T, U](t: T, u: U) = (t, u)
scala> tuple("Lincoln", 42)
res1: (String, Int) = (Lincoln,42)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile] {Example with upper bound}
\begin{lstlisting}
scala> def tupleIfSubtype[T <: U, U](t: T, u: U) = (t, u)
scala> tupleIfSubtype("Lincoln", 42)
\end{lstlisting}
\begin{uncoverenv}<2>
\begin{lstlisting}
res2: (String, Any) = (Lincoln,42)
\end{lstlisting}
\end{uncoverenv}
\end{frame}

\subsection{Constraint system}
\begin{frame}[fragile] {Puzzler}
\begin{block}{Why?}
\textit{type inference solves a constraint system}
\end{block}\pause

\begin{block}{so given}
\begin{lstlisting}
scala> def tupleIfSubtype[T <: U, U](t: T, u: U) = (t, u)
scala> tupleIfSubtype("Lincoln", 42)
res2: (String, Any) = (Lincoln,42)
\end{lstlisting}
\end{block}

\begin{block}{the constraints are satisfied with}
\begin{itemize}
\item String is a String of course
\item Int is a subtype of Any
\item String is also a subtype of Any
\end{itemize}
\end{block}
\end{frame}

\section{How does it work?}
\subsection{Simplified}
\begin{frame}[fragile] {Simplified implementation}
\begin{lstlisting}
trait <::<[-From, +To]

object <::< {
  implicit def conforms[A]: A <::< A = new <::<[A,A]{}
}
\end{lstlisting} \pause

\begin{block}{e.g.}
\begin{lstlisting}
def tupleIfSubtype[T, U](t: T, u: U)(implicit ev: T <::< U) = (t, u)

tupleIfSubtype(new Apple(), new Fruit())(<::<.conforms[Fruit])
tupleIfSubtype(new Apple(), new Fruit())(<::<.conforms[Apple])
\end{lstlisting}
\end{block}
\end{frame}

\subsection{Explained}
\begin{frame}{Why does it work?}
\begin{itemize}
\item Variance of the type parameters of
\lstinline{trait <::<[-From, +To]} is similar to a function: require less, provide more: $F \Rightarrow T$
\item $\rightarrow$ compiler tries to find a type \lstinline{<::<[A, A]} that conforms to e.g. \lstinline{<::<[Apple, Fruit]}
\end{itemize}
\end{frame}

\begin{frame}[fragile] {What constraints must be satisfied?}
\begin{itemize}
\item $F \Rightarrow T$ is required, what are the constraints to pass
\item $A \Rightarrow A$? \pause
\item Due to variance \lstinline{trait <::<[-F, +T]}:
\begin{itemize}
\item A must be a supertype of F: \lstinline{A >: F} or \lstinline{F <: A}
\item A must be a subtype of T: \lstinline{A <: T}
\end{itemize}\pause
\item $\rightarrow$ \lstinline{F <: A <: T}
\item $\rightarrow$ \lstinline{F <: T}
\end{itemize}
\end{frame}

\subsection{Implementation}
\begin{frame}{Real implementation}
\begin{itemize}
\item adds a nice error message: \newline
\lstinline{\@implicitNotFound(msg = "Cannot prove that $\{From\} <:< $\{To\}.")}
\item extends function:
\lstinline{class <:<[-From, +To] extends (From => To)}
\item uses a singleton value: \newline
\lstinline{val singleton_<:< =}
\lstinline{new <:<[Any,Any] \{ def apply(x: Any): Any = x \}}
\item and typecast it:\\
\lstinline{singleton_<:<.asInstanceOf[A <:< A]}
\item evidence also used as conversion $From \Rightarrow To$, e.g. in Option: \newline
\lstinline{def flatten[B](implicit ev: A <:< Option[B]): Option[B] =
if (isEmpty) None else ev(this.get)}
\end{itemize}
\end{frame}

\section{Other}
\begin{frame}[fragile] {Other}
\begin{itemize}
\item alternative: implicit class or type class $\rightarrow$ more overhead
\item exact type \lstinline{=:=[From, To]} vs \lstinline{<:<[-From, +To]} \pause
\item not subtype \textless:!\textless
\begin{lstlisting}
def unexpected : Nothing = sys.error("Unexpected invocation")
@scala.annotation.implicitNotFound("${A} must not be a subtype of ${B}")
trait <:!<[A, B] extends Serializable
implicit def nsub[A, B] : A <:!< B = new <:!<[A, B] {}
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected
\end{lstlisting}
\end{itemize}
\end{frame}

\end{document}


\documentclass[12pt,letterpaper]{article}
\usepackage{fullpage}
\usepackage[top=2cm, bottom=4.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{amsmath,amsthm,amsfonts,amssymb,amscd}
\usepackage{lastpage}
\usepackage{enumerate}
\usepackage{fancyhdr}
\usepackage{mathrsfs}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{hyperref}
\usepackage[section]{minted}
\usepackage{hyperref}
\usepackage{multirow}
\definecolor{mintedbackground}{rgb}{0.95,0.95,0.95}

\hypersetup{%
  colorlinks=true,
  linkcolor=blue,
  linkbordercolor={0 0 1}
}

\renewcommand\lstlistingname{method}
\renewcommand\lstlistlistingname{Algorithms}
\def\lstlistingautorefname{Alg.}

\colorlet{mygreen}{green!60!blue}

\newmintedfile[cppcode]{cpp}{
bgcolor=mintedbackground,
fontfamily=tt,
linenos=true,
numberblanklines=true,
numbersep=5pt,
gobble=0,
frame=leftline,
framerule=0.4pt,
framesep=2mm,
funcnamehighlighting=true,
tabsize=4,
obeytabs=false,
mathescape=false
samepage=true, %with this setting you can force the list to appear on the same page
showspaces=false,
showtabs =false,
texcl=false,
fontsize=\small
}

\setlength{\parindent}{0.0in}
\setlength{\parskip}{0.05in}

% Edit these as appropriate
\newcommand\course{EC6301}
\newcommand\name{Numerical Opimization}
\newcommand\hwnumber{2}                  % <-- homework number
\newcommand\NetIDa{20211046}           % <-- NetID of person #1
\newcommand\NetIDb{Hyeonjang An}           % <-- NetID of person #2 (Comment this line out for problem sets)
\newcommand\github{\url{https://github.com/hyeonjang/numerical-optimization}}

\pagestyle{fancyplain}
\headheight 35pt
\lhead{\github\\\NetIDa\\\NetIDb}                 % <-- Comment this line out for problem sets (make sure you are person #1)
\chead{\textbf{\Large Homework \hwnumber}}
\rhead{\course \\ \name \\ \today}
\lfoot{}
\cfoot{}
\rfoot{\small\thepage}
\headsep 1.5em

\begin{document}

\section*{Problem}

Discuss comparative study in terms of convergence speed between search algorithms for at least four optimization problems you generated accordingly.

\begin{enumerate}
\item Target functions
\begin{itemize}
\item From function1 to function3, they are same as assignment1.
function1 is general quaratic function, function2 is trigonometric function and
function3 is the minus signed version of gaussian function.
function4 and function5 are newly added for testing non-differentiable cases.
\end{itemize}
\begin{center}
    \begin{tabular}{| c | l | c | c | c |} \hline
                  & \multirow{2}{*}{functions}            & \multirow{2}{*}{bound} & \multicolumn{2}{c|}{performance} \\ \cline{4-5}
                  &                                       &                        & fibonacci & golden section\\ \hline
        function1 & $f(x)=x^4 +2x^3-3x^2-10x+7$           & [-7179, 8181]          &  14643ns  & 14453ns   \\
        function2 & $f(x)=\sin(x)+x^2-10$                 & [-3423, 11937]         &  5604ns   & 5120ns    \\
        function3 & $f(x)=-\exp(-\frac{x^2}{\sigma ^2})$  & [-6746, -6721]         &  5569ns   & 5023ns    \\
        function4 & $f(x)=|x-0.3|$                        & [-314, 166]            &  5020ns   & 4075ns    \\
        function5 & $f(x)=|\ln(x)|$                       & [0, 60]                &  5011ns   & 4496ns    \\
        \hline
    \end{tabular}
\end{center}

\item Conditions
\begin{itemize}
\item The bound is determined by the seeking bound algorithm.
The initial random values to search bound are chosen by
\hyperref[func:random]{\emph{ramdom\_int}} function.
\end{itemize}

\item Analysis
\begin{itemize}
\item The maximum iteration is set by 46, because of the limitation for maximum fibonacci sequence value.
The maximum integer value is now 2,147,483,647, but the 47th fibonacci value is 2,971,215,073.
If more complicated Implementation is added, the fibonacci sequence could be larger. But currently didn't.
Therefore, the performance is related to this maximum iteration condtion.
\item Apparently, the convergence speed of \emph{golden section search} is faster than \emph{fibonacci search}.
It would occur, due to the construction time of fibonacci sequence.
\item If we remind the root finding method result of homework 1, the convergence speed of unimodality methods, which uses the function evaluation, is slow compared with the root finding method.
\end{itemize}
\end{enumerate}

\newpage
\section*{Implementation}

\begin{enumerate}
\setlength\itemsep{0.5em}{}
\item Class: Optimizing Method
\begin{itemize}
  \item The bound of method is determined with constructor by \hyperref[func:seek]{\emph{seeking\_bound}} method
\end{itemize}
\cppcode
[
  firstline=9, lastline=44
]
{../../code/method.h}

\item Seeking bound

\begin{itemize}
  \item \label{func:seek} \emph{seeking\_bound}
  \item There exisit other possible implementations for increasing step size.
  But now the fixed $2^x$ incremental function is implementated.
\end{itemize}
\cppcode[ firstline=203, lastline=235 ]{../../code/method.cpp}

\newpage

\begin{itemize}
  \item \label{func:random} \emph{random\_int}
  \item random function to generate initial number for \emph{seeking\_bound} function
\end{itemize}
\cppcode[ firstline=238, lastline=250 ]{../../code/method.cpp}

\item Fibonacci search
\begin{itemize}
  \item Construction of Fibonacci
  \item Due to the maximum integer value is limited by 214748364 in 64bit C++ language,
  the maximum index of fibonacci sequence is currently 46.
  If in other case like unsigned or long integer, it could be changed.
\end{itemize}
\cppcode [ firstline=189, lastline=201 ]{../../code/method.cpp}

\newpage

\begin{itemize}
  \item Fibonacci search
\end{itemize}
\cppcode[ firstline=109, lastline=149 ]{../../code/method.cpp}

\item Golden section search
\begin{itemize}
  \item Golden ratio is given in constant value $1.0/1.618033988749895$
  \item Implementation detail is almost similar to \emph{fibonacci search}
\end{itemize}
\cppcode[ firstline=155, lastline=188]{../../code/method.cpp}

\end{enumerate}
\end{document}


% Created 2015-10-23 Fri 15:33
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage[margin=18mm]{geometry}
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{parskip}
\usepackage{standalone}
\usepackage{tikz,pgf,pgfplots}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks,decorations.markings,datavisualization,datavisualization.formats.functions,intersections,external}
\pgfplotsset{compat=1.9}
\newcommand*{\mexp}[1]{\ensuremath{\mathrm{e}^{#1}}}
\newcommand*{\laplace}[1]{\ensuremath{\mathcal{L} \{#1\}}}
\newcommand*{\laplaceinv}[1]{\ensuremath{\mathcal{L}^{-1} \{#1\}}}
\newcommand*{\realpart}[1]{\ensuremath{\operatorname{Re}(#1)}}
\newcommand*{\impart}[1]{\ensuremath{\operatorname{Im}(#1)}}
\newcommand*{\vsp}[1]{\rule{0pt}{#1}}
\newcommand*{\tderiv}[1]{\ensuremath{\frac{d^{#1}}{dt^{n}}}}
\newcommand*{\bbm}{\begin{bmatrix}}
\newcommand*{\ebm}{\end{bmatrix}}
\newcommand*{\obsmatrix}{\mathcal{O}}
\newcommand*{\contrmatrix}{\mathcal{C}}
\newcommand*{\cwh}{\ensuremath{\cos \omega h}}
\newcommand*{\swh}{\ensuremath{\sin \omega h}}
\newcommand*{\zethree}{\big(z - \mexp{-3h}\big)}
\providecommand{\alert}[1]{\textbf{#1}}

\title{Computerized control - preparation for partial exam 2}
\author{Kjartan Halvorsen}
\date{\today}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs Org-mode version 7.9.3f}}

\begin{document}

\maketitle

\section{Controller design}
\label{sec-1}

  A continous-time system is given by the transfer function
  \[ G(s) = \frac{1}{s}\left(\frac{-s+2}{s+2}\right).\]
  The system is to be controlled using error feedback
  \[ U(s) = F(s) \big(U_c(s) - Y(s) \big). \]

  \textbf{(a)} Determine the controller $F(s)$ so that the closed-loop system has the transfer function
  \[G_c(s) = \left(\frac{-s+2}{s+2}\right)\frac{1}{1+s/3}. \]
  Hint: Write $F(s)$ as a function of $G$ and $G_c$.

  \textbf{(b)} Figure \ref{fig:nonminclosed} shows the Bode diagram of two different systems. Which of the graphs (solid or dashed lines) correspond to the closed-loop system?  OBS: only the phase curves differ. What is the band-width of the closed-loop system?
  \begin{figure}
  \begin{center}
  \includegraphics[width=0.8\linewidth]{bode-nonminphase}
  \end{center}
  \caption{Bode diagram of the closed-loop system. Which is the correct phase-curve?}
  \label{fig:nonminclosed}
  \end{figure}

  \textbf{(c)} Figure \ref{fig:nonminopen} shows the Bode diagram of the open-loop transfer function using the controller from exercise (a). What cross-over frequency and phase-marginal was achieved with the controller?
  \begin{figure}
  \begin{center}
  \includegraphics[width=0.8\linewidth]{bode-nonminopen}
  \end{center}
  \caption{Bode diagram of the open-loop system. What is the cross-over frequency and phase margin?}
  \label{fig:nonminopen}
  \end{figure}

  \textbf{(d)} Obtain a sampled controller using Tustin's approximation.
\section{PID tuning}
\label{sec-2}

  Figure \ref{fig:critical} shows the step-response from an experiment to find the ultimate gain and the ultimate period for the system in exercise 1.
  \begin{figure}
  \begin{center}
  \includegraphics[width=0.5\linewidth]{bode-nonmincritical}
  \end{center}
  \caption{Step-response with ultimate gain. What is the ultimate period?}
  \label{fig:critical}
  \end{figure}

  \textbf{(a)} What is the ultimate period?

  \textbf{(b)} The gain is $K_u=2$. Write down the PID-controller obtained from the Ziegler-Nichols tuning method using the table 8.3 in Å\&W.

  \textbf{(c)} Figure \ref{fig:step} shows the step-responses of the closed-system obtained with the controller in exercise 1 (dashed) and with the controller using PID tuning (solid). How do they differ, and what does this say about the difference in phase margin and cross-over frequency? Why does the response with PID controller have a jump at $t=0$? Why do both responses start in the wrong direction (negative)?
  \begin{figure}
  \begin{center}
  \includegraphics[width=0.5\linewidth]{bode-nonminstep}
  \end{center}
  \caption{Step-responses using the two controllers from exercise 1 and 2. What are the interesting differences?}
  \label{fig:step}
  \end{figure}

  \textbf{(d)} Obtain a sampled controller using backward difference (for all parts of the PID).

\section{Solutions}
\label{sec-3}
\subsection{Controller design}
\label{sec-3-1}

   \textbf{(a)}
The close-loop system is given by
\[ G_c = \frac{GF}{1+GF}. \]
Solving this for $F$ gives
\begin{align*}
 F &= \frac{G_c}{G(1-G_c)} = \frac{ \left( \frac{-s+2}{s+2} \right) \frac{1}{1+s/3} } { \frac{-s+2}{s(s+2)}\left(1 - \left( \frac{-s+2}{s+2} \right) \frac{1}{1+s/3}\right)}\\
   &=  \frac{s}{(1+s/3)\left(\frac{(s+2)(1-s/3) - (-s+2)}{(s+2)(1+s/3)}\right)}\\
   &= \frac{s(s+2)}{2s - s^2/3} = \frac{s+2}{s/3 + 8/3}.
\end{align*}

\textbf{(b)} The argument of the closed loop system is given by
\[ \arg G_c(i\omega) = \arg(-i\omega+2) - \arg(i\omega+2) - \arg(1+i\omega/3) \]
For large frequencies, this is approximately
\[ \arg G_c(i\omega) \approx \arg -i\omega - \arg i\omega - \arg i\omega/3 = -3\pi/2, \]
or -270 degrees. \textbf{It must be the solid line in the bode diagram of figure 1.}

\textbf{(c)}
  \begin{center}
  \includegraphics[width=0.5\linewidth]{bode-nonminopen-margin-crop}
  \end{center}

\textbf{(d)}
  Substitute
  \[ s' = \frac{2}{h}\frac{z-1}{z+1}\]
  in the transfer function for the controller this gives
  \[ F_d(z) = F(s')|_{s'=\frac{2}{h}\frac{z-1}{z+1}} = 3\frac{\frac{2}{h}\frac{z-1}{z+1}+2}{\frac{2}{h}\frac{z-1}{z+1} + 8} =
3\frac{z-1 + h(z+1)}{z-1 + 4h(z+1)}\]
\subsection{PID Tuning}
\label{sec-3-2}

   \textbf{(a)} The ultimate period is actually $T_u=\pi$:
     \begin{center}
     \includegraphics[width=0.5\linewidth]{bode-nonmincritical-solution}
     \end{center}

   \textbf{(b)} With $K_u=2$ and $T_u=\pi$, the Ziegler-Nicholls tuning is
   \[ F(s) = 1.2\left(1 + \frac{2}{\pi s} + \frac{\pi}{8} s \right) \]

   \textbf{(c)} Interesting differences between the two step responses:
\begin{itemize}
\item The tuned PID-controller has a rather high overshoot (more than 50\%). The controller from exercise 1 has no overshoot at all.
\item The tuned PID-controller has a short rise-time, but much longer settling time.
\item The tuned PID-controller gives a jump in the wrong direction at $t=0$. The other controller starts from zero, but starts off in the wrong direction.

     The open-loop transfer function with the tuned PID must have a much smaller phase margin (in fact the phase margin is $24.7\degree$ compared to $62.4\degree$).

     The PID controlled system has an initial jump because there is a direct term from the input to the output. The open-loop system is given by
     \[ G_{o_{ZN}} = \frac{-0.47124 (s-2) (s+1.273)^2}{s^2 (s+2)} = -0.47124 + \frac{ 0.685s^2 + 1.636s + 1.527}{s^2 (s+2)} \]
     So at time $t=0$ the step in the command signal shows directly in $y$ multiplied with a negative factor.

     The controller from exercise 1 gives a closed-loop system that starts in the wrong direction due to the right-hand side zero of $G(s)$. This can be seen by taking the derivative of the step-response and use the inital-value theorem. The derivative will be negative at time $t=0$. The derivative of the step response is simply the impulse response. Since the close loop system is given by
     \[ G_{c_{exc1}} = \left(\frac{-s+2}{s+2}\right)\frac{1}{1+s/3} \]
     the initial-value theorem gives
     \[ \lim_{t \to 0} \left(\frac{d}{dt} y_{step}(t)\right) = \lim_{t \to 0} y_{impulse}(t) = \lim_{s \to \infty} sG_{c_{exc1}}(s) = -\frac{1}{3}. \]

     \textbf{(d)} Backward difference gives the sampled controller
     \begin{equation*}
       \begin{split}
        F_d(z) &= F_{ZN}(s')|_{s' = \frac{z-1}{zh}} =  1.2\left(1 + \frac{2}{\pi s} + \frac{pi}{8} s \right)|_{s' = \frac{z-1}{zh}}\\
               &= 1.2\left( 1 + \frac{2}{\pi \frac{z-1}{zh}} + \frac{\pi}{8} \frac{z-1}{zh}\right)
     \end{split}
     \end{equation*}
\end{itemize}

\end{document}


\documentclass[simplex.tex]{subfiles}
% NO NEED TO INPUT PREAMBLES HERE
% packages are inherited; you can compile this on its own

\onlyinsubfile{
\title{NeuroData SIMPLEX Report: Subfile}
}

\begin{document}
\onlyinsubfile{
\maketitle
\thispagestyle{empty}

The following report documents the progress made by the labs of Randal~Burns and Joshua~T.~Vogelstein at Johns Hopkins University towards goals set by the DARPA SIMPLEX grant.

%%%% Table of Contents
\tableofcontents

%%%% Publications
\bibliographystyle{IEEEtran}
\begin{spacing}{0.5}
\section*{Publications, Presentations, and Talks}
%\vspace{-20pt}
\nocite{*}
{\footnotesize	\bibliography{simplex}}
\end{spacing}
%%%% End Publications
}

\subsection{Randomer Forest (RerF)}
Random Forest (RF) remains one of the most widely used general purpose
classification methods, due to its tendency to perform well in a variety of
settings. One of its main limitations, however, is that it is restricted to
only axis-aligned recursive partitions of the feature space. Consequently, RF
is particularly sensitive to the orientation of the data. Several studies have
proposed “oblique” decision forest methods to address this limitation. However,
the ways in which these methods address this issue compromise many of the nice
properties that RF possesses. In particular, unlike RF, these methods either
don’t deal with incommensurate predictors, aren’t well-adapted to problems in
which the number of irrelevant features are overwhelming, have a time and space
complexity significantly greater than RF, or require additional hyperparameters
to be tuned, rendering training of the classifier more difficult. Additionally,
oblique methods tend to be more sensitive to data corruption than RF is. Our
proposed method, which we call RerF, seeks to address these limitations.

Most recently, we performed simulations in which we artificially rotated,
scaled,
rotated~+~scaled (labeled ``affine'' in figure~\ref{fig:RefF1}), and corrupted two
simulated datasets. The results demonstrate that both RerF and another existing
oblique method called RR-RF are more sensitive to scaling and corruption of the
data than is RF. The variants of these two methods that use rank
transformations to rescale the data first, called RerF(r) and RR-RF(r), become
significantly more robust to scale and corruption. While rank transformation is
not the only scale normalizing method (z-scoring and forcing all values to be
between $[0,1]$ are others), it is the only one that simultaneously helps against
data corruption. Across all simulation settings, RerF(r) is the overall best.

\begin{figure}[h!]
\begin{cframed}
\centering
\includegraphics[height=0.5\textheight]{./figs/RefF1.png}
\includegraphics[height=0.5\textheight]{./figs/RefF2.png}
\caption{
 Randomer Forest Comparisons
}
\label{fig:RefF1}
\end{cframed}
\end{figure}

We are developing an R implementation of the RerF algorithm in order to make it
faster and more easily distributable.  The Rerf algorithm requires a random
rotation of data at each node during the tree creating process, but this
rotation of data at each node increases memory requirements and both training
and testing times.  So, the initial R implementation will instead rotate the
sample data once prior to creating a tree instead of at each node in the tree.
This will still increase the same factors but not to the same degree.  Initial
tests show that this single rotation per tree yields benefits similar to the
per node rotation.

The classification performance of RerF2 using the R implementation is similar
to the performance of the original RerF2 implemented in MATLAB, but the
observed differences are greater than can be explained by the use of different
randomization algorithms.  Both implementations are being analyzed to determine
the source of this observed variability.

\begin{figure}[h!]
\begin{cframed}
\centering
\includegraphics[height=0.5\textheight]{./figs/rerF_benchmark.png}
\caption{
Classification performances of RF, RerF, RR-RF, and XGBoost were
compared on 119 benchmark datasets. RR-RF is identical to RF except that
the data is randomly rotated prior to building each tree. XGBoost is a
computationally efficient implementation of gradient boosted trees and
has been the winner of many recent Kaggle competitions. For each
dataset, for each algorithm, error was subtracted by that of RF and
normalized by the chance probability of error. Therefore, a negative
value indicates that an algorithm had a lower error rate than RF. These
normalized relative errors were then binned and the counts in each bin
were computed. The y-axis represents the bins. Color indicates how many
times the normalized relative error of an algorithm fell into a
particular bin. For instance, the figure shows that RerF had a
normalized relative error 0.05 to 0.10 less than that of RF on
approximately 15 datasets. The ``0 to 0'' bin indicates the number of
times the normalized relative error was exactly 0. Overall the figure
indicates that RerF rarely loses to RF by much and frequently does
substantially better. RR-RF and XGBoost, on the other hand, frequently
perform worse than RF by a large margin.
}
\label{fig:RefF3}
\end{cframed}
\end{figure}

%\begin{figure}[h!]
%\begin{cframed}
%\centering
%\includegraphics[height=0.5\textheight]{./figs/RefF2.png}
%\caption{
% Randomer Forest Comparisons
%}
%\label{fig:RefF2}
%\end{cframed}
%\end{figure}

\end{document}


\subsection{English auctions}

In an English auction bidders make ascending bids on a product, each bid needing to be higher than the last.

These are open auctions. Each bid is visible to all participants.

The price paid is the final bid.


\documentclass[12,a4paper]{article}

\usepackage{graphicx}
\usepackage{float}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\def\code#1{\texttt{#1}}

\lstset{
  language=C++,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\title{High Performance Computing}
\date{\today}
\author{Amarnath Karthi  201501005 \\ Chahak Mehta  201501422}

\setlength{\parindent}{0em}

\makeatletter
\begin{document}
    \begin{titlepage}
	\centering
	{\scshape\LARGE CS-301 \par}
	\vspace{0.1cm}
	{\huge \@title \par}
	\vspace{0.5cm}
	{\Large Assignment 6\par}
	\vspace{10cm}
	\Large Amarnath Karthi          201501005\\
	\Large Chahak Mehta             201501422\\
	\vspace{5cm}
	{\large \@date\par}
    \end{titlepage}

    \section{Hardware and Compiler specifications}
    \renewcommand{\arraystretch}{1.5}
    \begin{table}[H]
        \centering
        \begin{tabular}{ |P{0.4\textwidth}|P{0.6\textwidth}|}
            \hline
            \textbf{Architecture} & x86\_64\\
            \hline
            \textbf{CPU op-mode(s)} & 32-bit, 64-bit\\
            \hline
            \textbf{Byte Order} & Little Endian\\
            \hline
            \textbf{CPU(s)} & 12\\
            \hline
            \textbf{On-line CPU(s) list} & 0-11\\
            \hline
            \textbf{Thread(s) per core} & 1\\
            \hline
            \textbf{Core(s) per socket} & 6\\
            \hline
            \textbf{Socket(s)} & 2\\
            \hline
            \textbf{NUMA node(s)} & 2\\
            \hline
            \textbf{Vendor ID} & GenuineIntel\\
            \hline
            \textbf{CPU family} & 6\\
            \hline
            \textbf{Model} & 63\\
            \hline
            \textbf{Model name} & Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz\\
            \hline
            \textbf{Stepping} & 2\\
            \hline
            \textbf{CPU MHz} & 1211.531\\
            \hline
            \textbf{BogoMIPS} & 4804.10\\
            \hline
            \textbf{Virtualization} & VT-x\\
            \hline
            \textbf{L1d cache} & 32K\\
            \hline
            \textbf{L1i cache} & 32K\\
            \hline
            \textbf{L2 cache} & 256K\\
            \hline
            \textbf{L3 cache} & 15360K\\
            \hline
            \textbf{NUMA node0 CPU(s)} & 0-5\\
            \hline
            \textbf{NUMA node1 CPU(s)} & 6-11\\
            \hline\hline
            \textbf{Language} & C\\
            \hline
            \textbf{Compiler} & mpicc\\
            \hline
        \end{tabular}
    \end{table}
    \newpage
\section{Trapezoidal Integration}
    \textbf{
        Write and analyze serial code and parallel code for the following using OpenMP.
        \begin{enumerate}
            \item Integration of a function using trapezoidal rule. Verify your code by using it to calculate the value of $\pi$.
            \item Calculation of $\pi$ using series.
            \item Summation of two vectors
        \end{enumerate}
    }

    \section{Solution}
    \subsection{Integration using trapezoidal rule}
    \subsubsection{Theory}
    We use the following formula to compute the value of $\pi$ :
    \begin{equation}
        \int_{0}^{1} \frac{4}{1+x^2} dx
    \end{equation}

    \subsubsection{Approach}
    For the serial code, we use simple numerical integration over 0 to 1 using $n$ segments for trapezoidal integration. For parallelizing we just have to divide these $n$ segments between $p$ processors and run the serial algorithm on each processor to get a local integration. After this, each processor sends the local integration answer to the master processor which adds each answer to get the global answer.

    This is achieved using MPI.

    \subsection{Sending and Receiving schemes}
    We can use 2 send receive models in MPI -
    \begin{enumerate}
        \item \textbf{MPI\_Send() , MPI\_Recv()}: Using this model, the master sends the data $n$ to each slave processor iteratively and then gets the data from each in the same manner. This is not efficient if $p$ i.e. the number of processors is large.
        \item \textbf{MPI\_Bcast(), MPI\_Reduce()}: Using this model, the master sends the data to some processors. As soon as they get the data they forward this to other processors also. This way each processor is helping in sending data, thereby reducing the communication time and increasing the bandwidth utilization. This is good if we have large number of processors.
    \end{enumerate}
    \subsubsection{Results and Observations}

    \begin{figure}[H]
        \centering
        \includegraphics[width=0.8\textwidth]{plots/speedup_size.png}
        \caption{speedup vs problem size}
        \label{fig:q1p1}
    \end{figure}

    The above plot shows speedup as a function of number of the problem size. Notice that as we increase the number of processors, the \textbf{MPI\_Bcast()} model performs better due to the reasons mentioned in the previous section.

    Also there is a slight superlinear speedup so the efficiency becomes slightly greater than 1.

    \begin{figure}[H]
        \centering
        \includegraphics[width=0.8\textwidth]{plots/efficiency_size.png}
        \caption{speedup vs problem size}
        \label{fig:q2}
    \end{figure}
    \section{Matrix Multiplication}
    Perform matrix multiplication using MPI
    \subsection{Approach}
    For the following operation :
    \begin{equation}\nonumber
        AB=C
    \end{equation}
    The serial approach to evaluate C is naive matrix multiplication which runs in $O(n^3)$ time complexity. To parallelize the code, we broadcast $A$ to all processors and then decompose $B$ and distrbute it amongst $p$ processors.
    \subsection{Results and observations}
    \begin{figure}[H]
        \centering
        \includegraphics[width=\textwidth]{plots/mean_executionTime.png}
        \caption{Problem size vs time taken}
        \label{fig:my_label}
    \end{figure}
    The time curve is shown above for 1, 2, 4, 8 and 12 threads. Notice that 8 thread model is faster than the 12 thread model. This isbecause the inter processor communication cost is higher in 12 threads and is not compensated by the computational ease provided by the same. This can also be seen from the speedup curves shown below.
    \begin{figure}[H]
        \centering
        \includegraphics[width=\textwidth]{plots/mean_speedup.png}
        \caption{Problem size vs speedup}
        \label{fig:my_label}
    \end{figure}
\end{document}


\section{Homework}

\subsection{Biography: Leonhard Euler}

\begin{center}
 15 April 1707 - 18 September 1783
\end{center}

Born in Basel, Switzerland, he began his education at a young age.
His life would be greatly influenced by Johann Bernoulli,
a friend of his father, and one of the most distinguished
mathematicions of the time.
This influence would greatly determine his life, as it was Johann
who tutored him and convinced him to dedicate his life to the study
of mathematics.\\

His contributions to humanity are great in number,
and span across multiple fields of knowledge.
Some of his many achievements are listed below.

\begin{itemize}
 \item He laid the groundwork for graph theory by solving the problem of
Königsberg's bridges.
\item The Euler equation $\mathrm{e}^{ix} = \cos{x} + i \sin{x}$,
a contribution so important to mathematics it was called
`the most remarkable formula in mathematics` by Richard Feynman.
\item Invented calculus of variations, and its most important
result, the \emph{Euler-Lagrange equation}.
\end{itemize}

\subsection{Biography: Joseph-Louis Lagrange}
\begin{center}
 25 January 1736 - 10 April 1813
\end{center}

Born in Turin, his life had been planned out by his father.
He studied to become a lawyer at the University of Turin.
He did not show interest in mathematics until the age of seventeen,
an event of utmost importance. He delved into self-study and a year
later he had become an accomplished mathematician.
He contributed significantly to mathematics and physics, having published
multiple works in France and Germany.

Through his correspondence with Leonhard Euler, they developed
calculus of variations together. He eventually replaced Euler in
Frederick of Prussia's court as mathematician. During that time he
published his greatest work \emph{Méchanique analytique}, a work so great
it changed how physical problems were solved by moving from
Newton's geometrical methods to methods of mathematical analysis.

\subsection{Definitions}
% https://divisbyzero.com/2008/09/22/what-is-the-difference-between-a-theorem-a-lemma-and-a-corollary/
% https://divisbyzero.files.wordpress.com/2008/09/thcorlem.pdf
\begin{itemize}
 \item Theorem. A mathematical statement that is proved using
 rigorous mathematical reasoning.
  In a mathematical paper, the term theorem is often reserved for
  the most important results.
 \item Axiom/Postulate. A  statement  that  is  assumed  to  be  true  without  proof.
%  \item Lemma.
 \item Definition. A precise and unambiguous description of the meaning
 of a mathematical term.
 It characterizes the meaning of a word by giving all the properties
 and only those properties that must be true.
%  \item Postulate.
 \item Theory.  A set of statements or principles devised to
 explain a group of facts or phenomena,
 especially one that has been repeatedly tested or is
 widely accepted and can be used to make predictions
 about natural phenomena.
%  https://www.nap.edu/read/11876/chapter/2#11
\end{itemize}


\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{a4}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{float}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage{cite}
\usepackage{textgreek}
\usepackage{amsfonts}
\usepackage{amsmath}

\usepackage[margin=1in]{geometry}

\title{
  {\Huge \bf Power Systems Lab}\\
  \vspace{0.25in}

  {\bf Experiment 10}\\
  Laboratory Report
  \vspace{1in}
}
\author{
  \bf Syed Alisamar Husain, 17BEE012\\
  B.Tech Electrical Engg, 8th Semester
}

\begin{document}
  \begin{titlepage}
    \maketitle
    \vspace*{\fill}
    \begin{center}
      {\bfseries Department of Electrical Engineering} \\
      Jamia Millia Islamia, New Delhi
    \end{center}
    \thispagestyle{empty}
  \end{titlepage}

  \newpage
  \begin{center}
    \huge Experiment 10
    \vspace{0.5in}
  \end{center}

  \section{Objective}
  Develop a Simulink model for a series compensated
  transmission line.

  \section{Theoretical Background}
  Series compensation is the method of improving the system
  voltage by connecting a capacitor in series with the transmission line.
  In other words, in series compensation, reactive power is inserted in
  series with the transmission line for improving the impedance of the system.

  It improves the power transfer capability of the line. It is mostly
  used in extra and ultra high voltage line.

    \subsection{Advantages of Series Compensation}
    Series compensation has several advantages like it increases transmission
    capacity, improve system stability, control voltage regulation and ensure
    proper load division among parallel feeders.

    \begin{itemize}
      \item {\bf Increase in Power Transfer Capability}\\ The power transfer over a line is given by
      \begin{center}
        $ P_1 = \dfrac{V_S V_R}{X_L} sin \delta $
      \end{center}
      If a capacitor having capacitance reactance $X_C$ is connected in series with the line,
      the reactance of the line is reduced from $X_L$ to ($X_L - X_C$). The power transfer is given by
      \begin{center}
        $ P_2 = \dfrac{V_S V_R}{X_L - X_C} sin \delta $
      \end{center}

      \item {\bf Improvement in System Stability}\\ For same power transfer and for the same value
      of sending and receiving end voltage, the phase angle δ in the case of the series impedance
      line is less that for the uncompensated line. The reduced value of δ gives higher stability.

      \item {\bf Load Division among Parallel Line}\\ Series capacitors are used in transmission
      systems for improving the load division between parallel lines. When the new line with large
      power transfer capability is paralleled with an already existing line, then it is difficult
      to load the new line without overloading  the old line. In such case the series compensation
      reduces the series reactance and proper load division among parallel circuit can be done
      easily. Load division increases the power transfer capability of the system and reduced losses.

      \item {\bf Control of Voltage}\\ In series capacitor, there is an automatic change in Var
      (reactive power) with the change in load current. Thus the drops in voltage levels due to sudden
      load variations are corrected instantly.
    \end{itemize}

    \subsection{Location of Series Compensator}
    The location of the series capacitor depends on the economic and technical consideration of the
    line. {\bf The series capacitor may be located at the sending end, receiving end, or at the center of
    the line.} Sometimes they are located at two or more points along the line.

    The degree of compensation and the characteristic of the line decide the location of the
    capacitors. Their installation at the terminal provides the facility of maintenance, but
    the overvoltage appearing across the terminals of the capacitors under fault conditions will
    over stress the capacitor.

    The capacitors are installed in the intermediate switching station of comparatively long lines.
    The location at the center of the line also reduced the rating of the capacitor.
    Capacitor banks consist of small units connected in series, parallel, or both to get
    the desired voltage and Var rating.

    \subsection{Problems associated with Series Compensation}
    Some of the problems associated with the series-capacitor application are

    \begin{itemize}
      \item The series compensated line produces series resonance at frequencies lower than power
      frequencies. This is known as sub-synchronous resonance. The sub-synchronous produces
      mechanical stress due to which high torsional stress occurs in the rotor shaft. The problem
      of sub-synchronous resonance mostly occurs during faults or switching operation.

      \item Series capacitors produced high recovery voltages across the breakers contact.
      \item If the degree of compensation and location of capacitors are not proper, the distance
      relays used for line protection may not function properly.
      \item Switching in of an unloaded transformer at the end of a series compensation of the line
      may produce non-linear resonance or ferro resonance. This may result in uninterrupted oscillations.
      The frequency of the oscillation may be suppressed by using shunt reactors across the capacitors
      or short circuiting the capacitors temporary.
      \item Lightly load synchronous motors have got a tendency to experience hunting oscillations.
    \end{itemize}

  \pagebreak
  \section{Implementation}
  A three-phase, 60 Hz, 735 kV power system transmitting power from a power plant consisting of
  four 250 MVA generators to an equivalent network through a 300 km transmission line.

  The transmission line is series compensated by capacitors representing 40\% of the line reactance.
  The compensation equipment is located at the receiving substation where a 735/230 kV transformer
  feeds a 650 MW industrial load center and feeds a 230 kV distribution line.
  The generators are simulated with a Simplified Synchronous Machine block. Universal
  transformer blocks (two-windings) are used to model the transformers.

  \begin{figure}[H]
    \centering
    \includegraphics[width=6in]{img/model.png}
    \caption{Series Compensated Transmission Line Model}
    \label{model}
  \end{figure}

  \pagebreak
  \section{Observations}
  \begin{figure}[H]
    \centering
    \includegraphics[width=6in]{img/compennsated.png}
    \caption{Compensation of Reactive Power}
    \label{compennsated}
  \end{figure}

  From the graph of active and reactive power we can see that the reactive power demand on the line
  was compensated by the series capacitor in the compensating substation. This reduces the load on the
  line and thus more active power can be transmitted without needing to change the installed equipment.

  \section{Result}
  We developed a Simulink model for a series compensated transmission line, and obeserved the drop
  in reactive power demand on the line by the series capacitor in the compensating substation.

\end{document}


\documentclass[10pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb, graphicx, multicol, array, parskip}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{url}
% use straight quotes in texttt environment, make 0 different from O
\usepackage[zerostyle=b,straightquotes,scaled=.93]{newtxtt}

\usepackage{listings}
\lstset{
 basicstyle=\ttfamily,
 columns=fullflexible,
 upquote,
 keepspaces,
 literate={*}{{\char42}}1
 {-}{{\char45}}1
}
\usepackage[short labels]{enumitem}

\usepackage{soul}  % for strike through (\st{})
\usepackage[dvipsnames,usenames,table]{xcolor} % for colors
% \usepackage[htt]{hyphenat} % to break lines in texttt

\usepackage[framemethod=TikZ]{mdframed}
\usepackage{mdframed}
\global\mdfdefinestyle{simple}{linewidth=1pt,skipabove=.5em}
\newenvironment{AnswerBox}{\begin{mdframed}[style=simple]}{\end{mdframed}}
\newcommand\defeq{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny\sffamily def}}}{=}}}
\usepackage[colorlinks]{hyperref} % \href{http://link.com}{link text}
\hypersetup{linkcolor=NavyBlue,citecolor=NavyBlue,filecolor=NavyBlue,urlcolor=NavyBlue}
\usepackage{dsfont}
\newcommand{\required}[1]{{\color{blue}{#1}}}
\newcommand{\email}[1]{\href{mailto:#1}{\texttt{#1}}}
\newcommand{\PSnum}{5}

\author{
  \textbf{Name}: Solim LeGris     %Put your name here.
, \textbf{McGill ID}: 260807111   %Put your McGill ID here.
}

\begin{document}

\title{LING/COMP 445, LING 645\\Problem Set \PSnum}
\date{Due before 10:05 AM on Tuesday, November 23, 2021}
\maketitle
Please enter your name and McGill ID above.

This problem set consists only of questions involving mathematics or
English or or a combination of the two (no coding questions this time).
Please put your answers in an answer box like in the example below.

Once you have entered your answers, please compile your copy of this
\LaTeX{} into a PDF and submit
\begin{enumerate}[(i),noitemsep]
\item
the compiled PDF renamed to
\texttt{ps\PSnum-lastname-firstname.pdf} and
\item
the raw \LaTeX{} file renamed to
\texttt{ps\PSnum-lastname-firstname.tex}
\end{enumerate}
to the Problem Set \PSnum{} folder under `Assignments' on MyCourses.

\hrulefill %--------------------------------

\paragraph{Example Problem:}
This is an example question using some fake math like this
$L=\sum_0^{\infty} \mathcal{G} \delta_x$.

\paragraph{Example Answer:} Put your answer in the box provided, like this:
\begin{AnswerBox}
Example answer is $L=\sum_0^{\infty} \mathcal{G} \delta_x$.
\end{AnswerBox}

\hrulefill%--------------------------------

\pagebreak%

\hrulefill %--------------------------------

\paragraph{Problem 1:}

In class we gave the following equation for the bigram probability of
a sequence of words $W^{(1)},\dots,W^{(k)}$:

\begin{equation}\label{eq:bigram}
\Pr(W^{(1)},\dots,W^{(k)})
\defeq\prod_i^k \Pr(W^{(i)} | W^{(i-1)}=w^{(i-1)})
\end{equation}

 Using this formula, give an expression for the bigram
probability of the sentence $abab$, where each character is treated as
a word. Try to simplify the formula as much as possible.

\paragraph{Important note:} Throughout this problem set, the vocabulary will be
$V\defeq\{a,b\}$. We will assume the length of the sentence is fixed at some $k$,
and \emph{we will not use the stop symbol}.
That is, in a sentence of length $k$, for $1 \le i \le k$, the possible
values for the random variable $W^{(i)}$ are just $a$ and $b$, and we
will refer to the beginning of the string as  $W^{(0)} = \rtimes$ always.
So, $\Pr(W^{(1)}=a \mid W^{(0)}=\rtimes)$ is
the probability that the string starts with $a$.

\paragraph{Answer 1:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
\begin{eqnarray*}\label{bigram:abab}
    \Pr(W^{(1)}=a, W^{(3)}=b, W^{(3)}=a, W^{(4)}=b) & = & \Pr(W^{(1)}=a \mid W^{(0)}=\rtimes) \\
    & & \times \Pr(W^{(2)}=b \mid W^{(1)}=a) \\
    & & \times \Pr(W^{(3)}=a \mid W^{(2)}=b) \\
    & & \times \Pr(W^{(4)}=b \mid W^{(3)}=a) \\
    & = & \Pr(W^{(1)}=a) \\
    & & \times  \Pr(W^{(2)}=b \mid W^{(1)}=a) \\
    & & \times \Pr(W^{(3)}=a \mid W^{(2)}=b) \\
    & & \times \Pr(W^{(4)}=b \mid W^{(3)}=a)
\end{eqnarray*}
\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 2:}

There are two possible symbols/words in our language, $a$ and $b$. There are
three conditional distributions in the bigram model for this language,
$\Pr(W^{(i)} | W^{(i-1)}=a)$, $\Pr(W^{(i)} | W^{(i-1)}=b)$, and
$\Pr(W^{(i)} | W^{(i-1)}=\rtimes)$.
These conditional distributions are associated with the
parameter vectors $\vec{\theta}_{a}$, $\vec{\theta}_{b}$, and
$\vec{\theta}_{\rtimes}$, respectively (these parameter vectors were implicit in the
previous problem). For the current problem, we will assume that these
parameters are fixed. Use a second subscript notation to denote components of
these vectors, so $\theta_{ab}= \Pr(W^{(i)}=b\mid W^{(i-1)}=a)$.

 Suppose that we are given a sentence $W^{(1)},\dots,W^{(k)}$. We will
use the notation $n_{x \rightarrow y}$ to denote the number of times
that the symbol $y$ occurs immediately following the symbol $x$ in the
sentence. For example, $n_{a \rightarrow a}$ counts the number of
times that symbol $a$ occurs immediately following the symbol $a$.
Using Equation \ref{eq:bigram}, give an expression for the probability
of a length $k$ sentence in our language:
\begin{equation*}
\Pr(W^{(1)},\dots,W^{(k)} | \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes})
\end{equation*}

 The expression should make use of the $n_{x \rightarrow y}$ notation
defined above.

 Hint: the expression should be analogous to the formula that we found
for the likelihood of a corpus under a bag of words model.

\paragraph{Answer 2:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
With $V=\{a,b\}$,
\begin{equation}\label{eq:prob_se}
    \Pr(W^{(1)},\dots,W^{(k)} | \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes}) = \prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}} \theta_{bw}^{n_{b \rightarrow w}} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}}
\end{equation}
\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 3:}

Assume the parameter vectors in our bigram model have the following values:
\begin{align*}
&\vec{\theta}_{a} = (0.7,0.3)\\
&\vec{\theta}_{b} = (0.2,0.8)\\
&\vec{\theta}_{\rtimes} = (0.5,0.5)
\end{align*}

 The first vector indicates that if the current symbol $a$,
there is probability $0.7$ of transitioning to the symbol $a$, and
probability $0.3$ of transitioning to the symbol $b$. Using your
answer to the previous problem and these parameter values, calculate
the probability of the string $aabab$.

\paragraph{Answer 3:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%

\begin{align*}
    \Pr(W^{(1)} = a,W^{(2)} = a, W^{(3)} = b, W^{(4)} = a, W^{(5)} = b | \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes}) & = 0.5\times 0.7\times 0.3^2 \times 0.2 \\
    & = \frac{63}{10000}
\end{align*}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 4:}

In Problem 2, you found an expression for the bigram probability of a
sentence in our language, which contains the symbols $a$ and $b$. In
that problem, we assumed that there were fixed parameter vectors
$\vec{\theta}$ associated with each conditional distribution. In this
problem, we will consider the setting in which we have uncertainty
about the value of these parameters.

 As we did in class, we will use the Dirichlet distribution to
define a prior over parameters.  Assume each parameter vector is drawn
independently given $\vec{\alpha}$:
\begin{align}
\vec{\theta}_{\mathbf{c}} \mid \vec{\alpha}
    &\sim\mathrm{Dirichlet}(\vec{\alpha}) \\
w^{(i)} \mid  w^{(i-1)}
    &\sim\mathrm{categorical}(\vec{\theta}_{w^{(i-1)}}) \\
w^{(1)}
    &\sim \mathrm{categorical}(\vec{\theta}_{\rtimes})\
\end{align}

 Suppose that we have a fixed-length sentence
$S=W^{(1)},\dots,W^{(k)}$. Give an expression for the joint
probability
$\Pr(S, \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes} |
\vec{\alpha})$
using the definitions of Dirichlet distributions and likelihoods we
defined in class.

\paragraph{Answer 4:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
The joint probability can be expressed as follows, \\
\begin{align*}
    \Pr(S, \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes} | \vec{\alpha}) & = \Pr(S|\vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes})\Pr(\vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes}|\vec{\alpha})
\end{align*}
We know $\Pr(S|\vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes})$ from Equation \ref{eq:prob_se} derived in Problem 2. Now, knowing that each parameter vector is drawn independently given $\vec{\alpha}$, we can express the prior using the following:

\begin{align*}
    \Pr(\vec{\theta}_{a}|\vec{\alpha}) & =  \frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\prod_{w\in V}\theta_{aw}^{\alpha_{w} -1}\\
    \Pr(\vec{\theta}_{b}|\vec{\alpha}) & =  \frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\prod_{w\in V}\theta_{bw}^{\alpha_{w} -1}\\
    \Pr(\vec{\theta}_{\rtimes}|\vec{\alpha}) & =  \frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\prod_{w\in V}\theta_{\rtimes w}^{\alpha_{w} -1}
\end{align*}
Using these equations, we can now define our joint probability equation,

\begin{align}\label{eq:joint}
    \Pr(S, \vec{\theta}_{a}, \vec{\theta}_{b}, \vec{\theta}_{\rtimes} | \vec{\alpha}) & =
     \left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3 \prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}+ \alpha_{w} -1} \theta_{bw}^{n_{b \rightarrow w}+\alpha_{w} -1} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}+\alpha_{w} -1}
\end{align}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 5:}

In the previous problem, you found a formula for the joint probability
of a sentence and a set of bigram model parameters. Using this, give a
formula for the marginal probability of the sentence
$\Pr(S|\vec{\alpha})$.

 Hint: The formula should be analogous to the formula derived
in class for marginal probability of a corpus under a bag of words
model. Whereas before there was only a single parameter vector
$\vec{\theta}$, now there are three parameter vectors that need to be
marginalized away. Otherwise the calculation will be similar.

\paragraph{Answer 5:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%

\begin{align*}
    \Pr(S|\vec{\alpha}) & =  \left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3
    \int_{\Theta_{a}} \int_{\Theta_{b}}
    \int_{\Theta_{\rtimes}}
    \prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}+ \alpha_{w} -1} \theta_{bw}^{n_{b \rightarrow w}+\alpha_{w} -1} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}+\alpha_{w} -1}d\theta_a d\theta_b d\theta_{\rtimes}\\
    & = \left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3
    \left (\frac{\prod_{w\in V} \Gamma (n_{a \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{a \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma (n_{b \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{b \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma(n_{\rtimes \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{\rtimes \rightarrow w}+ \alpha_{w})}\right)\\
    %% Maybe this can be expressed as a product over (a,b,start)
\end{align*}

To simplify, we express the former using a product over the set $s \in \{a,b,\rtimes \}$,
\begin{equation}\label{eq:marginal_data}
      \Pr(S|\vec{\alpha})  = \left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3
    \prod_{s\in V\cup \{\rtimes\}} \left (\frac{\prod_{w\in V} \Gamma (n_{s \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{s \rightarrow w}+ \alpha_{w})}\right)
\end{equation}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 6:}

Let us assume that the parameters of the Dirichlet distribution are
$\vec{\alpha} = (1,1)$. Using your solution to the previous problem,
write an expression for $\Pr(S=aabab\mid \vec \alpha = (1,1))$,
the marginal probability of the string $aabab$.
The expression should should contain the
\href{https://en.wikipedia.org/wiki/Gamma_function}{gamma function}
$\Gamma(\cdot)$. Using the properties of the gamma function discussed
in class (i.e., it's relationship to the factorial) or an online
calculator, compute a numerical value for this expression.

\paragraph{Answer 6:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
\begin{align*}
      \Pr(S=aabab|\vec{\alpha}=\langle 1, 1 \rangle)  & = \left (\frac{\Gamma (2)}{\Gamma (1) \Gamma (1)}\right )^3 \left (\frac{\Gamma (2)\Gamma (3)}{\Gamma (5)}\right)\left(\frac{\Gamma (2)\Gamma (1)}{\Gamma (3)}\right)\left(\frac{\Gamma (2)\Gamma (1)}{\Gamma (3)}\right)\\
      & = \left(\frac{1}{1}\right)^3 \left(\frac{2}{24}\right)\left( \frac{1}{2}\right)\left( \frac{1}{2}\right)\\
      & = \frac{1}{48}
\end{align*}
\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 7:}

Suppose that we have observed a sentence
$S=W^{(1)},\dots,W^{(k)}$. Find an expression for the posterior
distribution over the model parameters,
$\Pr(\vec{\theta_a}, \vec{\theta_b}, \vec{\theta}_{\rtimes} \mid S,\vec{\alpha})$.

 Hint: Use the joint probability that you computed in Problem
4 and Bayes' rule. The solution should be analogous to the posterior
probability for the bag of words model.

\paragraph{Answer 7:}  Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%

\begin{align*}
    \Pr(\vec{\theta_a}, \vec{\theta_b}, \vec{\theta}_{\rtimes} \mid S,\vec{\alpha}) & = \frac{\left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3 \prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}+ \alpha_{w} -1} \theta_{bw}^{n_{b \rightarrow w}+\alpha_{w} -1} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}+\alpha_{w} -1}}{\left (\frac{\Gamma (\sum_{w\in V} \alpha_{w})}{\prod_{w\in V} \Gamma (\alpha_{w})}\right )^3
    \left (\frac{\prod_{w\in V} \Gamma(n_{a \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{a \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma (n_{b \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{b \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma (n_{\rtimes \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{\rtimes \rightarrow w}+ \alpha_{w})}\right)} \\
    \Pr(\vec{\theta_a}, \vec{\theta_b}, \vec{\theta}_{\rtimes} \mid S,\vec{\alpha}) & = \frac{\prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}+ \alpha_{w} -1} \theta_{bw}^{n_{b \rightarrow w}+\alpha_{w} -1} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}+\alpha_{w} -1}}{
    \left (\frac{\prod_{w\in V} \Gamma (n_{a \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{a \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma (n_{b \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{b \rightarrow w}+ \alpha_{w})}\right)
    \left (\frac{\prod_{w\in V} \Gamma (n_{\rtimes \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{\rtimes \rightarrow w}+ \alpha_{w})}\right)}
\end{align*}

\begin{align}
    \Pr(\vec{\theta_a}, \vec{\theta_b}, \vec{\theta}_{\rtimes} \mid S,\vec{\alpha}) & = \frac{\prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}+ \alpha_{w} -1} \theta_{bw}^{n_{b \rightarrow w}+\alpha_{w} -1} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}+\alpha_{w} -1}}{\prod_{s\in V\cup \{\rtimes\}} \left (\frac{\prod_{w\in V} \Gamma (n_{s \rightarrow w}+ \alpha_{w})}{\Gamma (\sum_{w\in V} n_{s \rightarrow w}+ \alpha_{w})}\right)}
\end{align}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 8:}

Using your formula for the posterior distribution and setting
$\vec{\alpha} = (1,1)$, given an expression for the posterior
distribution over parameters given the sentence $aabab$. There should
be an easy way to interpret the posterior distribution, and how it was
derived from the observed sentence. What is this interpretation?

\paragraph{Answer 8:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%

\begin{align*}
    \Pr(\vec{\theta_a}, \vec{\theta_b}, \vec{\theta}_{\rtimes} \mid S =aabab,\vec{\alpha}=\langle 1, 1 \rangle) & = \frac{\prod_{w\in V} \theta_{aw}^{n_{a \rightarrow w}} \theta_{bw}^{n_{b \rightarrow w}} \theta_{\rtimes w}^{n_{\rtimes \rightarrow w}}}{\prod_{s\in V\cup \{\rtimes\}} \left (\frac{\prod_{w\in V} \Gamma (n_{s \rightarrow w}+ 1)}{\Gamma (\sum_{w\in V} n_{s \rightarrow w}+ 1)}\right)}\\
    & =   \frac{\theta_{aa} \theta_{ba} \theta_{\rtimes a} \theta_{ab}^{2} \theta_{bb}^{0} \theta_{\rtimes b}^{0} }{
    (\frac{\Gamma (2)\Gamma (3)}{\Gamma (5)})
    (\frac{\Gamma(2)\Gamma (1)}{\Gamma (3)})
    (\frac{\Gamma(2)\Gamma (1)}{\Gamma (3)})}\\
    &= \frac{\theta_{aa} \theta_{ba} \theta_{\rtimes a} \theta_{ab}^{2}}{
    (\frac{2}{24})
    (\frac{1}{2})
    (\frac{1}{2})}\\
    &= \frac{\theta_{aa} \theta_{ba} \theta_{\rtimes a} \theta_{ab}^{2}}{
    (\frac{1}{48})}\\
    &= 48 \times \theta_{aa} \theta_{ba} \theta_{\rtimes a} \theta_{ab}^{2}
\end{align*}
    The posterior distribution is obtained using Bayes' rule and it was derived using the evidence (Equation \ref{eq:marginal_data}) and the joint distribution (Equation \ref{eq:joint}). Plugging in different values of model parameters in this equation will tell us how well these values fit this data.
\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 9:}

Consider the language $L=\{a^* b a^*\}$, that is, the language
consisting of some number of a's, followed by a single b, followed by
some number of a's. Show that this language is not strictly
$2$-local.

 Hint: use $n$-Local Suffix Substitution Closure ($n$-SSC).

\paragraph{Answer 9:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
\begin{proof}
    Let $s_1 = \rtimes aaabaa \ltimes$ with $u_1 = \rtimes aaab$, $x=a$ and $u_2=a\ltimes$. Let $s_2=\rtimes aaba$ with $v_1=\rtimes$, $x=a$ and $v_2=aba\ltimes$. Our new string is $\rtimes aaababa \ltimes \notin L \Rightarrow \neg 2\mathrm{-SSC}(L) \Rightarrow L \notin \mathrm{SL}_2$
\end{proof}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 10:}

Consider the language
$L= \{a^n b^m c^n d^m\ | n, m \in \mathbb{N} \}$, that is, the
language consisting of $n$ a's followed by $m$ b's followed by $n$ c's
followed by $m$ d's where $n$ and $m$ are
natural numbers. Show that this language is not strictly $2$-local.

 Hint: use the same property as in the problem above.

\paragraph{Answer 10:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
\begin{proof}
    Let $s_1 = \rtimes abcd \ltimes$ with $u_1 = \rtimes ab$, $x=c$ and $u_2=d\ltimes$. Let $s_2=\rtimes aabccd\ltimes$ with $v_1=\rtimes aab$, $x=c$ and $v_2=cd\ltimes$. Our new string is $\rtimes aabd \ltimes \notin L \Rightarrow \neg 2\mathrm{-SSC}(L) \Rightarrow L \notin \mathrm{SL}_2$
\end{proof}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 11:}

Show that the language $L= \{a^n b^m c^n d^m\ | n, m \in \mathbb{N} \}$ is not
strictly $k$-local, for any value of $k$.

\paragraph{Answer 11:} Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%
\begin{proof}
    Let $k \in \mathbb{N}$ and then choose $n=k$. Take a string in $L$ such that $s_1 = a^nb^{n+1}c^nd^{n+1}$ with $u_1=a^n$, $x=b^{n-1}$ and $u_2=b^2c^nd^{n+1}$ and another string $s_2 = a^{n+1}b^{n}c^{n+1}d^{n}$ with $u_1=a^{n+1}$, $x=b^{n-1}$ and $u_2=bc^{n+1}d^{n}$. The resulting string is $s_3=a^nb^{n-1}bc^{n+1}d^n = a^nb^{n}c^{n+1}d^n \notin L \Rightarrow \neg k\mathrm{-SSC}(L)  \Rightarrow L \notin \mathrm{SL}_k$ for any $k\in \mathbb{N}$
\end{proof}

\end{AnswerBox}%% Do not delete %%%%%%%

\hrulefill %--------------------------------

\paragraph{Problem 12:}

In class we proved that
$k\mathrm{-SSC}(L) \implies L \in \mathrm{SL}_k$. In other words, if a
language satisfies $k$-Local Suffix Substitution Closure, then it is
$k$-strictly local.

 Use this theorem to prove that $k$-strictly local languages
are closed under intersection. More precisely, prove that if
$L_1 \in \mathrm{SL}_k$ and $L_2 \in \mathrm{SL}_k$, then
$L_1 \cap L_2 \in \mathrm{SL}_k$.

\paragraph{Answer 12:}  Please put your answer in the box below.

\begin{AnswerBox}%% Do not delete %%%%%%%

\begin{proof}
    Suppose $L_1 \in \mathrm{SL}_k$ and $L_2 \in \mathrm{SL}_k$ and take $s_1 \in L_1\cap L_2$ and $s_2\in L_1\cap L_2$ where $s_1=u_1xu_2$, $s_2=v_1xv_2$ and $|x|=k-1$. We know from proving it in class that $L_1$ and $L_2$ are closed under $k$-local suffix substitution. Now, consider the string $s_3=u_1xv_2$. Since $s_1\in L_1$, $s_2\in L_1$ and $k\mathrm{-SSC}(L_1)$, then $s_3\in L_1$ but $s_3\in L_2$ as well since $s_1\in L_2$, $s_2\in L_2$ and $k\mathrm{-SSC}(L_2)$.\\
    $\therefore s_3\in L_1\cap L_2 \Rightarrow k\mathrm{-SSC}(L_1\cap L_2) \Rightarrow L_1\cap L_2\in \mathrm{SL}_k$
\end{proof}

\end{AnswerBox}%% Do not delete %%%%%%%

\end{document}


%% LyX 2.3.4 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[oneside,english]{amsart}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{wasysym}
\usepackage[authoryear]{natbib}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\numberwithin{equation}{section}
\numberwithin{figure}{section}
\theoremstyle{plain}
\newtheorem{thm}{\protect\theoremname}
\theoremstyle{definition}
\newtheorem{example}[thm]{\protect\examplename}
\theoremstyle{plain}
\newtheorem{lem}[thm]{\protect\lemmaname}

\makeatother

\usepackage{babel}
\providecommand{\examplename}{Example}
\providecommand{\lemmaname}{Lemma}
\providecommand{\theoremname}{Theorem}

\begin{document}

\section{Ordinal alpha\label{sec:Ordinal alpha}}

Recall the congeneric model ((cite))

\[
X=\lambda Z+\Psi^{1/2}\epsilon
\]
Consider the scenario when we do not observe the $X_{i}$s directly,
but rather $X_{i}$s discretized into $m_{i}$ categories according
to

\begin{equation}
Y_{i}=k1[\tau_{i(k-1)}\leq X_{i}\leq\tau_{ik}],\label{eq:discretization model}
\end{equation}
where $-\infty=\tau_{i0}<\tau_{i1}<\ldots<\tau_{i(m_{i}+1)}=\infty$
are thresholds for each $i$ and $1[A]$ is the indicator function.
This is a common model for Likert scales with $m_{i}$ levels for
the $i$th item. If $X$ is multivariate normal, model (\ref{eq:discretization model})
is a congeneric \emph{multivariate ogive model} \citep{Swaminathan2016-rg}.
Under multivariate normality the correlation matrix $\Phi$ is point-indentified
from the distribution of $Y$, and is called the \emph{polychoric
correlation}. It can be estimated using maximum likelihood directly
or a two-step procedure \citep{Olsson1979-ti}. To make the model
identified, usual practice is assume $\Psi^{1/2}$ to be a correlation
matrix.

\citet{Zumbo2007-ap} propose what I call the \emph{theoretical ordinal
alpha}

\begin{equation}
\alpha=\frac{k}{k-1}\left(1-\frac{k}{\mathbf{i}^{T}\Phi\mathbf{i}}\right)\label{eq:ordinal alpha}
\end{equation}
where $\Phi=\Cor X$ is polychoric correlation coefficient of $Y$.
Theoretical ordinal alpha is a standardized alpha, but it is based
on the latent correlation matrix between the unobserved $X$ instead
of the observed correlation matrix between the $Y$s. The population
values of the theoretical ordinal alpha and the standardized alpha
are not the same in general. \citet{Zumbo2007-ap}'s main argument
in favor of the ordinal alpha is ``coefficient alpha (and KR-20)
are correlation-based statistics and hence assume continuous data\textquoteright \textquoteright{}
\citep[p. 27]{Zumbo2007-ap}. \citet[p. 1060, "Misconcetion 1"]{Chalmers2018-fj}
persuasively argues against this idea, see also \citep{Raykov2019-yr}
and the section ((cite)) of this paper. The argument is reiterated
by \citep{Gadermann2012-jl}. However, \citet{Zumbo2019-lm} argue
the the benefit of sample ordinal alpha is that we are truly interested
in the reliability of $X$ as a predictor of $Y$ (in the parlance
of this paper).

A related notion is the \emph{theoretical ordinal reliability, }the
analogue of the weighted reliability ((ref)) based on the polychoric
correlation coefficient. Its definition is
\begin{equation}
\omega_{w}=\frac{w\lambda_{i}^{\star}}{w\lambda_{i}^{\star}+\sigma_{i}^{\star}}\label{eq:ordinal omega}
\end{equation}
were $\lambda_{i}^{\star}=\lambda_{i}/\sqrt{\lambda_{i}^{2}+\sigma_{i}^{2}}$
and $\sigma_{i}^{\star}=\sigma_{i}/\sqrt{\lambda_{i}^{2}+\sigma_{i}^{2}}$.

There several reasons to avoid the theoretical ordinal alpha. If your
goal is to estimate the congeneric reliability or the standardized
reliability, the theoretical ordinal alpha is inappropriate since
it does not equal the congeneric reliability, even under the parallel
model \citep[p. 1062, "Misconception 2"]{Chalmers2018-fj}, a point
conceded by \citet{Zumbo2019-lm}. But this is not the worst problem.
For even if you are interested in estimating the congeneric reliability
for the multivariate normal ogive model, you should avoid the theoretical
alpha.

Theoretical ordinal alpha is apparently not connected to any predictor
of $Z$ that depends on the data $Y$, only the correlation $\Cor^{2}(\sum_{i=1}^{k}w_{i}X_{i},Z)$.
This correlation is barely interesting though, since we do not observe
the latent $X_{i}$s. The theoretical alpha does not have the concrete
interpretation of the congeneric alpha mentioned in section 2, namely
``how good can I expected my prediction of $Z$ based on $X$ to
be?'', as $X$ is unknown and you cannot for your prediction. This
lack of knowledge about $X$ also makes correction for attenuation
impossible. Since $X$ is unknown, it is impossible to directly calculate
the correlation between a function of $X$ and a function of some
other quantity $X'$. Due to this, the correction for attuation formula
will not work. What you can calculate is the correlation between some
function of the observed variables $Y$ and a function of $X'$.

Moreover, if you want the standardized reliability of the unobserved
$X$, the ordinal reliability is a better choice. For the conditions
required for standardized alpha to equal the standardized reliability
are still needed, i.e. the weighted $\tau$-equivalent model (). The
theoretical ordinal reliability can be estimated using e.g. $\mathtt{lavaan}$
without much added complexity. Using the same arguments as in the
previous section, the theoretical ordinal reliability with optimal
weights (corresponding to coefficient \emph{H}) or weights $w_{i}=\sigma_{i}$
(corresponding to the sigma reliabiltiy) should be prefered to the
standardized reliability too.

Finally, theoretical ordinal alpha only makes sense in relation to
the congeneric multivariate ogive model. But this model depends on
the fundamentally unverifiable assumption of multivariate normality
of $X$. The polychoric correlation coefficient is not robust against
normality in general \citep{Foldnes2019-yd}.

\subsection{A concrete ordinal reliability}

The main argument of \citet{Zumbo2019-lm}'s rebuttal of \citep{Chalmers2018-fj}
is that the congeneric multivariate normal ogive model (\ref{eq:discretization model})
is a better approximation to reality than the congeneric model (ref).
So assume we believe in the discretized model and can be reasonably
sure that $X$ is multivariate normal. Then ordinal alpha would still
not be appropriate as it is theoretical and not concrete. However,
it is possible to create a class of reliability coefficients for the
multivariate normal discretized model.

Let $\widehat{X}_{i}$ be the mean of a standard normal distribution
truncated to $(\tau_{i(Y_{i}-1)},\tau_{iY_{i}}]$, or equivalently

\begin{equation}
\widehat{X}_{i}=-\frac{\phi(\tau_{iY_{i}})-\phi(\tau_{i(Y_{i}-1)})}{\Phi(\tau_{iY_{i}})-\Phi(\tau_{i(Y_{i}-1)})},\label{eq:Predictor of X}
\end{equation}
see \citep[Section 10.1]{Johnson1994-ag} for the formula for the
expectation of a truncated normal. Since $\widehat{X}_{i}$ is the
conditional expectation of $X_{i}$ given $Y_{i}$ it is the optimal
predictor of $X_{i}$ given $Y_{i}$ in the sense of minimizing the
mean squared error. A natural predictor of $Z$ given $Y$ is a linear
function of $\hat{X}_{i}$, i.e. $\sum_{i=1}^{k}w_{i}\hat{X}_{i}$
for some weights $w_{i}$, which gives rise to the analogue of the
weighted reliability (ref) in the context of the discretization model
(\ref{eq:discretization model}) when $X$ is multivariate normal
with standard normal marginals. Define the weighted ordinal reliability
as

\begin{equation}
\omega_{w}'=\Cor^{2}(\sum_{i=1}^{k}w_{i}\hat{X}_{i},Z).\label{eq:Weighted ordinal reliability}
\end{equation}
The following theorem shows how to calculate $\omega_{w}^{'}$ and
some of its properties.
\begin{thm}
\label{thm:omega-prime}Let $\hat{Z}=\sum_{i=1}^{k}w_{i}\hat{X}_{i}$
with $\hat{X}$ defined as in \ref{eq:Predictor of X} and $\Xi=\Cov\hat{X}$.
Assume $X$ is multivariate normal with standard normal marginals,
such that $\sqrt{\lambda_{i}^{2}+\sigma_{i}^{2}}=1$ for all $i$.
\begin{enumerate}
\item Let $v_{i}=$ be the Thurstone weights. Then $\sum_{i=1}^{k}w_{i}\hat{X}_{i}$
is the optimal predictor of $Z$, i.e. it minimizes the mean squared
error $\MSE(\hat{Z})$.
\item The weighted ordinal reliability is
\begin{eqnarray}
\omega_{w}' & = & \frac{(w^{T}\Xi v)^{2}}{w^{T}\Xi w}\label{eq:Omega prime}\\
 & = & 1-\MSE(v_{0}\hat{Z})\nonumber
\end{eqnarray}
where $v_{i}=\lambda_{i}/[\sigma_{i}^{2}(1+k\overline{\lambda^{2}\sigma^{-2}})],\:i=1,\ldots,k$
are the Thurstone weights for $X$ and $v_{0}=w^{T}\Xi v$ is the
minimizer of $\MSE(v_{0}\hat{Z})$. Moreover, $\omega_{v}'=v^{T}\Xi v$
for the Thurstone weights.
\item When the underlying congeneric model is parallel and $w=\mathbf{i}$,
\begin{eqnarray}
\omega_{w}' & = & \frac{\lambda^{2}}{\left(k\lambda^{2}+\sigma^{2}\right)^{2}}\mathbf{i}^{T}\Xi\mathbf{i},\label{eq:Alpha prime}\\
 & = & \alpha_{s}\frac{\mathbf{i}^{T}\Xi\mathbf{i}}{\mathbf{i}^{T}\Psi\mathbf{i}},\nonumber
\end{eqnarray}
where $\alpha_{s}$ equals the ordinal alpha.
\item For any weight $w$, $\omega'_{w}\to\omega_{w}$ when $\Xi\to\Phi$.
\end{enumerate}
\end{thm}

\begin{proof}
See the appendix, page \pageref{proof:omega-prime}.
\end{proof}
Point (i) is a strong justification for using $\hat{Z}=\sum_{i=1}^{k}v_{i}\hat{X}_{i}$,
where $v_{i}$ are the Thurstone weights. This is not only the optimal
linear predictor of $Z$ given the data $Y$, it is the optimal predictor
of $Z$ period. The analogous result for the congeneric model with
multivariate normal $X$ is also true, that is, $Z=\sum_{i=1}^{k}v_{i}X_{i}$
is the optimal predictor of $Z$. (This follows from the proof of
Theorem \pageref{thm:omega-prime}.) The representation of the ordinal
reliability in (ii) is similar to the representation of the weighted
reliability in Proposition (ref), but the covariance $\Xi$ is used
instead of $\Sigma$. Moreover, $v^{T}\Xi v$ is easy to estimate
provided we are able to estimate the Thurstone weights $v$.

Point (iii) demonstrates that $\omega'$ with unit weigths can be
calculated without running a factor analysis, since the average correlation
$\mathbf{i}^{T}\Xi\mathbf{i}/\mathbf{i}^{T}\Psi\mathbf{i}$ is a simple
function of the polychoric correlation matrix and the covariance matrix
of $\hat{X}.$ Moreover, $\alpha_{s}(\mathbf{i}^{T}\Xi\mathbf{i}/\mathbf{i}^{T}\Psi\mathbf{i})$
is the natural variant of the standardized alpha in the ordinal setting,
as it is defined in terms of the matrices $\Xi$ and $\Psi$ only,
and is designed to equal the ordinal reliability under the parallel
model.

\citet[p. 1068]{Chalmers2018-fj} discusses one possible use of the
ordinal alpha, namely as the theoretical reliability when the number
of thresholds $\tau$ goes towards infinity. Point (iv) makes this
claim concrete In this case we know $Y$ exactly except for its scale,
so we can only form reliability coefficients based on the standardized
$\lambda,\sigma$. The practical use of this quanitity remain illusive
to me, however.
\begin{example}
We will yet again have a look at $6$-level agreeableness factor the
personaliy data $\mathtt{bfi}$ from $\mathtt{psychTools}$. I used
the $\mathtt{psych}$ package to fit the congeneric multivariate ogive
model \ref{eq:discretization model} using the two-step procedure
to estimate the polychoric correlation matrix. Recall the assumptions
of multivariate normality of $X$ and that $\lambda,\sigma$ are standardized.
The resulting estimates are $\hat{\lambda}=(0.43,0.71,0.80,0.52,0.67)$
and $\hat{\sigma}=(0.90,0.70,0.59,0.86,0.74)$. The ordinal relability
with optimal weights is $\omega'_{H}=0.68$, and with equal weights
and $\omega'=0.64$. But the theoretical variants are $\omega'_{H}=0.81$
and $\omega'=0.77$, both missed by large magin. In comparison, the
reliabilities calculated under the congeneric model with $\mathtt{lavaan}$
equals $\omega'_{H}=0.77$ and $\omega'=0.74$.

I calculated $\omega'_{H}$ and $\omega'$ for selection of Likert
scales with $\lambda=\hat{\lambda}$ and $\hat{\sigma}=\sigma$ and
the thresholds $\tau$ at $\{\Phi^{-1}[i/(k+1)]\}_{i=1}^{k+1}$, which
approximates the reliabilities we would have obtained had we used
$k+1$. Figure \ref{fig:Ordinal reliability} contains the results.
\end{example}

\begin{figure}
\noindent \begin{centering}
\includegraphics[scale=0.3]{C:/GitHub/standardized/chunks/ordinals}
\par\end{centering}
\caption{\label{fig:Ordinal reliability}Ordinal reliability ($\fullmoon$)
and ordinal $H$ ($\newmoon$) for a selection of Likert levels. The
theoretical coefficient $H$ is $0.81$ (solid line), and the theoretical
composite reliability is $0.77$ (dashed line). All cutoffs are uniform,
and every reliability has been calculated with $\lambda=(0.43,0.71,0.80,0.52,0.67)$
and $\sigma=(0.90,0.70,0.59,0.86,0.74)$. The original number of levels
($6$) and the ordinal reliabilities $(\omega'_{H}=0.71,\omega'=0.67)$
are marked for reference.}
\end{figure}

\section*{Appendix}
\begin{lem}
\label{lem:r^2 and correlation}Assume $Y$ and $\hat{Y}$ have finite
variance. Then

\begin{equation}
\Cor^{2}(\hat{Y},Y)=1-\frac{\MSE(\alpha+\beta\hat{Y})}{\Var(Y)},\label{eq:rsq and correlation}
\end{equation}
where $\alpha=E(Y)-E(\hat{Y})$ and $\beta=\Cov(\hat{Y},Y)/\Var(\hat{Y})$.
\end{lem}

\begin{proof}
Recenter $Y$ and $\hat{Y}$ so that $E(Y)=E(\hat{Y})=0$. Then
\begin{eqnarray*}
\MSE(\alpha+\beta\hat{Y}) & = & \beta^{2}\Var(\hat{Y})-2\beta\Cov(\hat{Y},Y)+\Var Y^{2}
\end{eqnarray*}
Differentiate with respect to $\beta$ and set equal to zero, so that
$2\beta\Var(\hat{Y})-2\Cov(\hat{Y},Y)=0$. Then $\beta=\Cov(\hat{Y},Y)/\Var(\hat{Y})$
\end{proof}
%
\begin{proof}[Proof of Theorem (\ref{thm:omega-prime})]
\label{proof:omega-prime}(ii) That $\omega'=1-\MSE(v_{0}\hat{Z})$
follows from Lemma \ref{lem:r^2 and correlation} and the formula
for the covariance, which we now show. The covariance between $\sum_{i=1}^{k}w_{i}\widehat{X}_{i}$
and $Z$ is
\begin{eqnarray*}
\Cov(\sum_{i=1}^{k}w_{i}\widehat{X}_{i},Z) & = & E[\Cov(\sum_{i=1}^{k}w_{i}\widehat{X}_{i},Z\mid\{Y_{i}\})]+\Cov(E\left[\sum_{i=1}^{k}w_{i}\widehat{X}_{i}\mid\{Y_{i}\}\right],E\left[Z\mid\{Y_{i}\}\right]),\\
 & = & -\Cov\left(\sum_{i=1}^{k}w_{i}\widehat{X}_{i},E\left[Z\mid\{Y_{i}\}\right]\right).
\end{eqnarray*}
The expectation $E\left[Z\mid\{Y_{i}=k_{i}\}\right]$ equals $E\left[E\left[Z\mid X\right]\mid\{Y_{i}\}\right]$.
The conditional expectation $E(Z\mid X)=\sum_{i=1}^{k}v_{i}X_{i}$
under multivariate normality, where $v_{i}=\lambda_{i}/[\sigma_{i}^{2}(1+k\overline{\lambda^{2}\sigma^{-2}})]$
are the Thurstone weights, see \citet[Theorem 3.3.4]{Tong1990-lm}.
Consequently, $E[E(Z\mid X)\mid\{Y_{i}\}]=-\sum_{i=1}^{k}v_{i}\hat{X_{i}}$
and
\begin{eqnarray*}
\Cov(\sum_{i=1}^{k}w_{i}\widehat{X}_{i},Z) & = & \Cov\left(\sum_{i=1}^{k}w_{i}\widehat{X}_{i},\sum_{i=1}^{k}v_{i}\widehat{X}_{i}\right)=w^{T}\Xi v.
\end{eqnarray*}
Standardize to find the correlation $w^{T}\Xi v/\sqrt{w^{T}\Xi w}$,
and the squared correlation is $\omega'=(w^{T}\Xi v)^{2}/w^{T}\Xi w$.
This completes point (i).

As for (iii), under the parallel model, $v=w_{0}\mathbf{i}$, where
$w_{0}=\lambda/[\sigma^{2}+k\lambda^{2}]$ and the squared correlation
is $w_{0}^{2}\mathbf{i}^{T}\Xi\mathbf{i}=\lambda^{2}\mathbf{i}^{T}\Xi\mathbf{i}/\left(k\lambda^{2}+\sigma^{2}\right)^{2}$.
For the second equation, first notice that
\[
\alpha_{s}=\frac{k}{k-1}\left(\frac{i^{T}\Phi i-k}{i^{T}\Phi i}\right)=k^{2}\left(\frac{\overline{\rho}}{i^{T}\Phi i}\right).
\]
Since $(k\lambda^{2}+\sigma^{2})^{2}=((i^{T}\Phi i)/k)^{2}$,
\begin{eqnarray*}
\frac{\lambda^{2}}{\left(k\lambda^{2}+\sigma^{2}\right)^{2}}\mathbf{i}^{T}\Xi\mathbf{i} & = & k^{2}\frac{\overline{\rho}}{(i^{T}\Phi i)^{2}}\mathbf{i}^{T}\Xi\mathbf{i},\\
 & = & \alpha_{s}\frac{\mathbf{i}^{T}\Xi\mathbf{i}}{\mathbf{i}^{T}\Psi\mathbf{i}},
\end{eqnarray*}
as claimed.

(i) Follows from the proof (ii); the Thurstone weights optimize $(w^{T}\Xi v)^{2}/w^{T}\Xi w$
since $E[E(Z\mid X)\mid\{Y_{i}\}]=-\sum_{i=1}^{k}v_{i}\hat{X_{i}}$.
\end{proof}
\bibliographystyle{plain}
\bibliography{C:/GitHub/standardized/standardized}

\end{document}