\documentclass{svproc}
%\documentclass{article}
\def\UrlFont{\rmfamily}
\usepackage{graphicx}
%\usepackage{subcaption}
\usepackage{bm}
%\usepackage{geometry}
\usepackage{float}
\usepackage{caption}
\usepackage{pdfpages}
\usepackage{setspace}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{multicol}
\usepackage{color}
\doublespacing
\usepackage[margin=1.1in]{geometry}% to typeset URLs, URIs, and DOIs
\usepackage{url}
\usepackage{threeparttable}
\usepackage[bottom]{footmisc}
\usepackage{adjustbox}
\usepackage{multirow}
\usepackage{makecell}
\usepackage{caption}
\usepackage{subfig}
\def\UrlFont{\rmfamily}
\raggedbottom
\newenvironment{centermath}
 {\begin{center}$\displaystyle}
 {$\end{center}}
\newcommand\scalemath[2]{\scalebox{#1}{\mbox{\ensuremath{\displaystyle #2}}}}

\begin{document}
\mainmatter              % start of a contribution
%

\title{Solar Analysis}
%
\titlerunning{Solar Analysis}  % abbreviated title(for running head)
%                                     also used for the TOC unless
%                                     \toctitle is used
%
\author{Jacob Merrell}

\institute{}
%
%%%% list of authors for the TOC(use if author list has to be modified)

\maketitle

\begin{abstract}
   Solar energy is environmentally friendly, but can it also be cost efficient? This study focuses on power bill data collected from one solar panel owner. The study confirms that solar power helps save money, and hypothesizes that on average the savings from using the solar panel after 8 years will cover the costs of the installation. We explore ways to account for the correlation in the data, and to also incorporate temperature into the model.
\end{abstract}

\section{Introduction}

Ideally energy would be efficient, cheap, and environmentally friendly. Finding the right balance is a difficult question for scientists to answer. Sunlight is available to all, given the weather is right. Harnessing the power of solar energy is an environmentally friendly alternative to other more pollutant producing energy sources. The purpose of this analysis to be able to: (1) predict how much power bills are going to be in the future (2) how much can someone save, on average, using solar energy, and (3) how long it will take to earn back the money used to install solar equipment. Regression techniques to handle correlated data will be used to answer the goals of the analysis.

\section{Exploratory Data Analysis}

\begin{center}
\includegraphics [height=7.5cm]{solar_data.pdf}
\end{center}

The graph above shows the power bill for the subject given dates in time. There is a sizable difference between the power bill after switching to solar energy. Exploring the data shows that normal regression methods are inadequate for this data. There appears to be a seasonal trend in the power bill. This makes sense because in very cold and very hot months, more energy is used to maintain the climate inside the home. The dataset includes 51 months of power bills from the same subject.

\section{Model Selection}

The model used for this analysis is

\begin{equation}
 Y \sim N(X\beta, \sigma^2R)
\end{equation}

In the model $Y$ denotes the vector of power bill amounts, and the $X$ matrix contains the observed values of the explanatory variables at the date of each power bill. The $\beta$ vector contains the model coefficients, $\sigma^2$ is the variance of the residuals, and $R$ is the variance covariance matrix. The variance covariance matrix in this model is structured as AR(1). This model assumes that correlation of each observation with the previous observation is constant and equal to $\rho$. This makes sense to use an AR(1) model since the data are measured in discrete and consistent time intervals.

The effects being measured in the model are solar v non-solar power bills, the interaction between solar status and season (summer or winter), and the interaction between season and temperature. Temperature was not originally given in the dataset, but was gathered from the US Climate Data website. The reason for gathering this data was to see the effect extreme temperatures had on the power bill.

The model assumptions are normality of the standardized residuals, homoscedacisity, and that the data are multivariate normally distributed. Normality of the residuals can be observed by plotting the residuals to verify if they seem normally distributed. Homoscedacisity is verified through a graph of the fitted values v. the residuals. A constant variability or jitter of the residual values about 0 should be similar across all fitted values. The model follows multivariate normal distribution.

The model will help us achieve our goals by estimating coefficients and allowing us to make predictions of the power bill amount at each month. The model will also help us capture the seasonality trends inherent in the data.

\section{Model Justification}
Since the data are correlated, using the residuals from the model without any adjustment can lead to incorrect conclusions about the assumptions. To correct for this issue, we used a decorrelated regression model. After undergoing the decorrelation transformation, the residuals and fitted values can be used to verify whether or not the assumptions hold. The assumptions for the model are explored in the graphs below

\begin{center}
\includegraphics [height=7.5cm]{assump.pdf}
\end{center}

The histogram of the standardized residuals shows a normal distribution. The fitted v. residual plot shows that the variance of the residuals about the 0 line is about the same. All assumptions hold for this study.

\section{Performance Evaluation}

The model had an adjusted R-squared of 0.9376. This mean 93.76\% of variation in power bill is explained by the model. To test the predictive power of the model, we used test and training data to cross validate. The test and training datasets were chosen to preserve the time series; in this case choosing random points would interfere with the correlated structure inherent in the data. The training data was used to predict the test data. The RMSE, bias, and 95\% prediction interval coverage we calculated. This process was repeated several times to get more reliable results. On average, the RMSE was \$27.21; this means the predictions were off by \$27.21 on average. The 95\% confidence interval for the RMSE was (\$6.74, \$54.48). The bias showed that we were underestimating the power bill by \$0.67 on average, and almost 95\% of all observed power bill values were contained within the prediction intervals. Even though the coverage was very good, the average prediction window was \$104.57. This means most of the predictions for a power bill during a given month would have been in a ballpark of plus or minus \$50 away from the point estimate.

\section{Results}

The graph below shows the fitted values from the model compared to the actual power bill values.

\begin{center}
\includegraphics [height=7.5cm]{solar_fit.pdf}
\end{center}

While the model uses an AR(1) covariance structure, the model estimate for $\rho$ is very small (.0377). This means the effects and interactions included in the model account for most of the correlation inherent in the data. To achieve the goals of the study we used the model to predict the amount of money spent on power bills over the next 10 years. Then we compared the amount of money spent per month when using solar power v. the amount of money spent on power when not using solar energy. The average monthly savings when using solar energy is \$86.69. The 95\% confidence interval for the monthly savings is (\$80.41,\$92.97). In this the study, the subject using solar power in their home paid \$8,000 (after government subsidies) to install the solar panels. Assuming the average monthly savings, it would take 7.69 years of cumulative saving to cover the installation costs. The 95\% confidence interval for the amount of time it would take to recover the initial investment is (7.17,8.29).

\section{Conclusion}
Using solar power helps save \$86.69 on average each month. However, the RMSE is fairly given the size of the monthly power bill. Predictions for individual months will vary much more than average yearly totals or average seasonal totals. Assuming an initial \$8,000 investment, it will take 7.69 years on average for the investment in solar panels to pay off. All power bill data in this study was gathered about 1 home owner. One improvement to be made for future studies is to include more solar panel owners across a broad range of geographical locations. Another element not incorporated into the study was the impact of inflation or changing utility costs on the power bill amount. The data overall could have used more explanatory variables to explain the variation in power bill. If one plans on living in their home for an extended period of time, solar energy is a cost effective and nature friendly way of using electricity.

\newpage
\begin{thebibliography}{6}

\bibitem{R}
R: A Language and Environment for Statistical Computing.R Core Team.
R Foundation for Statistical Computing.Vienna, Austria.
(2017).url = {https://www.R-project.org/}

\end{thebibliography}

\end{document}


%%%
%%% Variables
%%%
\part{Variables}
\begin{frame}
\thispagestyle{empty}
\textbf{\huge{Variables}}
\end{frame}

\begin{frame}{Variables Contents}
 \tableofcontents
\end{frame}

\section{Basics}
\subsection{Generate}
\begin{frame}[fragile]{Generate} \index{Generate!generate} \index{Generate!gen} \index{Generate!clonevar} \index{Recode!recode} \index{Table!tab} \index{Generate}
Time and time again generation of variables is required
\begin{lstlisting}
  ** clone variable
  clonevar female = sex

  ** generate variable
  gen gender = .
  recode gender . = 1 if sex == 1
  recode gender . = 2 if sex == 2

  ** spot the difference
  tab female
  tab gender
\end{lstlisting}
\end{frame}

\subsection{Rename}
\begin{frame}[fragile]{Rename}
Thinking about it we dislike the variable name \texttt{gender}. \index{Rename!rename} \index{Rename}
\begin{lstlisting}
 drop sex // throw this variable away
 ** gender is now called sex
 rename gender sex
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Moment \texttt{generate \dots~= .} ?}
Missing values in Stata are identified by the dot. \index{Missing Values!.} \index{Recode!recode}
\begin{lstlisting}
  clonevar edu = school

  ** create a missing
  recode edu 10 = .
  ** different missing values (Stata knows .a - .z)
  recode edu 9 = .a
  recode edu 8 = .b
  recode edu 7 = .c
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{Additionaly the dot is $\pm\infty$ and is Statas biggest value.}}
    \end{minipage}
    };
\end{tikzpicture}

\end{frame}

\begin{frame}[fragile]{A moment please \texttt{\dots~if \dots~== 1} ?}
% Durch \texttt{if} werden in Stata Bedingungen festgelegt. Im Beispiel wird die Variable \texttt{geschlecht} auf den Wert $1$ gesetzt, wenn die Variable \texttt{sex} den Wert $1$ hat. Solche Bedingungen sind für eine ganze Reihe von Befehlen möglich und können im Regelfall auch verknüpft werden.
if-conditions \index{Table!tab} \index{if} \index{if!and} \index{if!or}
\begin{lstlisting}
** sex in county 1 and school 1 or 2
  tab sex if county == 1 & ///
    (school == 1 | school == 2)
\end{lstlisting}
  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{The condition is following the if-statement. Different contditions can be linked using $\&$ and $|$. Linked conditions require variable names within each condition!}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\begin{frame}[fragile]{and \texttt{tab} ?}
% Der Befehl \texttt{tab} ist eine Kurzform von tabulate. Kurzschreibweisen findet man durch das Studium der Helpfiles.\vspace{0.5cm}
\texttt{tab} is short for\\ \vspace{0.5cm} \index{Table!tab} \index{Help!help}
\begin{scriptsize}
\texttt{
  \textcolor{Statakeywords}{help tabulate twoway} \\
  ~\underline{tab}ulate \textcolor{Statakeywords}{varname1} \textcolor{Statakeywords}{varname2} [\textcolor{Statakeywords}{if}] [ \dots
} \vspace{0.5cm}
\end{scriptsize}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{The underlined part shows the minimum letters Stata requires for identification of a command.}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\subsection{Label}
\begin{frame}[fragile]{Label} \index{Label!label} \index{Label!label variable} \index{Label!label define} \index{Label!label values} \index{Table!nolabel}
To label a variable, a label has to be defined and afterwards applied
\begin{lstlisting}
  ** label a variable
  label variable sex "Sex"
  ** define label for values - labsex is the label name
  label define labsex 1 "male" 2 "female"
  ** assign label to variable
  label values sex labsex
\end{lstlisting}
Now labels are assigned
\begin{lstlisting}
  tab sex
  tab sex, nolabel
\end{lstlisting}
\end{frame}

\section{Elaborated}
\subsection{Generate using Math}
\begin{frame}[fragile]{Generate II} \index{Generate!gen} \index{Generate}
It is possible to create variables using mathematical expressions
\begin{lstlisting}
  // create fantasy weight and height
  replace weight = weight / 2.2
  replace height = height * 0.004

  // bmi for lbs and in
  gen bmi = (weight / (height ^ 2))

  /* other examples
  ** householdincome
  gen hhinc = inc_female + inc_male
  ** age in 2016
  gen age = 2016 - gebjahr
  */
\end{lstlisting}
\end{frame}

\subsection{Recode into a new Variable}
\begin{frame}[fragile]{Generate III} \index{Generate!gen} \index{Recode!recode}
A new variable is created from recoded values. The original variable is not modified.
\begin{lstlisting}
  recode school ///
    (10=.) ///
    (1=1 "One") ///
    (2=2 "Two") ///
    (3 5 6 7 8 = 99 "Ninetynine") ///
    (4=3 "Three") ///
    (9=5 "Five"), gen (school_rec)
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{Variable names begin with a character (a-z or A-Z).}}
    \end{minipage}
    };
\end{tikzpicture}

\end{frame}

\begin{frame}[fragile]{A moment please \texttt{///} ?} \index{display} \index{newline} \index{newline!///} \index{newline!delimit}
Three following \textit{Slashes} indicate to Stata, a command will continue in the following line.
\begin{lstlisting}
  ** everything in a single line
  display 1 + 1
  ** now with a (unneeded) linebreak
  display 1 + ///
    1
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{This way two and more lines can be connected. This makes your syntax more readable. Else check help delimit}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\subsection{Categorize}
\begin{frame}[fragile]{Recoding} \index{Categorize} \index{Generate!clonevar} \index{Recode!recode}
Sometimes it is helpful to categorize metric variables
\begin{lstlisting}
  ** create rounded values
  gen bmi_r = round(bmi)
  recode bmi_r ///
    (0/18.5=1) ///
    (18.5/25.0=2) ///
    (25.0/30.0=3) ///
    (30.0/60=4)

  tab bmi_r
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{0/18 means from 0 to 18. 25/30 from 25 to 30. Be careful with the category range!}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\subsection{Missing Values}
\begin{frame}[fragile]{Data preparation} \index{Missing Values} \index{Missing Values!mvdecode} \index{Missing Values!mv()}
Lets have a look at our fantasy weights data. We replace a few values with different values
\begin{lstlisting}
  ** weight
  replace weight = -1 if weight < 40
  replace weight = -2 if weight > 140

  mean weight
\end{lstlisting}
Now we decode these values as missing values
\begin{lstlisting}
  mvdecode weight, mv (-1=.a\-2=.b)
  mean weight
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.5) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{In the SOEP negative values are typically missing values.}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\subsection{Variable Handling}
\begin{frame}[fragile]{Mistakes} \index{Mistakes} \index{drop}
% \begin{minipage}{11cm}
After hours of work you figure that you have made a mistake and want to modify the just created variable \texttt{bmi\_r}. What now?
\begin{itemize}
 \item Luckily you have not touched the original data. Just run your do-file up to the point where the mistake is located.
 \item You already spotted the error and want to rerun the variable creation. Simply remove the variable
 \begin{lstlisting}
  drop bmi_r
 \end{lstlisting}
\end{itemize}
% \end{minipage}
\end{frame}

\section{Save and Import}
\subsection{Save}
\begin{frame}[fragile]{Save Data} \index{Save!save} \index{Save!replace} \index{Save}
% Ein langer erfolgreicher Arbeitstag geht zuende. Sie wollen Ihre bisherigen Ergebnisse sichern.
Save your do-file into a directory on your computer (e.g. \textit{do-files}) and your data into a different folder(e.g. \textit{data}).
\begin{lstlisting}
  save "D:/Data/data/testdata_recoded.dta"
\end{lstlisting}
If a file with the same name exists, Stata does not want to erase anything by mistake and asks you for the \texttt{replace} option
\begin{lstlisting}
  save "D:/Data/data/testdata_recoded.dta", replace
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.8) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{Replaced data is forever gone. Never \underline{ever} replace your original data!}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}

\subsection{Import}
\begin{frame}[fragile]{Import Data} \index{Import!use} \index{Import!clear} \index{Import}
With \texttt{use ..., clear} the currently imported data set will be replaced
\begin{lstlisting}
  use "D:/Data/data/testdata.dta", clear
\end{lstlisting}

  \begin{tikzpicture}[transform shape, rotate=10, overlay]
\node at (7.5,-1.8) [mybox] (box) {%
    \begin{minipage}[t!]{0.35\textwidth}
    \tiny\textcolor{black}{\texttt{Unsaved modifications to the current data set are ignored and are not saved. Using the clear option forces Stata to do something it would otherwise warn you about.}}
    \end{minipage}
    };
\end{tikzpicture}
\end{frame}


\section{State of the art review} \label{soa}
Detection of face and non face images is a classical problem in computer vision forming the first step to all the facial analysis methods like face recognition, face alignment and face modeling.
For this reason, building a system that detects faces in images can be very useful and it could be implemented in cctv systems or even in lip reading systems.

Although it's a hard problem, over the years proposals of face detection applications have been made, being frequent the usage of SVM as can be found in \cite{1619082, 609310} and NN such as in \cite{5233453, 8300832} (Neural Networks are also frequently used in facial recognition as stated in \cite{8300832}). Most recently there have also been some studies to analyse which of these models are better options depending on the use-case scenario, as in \cite{7943228}, where is conducted an in-depth analysis of the advantages and disadvantages of using SVM and NN applications to perform face detection.

In addition, it is not only important to choose which model to use, but also which strategy to process the images, because the full processing of an image would be too expensive and unrealistic. Fortunately, there is also a lot of research in this field and some alternatives such as DCT and HOG, that are both explored in detail in \cite{7467714}, or even Skin Segmentation as detailed in \cite{5076875}.

At last, given the small dataset that we had, we also did some research about the advantages of using data augmentation to expand our universe of examples and found a recent work done in 2019 where it is detailed how data augmentation can improve the accuracy and performance of models based on NN and CNN, details can be found in \cite{8858529}.


% Created 2021-04-21 Wed 21:01
% Intended LaTeX compiler: xelatex
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{minted}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{subcaption}
\usepackage{mathtools}
\usepackage{xfrac}
\usepackage[margin=1in]{geometry}
\usepackage{marginnote}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{epsf}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{pslatex}
\usepackage{hyperref}

\usepackage{beton}
\usepackage{euler}
\usepackage[OT1]{fontenc}

\usepackage{textgreek}
\renewcommand*{\textgreekfontmap}{%
{phv/*/*}{LGR/neohellenic/*/*}%
{*/b/n}{LGR/artemisia/b/n}%
{*/bx/n}{LGR/artemisia/bx/n}%
{*/*/n}{LGR/artemisia/m/n}%
{*/b/it}{LGR/artemisia/b/it}%
{*/bx/it}{LGR/artemisia/bx/it}%
{*/*/it}{LGR/artemisia/m/it}%
{*/b/sl}{LGR/artemisia/b/sl}%
{*/bx/sl}{LGR/artemisia/bx/sl}%
{*/*/sl}{LGR/artemisia/m/sl}%
{*/*/sc}{LGR/artemisia/m/sc}%
{*/*/sco}{LGR/artemisia/m/sco}%
}
\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother
\DeclarePairedDelimiterX{\infdivx}[2]{(}{)}{%
#1\;\delimsize\|\;#2%
}
\newcommand{\infdiv}{D\infdivx}
\DeclarePairedDelimiter{\norm}{\left\lVert}{\right\rVert}
\DeclarePairedDelimiter{\ceil}{\left\lceil}{\right\rceil}
\DeclarePairedDelimiter{\floor}{\left\lfloor}{\right\rfloor}
\def\Z{\mathbb Z}
\def\R{\mathbb R}
\def\C{\mathbb C}
\def\N{\mathbb N}
\def\Q{\mathbb Q}
\def\noi{\noindent}
\onehalfspace
\usemintedstyle{bw}
\author{Sandy Urazayev\thanks{thecsw@Mania.local}}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={Sandy Urazayev},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.0.50 (Org mode 9.4.5)},
 pdflang={English}}
\begin{document}

\section*{Differential Equations Cookbook 🔥}
\label{sec:org68348f2}

May 12th, 2020

\href{./index.pdf}{(PDF Version)}

\subsection*{Abstract}
\label{sec:org9f36d0e}

When I got first introduced to differential equations, I had a love-hate
relationship with it. Mainly due to some back-of-the-book problems we were given
and never-ending projects we were assigned to. After some time, differential
equations is a way to truly understand physics and the foundations of gravity,
fields, and everything. This articles is merely an intro on manually solving
common forms of differential equations. Hope you enjoy

\subsubsection*{Quick notes}
\label{sec:orgb1870c9}

\begin{itemize}
\item \(f_x \iff \partial_x f\)
\item \(A,B,C\) are usually constants
\item \(c_k\) is usually solution's constant that is defined with initial conditions
\item Most of the functions are \(\mathbb{R}^k \to \mathbb{R}\), \(k \in \mathbb{N}^+\)
\item If you found a typo or want to comment, feel free to email me. Email on
top of the page.
\end{itemize}

\subsubsection*{First-order, linear}
\label{sec:org122c719}

Those equations have the form: \(y' + p(t) y = q(t)\)

Find \(\mu(t) = e^{\int p(t) dt}\)

Then \(\frac{d}{dt}(\mu(t)y) = q(t) \mu(t)\)
\(\implies y = \frac{\int q(t) \mu(t) dt}{\mu(t)}\)

\subsubsection*{First-order, separable}
\label{sec:orgd322a73}

Those equations have the form: \(\frac{dy}{dx} = f(x)g(y)\)

Find the solution by solving \(\int \frac{dy}{g(y)} = \int f(x) dx\)

Solve for exact (explicit) values of \(y\)

\subsubsection*{Exact equations}
\label{sec:orgab1ad9e}

They have the form \(M(x,y) + N(x,y) \frac{dy}{dx} = 0\)

\begin{itemize}
\item \((\xi)\) If \(M_y = N_x\)

\(\implies\) Find such \(F(x,y)=C\), where \(F_x = M\), \(F_y = N\)

\item otherwise, make it exact, such that

\(\frac{M_y-N_x}{N}\) only depends on \(x\) \textbf{or} \(\frac{N_x-M_y}{M}\) only
depends on \(y\)

Find \(\mu(x) = e^{\int \frac{M_y-N_x}{N} dx}\) \textbf{or}
\(\mu(y) = e^{\int \frac{N_x-M_y}{M} dy}\), multiply both by \(M\) \textbf{and} \(N\),
so the condition \(M_y = N_y\) is satisfied. Go to \((\xi)\) and proceed with
finding \(F(x,y)\)
\end{itemize}

\subsubsection*{Second-order, linear, constant-coefficient equations}
\label{sec:org57d7f32}

They have the form \(y'' + p y' + q y = f(t)\)

\begin{itemize}
\item First, solve for the homogeneous case, where \(y'' + p y' + q y = 0\)

Make a characteristic polynomial, let \(y = e^{rt}\):

\(r^2+pr+q=0\)

Find roots, solution (general) will be \(y = c_1 e^{r_1 t} + c_2 e^{r_2 t}\)

\textbf{**} if repeated root \(\implies y = c_1 e^{rt} + c_2 t e^{rt}\)

\textbf{**} if \(r = \alpha \pm i \beta\) \(\implies y=c_1 \cos(\beta t)e^{\alpha t} + c_2 \sin(\beta t) e^{\alpha t}\)

\item Solving for particular solution \(y_p(t)\)

\textbf{**} Undetermined coefficients (superpositioned) for \(f(t)\)

Whatever is in \(f(t)\), start adding up the corresponding coefficients into
\(y_p(t)\)

\textbf{\textbf{*}} \(e^{nt} \to Ae^{nt}\)

\textbf{\textbf{*}} \(t^m \to A_m t^m + \ldots + A_1 t + A_0\)

\textbf{\textbf{*}} \(\cos(\beta t)\) or \(\sin(\beta t) \to Acos(\beta t) + B\sin(\beta t)\)

NOTE: should not be linearly dependent with the general solution. If it
is, multiply by \(t\) until it is linearly independent.

\textbf{**} Variation of parameters

Seek \(y_p(t) = v_1(t)y_1(t)+v_2(t)y_2(t)\), where

\(\begin{cases}v_1'y_1+v_2'y_2=0\\v_1'y_1'+v_2'y_2'=f(t)\end{cases}\)
\end{itemize}

So the final solution is \(y(t)=c_1 y_1(t) + c_2 y_2(t) + y_p(t)\)

\subsubsection*{Second-order, linear, variable-coefficient equations}
\label{sec:orge10bbe0}

Equations have the form

\begin{itemize}
\item \((1)\): \(a(t)y'' + b(t)y'+c(t)y = f(t)\)
\item \((2)\): \(y'' + p(t)y'+q(t)y = g(t)\)
\end{itemize}

In general case, guess the first homogeneous solution (try \(y_1=e^t\)) and
use reduction of order to find the second homogeneous solution, so that

\(y_2(t) = v(t)y_1(t)\)

\(\implies y_2'' + p(t)y_2' + q(t)y_2 = 0\)

\(\implies (v(t)y_1(t))''+p(t)(v(t)y_1(t))'+q(t)(v(t)y_1(t))=0\)

NOTE: Also applicable with form \((1)\)

You will probably have another differential equation emerge from above. It
should have lower order than our current equation, so just refer to one of
the techniques above to find \(v(t)\) and then you can find
\(y_2(t)=v(t)y_1(t)\)

Use \textbf{variation of parameters} to find a particular solution. It's that
system with \(v\)

NOTE: What you if you have a \textbf{Cauchy-Euler equation}?

They have the form \(at^2y''+bty'+cy=0\)

then \(y=t^r \implies ar^2+(b-a)r+c=0\)

\begin{itemize}
\item if \(r\) is repeated, \(y_1=t^r\), \(y_2=ln|t|t^r\)
\item if \(r=\alpha\pm i\beta\), \(y_1=t^{\alpha}\cos(\beta ln|t|)\) and
\(y_2=t^{\alpha}\sin(\beta ln|t|)\)
\end{itemize}

Generally, solution has the form \(y=c_1t^{r_1}+c_2t^{r_2}\)

\subsubsection*{Higher-order, linear equations}
\label{sec:org9fadd1a}

\(a_n(t)y^{(n)}+a_{n-1}(t)y^{(n-1)}+\ldots+a_1(t)y'+a_0(t)y=g(t)\)

All second-order methods above extend to \(n^{th}\) order.

\subsubsection*{Laplace transform}
\label{sec:org42331b3}

Laplace is a holy grail of solving differential equations with initial
values defined. Laplace is the same kind of Bible to engineers like Taylor
Series is.

\(\mathcal{L}\{f\}(s) = \int_0^{\infty} e^{-st} f(t) dt\)

assuming \(f\) is piecewise continuous and of exponential order.

Table of common transformations:

\begin{center}
\begin{tabular}{ll}
\(f(t)\) & \(\mathcal{L}\{f\}(s)\)\\
\hline
\(1\) & \(\frac{1}{s}\)\\
\(e^{at}\) & \(\frac{1}{s-a}\)\\
\(\sin(bt)\) & \(\frac{b}{s^2+b^2}\)\\
\(\cos(bt)\) & \(\frac{s}{s^2+b^2}\)\\
\(u(t-a)\) & \(\frac{e^{-as}}{s}\)\\
\(\delta(t-a)\) & \(e^{-as}\)\\
\end{tabular}
\end{center}

Where \(u(t)\) is the \href{https://en.wikipedia.org/wiki/Heaviside\_step\_function}{Heaviside step function} and \(\delta(t)\) is the \href{https://en.wikipedia.org/wiki/Dirac\_delta\_function}{Dirac
delta function}.

Some Laplace transform properties:

\begin{itemize}
\item \(\mathcal{L}\{e^{at}f(t)\}(s) = \mathcal{L}\{f(t)\}(s-a)\)
\item \(\mathcal{L}\{t^nf(t)\}(s) = s^n\mathcal{L}\{f\}(s)-s^{n-1}f(0)-\ldots-sf^{(n-2)}(0)-f^{(n-1)}(0)\)
\item \(\mathcal{L}\{t^nf(t)\}(s) = (-1)^n \frac{d^n}{ds^n} \mathcal{L}\{f(t)\}(s)\)
\end{itemize}

If \(f\) is a T-periodic function,

\(\mathcal{L}\{f(t)\}(s) = \frac{\int_0^T e^{-sT} f(t) dt}{1-e^{-sT}}\)

where \(\int_0^T e^{-sT} f(t) dt = \mathcal{L}\{f_T(t)\}(s)\), the sum of
integrals of different parts of the piecewise function.

Convolutions:

\begin{itemize}
\item \((f*g)(t) = \int_0^t f(t-v)g(v)dv\)
\item \(\mathcal{L}\{(f*g)(t)\} = \mathcal{L}\{f(t)\}(s)\cdot \mathcal{L}\{g(t)\}(s)\)
\item \((f*g)(t) = \mathcal{L}^{-1}\{F\cdot G\}(t)\), where
\(F=\mathcal{L}\{f\}(s)\) and \(G=\mathcal{L}\{g\}(s)\)
\end{itemize}

Heaviside/unit step function:

\begin{itemize}
\item \(\mathcal{L}\{u(t-a)f(t)\}(s) = e^{-as}\mathcal{L}\{f(t+a)\}(s)\)
\item \(\mathcal{L}^{-1}\{e^{-as}F(s)\}(t)=u(t-a)\mathcal{L}^{-1}\{F(s)\}(t-a)\)
\end{itemize}

If IVP is not at 0, define some new function like \(w(t)=y(t+\alpha)\), and
solve for \(w\). Finally, you can offset to find \(y\)

Step (block) function:

\begin{itemize}
\item \(\Pi_{a,b}(t) = u(t-a)-u(t-b)\)
\item \(\mathcal{L}\{\Pi_{a,b}(t)\}(s)=\frac{e^{-sa}-e^{-sb}}{s}\)
\end{itemize}

\subsubsection*{Constant-coefficient, homogeneous systems of ODE}
\label{sec:org1715575}

\(\vec{x}' = A \vec{x}\), where \(A\in\mathbb{R}^{n\times n}\), \(x\in\mathbb{R}^n\)

If \(A\) has n linearly independent eigenvectors \(\vec{u_i}\) associated to n
eigenvalues \(\lambda_i\), then a general solution of the system is given by
\(\vec{x}(t) = c_1 e^{\lambda_1 t}\vec{u_1}+c_2e^{\lambda_2t}\vec{u_2} + \ldots + c_ne^{\lambda_nt}\vec{u_n}\)

\begin{itemize}
\item If \(\lambda=\alpha \pm i \beta\), so \(\vec{u}=\vec{a}+i\vec{b}\), we have
\end{itemize}
\(\vec{x}=c_1e^{\alpha t}(\cos(\beta t)\vec{a}-\sin(\beta t)\vec{b}) + c_2e^{\alpha t}(\cos(\beta t)\vec{b}+\sin(\beta t)\vec{a})\)

\begin{itemize}
\item Matrix exponential
\end{itemize}

\(e^{At} = \sum_{k=0}^{\infty} \frac{A^k t^k}{k!}\), where \(A^0=I\), an
identity matrix.

\begin{itemize}
\item Find solutions for any eigenvalues
\end{itemize}

\begin{itemize}
\item Compute the characteristic polynomial \(p(\lambda)\) of \(A\)
\label{sec:org9452c21}

\(p(\lambda)=det(A-\lambda I)\)

\item Factor \(p(\lambda)\) into linear factors to yield
\label{sec:org8696d39}

\(p(\lambda) = c(\lambda-\lambda_1)^{m_1} \cdot \ldots \cdot (\lambda-\lambda_k)^{m_k}\), where \(c=\pm 1\)

\item For each \(\lambda_j\), find \(m_j\) linearly independent generalized eigenvectors \(\{\vec{u_j}^{m_1},\cdots,\vec{u_j}^{m_j}\}\) satisfying
\label{sec:org8178bbd}

\((A-\lambda_i I)^{m_j} \vec{u} = \vec{0}\)

\item For each \(\vec{u_j}^i\) computed in the previous step, compute \(e^{At}\vec{u_j}^i\) by
\label{sec:orgef0c1a3}

\(e^{At}\vec{u_j}^i\)

\(=e^{\lambda_jt}e^{(A-\lambda_jI)t}\vec{u_j}^i\)

\(=e^{\lambda_jt}(\vec{u_j}^i+t(A-\lambda_jI)\vec{u_j}^i+\cdots+\frac{t^{m_j-1}}{(m_j-1)!}(A-\lambda_jI)^{m_j-1}\vec{u_j}^i)\)
\end{itemize}

\subsubsection*{Linear systems of ODE}
\label{sec:orgf164fc6}

\(\vec{x}' = A(t)\vec{x} + \vec{f}(t)\), where \(A\in\mathbb{R}^{n\times n}\),
\(x\in\mathbb{R}^n\), \(f\in\mathbb{R}^n\)

If \(X(t)\) is a matrix whose columns are made up of n linearly independent
homogeneous solutions (\(X(t)\) is the fundamental matrix), then a general
solution may be written as \(\vec{x}(t_0)=\vec{x_0}\)

\(\vec{x}(t) = X(t)X^{-1}(t_0)\vec{x_0}+X(t)\int_{t_0}^{t}X^{-1}(s)f(s)ds\)

If \(A(t)\) is constant-coefficient, then we recover Duhamel's formula:

\(\vec{x}(t) = e^{A(t-t_0)}x_0 + \int_{t_0}^{t}e^{A(t-s)}f(s)ds\)

\subsubsection*{Applications}
\label{sec:org4ccdfb4}

There are many applications of differential equations in classical
mechanics, fields, etc. Below you will find just a snippet of some very
common Physics 1/2 scenarios

\begin{itemize}
\item Falling object
\label{sec:orga0f31b1}

\(m\frac{dv}{dt}=mg-bv\), where \(b\) is the air resistance

\item Fluid mix, define \(R_{in}\) and \(R_{out}\)
\label{sec:org4bd501f}

\(\frac{dx}{dt}=R_{in}-R_{out}\)

\item Mass-Spring System
\label{sec:orgd309b6d}

\begin{itemize}
\item Vertical spring (direction of gravity)
\label{sec:org6762ecf}

\(my''=-by'-k(L+y)+mg+F_{ext}(t)\), assume \(KL=mg\), where \(b\) is dumping, and \(k\) is stiffness

\item Horizontal spring
\label{sec:org3c38231}

\(my''=-by'-ky+F_{ext}(t)\), where \(b\) is dumping, and \(k\) is stiffness
\end{itemize}
\end{itemize}

\subsubsection*{Conclusion}
\label{sec:orgfb53db8}

This is as much as I can recover from my initial experience with differential
equations. This article is not as much to teach you how to solve them but
provide a quick lookup cheatsheet if needed or glance at different forms that we
can actually solve! There are infinitely many differential equations that we
cannot find an exact solution for!
\end{document}


% Tell Texshop where the project root is
%!TEX root = requirements.tex
%
\subsection{Sorption}  \label{sec:Sorption}

\subsubsection{Overview}

Sorption involves the attachment of dissolved and/or colloidal species to mineral or other solid surfaces.  Sorption has the effect of slowing the effective transport rate of a species through porous media through its retardation effect.  The retardation effect for a species, $R_f$, is given by~\citep{bouwer-1991}
\begin{equation}
  R_f = \frac{V_{gw}}{V_{sp}},
\end{equation}
where
$V_{gw}$ is the velocity of the groundwater and $V_{sp}$ is the velocity of the species.
A variety of models have been used to describe sorption and can be broadly divided
into those that describe it as a bulk process versus those that are mineral or solid phase specific.
The latter approach involves the calculation of  bulk sorption from the sum of sorption on individual solid phases,
an assumption referred to as \textit{Component Additivity}.
Within the class of bulk sorption models, a distinction can be made between those
which assume a finite number of sorption sites (these are referred to as showing
Langmuir type behavior and include the Langmuir isotherm itself and most surface complexation
and ion exchange models) and those that assume either an infinite sorption capacity
or at least a capacity that is not tightly constrained
(these include the linear distribution coefficient and the nonlinear Freundlich isotherm).
Alternatively, one could also distinguish between single component,
non-competitive models (e.g., Langmuir and Freundlich) and multicomponent competitive models (surface complexation and ion exchange).

Another possible distinction is between equilibrium and kinetic sorption models.
In many cases, the formulations for the equilibrium and kinetic cases differ
only insofar as the kinetic case involves involves a thermodynamic driving force
(as in the equilibrium case), but modified by a finite rate constant.
In some cases, however, sorption is described as irreversible, which implies that there is no back reaction (desorption).

%%\paragraph{Assumptions, Approximations, Applicability.}

\subsubsection{Process Model Equations}

\paragraph{Linear Distribution Coefficients ($K_d$).}
A simple approach to describe metal or ionic radionuclide sorption by a sediment,
\begin{equation}
  A_{aq}\leftrightharpoons A_{ads},
\end{equation}
is to use a constant distribution coefficient, defined by:
\begin{equation} \label{eq:Kd}
  K_d = \frac{[A_{ads}]}{[A_{aq}]},
\end{equation}
where $K_d$ is the distribution coefficient (L/kg), $[A_{ads}]$ is the sorbed concentration (mol/kg) to the bulk solid phase,
and $[A_{aq}]$ is the total dissolved concentration in groundwater (mol/L) \citep{davis-1990}.
One of the key advantages of representing sorption with a distribution coefficient is that it can be easily incorporated
into reactive transport models used for migration predictions.

Equation \eqref{eq:Kd} shows that if one assumes that the amount of sorption is proportional to the dissolved concentration,
then there is a linear relationship where the $K_d$ value is the slope.
In this simple case, referred to as a linear isotherm, retardation of a concentration  front in simple porous media is given by
%
\begin{equation}  \label{eq:KdRetardation}
  \frac{\bar{v}}{\bar{v}_{c} }
  =
  1+\frac{\rho _{b} }{n} K_{d}  ,
\end{equation}
where $\rho_b$ is the bulk density,
$n$ the porosity,
$\bar{v}$ the average linear velocity of the groundwater and
$\bar{v}_{c} $ the velocity of the point on the concentration profile
where the concentration is half that of the input concentration \citep{freeze-1979}.
Note that the ratio $\bar{v}$/$\bar{v}_{c} $ here is the retardation factor and represents
the retardation of the movement of front relative to the flowing groundwater.
While this is a simplified example, it serves to illustrate the key point that the $K_d$ value
directly influences predictions of adsorbing metal or radionuclide mobility.

%Could have the following
\subparagraph{Assumptions and Applicability}
Sorption is proportional to the dissolved concentration.
The aqueous and adsorbed phases are in equilibrium.

\subparagraph{Data Needs}
Typically $K_d$ values are determined for a particular subsurface material
from the slope of a fitted line to the concentration of the sorbed species, $A_{ads}$,
plotted versus the dissolved concentration of the same species, $A_{aq}$.
These data may be derived from laboratory analyses, where one typically varies the dissolved concentrations systematically,
or they may be derived from in situ field data.
Since $K_d$ values may be variable, and in particular a function of temperature,
pH or the redox state of the system (see below), it is often necessary to compile them in a lookup table for use by a particular computer code.

\paragraph{Langmuir Isotherm.}
The Langmuir isotherm assumes that the sorption sites, S, on the surface of a solid (absorbent) become occupied
by an absorbate from the solution, A.  Implying a 1:1 stoichiometry
%
\begin{equation}  \label{eq:langmuir}
  S + A \leftrightharpoons SA,
\end{equation}
where $SA$ is the adsorbed species on the surface. At equilibrium, a
standard mass action equation can be written:
%
\begin{equation}  \label{eq:langmuir_massaction}
  K_{ads,L} = \frac{ [ SA ] }{ [S] \{A\} },
\end{equation}
where the square brackets here refer to the concentration of the
species or site, and the curly brackets refer to the aqueous
activity.  Using the maximum concentration of surface sites, $S_T$
%
\begin{equation}  \label{eq:totalsurfacesites}
  [S_T] = [S] + [SA],
\end{equation}
one can write the Langmuir isotherm in its familiar hyperbolic form
%
\begin{equation}  \label{eq:hyperbolicLangmuir}
  [SA] = [S_T] \frac{ K_{ads} \{A\} }{1 + K_{ads} \{A\} }.
\end{equation}

%Could have the following
\subparagraph{Assumptions}
For the following, it is assumed that the surface and aqueous species are in equilibrium.

\subparagraph{Data Needs}
The equilbrium constant, $K_{ads}$, is typically obtained from experimental data.
It depends on the specified absorbent and absorbate, and may be a function of temperature.
It may be calculated from:
\begin{equation}
  K_{ads} = \exp\left(\frac{-\Delta G^{\circ}_{ads}}{RT}\right),
\end{equation}
where $\Delta G$ is the change in free energy for the reaction,
typically obtained from a database, $R$ is the gas constant and $T$ is the temperature.

\paragraph{Freundlich.}
The Freundlich isotherm is another equilibrium model for sorption of absorabte A onto sorption sites, S
\begin{equation}  \label{eq:langmuir_reaction}
  S + A \leftrightharpoons SA.
\end{equation}
Represented by the mass action equation:
\begin{equation}
  K_{ads,F} = \frac{ [ SA ] }{ \{A\}^{\beta_F} },
\end{equation}
where the square brackets again refer to the concentration of the
species or site, the curly brackets refer to the aqueous activity.
$K_{ads,F}$ and ${\beta_F}$ are the Freundlich parameters
\citep[e.g.][]{langmuir-1997, stumm-1992}.

%Could have the following
\subparagraph{Assumptions}
For the following, it is assumed that the surface and aqueous species are in equilibrium.

\subparagraph{Data Needs}
The Freundlich parameters, $K_{ads,F}$ and $n$, are generally obtained by fits to experimental data
for a specific surface and aqueous species.
They will generally be obtained from a database, and
may be represented by a functional form or lookup table.

%Steefel
\paragraph{Multi-site, Multi-component Ion Exchange.}
An ion exchange reaction can be described via a mass action expression
with an associated equilibrium constant \citep{vanselow-1932,sposito-1981,appelo-1993}.
The exchange reaction can be written in generic form as
\begin{equation}
  vACl_{u} (aq)+uBX_{\nu } (s)\leftrightharpoons uBCl_{\nu } (aq)+vAX_{u} (s),
\end{equation}
where X refers to the exchange site occupied by the cations $A^{u+}$ and $B^{v+}$.
The equilibrium constant, $K_{eq}$, for this reaction can be written as \citep{vanselow-1932}
\begin{equation}
  K_{eq} =\frac{\{BCl_{\nu } \}^{u} \{AX_{u}\}^{\nu } }{\{ACl_{u} \}^{\nu } \{BX_{\nu } \}^{u} },
\end{equation}
where the curly braces refer to the thermodynamic activities.
Several activity conventions are in wide use.  One possibility is the
Gaines-Thomas activity convention, which assumes a reaction
stoichiometry of the following form \citep{appelo-1993}, written
here assuming the $Cs^+$ is the relevant cation of interest
\begin{equation}
  Cs^{+} + (1/z) MX(i)_{z} \leftrightharpoons CsX(i) + (1/z) M^{z+},
\end{equation}
where $M$ is the competing cation ($Na^+$, $K^+$, $Ca^{++}$), $z$ is
its charge, and $X(i)$ refers to the $i^{th}$ type of exchange site.
In the Gaines-Thomas convention, each exchange site, $X(i)$ has a charge of -1.
The activities of adsorbed species correspond to the charge equivalent fractions, $\beta (i)_{M} $,
\begin{equation}
  \beta (i)_{M} =\frac{z_{M} q(i)_{M} }{\sum _{M}z_{M} q(i)_{M}  } = \{X(i)_{M} \},
\end{equation}
where $z_M$ is the charge of cation $M$, $q(i)_M$ is the concentration
of adsorbed cation $M$ in exchange site \textit{i} (moles/g),
and the curly brackets denote activities.
The Gapon activity convention is obtained by writing the reactions in every case with a
single exchanger \citep{appelo-1993}.
Alternatively, the Vanselow convention \citep{vanselow-1932} describes the exchanger activity
with mole fractions
\begin{equation}
  \beta (i)_{M} =\frac{q(i)_{M} }{\sum _{M}q(i)_{M}  } = \{ X(i)_{M} \}.
\end{equation}
The exchange reactions can then be used to write a mass action equation for binary Cs-M exchange:
\begin{eqnarray}
  K_{M/Cs} & = & \frac{\beta (i)_{M} ^{1/z} \{Cs^{+} \}}{\beta (i)_{Cs} \{M^{z+} \}^{1/z} } \\
  & = &
\frac{\{X(i)_{M} \}^{1/z} \{Cs^{+} \}}{\{X(i)_{Cs} \}\{M^{z+} \}^{1/z} } .
\end{eqnarray}

In a single-site ion exchange model, the CEC is equal to the sum of the charge equivalent concentrations of the adsorbed cations:
\begin{equation}
  CEC = \sum _{M}z_{M} q_{M},
\end{equation}
while in a multi-site model, the CEC is the charge summed over all of the cation exchange sites \citep{cernik-1996, voegelin-2000}
\begin{equation}
  CEC = \sum _{i}\sum _{M}z_{M} q(i)_{M}   .
\end{equation}

%Could have the following
\subparagraph{Assumptions}
For the following, it is assumed that the surface and aqueous species are in equilibrium.
%\todo{Need short description here}

%%\subparagraph{Data Needs}
%%\todo{Need short description here}

\paragraph{Surface Complexation.}
\label{sec:surfaceComplexation} An alternative approach that allows a modeler to describe sorption while
simultaneously considering variable chemical conditions in the subsurface is a surface complexation model \citep{davis-2004}.
In this approach, the sorbing sediment surfaces are considered to possess
surface functional groups that can form complexes analogous to the
formation of aqueous complexes in solution.
These surface reactions include proton exchange, cation binding and anion binding via ligand
exchange at surface hydroxil sites (represented here as $XOH$ to avoid
confusion with other chemical species).
For example, the sorption of a metal could be represented as
%
\begin{equation} \label{eq:metalSorption}
  XOH + M^{z_+} \leftrightharpoons XOM^{z_+ - 1} + H^{+}  .
\end{equation}

At equilibrium, the sorption reactions can be described by the mass law equation
%
\begin{equation} \label{eq:sorptionMassAction}
  K_{app} =\frac{\left[XOM^{z_{+}-1 } \right]\{H^{+} \} }{\left[XOH\right] \{M^{z+} \} }  ,
\end{equation}
where $K_{app}$ is referred to as the apparent equilibrium constant,
because it includes surface charge effects and hence is dependent on
the extent of surface ionization \citep{dzombak-1990}, $\{i\}$
is the thermodynamic activity of aqueous species $i$, and the terms in
square brackets represent the concentration of surface complexes (mol/kg).

Surface complexation differs from the simpler isotherm and
ion-exchange models in several important ways. Surface complexation is
based on the electrical double layer (EDL) theory. EDL theory assumes
that the surface charge of a sorbent in contact with solution
generates an electrostatic potential that declines rapidly away from
the sorbent surface, creating an electrostatic field. An additional
energetic term accounting for the work needed for the aqueous species
to travel across the surface electric field is required:
%
\begin{eqnarray} \label{eq:EDLdeltaG}
\Delta G_{ads} & = & \Delta G_{intr} + \Delta G_{coul} \nonumber \\
               & = & \Delta G_{intr} + (\Delta G_{\psi =0} - \Delta G_{\psi =\psi_{0} } ) \nonumber \\
               & = & \Delta G_{intr} - z F \psi_{0}  .
\end{eqnarray}
\noindent where $\Delta G_{ads} $ is the free energy change of the overall adsorption reaction, $\Delta G_{intr} $ and $\Delta G_{coul} $ are the free energy change due to chemical bonding and to the electrostatic work (Coulombic attraction), respectively, $z$ is the charge of the surface species, $F$ the Faraday's constant (96485 C/mol), and $\psi _{0} $ is the mean surface potential ($V$). Since
%
\begin{equation} \label{eq:deltaGKeq}
  \Delta G = -RTlnK,
\end{equation}
%
\noindent Equation~\eqref{eq:EDLdeltaG} can be rewritten as
\begin{equation} \label{eq:KappEDL}
K_{app}  = K_{int} \exp\left({\frac{z F \psi _{0} }{RT} } \right),
\end{equation}
where $R$ is the gas constant (8.314 J/mol/K), $T$ is the absolute
temperature (K), and $K_{int}$ is the intrinsic equilibrium constant
which does not depend on the surface charge.

%Could have the following
%\subparagraph{Assumptions}
%\subparagraph{Issues Associated with the Application of Surface Complexation}

\subparagraph{Bulk and Mineral Specific Surface Complexation. }

There are two major approaches for applying the surface complexation
concept to soils and sediments: the Component Additivity (CA) and
Generalized Composite (GC) approaches \citep{davis-2004, davis-1998}. In
the CA approach, it is assumed that a mineral assemblage is composed
of a mixture of one or more reference phases, whose surface chemical
reactions are known from independent studies of each phase \citep[e.g.][]{
landry-2009, davis-2004, arnold-2001}. Based
on a measurement of the relative amounts or surface areas of each
mineral present in the soil or sediment, sorption by the mixture of
phases can be predicted by an equilibrium calculation, without any
fitting of experimental data for the mixture. CA model predictions are
sometimes made by assuming that one mineral component dominates
sorption \citep{zhang-2009, davis-2004, payne-2004, barnett-2002},
% Barnett et al., 2002
allowing a straightforward equilibrium calculation, if the exposed
surface area of that mineral component in the soil or sediment can be
quantified.

In the GC approach, the surface of the mineral assemblage is
considered too complex to be quantified in terms of the contributions
of individual phases to sorption and/or that the contribution of
individual components is not additive. The complexity is caused, in
part, by the difficulties in quantifying the electrical field and
proportions of surface functional groups at the mineral-water
interface in the mixture of mineral phases and associated surface
coatings. In the GC approach, it is assumed that sorption can be
described by mass laws written with ``generic'' surface functional
groups, with the stoichiometry and formation constants for each mass
law determined by fitting experimental data for the mineral assemblage
as a whole \citep{hyun-2009, bond-2008, davis-2004}.
%Bond et al., 2008;
The GC modeling approach has generally been applied using a
non-electrostatic model (NEM), which considers surface equilibria
strictly as chemical reactions without explicit correction for
electrostatic attraction or repulsion \citep{yabusaki-2008,
  davis-2004, kent-2000}. In an NEM, the apparent binding constants
and stoichiometry of the mass action equations are derived by fitting
the \textit{macroscopic} dependence of adsorption as a function of pH
\citep{davis-1998}. Because of the exclusion of electrical double
layer terms, the mass action equations are not expected to provide
accurate representations of the stoichiometry of the reactions
\textit{at the molecular scale}, however, the surface reactions can
still be coupled with aqueous complexation reactions to provide
simulations of macroscopic sorption as a function of aqueous chemical
conditions.

Although there are differences between the GC and CA approaches,
they are very similar with respect to their scientific basis.
The following concepts form the basic tenets of both GC and CA modelling approaches \citep{davis-1998}:

\begin{enumerate}
\item Mineral surfaces are composed of chemical functional groups that can react with dissolved solutes to form surface complexes (coordinated
%~\todo{coordinated? - Williamson}
complexes or ion pairs) in a manner analogous to aqueous complexation reactions in homogeneous solutions.

\item The equilibria of surface complexation and ionization reactions can be described via mass law equations, either with or without correction factors applied for electrostatic attraction to or repulsion from the surface.

\item The apparent binding constants determined for the mass law equations of surface complexation and ionization reactions are semi-empirical parameters related to thermodynamic constants via rational activity coefficients for surface species.
\end{enumerate}

Both CA and GC models may:
\begin{enumerate}
\item be coupled to the same critically reviewed aqueous thermodynamic data
\item use spectroscopic data to constrain and/or determine surface complex chemical composition and stoichiometry, and
\item use the same mass laws and surface species.
\end{enumerate}
The differences among the model approaches lie primarily in the manner in which the models are calibrated and assumptions about various model parameters (in particular, whether the contributions of the various mineral phases to sorption and electrostatic fields can be considered as additive). CA models have almost always been applied using mass laws with electrostatic correction factors, while GC models have not usually used these factors.

\subparagraph{Experimental and Modeling Issues Associated with SCMs for Soils and Sediments.}

Common to all applications of surface complexation approaches in soils
and sediments is an initial characterization with respect to surface
area, bulk mineralogy, and clay and organic carbon content.  In
addition, if the sediment is already contaminated with a metal or
radionuclide, a measurement of the labile fraction of the contaminant
needs to be determined \citep{kohler-2004, curtis-2004, bond-2008}.

In the GC approach, laboratory experiments are conducted with the field site sediments across the range of chemical conditions that are relevant to the scenarios of the physical and temporal modeling domains. Then, mass law relationships are derived that describe the change in metal or radionuclide sorption with variations in the aqueous chemical conditions \citep{davis-2004}.  Total surface functional groups are typically estimated from surface area measurements.  The number of surface site types and surface binding reactions is a practical modeling decision made based on the goodness-of-fit and the desired number of modeling parameters \citep{hyun-2009}.

In the CA modeling approach, after the sediment mineralogy is known, an estimate of the distribution of mineral surface areas is made.  This can be done by simply assuming that the bulk weight abundances of various mineral phases are related to the distribution of functional groups at the sediment surface.  For example, if quartz represents 60\% by weight of the sediment, then an initial estimate could be that 60\% of the surface area is represented by the quartz surfaces. Then a model of metal or radionuclide adsorption on quartz (as a function of chemical conditions relevant to the field site) is chosen from available literature data.  Similar models for other minerals in the sediment are also catalogued.  In some cases, model parameters may need to be re-derived from the original experimental data to develop a dataset that is self-consistent. In particular, this may be necessary if different electrical double layer models were used in the reference mineral models.  Other approaches for estimating the distribution of mineral surface areas may be used, including chemical extractions and other methods \citep{davis-2004, davis-1998}.  Once the component mineral models have been chosen, a predictive calculation of metal or radionuclide sorption for a specific set of chemical conditions can be made.

Possible limitations inherent to the surface complexation approach include poor representation of: a) surface functional groups, b) surface area, c) electrical double layer properties, d) surface species, e) surface binding constants, and f) competing surface reactions and their electrostatic effects. These limitations exist for both GC and CA modeling approaches, but the GC approach attempts to resolve some of the issues by using empirical data to overcome unknown factors and unmeasured parameters. For example, consider the representation of surface functional groups: Assume that only silanol, aluminol, ferrinol, and clay mineral edge sites are of importance in a particular sediment sample. At present it is very difficult or expensive to determine the distribution of mineral surface areas in a mixed mineral assemblage. Extractions, X-ray diffraction, and surface spectroscopies have been used by various investigators, but each of these methods provides estimates that are difficult to confirm independently. This uncertainty is circumvented in the GC approach by assuming that the distribution of site types is an unknowable quantity, and only generic sites are used. However, this requires that experimental data for the metal or radionuclide sorption on a site-specific sediment sample are collected, whereas in principle at least, additional characterization experiments are not needed for the CA modeling approach.

\subparagraph{Quantifying Surface Sites}

Surface area is an important experimental quantity to be characterized in all surface complexation approaches. Typically a mixed mineral assemblage is characterized by BET analysis of nitrogen gas adsorption. Adjustments may need to be made for samples that contain high abundances of clay minerals, depending on whether there is evidence of sorption on the basal planes of clay mineral particles. Many investigators have concluded that surface functional groups of the basal planes are unreactive for metal and radionuclide sorption, and therefore the surface area of the basal planes does not need to be included in most applications. Fortunately, the BET method does not typically measure the surface area of the basal planes. In GC applications, the surface area is typically used in a straightforward manner to quantify the total abundance of surface sites using a conversion factor. In CA applications, however, the surface area should be distributed among different functional group types.

Multiple site-types are commonly used in formulating SCMs and approximate the nonlinear isotherms commonly observed for cation adsorption on well-characterized oxide mineral phases \citep{dzombak-1990, davis-1990}.  Postulating multiple site-types is also important for simulating peak tailing observed in experimental studies of U(VI) transport in columns \citep{kohler-1996}.  Reactive transport simulations that use multisite adsorption models can also simulate significant peak tailing in field-scale simulations \citep{curtis-2006, kent-2000, kent-2007, kent-2008}.

%\subparagraph{Data Needs}

\subparagraph{Sub-models}

\begin{enumerate}
\item \textbf{Non-electrostatic Models:}
EDL models differ in whether coulombic attraction or repulsion terms are considered in the mass laws of surface reactions. A non-electrostatic EDL means that the term
\begin{equation}  \label{eq:ElectrostaticExponential}
  \exp\left(\frac{zF\psi_0 }{RT}\right)
\end{equation}
in Equation~\eqref{eq:KappEDL} need not be considered.
While electrical double layer (EDL) models may represent these terms well for simple systems with single mineral phases,
the approaches for treating these terms in mixed mineral assemblages have not been studied.
In Component Additivity (CA) \citep{davis-2004, davis-1998} applications to sediments,
typically authors assume that the EDL properties of pure, clean mineral phases
investigated remain the same in mixed mineral assemblages \citep{davis-2004}.
This ignores the likely effects of surface contaminants (adsorbed major solutes such as silicate, organic compounds, etc.)
and the overlapping EDL regions of particles that are known to change coulombic terms.
In Generalized Composite (GC) approaches \citep{davis-2004, davis-1998},
the coulombic attraction or repulsion terms are not included,
but are instead built into the semi-empirical model calibration of reaction stoichiometries and binding constants to experimental data.
That is, whatever EDL forces exist, they are lumped into the model fitting of reactions and binding constants.
In each case, there is inherent uncertainty in the modeling approach.
The errors within the GC model may not be that significant because of model calibration to experimental data,
but the error is only minimized by confining model calculations to chemical conditions interpolated between those
investigated in laboratory experiments.
Extrapolation of any non-electrostatic model to uninvestigated chemical conditions is unwise
because the EDL forces for those conditions will not necessarily be captured accurately by the model calibration.
In addition the formation of unknown surface species may not be realized if calculations are extrapolated to chemical conditions
not investigated at all.

\item \textbf{Electrostatic models:}
When the coulombic attraction or repulsion terms is considered as shown in Equation~\eqref{eq:KappEDL},
the electrostatic models differs also among themselves in how they conceptualize the structure of the double-layer
and describe changes in surface potential and surface charge from the surface of the sorbent phase to the bulk solution.
In the constant capacity and diffuse-layer models, all adsorbed species are considered specifically adsorbed at the zero plane
while the triple layer model can assign adsorbed species to either a zero plane or more distant plane.
The constant capacity and diffuse-layer model are elaborated in the following sections.

\begin{enumerate}
\item \textbf{Constant Capacitance} The constant capacitance model is
  a special case of the diffuse-layer model. Both models are based on
  the assumption that all the species are adsorbed in the same layer
  and a diffuse layer of counterions constitutes the transition to
  homogenous solution.  Additionally, it is assumed that the surface
  potentials are small, or the double layer has been compressed (very
  high ionic strength).  However, differently from the diffuse-layer
  model, the relationship between the surface charge and the potential
  is assumed to be linear:
%
\begin{equation} \label{eq:constantCapacitance}
  \sigma  = \mathbb{C}\psi  ,
\end{equation}
where $\sigma$ is the surface charge, $C\;m^{-2}$, $\psi$ is the
potential at the surface, $V$, and $\mathbb{C}$ is a constant
capacitance value, $C\;V^{-1}\;m^{-2}$, to be obtained from fitting
experimental data.  Equation~\eqref{eq:constantCapacitance} is solved
for the potential and substituted into Equation~\eqref{eq:KappEDL}.

\item \textbf{Diffuse Double Layer Model}
The diffuse layer model has been described in great detail by \citet{dzombak-1990} and was applied to adsorption of metals on iron oxide surfaces. In the diffuse layer model, the solid-water interface is composed of two layers: a layer of surface-bound complexes and a diffuse layer of counter ions in solution. The surface charge is calculated from the total surface species adsorbed on the layer:
\begin{equation} \label{eq:doubleLayerSurfaceChargeDensity}
  \sigma _{p} = \frac{F}{A} \sum _{k=1}^{N_{s} } z_{k} y_{k}  .
\end{equation}
Here $A$ is the surface area sorbent per liter solution ($m^2/L$), $F$ is the
Faraday constant ($96,480 C/mol$), $z_k$ is the charge of the ion, and $y_k$ is the concentration (mol/L) of surface bound ions in the Stern Layer.  According to the Gouy-Chapman theory, the surface charge density $\sigma_{p}$ ($C/m^2$) is related to the potential at the surface (volts) by:
\begin{equation} \label{eq:doubleLayerGouyChapman}
  \sigma _{p}  =  (8RT\epsilon_R \ \epsilon _{0}\; C_e \times 10^{3} )^{1/2} \sinh \left(\frac{zF \psi _0 }{ 2RT}\right)  ,
\end{equation}
where $R$ is the molar gas constant (8.314 $J mol^{-1} K^{-1}$), $C_e$
is the molar electrolyte concentration ($M$), $z$ is the electrolyte
charge, $T$ is the absolute temperature ($K$), $\epsilon_R$ is the
relative dielectric constant of water ($\epsilon = 78.5$ at
$25^{\circ}C$), and $\epsilon_{0}$ is the permittivity of free space
($8.854\times 10^{-12}$ $C\;V^{-1} m^{-1}$).  Equation
\eqref{eq:doubleLayerGouyChapman} is only valid for a symmetrical
electrolyte, the anion and cation must have the same charge.  Note
that $C$ the unit (coulombs or celcius) is not a concentration.
Capacitance is not solved for explicitly, but is implicitly accounted
for in Equation~\eqref{eq:doubleLayerGouyChapman}.  It is common to use
the linearized version of Equation \eqref{eq:doubleLayerGouyChapman} for
low values of the potential:
%
\begin{equation} \label{eq:doubleLayerLinearizedGouyChapman}
  \sigma _{p} =\epsilon \ \epsilon _{0} \kappa \psi _{0},
\end{equation}
where $1/\kappa$ (m) is the double-layer thickness defined as
%
\begin{equation} \label{eq:doubleLayerThickness}
  \frac{1}{\kappa }  = \left( \frac{\epsilon \ \epsilon_{0} RT}{2 F^{2} \cdot 1000 \;I} \right)^{1/2}  ,
\end{equation}
where $I$ is the ionic strength $mol\;L^{-1}$. The first term of Equation~\eqref{eq:doubleLayerGouyChapman}, $(8RT\epsilon \ \epsilon _{0} C_e \times 10^{3} )^{1/2}$, can be rewritten at $25^{\circ}C$:
%
\begin{equation} \label{eq:doubleLayerGouyChapman25}
  \mathop{\sigma }\nolimits_{p} = 0.1174\;C_e^{1/2} \sinh \left( \frac{zF \psi_d}{2RT} \right) .
\end{equation}
Therefore, in the diffuse-layer model, the value of the capacitance $\mathbb{C}$ relating the surface charge and the potential can be calculated based on theoretical considerations instead of being an experimental fitting parameter.

\item \textbf{Triple Layer Model}
The triple layer model is similar to the double layer model, but
divides the sorbed species into two layers,
Figure~\ref{fig:triple-layer-model}. Strongly sorbed species are
located close to the surface, the zero plane, while weakly sorbed
species reside in the beta plane, seperated from the surface by the
strongly sorbed species and hydration layers \citep[e.g.][]{langmuir-1997}. Further out from the surface is a diffuse layer and the bulk solution similar to the double
layer.

The charge balance equation for the triple layer model is
\begin{equation}
  \sigma_0 + \sigma_{\beta} + \sigma_d = 0,
\end{equation}
\noindent where $\sigma_0$, $\sigma_{\beta}$ and $\sigma_d$ are the
net surface charges in the zero, beta and diffuse planes respectively, ($C/m^2$).
The net surface charge in the zero plane is given by:
\begin{equation}    \label{eq:ChargeDensityZeroPlane}
  \sigma_0 =  \frac{F}{A} \sum _{k=1}^{N_{s} } z_{k} y^{0}_{k}  ,
\end{equation}
where the variables are as defined in Equation \eqref{eq:doubleLayerSurfaceChargeDensity} with the exception of $y^{0}_{k}$, which is the concentration (mol/L) bound in the zero plane. Similarly, the net surface charge of the beta plane is
\begin{equation}    \label{eq:ChargeDensityBetaPlane}
  \sigma_{\beta} =  \frac{F}{A} \sum _{k=1}^{N_{s} } z_{k} y^{\beta}_{k}  ,
\end{equation}
where $y^{\beta}_{k}$ refers to the ions bound in the beta plane.
Note that the composition of the diffuse layer is not often calculated explicitly in either Triple Layer Model or the Diffuse Double Layer Model, although a method to do so has been presented by \citet{leroy2007modeling}.

The triple layer model assumes constant capacitances between the zero
plane and beta plane, $\mathbb{C}_1$, and the beta plane and d-plane, $\mathbb{C}_2$.
These are related to the surface charges and potentials by:
%\todo{Two captions here and it is not referenced. -Williamson}
\begin{eqnarray}
  \sigma_0 &=& \mathbb{C}_1 \left( \psi_0 - \psi_{\beta} \right), \\
  \sigma_{\beta} &=& \mathbb{C}_1 \left( \psi_{\beta} - \psi_0 \right) + \mathbb{C}_2 \left( \psi_{\beta} - \psi_d \right),\\
  \sigma_{d} &=& \hspace{28mm}\mathbb{C}_2 \left( \psi_d - \psi_{\beta} \right).
\end{eqnarray}

\begin{figure}
  \begin{center}
    \includegraphics[width=0.75\linewidth]
                    {figs/sorption/goncalves-2007-TLM-Fig-a.pdf}
    \\[24pt]
    \includegraphics[width=0.75\linewidth]
                    {figs/sorption/goncalves-2007-TLM-Fig-b.pdf}
  \end{center}
  \caption{Schematic of the TLM model from \citet{goncalves-2007}.}
  \label{fig:triple-layer-model}
\end{figure}

\end{enumerate}
%  End electrostatic models

\end{enumerate}
%  End sub-models

\subsubsection{Common Data Needs for Sorption Models}
%\todo{Need to gather more requirements for this}

All sorption models will require access to a database of parameter values that are potentially independent of the specific contaminated site under consideration.  For example, the cation exchange capacity (CEC) of a mineral like smectite or kaolinite can be described with a range of values.  However, it is likely that site-specific experimental data will have to
be collected and either collected in a site-specific database, or serve as the basis of a site-specific lookup table.

%\todo{Davis: A database for Freundlich parameters is required? Is
% there such a database? In general, I think the model requirements
% for Kd, Langmuir, Freundlich, surface complexation and ion exchange
%  should state that it is likeley that experimental data have to be
%  collected with site-specific samples.}


\section{Optimizations}

Inferred type information can be used to remove runtime type
discrimination (typeq instructions, as described in \refSec{sec-hir})
from the code. If the type of an input to typeq is inferred, it could
be that only one branch of the typeq is ever taken, and thus the typeq
itself can be removed.

For example, consider the code:

\begin{verbatim}
(if (consp item)
    (let ((key (car item))
          (value (cdr item)))
      ...)
    ...)
\end{verbatim}

This would initially appear in HIR as \refFig{fig-unoptimized},
with the \texttt{consp} being inlined as a \texttt{typeq}. The
\texttt{car} and \texttt{cdr} calls involve two additional typeqs
each, though they are obviously redundant.

\begin{figure}
\begin{center}
\inputfig{fig-unoptimized-hir.pdf_t}
\end{center}
\caption{\label{fig-unoptimized}
Unoptimized HIR fragment.}
\end{figure}

The inferencer can determine from the earliest typeq that ``item'' must
be a cons in the first if branch. The other four typeqs can then be
eliminated, resulting in the HIR in \refFig{fig-optimized}. The
semantics of the original program are preserved, but it is implemented
without unnecessary branching.

\begin{figure}
\begin{center}
\inputfig{fig-optimized-hir.pdf_t}
\end{center}
\caption{\label{fig-optimized}
HIR fragment after removing redundant typeqs.}
\end{figure}

In the Clasp implementation of Common Lisp, this optimization has been
tested to reduce the execution time (over ten million iterations) of a
Fibonacci function by ~56\%.


\documentclass{article}

\usepackage[final]{styles}
\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{csquotes}
\usepackage{float}
\title{Unifying Inference in Ergodic World Models}
% \title{Unifying Inference in Ergodic Latent Space}
% \title{Free-energy Dynamics in Ergodic Domains}
% \title{Fast \& Slow Inference in Ergodic Domains}
% The \author macro works with any number of authors. There are two commands
% used to separate the names and addresses of multiple authors: \And and \AND.
%
% Using \And between authors leaves it to LaTeX to determine where to break the
% lines. Using \AND forces a line break at that point. So, if LaTeX puts 3 of 4
% authors names on the first line, and the last on the second line, try using
% \AND instead of \And before the third author name.

\author{%
  Luke J. Pereira \\
%   \texttt{lukejoepereira@gmail.com} \\
  % examples of more authors
  % \And
  % Coauthor \\
  % Affiliation \\ consciousness
  % Address \\
  % \texttt{email} \\
  % \AND
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
  % \And
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
  % \And
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
}

\begin{document}
\maketitle
% \vspace{-0.5cm}

\begin{abstract}

Fast and slow inference are presented as trajectories existing on a high-dimensional spectrum with their extrema bounding surfaces of a latent phase space. On one end, an Autoencoder is trained on sensory observations and impresses latent encodings onto a Discriminator-like energy manifold. On the other end, the latent space is delimited by repeating the training process on sparse and low-dimensional symbolic abstractions, producing an upper bounding manifold. An agent's trajectory corresponds to a flow of a symbolic dynamical system that minimizes the KL divergence between the upper and lower boundaries, which minimizes the free-energy of the system and can be learned from a series of initial value random walks.

% (Equivilently complex PDEs trajectories have dimensions reduced using ordinary differential representation of neural networks.)
Birkhoff's theorem states that in an ergodic domain, the average of a function $f$ over an infinite number of flows converges to the continuous integral of the function over the phase space. Defining $f$ to be a function that minimizes the joint divergence of the boundaries of a world model will produce a measure preserving ergodic flow. Moreover, we can show that the time average can be derived from inference using standard Bayesian expected loss while the space average can be derived from energy-based inference methods. This reveals that the two methods of inference become pointwise unified in ergodic domains, like language generation. This in turn explains the effectiveness of modern deep learning methods that, despite being restricted to surface level System 1 thinking, are able to imitate System 2 thinking. It is possible for a model to learn how to construct and maintain a minimal ergodic latent space even when sampling from sparse non-ergodic observations. This pointwise equivalence enables an agent to fluidly switch between systems of thinking and improve inference performance.

% known as a time average, known as its space average
% The  implications of an analogous process potentially occurring in biological neural configurations are examined.
%\textbf{}
% Free energy minimization unifies fast and slow thinking in ergodic latent spaces.
%  where the time average of infinitely many trajectories approach an equivalence to an integral of a space average
%  correspond to inference that has been perfected by SOTA machine learning models vs. dialogue response needed to pass a sufficient Turing test,

% If we think of the measure as being an associative memory dimension, we find that the time average of

% Internal modesls correspond to intermideary state between humans and the world
\end{abstract}

\section{Systems of Thinking}

In \textit{Thinking, Fast and Slow} (Kahneman, 2011), cognitive processes are categorized into two modes. In System 1, thought processes are fast, intuitive, unconscious, and habitual. In System 2, thought processes become slower, logical, conscious, algorithmic, and may involve planning or reasoning. It can be claimed that the system of fast thought closely corresponds to many state-of-the-art deep learning approaches (Bengio, 2019). These methods aim to discover underlying distributions of data in the form of a normalized probability density by training on many examples while minimizing a cost function. In doing so, a model is able to quickly perform inference when being tested on unseen examples.
% Training probabilistic models with neural networks is intractable in many cases and requires using expensive and problematic approximate inference algorithms like Markov chain Monte Carlo (MCMC) or mean-field variational inference (VI).
System 2 thinking instead embodies the future aspirations of models that require very few examples and can learn in a self-supervised manner. Though the model may perform inference slowly, this method of thinking appears to be a necessary aspect in developing an artificial general intelligence (AGI) that matches and exceeds our own abilities. It's possible to draw a comparison between System 2 thinking and energy-based models (EBM) which aim to capture dependencies between variables by associating a scalar energy to each configuration of the variables (LeCunn, 2006). Learning is often faster than with a standard loss functions and consists in finding an energy function where observed configurations of the variables are given lower energies than unobserved ones. Inference is often slower and involves searching the energy function using optimization methods like stochastic gradient descent to find compatible variables that minimize the energy function.

% where learning consists of finding an energy
% function so that observed configurations of the variables are given lower energies than unobserved ones.  an energy function $F$ by using contrastive or regularized latent variable methods. Inference  involves searching the energy function using a optimization methods like gradient descent to find compatible $y$’s that minimize $F$. This can be expressed as $\hat y = \text{argmin}_{y}{F(x, y)}$\footnote{\url{https://lukepereira.github.io/notebooks/documents/2020-neural-nets/main.pdf}}.

\section{Birkhoff's Ergodic Theorem}
A dynamical system, usually written as the tuple $(T, X)$, is described by a transformation that maps a phase space onto itself, $T: X \to X$. The set of points attained from repeated applications of the transformation from some starting point is known as its forward orbit or trajectory. ergodicity can be viewed as an indecomposability condition and is concerned with how a typical orbit of a dynamical system is distributed throughout the phase space with these qualitative distributional properties being expressed in terms of measure theory. Measure preserving means that $P(T^{-1}(A)) = P(A)$ for all measurable sets $A \in \mathcal A$. ergodic means that $T(A) = A$
implies $P(A) = 0$ or $P(A) = 1$ for all $A \in \mathcal A$.

\textit{Birkhoff's Ergodic Theorem} states that if a mapping is ergodic, as the number of finite averages taken along any of its orbits increases to infinity (the time average), this value will converge to the continuous integral (the space average). That is, a finite average sampling of points of any orbit will be as accurate as a continuous average integral over the entire state space.  Formally, let $(X, \mathcal{B}, \mu)$ be a probability space and let $T : X \mapsto X$ be a measure-preserving transformation (on a $\sigma$-finite measure space $\mu(X)< \infty$). If $f$ is any integrable function and $T$ is ergodic, then the time average is constant in measure $\mu$ almost everywhere and is equivalent to the space average,
\begin{align*}
    \underbrace{
        \lim_{n\to \infty} \frac{1}{n}\sum_{i=0}^{n-1}f(T^{i}(x))
    }_\textrm{Time Average, $\hat f$}
    &=  \underbrace{
        \vphantom{ \sum_{i=0}^{n-1} }
        \frac{1}{\mu(x)} \int_{X} f d \mu
    }_\textrm{Space Average, $\bar f$}\\
    \intertext{Corollary (Point-wise Ergodic Theorem):}
        \lim_{n\to \infty} \frac{1}{n}\sum_{i=0}^{n-1}f(T^{i}(x))
    &=
        \vphantom{ \sum_{i=0}^{n-1} }
        E(f)
\end{align*}
Working backwards from the expected value, we can derive the standard Bayesian training method of averaging over a large number of training examples.  We consider $x \in X$ to be a sampled observation, $f$ is our cost minimization function, and $T$ represents a transformation attained from a trained model that encodes and predicts a future state. As the number of training orbits increases to infinity, the encoding and prediction mechanisms converge to the continuous integration over the space and the expected output of the function in the point-wise interpretation.

Energies can be thought of as being \textit{unnormalized negative log probabilities}. That is, we may use the Gibbs-Boltzmann distribution to convert an energy function to its equivalent probabilistic representation after normalization, i.e. $P(y \mid x)$. Recall, \textit{marginalisation} is a method that sums over the possible values of one variable to determine the marginal contribution of another. $P(y \mid x)$ is just an application of the Gibbs-Boltzmann formula with latent variables $z$ being marginalized implicitly through integration, i.e. $P(y \mid x) = \int_z P(y,z | x)$. Then,
\begin{align*}
    P(y \mid x) &= \frac{ \int_z \exp(-\beta E(x,y,z)) }{ \int_y \int_z \exp(-\beta E(x, y, z))} \\
    % &= \frac{
    %     \exp \bigg [  -\beta (-\frac{1}{\beta} \log  \int_z \exp(-\beta E(x,y,z)) ) \bigg ]
    % }{
    %     \int_y \exp \bigg [  -\beta (-\frac{1}{\beta} \log  \int_z \exp(-\beta E(x,y,z)) ) \bigg ]
    % }
\end{align*}
The derivation introduces a $\beta$ term which is the inverse of temperature $T$, so as $\beta \rightarrow \infty$ the temperature goes to zero. $\beta$ is a positive constant that needs to be calibrated to fit the model. A larger $\beta$ value produces a more fluctuate model while a smaller $\beta$ gives a smoother model. When $\beta \rightarrow \infty$, we see that $\check{y} = \text{argmin}_{y} E(x,y)$. So we can redefine our energy function as an equivalent function using $F_\beta$,
\begin{align*}
    F_{\infty} (x,y) &= \text{argmin}_z E(x,y,z)\\
    F_{\beta} (x,y) &= -\frac{1}{\beta} \log \int_z \exp(-\beta E(x,y,z)).
\end{align*}

In physics, $F_\beta$ is known as the free energy and $E$ is the energy. If we have a latent variable model and want to eliminate the latent variable $z$ in a probabilistically correct way, we just need to redefine the energy function in terms of $F_\beta$,
\[
    P(y \mid x) = \frac{ \exp(-\beta F_\beta(x,y,z)) }{ \int_y \exp(-\beta F_\beta(x, y, z))}. \\
\]

% The Bayes classifier is
% \[
% C^\text{Bayes}(x) = \underset{r \in \{1,2,\dots, K\}}{\operatorname{argmax}} \operatorname{P}(Y=r \mid X=x).
% \]
% \textit{The ergodic hypothesis} says that, over long periods of time, the time spent by a system in some region of the phase space of microstates with the same energy is proportional to the volume of this region, i.e., that all accessible microstates are equiprobable over a long period of time.

\section{Ergodic World Models}
% If we let $E(f|{\mathcal {C}})$ be the conditional expectation given the $\sigma$-algebra ${\mathcal {C}}$ of invariant sets of $T$, then the probabilistic formulation is given by the Birkhoff–Khinchin theorem,
% \[
% \displaystyle \lim_{n\to \infty} \frac{1}{n}\sum_{i=0}^{n-1}f(T^{i}(x)) = E(f\mid {\mathcal {C}})(x)
% \]

% procedure minimizes the standard Kullback-Leibler (KL) divergence $\text{KL}(q_\theta(z) || p(z | x))$, where $q_\theta(z)$ is an explicit amortized distribution.

% Zhang et al. (2018) described a method to combine MCMC with VI that tries to directly minimize KL(q(t)θ(z) || p(z | x)), which is intractable
We see that in ergodic domains, an averaging of an increasingly large number of examples will become increasingly close to the average behaviour of the underlying causative process depicted by the space average integral. We can claim an equivalence between the time averages of a summation with the first system of thinking and the space average of an integral with the second system of thinking. It can then be claimed that the dichotomy of the systems of thought become increasingly unified in ergodic domains.

In environments that contain non-ergodic domains, a learning agent can establish an internal ergodic world model by actively selecting which observations are sampled and manipulating how these observations are encoded and stored. To maintain ergodic averages, only a local stablility is necessary, making this model biologically plausible. An ergodic world model allows the agent to attain a pointwise fluidity between System 1 and System 2 inference capabilities which ultimately improves its decision-making speed and quality in unpredictable environments. When applied to the energy-based dynamical system described in a previous paper, the latent space explored during the dream phase of training can be manipulated in order to generate ergodic measure preserving flows despite the external world explored during its waking test phase not necessarily being ergodic. Recall, the flows are attained by minimizing the free-energy of the system, represented as a Kullback–Leibler divergence or relative entropy of bounding energy manifolds.

It follows that the latent trajectories can be interpreted with symbolic dynamics. A symbolic orbit is a sequence of symbols corresponding to the successive partition elements visited by the point in its orbit is typically be represented as a Bernoulli Scheme. Instead, we use an Autoencoder to learn an encoding that will partition the space while maintaining ergodicity. This means the learned trajectories on the discrete space will on average be consistent with those in dense continuous space, which reduces the memory and computation of what would otherwise involve solving high dimensional differential equations on a continuous domain. The learned symbolic encodings of the Autoencoder are closely correlated with the form of the latent world model itself.

\subsection{Ornstein Isomorphism Theorem}
The Ornstein isomorphism theorem is a deep result for ergodic theory. It states that if two different Bernoulli schemes have the same Kolmogorov entropy, then they are isomorphic. It reveals that many systems previously believed to be unrelated are in fact isomorphic; these include all finite stationary stochastic processes, including Markov chains and subshifts of finite type, Anosov flows and Sinai's billiards, ergodic automorphisms of the n-torus (uniform hyperbolic dynamics), and the continued fraction transform.

% \section{Language-based Thought as an Ergodic Domain}
% We see that in Ergodic domains, an averaging of an increasingly large number of examples will become close to the average behaviour of the underlying causative process captured in the space average integral. We can see an equivalence between the time averages of a summation and the first system of inference and the space average of an integral and the second system of thinking. It can then be claimed that the dichotomy of the systems of thought become unified in Ergodic domains.

% We find evidence for this in the increasing effectiveness of contemporary machine learning models that are trained on a massive number of examples. Language models like GPT-3 are a good example of showing that in certain domains, a large model with billions of parameters will produce output that closely resembles the thinking and reasoning a human does during System 2 cognitive processes, despite the model only ever being trained in a probabilistic System 1 like manner. In other words, we see that GPT-3 produces time-averages that approach the space-average of human thought as the number of example orbits that it is trained on increases.

% Though it may be difficult or even impossible to formally prove, we can use this as evidence to show that language-based thinking is an Ergodic domain. When we think and speak we are fluidly switching between systems of thinking. Moreover, this fluidity between unconscious stochastic inference and conscious deterministic inference will exist in any Ergodic domain. Although modern machine learning techniques appear to be effective at imitating human reasoning in these domains, they require a massive amount of compute and training examples and will be constrained to certain domains of problems. They will also suffer from being unconscious and may lack the ability to perform well in tasks that require creative improvisation that diverges from expected behaviour. This can be thought of as an artist that can successfully write a speech or play but is unable to answer questions well or be interviewed, as is necessary in a Turing test.

% \section{Implications on Consciousness and Psychology}

% Impactful and traumatic experiences are often difficult to reconcile because our world model becomes deeply shaped around these moments as our mind reinforces its world model over time. The latent space world model is capable of reshaping itself but must always maintain coherence. Here, coherency can be thought of as some threshold on the divergence between experienced reality and feasible observations.

% With little control over how our automatic consciousness shapes latent space in to be Ergodic, our world model can grow highly incoherent and stochastic which may result in mental illness. A top-down effort is necessary to push down on the traumatic memory using constrastive methods to reshape the latent space into a coherent and more desirable world model.

% As trajectories move higher in the space, they lose coherence and become highly stochastic. Coherence can be thought of as a divergence value that is normalized with a variable threshold and represents a mask made of potential walls of a certain threshold. the latent space is restricted by some rule-set so that the many orbits through it maintain coherence despite entropy from loss of information.

% The lower bounds of our world model represent our experiences and our memory, while the upper bound represents the limits of coherence in our imaginations.
%
% Experiment in mice shows how memories could be altered while at rest

\section*{References}

\small

[1] Kahneman, Daniel. Thinking, Fast and Slow. New York: Farrar, Straus and Giroux, 2011.

[2] Bengio, Yoshua, From System 1 Deep Learning to System 2 Deep Learning, NeurIPS 2019.

[3] LeCun, Yann, A Tutorial on Energy-Based Learning, 2006.
% http://yann.lecun.com/exdb/publis/pdf/lecun-06.pdf

% https://arxiv.org/pdf/1811.07192.pdf
% https://openreview.net/pdf?id=HJx4KjRqYQ

\end{document}