In this chapter, the origin of blockchain, its concepts, terms, related technique will be introduced, which lays the foundation of the whole master thesis. It starts from Bitcoin, where the blockchain technology was originated. Then followed by the process of its development and derivatives.

\section{Bitcoin}
Digital currencies (e.g. Flooz, Beenz) appeared with the tech tide in the 90s, which would have made the online payment and transaction more convenient,
however, most of those systems utilized a trusted third party (TTP) approach, meaning that the two-party trusted company verified and facilitated the transactions. On the one hand, this method will inevitable encounter single-failure problem, on the other hand, usual framework of digital currency made from digital signature may cause the double-spending problem.

\textbf{Double-spending} \\
This problem is a potential flaw in a cryptocurrency or other digital cash scheme whereby the same single digital token can be spent more than once, and this is possible because a digital token consists of a digital file that can be duplicated or falsified.
\cite{DoubleSpend}
\begin{figure}[H]% order of placement preference: here, top, bottom
	\includegraphics[width=\textwidth]{charts/DoubleSpend}
	\caption{Alice and Bob received repeated transaction }
\end{figure}

In 2008, a paper "Bitcoin: A Peer-to-Peer Electronic Cash System", written by Satoshi Nakamoto appeared on a US mailing list. This is the very beginning of Bitcoin. It proposed a mechanism based on the peer-to-peer network, using proof-of-work to record the public history of a transaction
that quickly becomes computationally impractical for an attacker to change if honest nodes control a majority of CPU power.

The network timestamps transactions by hashing them into an ongoing chain of hash-based proof-of-work, forming a record that cannot be changed without redoing the proof-of-work.\cite{sn} Bitcoin brought together a set of  techniques to enable the distrusting entities to transact directly with a digital currency. The following several elements are very important to help build Bitcoin system.

\subsection{Peer-to-peer network}

 In a general client-server network, a server takes charge of preservation and operation of data while clients request the server for the access of data and resource. On the contrary, in a P2P network, all participating nodes (referring to computers, also called "peers") hold data respectively and create an autonomous network wherein data are requested, meaning that each node acts both of server and client. P2P nodes have significant or total autonomy from central servers.

P2P networking technology has contributed to developing a base for a complete distributed network and eliminating single point of failure in Bitcoin.

\subsection{Cryptographic hash function}
A Cryptographic hash function is any function that can be used to map data of arbitrary size to data of a fixed size. This mechanism is characterized by the fact that the same hash value is obtained from the same data but only a slight difference in the original data results in a completely different hash value.

It is extremely difficult to infer the original data based on a hash value (non-invertible feature). Taking advantage of such characteristics, this mechanism is used for the detection of falsification of data, and in the Bitcoin system, it is used for the verification and guarantee the continuity of blockchain data and the creation of blockchain through Proof of Work utilizing the calculation of hash value.

\subsection{Consensus Mechanism}
The distributed nature of the peer-to-peer network requires the members (nodes) in the network to reach a consensus which validates the new coming data blocks which contain transactions by following a set of rules. The rules are specified in the algorithmic design of the blockchain system and can vary depending on its nature, purpose, and underlying asset.

In Bitcoin system, participants begin to propose the transactions. Before a transaction is allowed to be added to the global ledgers, other participants (also called miners) in the network first verify the validation of the transaction by solving a computational problem, once it is solved, they propagate answers to other miners along with the block of transactions. The other miners will accept the solutions along with the block of transactions and add those transactions to the global ledger. The Bitcoin transaction process is explained in the Figure 2.2.

The Bitcoin system, uses "Proof of work" (PoW) algorithm to establish consensus. Other frequently are used algorithms like Proof of Stake(PoS),  Practical Byzantine Fault Tolerance (PBFT).

\begin{figure}[!htb]% order of placement preference: here, top, bottom
	\includegraphics[width=\textwidth]{charts/BitcoinTx}
	\caption{Overview: Path of a Bitcoin Transaction\cite{sn}}
\end{figure}

\begin{itemize}
	\item \textbf{Proof-of-Work}

	Proof-of-Work (PoW) generally refers to a mechanism to confirm a node’s request for add a block (a block might contain several transactions) to the blockchain that involves solving a computational challenging puzzle in order to create a new block. PoW is also called mining in Bitcoin.

	\item \textbf{Proof-of-Stake}

	The Proof of Stake (PoS) algorithm is an energy-saving generalization of the Proof of Work algorithm. In PoS, the nodes are known as the "validators" and, rather than mining the blockchain they validate the transactions to earn a transaction fee. It based on the idea that the more stake of a node has, the more capable it can mine a block successfully. Thus nodes are randomly selected to validate blocks, and the probability of this random selection depends on the amount of stake held.

	\item \textbf{ Practical Byzantine Fault Tolerance (PBFT)}

	PBFT is an algorithm for solving a \textbf{Byzantine Fault} resulting from a failure in building a consensus caused by the Byzantine Generals Problem. Simply speaking, this algorithm ensures the consistency of consensus as long as two thirds of the network’s nodes are safe (i.e., not malicious or faulty). This is enabled by replicating behaviors (i.e., state machines) of generating nodes and applying protocols for choosing a leader among them. However,
	this method requires that all the generating nodes know each
	other since they need to communicate. In other words, all the
	parties have to agree on the exact list of participants.
	\cite{pbft}

	\item \textbf{Tendermint \cite{tendermint}}

    Tendermint is another byzantine consensus algorithm without mining work. It makes the assumption that the network is partially synchronized since the time factor is central to this protocol. For each new block, a validator node is selected in a round-robbin manner which has to propose a block. This block is then spread into the
    network and has to gather more than two thirds of votes of
    members within a given time period before being added to
    the blockchain. However, these members are selected based
    on their stake and thus ties trust to resource ownership.
\end{itemize}

\begin{table}[H] \centering
%	\includegraphics[scale=0.9]{charts/consensus}
     \ra{1.3}
     \begin{tabular}{{m}{3cm}ccccc}\toprule[0.5mm]
      Property & PoW & PoS & PBFT & Tendermint\\
      \midrule[0.3mm]
      Use case & Bitcoin & Gridcoin & Hyperledger Fabric& Tendermint\\
      Energy saving & No&Partly&Yes&Yes\\
      Mining process & Yes & No & No & No\\
      Block creating speed& Slow&Fast&Fast&Fast\\
      Forking& Yes, very likely&hard&No&No\\

      \bottomrule[0.5mm]
     \end{tabular}

	\caption{Typical Consensus Algorithms Comparison}
\end{table}
In the PoW the mining process is a brute-force approach, thus that is rather energy-consuming. While other algorithms without the mining process will be much more efficient. It also reflects on the speed of generating blocks. In the PoW, forking can happen if two miners find a suitable nonce at the same time. Meanwhile with
PoS, it is very difficult, happening only when a miner can own up to 51\% of all stake in the whole
verifying network. In the BFT-like consensus, e.g. PBFT and Tendermint, the validation essentially bases on the voting, it hardly forks.

\section{Blockchain Technology}
Blockchain originally came from Bitcoin's basic technology, referring a series of blocks created through PoW, and those blocks compiling transaction data for a certain period of time are linked into a chain. With the generalization of blockchain technology, it has more wide definition. Blockchain is a particular type of data structure used in some distributed ledgers which stores and transmits data in packages called "blocks" that are connected to each other in a "chain". Blockchain employs cryptographic and algorithmic methods to
record and synchronize data across a network in an immutable manner. \cite{WBank}

\begin{figure}[!htb]% order of placement preference: here, top, bottom
	\includegraphics[width=\textwidth]{charts/blocks}
	\caption{blockchain appeared in Satoshi Nakamoto's paper}
\end{figure}

\subsection{Distributed Ledger Technology(DLT)}
Whenever the term blockchain is talked, frequently the related key word DTL would be mentioned.
Distributed Ledger Technology actually exists prior to Bitcoin, at least those techniques it represents are quite mature then. DLT is actually an umbrella term to the technology which is simply a decentralized database that is managed by various participants. Bitcoin blockchain is a milestone, which indicates the convergence of a host of technologies, including timestamping of transactions, Peer-to-Peer networks, cryptography, and shared computational power, along with a new consensus algorithm.

Distributed Ledger Technology generally consists of three basic components\cite{sawtooth}
\begin{description}
	\item [$\bullet$ A data model]that captures the current state of the ledger
	\item [$\bullet$ A transactions flow] that changes the ledger state
	\item [$\bullet$ A protocol] used to build consensus among participants around which transactions will be accepted, and in what order, by the ledger.
\end{description}
\textbf{Blockchain}, a particular
type of DLT, uses cryptographic and algorithmic methods to create and verify a continuously growing, append-only data structure that takes the form of a chain of so
called 'transaction blocks' – the blockchain – which serves the function of a ledger.\cite{WBank}

\subsection{Components of Blockchain system}
Having the foundation of Bitcoin's concept and DLT's structure, if we want to build a blockchain system, those components are likely required.

\begin{outline}
	\1 \textbf{Peer-to-peer network architecture}\\
	Due to the distributed nature, that each node in the network should keep a copy of the ledger. p2p network is the essential innovation shift to decentralized system.

    \1 \textbf{Consensus mechanism}\\
	As mentioned above, how consensus mechanism impacts the success of a blockchain system. It helps to validate the transaction, without the trusted third party.
	\1 \textbf{Smart contract}\\
	Smart contracts are simply predefined computer programs that execute actions when pre-agreed conditions within the system are met. Smart contracts provide the language of transactions that allow the ledger state to be modified. They can facilitate the business logic (e.g.  the exchange of shares, money, content, property). Smart contracts can be done in traditional centralized ledger systems as well, but the design of centralized ledger systems require such actions to be implemented only after the concerned parties have agreed to the underlying transaction as recorded in the central system.
		\2 \textbf{Decentralized Autonomous Organization(DAO)}\\
		A DAO can be seen as the most complex form of a smart contract, where the bylaws of the decentralized organization are embedded into the code of the smart contract, using complex token governance rules. At today’s evolutionary stage, a DAO materializes as a smart contract – a piece of code – executed on top of an increasingly opaque stack of distributed networking and consensus technology like the Ethereum blockchain or similar blockchains.\cite{dao}

	\1 \textbf{Cryptography}\\
	Cryptography has a key role to play both in the security, as well as in the immutability of the transactions recorded on blockchain . Cryptography is the study of the techniques used to allow secure communication between different parties and to ensure the authenticity and immutability of the data being communicated. For blockchain technology, cryptography is used to prove that a transaction was created by the right person. It is also used to link transactions into a block in a tamper-proof way, as well as create the links between blocks, to form a blockchain.

\end{outline}

\subsection{Types of Blockchain System}
Blockchain systems can be categorized as permissionless and permissioned. \textbf{Permissioned blockchain system} means that the parties that join the network are authenticated and authorized by an entity or
an administrator of the ledgers to participate on the network. while in \textbf{permissionless blockchain systems}, there is no central owner who controls network access. All that is needed to join the network and add transactions to the ledger is a server with the software.
The detailed comparisons are in the following Figure 2.4.\\
\begin{figure}[!htb]% order of placement preference: here, top, bottom
	\includegraphics[width=\textwidth]{charts/permission}
	\caption{Comparison of permissioned and permissionless DLT}
\end{figure}
In permissionless blockchain systems, like the Bitcoin or the Ethereum, anyone can join the network, as well as write and read transactions. The actors in the system are not known, which means there could be some malicious actors within the network. Permissioned blockchain reduces these security risks and ensures that only the parties with valid identification can transact.

There are several examples which base on the essential blockchain concepts and establish customized blockchain systems, which provide developers and companies with the architecture, where they can develop their own Dapps (Decentralized Applications).

\begin{itemize}
	\item \textbf{Ethereum} is an open blockchain platform(permissionless) that lets anyone build and use decentralized applications that run on blockchain technology.\cite{EthereumWhitePaper}  As the most popular blockchain for smart contracts, it facilitates the scripting functionality, or smart contracts which are run through the nodes in the network.
	\item  \textbf{Hyperledger Fabric} is a permissioned blockchain framework and one of the Hyperledger projects hosted by The Linux Foundation. Intended to create enterprise grade, open source, distributed framework for developing applications or solutions with a modular architecture.
	\item  \textbf{Corda} is permissioned platform developed by R3 in collaboration with over 200 technology and industry partners. Smart contracts allow Corda to do this using complex agreements and any asset type. This capability has broad applications across industries including finance, supply chain and healthcare.
	\item \textbf{IOTA} refers not only a cryptocurrency, but also a platform that entails a generalization of the blockchain protocol (the technology called Tangle) that sits at the backend of the IOTA platform. It enables machine-to-machine (M2M) transactions, which enhances the use of connected devices or the Internet of Things.
\end{itemize}

\subsection{Application of Blockchain Technology}
Deriving from Bitcoin, blockchain technology has a breadth of potential applications beyond cryptocurrencies in the financial field and in a wide variety of other industries.

According to World Bank's white paper, The two biggest trends in the development of blockchain applications are: 1) commercial Fintech start-ups are developing digital applications for a variety of purposes that utilize the public blockchain infrastructure, mostly Bitcoin and Ethereum;  2) industry consortia are forming to research and develop private, permissioned blockchain to solve industry-specific enterprise solutions. Actually the blockchain technology has been widely tried, in 2016 Nomura Research Institute have alreday conduct a survey on Blockchain Technologies, it visualized those applications based on Blockchain.
\begin{figure}[!htb]% order of placement preference: here, top, bottom
	\includegraphics[width=\textwidth]{charts/Fields}
	\caption{Use cases and exmaples of services using blockchains\cite{JapanS}}
\end{figure}

\begin{itemize}
	\item \textbf{Finance}\\
	Ripple is one of the representative blockchains in this field.
	\item \textbf{Loyalty points and reward}\\
	GyftBlock, which provides an exchange service of gift
	cards using a blockchain.
	\item \textbf{Funding}\\
	Swarm provides a service to procure funds through cloud funding on a
	blockchain.
	\item \textbf{Communication}\\
	Messaging services and social networking services (SNS) have been
	made available using blockchains.
	\item \textbf{Asset management}\\
	Factom, etc. commenced the provision of a service.
	\item \textbf{Storage}
	Storj provides a service to manage various electronic files using a blockchain. Similar application like BigchainDB.
	\item \textbf{Authentication}\\
	uPort is a self-sovereign identity and user-centric data platform.
	\item \textbf{Sharing}\\
	LaZooZ aims to provide a sharing service using a blockchain. At present, it provides a ride sharing application like Uber.
	\item \textbf{Commercial distribution management:}\\
	Everledger provides a system to manage
	diamonds. The serial number and carat, various commodity information, ownership and distribution record of each diamond are managed.
	\item \textbf{Content}\\
	Streamium provides a service to support content delivery, having established a system to charge by the second (paid with bitcoins) for video delivery, etc.
	\item \textbf{Prediction}\\
	Augur provides a decentralized prediction market platform where participants cast votes on various events to predict the future through the wisdom of the crowd.
	\item \textbf{Public}\\
	Neutral Voting Bloc (NVB) is a service provided in Australia, advocating itself as a new political party.
	\item \textbf{Medical services}\\
		BitHealth aims to achieve its goal to enable users to safely check
	their own health records from anywhere in the world using a blockchain.
	\item \textbf{IoT}\\
	Such services as ADEPT by IBM and Samsung are attracting attention.

\end{itemize}


Now that we have defined the EF1 objective for filter optimization, we need to find efficient ways to (approximately) optimize it.  To this end, this section first describes two scalable and efficient greedy algorithms to optimize EF1.
%used to build filters setting for querying large data graphs in order to retrieve relevant UI elements that are of interest to the users.
Following this, we describe an optimization-based approach based on Mixed Integer Linear Programming (MILP) to benchmark the performance of the proposed greedy algorithms on moderate-sized problems.

\subsection{Greedy search}

% Should discuss greedy algorithm generically as having a metric and at each step
% a choice of k restrictions, which are each scored against the metric with the
% highest score chosen at each step.  Then each individual filter only has to
% specify what the choices are and how the choice restricts the set of emails
% selected.  What is a good succinct notation for this?

As discussed previously, we assume that three types of ``sub-filters'' are used to select a subset of relevant information in a VID: Keyword Filter, Time Filter, and Space Filter.  A Global Filter is generated by \emph{conjoining} these three sub-filters. In the following, we describe how to greedily build each of these sub-filters, and then how to combine them into a Global Filter.

\subsubsection{Greedy Keyword Filter algorithm}

Given a set of information elements, the greedy Keyword Filter algorithm aims to select a set of keywords to form a negation query in order to exclude a subset of elements containing these keywords for the purpose of maximizing the EF1-Score.

Formally, given a keyword query representation $Q_k$, the algorithm aims to select an
optimal subset of $k$ terms $T_{k}^{*}\subset E$ (where $|T_{k}^{*}|=k$ and $E$ is the initial set of elements) to build a negation keyword query $Q_k$, i.e. $Q_k=\{\neg t_{1}^{*},\dots \neg t_{k}^{*}\}$, in order to optimize the EF1-score. This is achieved by
building $T_{k}^{*}$ in a greedy manner by choosing the next optimal
term $t_{k}^{*}$ given the previous set of optimal term selections
$T_{k-1}^{*}=\{t_{1}^{*},\ldots,t_{k-1}^{*}\}$ (assuming $T_{0}^{*}=\emptyset$)
using the following selection criterion:
\begin{equation}
t_{k}^{*}=\argmax_{t_{k}\notin T_{k-1}^{*}}\hspace{-0.3mm}[EF1(E^{*} \textrm{ that satisfies } Q_k=\{\neg t_{1}^{*},\dots \neg t_{k}^{*}\})]
\end{equation}
where  $E^{*}$ is a subset of the initial element set $E$ that satisfy the negation keyword query $Q_k$. In order to reduce the keyword search space, we propose to use the top 100 terms ranked using Mutual Information to identify the keywords that are predictive of the ``supervised'' relevance measure.  We remark that other metrics like frequency would be more appropriate for unsupervised tasks.  We further remark that we have chosen to use a negation query as a means to effectively prune (or filter) the content as more terms are selected.
%mainly for technical reasons as a negation query generates queries with much fewer terms.
The best indexing strategy to support this greedy search is the inverted index data structure~\cite{Zobel2006}.

\subsubsection{Greedy Time Filter algorithm}

The idea behind the time-based greedy filter algorithm is as simple as finding a time window range $Q_t=[t_{start},t_{end}]$, which allows to select a subset of elements $E^{*}\subseteq E$ falling in that time window, with $E^{*}$ having the highest EF1-Score.

Formally, a list of elements $E=\{j_{t_1}\leq \dots \leq j_{t_n}\}$, where "$\leq$" specifies the timestamp order, we propose the following two time greedy algorithms:

\subfour{Naive greedy algorithm:} First, at each iteration of this algorithm, an early ranked element $j_{t_i}$ is removed, and then, the remaining set is assessed using EF1-Score. If the remaining set has a lower EF1-score value than the set of the previous iteration, the algorithm assigns $t_i$ to the lower time bound of the time query, i.e., $t_{start}=t_i$.
Next, the algorithm does the same set of operations, by removing at each iteration a lastly ranked element  $j_{t_i}$, and by stopping once the removal of  $j_{t_i}$ causes a decrease in the EF1-Score value. Then, the algorithm assigns $t_i$ to the upper time bound of the time query, i.e., $t_{end}=t_i$.
Lastly, the algorithm returns the  time query $Q_t=[t_{start},t_{end}]$, with obviously $EF1(E^{*} \textrm{ that satisfies } Q_t=[t_{start},t_{end}]) \geq EF1(E)$.

\subfour{Binary Partition Search algorithm:} Large datasets with sparse positive data (e.g., 0.5\% of alerts in a security graph) will cause the previous algorithm to take a large number of iterations to terminate since it greedily adjusts filter settings in a minimal way at each step.  A way to address this problem is to use binary partitioning search (BPS).  Hence, instead of removing a single element $j$ at each iteration, this algorithm operates by selecting between two distinct alternatives (binary partitions) at each iteration.

\begin{algorithm}[t]
%\scriptsize
\caption{Binary Partition Search (BPS) Algorithm}
\SetAlgoLined
\SetKwData{Left}{left}\SetKwData{This}{this}\SetKwData{Up}{up}
\SetKwFunction{Union}{Union}\SetKwFunction{FindCompress}{FindCompress}
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{A set of ordered elements $E=\{j_{v_1} \dots j_{v_n}\}$ }
\Output{A timestamp $t$;}
\BlankLine
\label{alg:Dichotomy}

$v_{min}=v_1$; $v_{max}=v_n$;  $v_{mid}=\tfrac{v_1+v_n}{2}$;

\While {$v_{min}!=v_{mid}!=v_{max}$}{

\eIf{$[EF1(\{j_{v_{min}} \dots  j_{v_n}\}) \geq EF1(\{j_{v_{mid}} \dots  j_{v_n}\})]$}{
$v_{max}=v_{mid}$;
$v_{mid}=\tfrac{v_{min}+v_{mid}}{2}$;
}{
$v_{min}=v_{mid}$;
$v_{mid}=\tfrac{v_{min}+v_{max}}{2}$;
}
}
\Return  $v_{mid}$;

\label{alg:return}
\end{algorithm}

%Therefore, the algorithm first sets the values $t_{min}=t_1$, $t_{max}=t_n$,  and $t_{mid}=\tfrac{t_1+t_n}{2}$. Then, for each iteration, if $[EF1(\{d_{t_{min}}\leq \dots \leq d_{t_n}\}) \geq EF1(\{d_{t_{mid}}\leq \dots \leq d_{t_n}\})]$, the algorithm sets $t_{max}=t_{mid}$, $t_{mid}=\tfrac{t_{min}+t_{mid}}{2}$ and makes a new iteration, else, the algorithm sets $t_{min}=t_{mid}$,  $t_{mid}=\tfrac{t_{min}+t_{max}}{2}$  and makes a new iteration. The algorithm keeps iterating until $t_{min}=t_{mid}=t_{max}$, where it assigns $t_{mid}$ to the lower time bound of the time query, i.e., $t_{start}=t_{mid}$.

As an example of the BPS approach for the time sub-filter, the algorithm first sorts $E$ in increasing order of time stamp. Then, it applies the procedure described by Algorithm \ref{alg:Dichotomy}. This procedure will return  the lower time bound of the time query, i.e., $t_{start}=t_{mid}$.
Next, the algorithm sorts $E$ in decreasing order of time stamp, and then, it applies again the procedure described by Algorithm \ref{alg:Dichotomy} to get the upper time bound of the time query, i.e., $t_{end}=t_i$. Lastly, the algorithm returns the  time query $Q_t=[t_{start},t_{end}]$, such that $EF1(E^{*} \textrm{ that satisfies } Q_t=[t_{start},t_{end}]) \geq EF1(E)$. Note that this algorithm proceeds in a total of $log(n)$ iterations in the best case, and $2\times log(n)$ iterations in the worst case.

For both the naive and time-based greedy algorithms, we use the red-black tree as the indexing data structure \cite{Guibas1978}.

\subsubsection{Greedy Spatial Filter algorithm}

The aim of this algorithm is to return coordinates $Q_p=[(x_{min},y_{min},(x_{max},y_{max})]$ representing the EF1-Score maximizing bounding box represented by the lower and upper bound coordinates -- respectively $(x_{min},y_{min})$ and $(x_{max},y_{max})$. This 2D problem is similar to the previous one dimensional problem of finding the best time window. Therefore, the two greedy algorithms described above can be adapted for this problem by first applying each algorithm on the x-axis to determine $(x_{min},x_{max})$, then on the y-axis to determine $(y_{min},y_{max})$.
% We need to ditch some citations and this removes the complaint that we did not provide all details -- we claim it is obvious.  -Scott
%We omit the description of these two algorithms for lack of space, but a detailed description can be found in the technical report~\cite{Bouadjenek2018}.
We use the R-tree as a data structure for indexing multi-dimensional continuous data~\cite{Guttman1984}.

\subsubsection{Global Filter algorithm} To obtain a Global Filter combining all sub-filters, we propose a greedy algorithm, which at each iteration applies all sub-filters and chooses the one that most reduces EF1.  The selected filter is updated with its new setting and the iteration continues.  Iterations termination when no sub-filter can unilaterally improve EF1 and the final filter settings are returned as the Global Filter.
%to, theselects the best sub-filter to apply (according to its reduction in EF1-Score), then checks if that filter improves the EF1-Score. If so, the algorithm continues with the updated filter settings, otherwise, it terminates.
% Don't get the following -- seems non-essential, at least for submission.  -Scott
%Note that here, we use  $k=1$ for the keywords greedy algorithm.
%The algorithm will then determine a sequence of filters to apply on the initial set, and the final query is then built by combining these filters by types.

 %such as: $\{Keyword \to Time -> Position \to Time \to Keyword\} $

% I don't follow this example, but I think the algorithm is intuitive enough and we need space.  -Scott
% For example, let's suppose the algorithm determines the following filter sequence: $\{Q_{k_1}=\{ \neg natural\} \to Q_{t_1}=[50,2030] \to Q_{p_1}=[(10,60), (50,100)] \to Q_{t_2}=[60,1230] \to Q_{k_2}=\{ \neg fictive\}  \to  Q_{p_2}=[(10,60), (30,85)] \to  Q_{t_3}=[60,800] \}$.
% The final query is built by combining these filters by types as follows: $Q_k=Q_{k_1} \cup Q_{k_2}=\{\neg natural,\neg fictive\}$, $Q_t =Q_{t_1} \cap Q_{t_2} \cap Q_{t_3}=[60,800]$, and  $ Q_p=Q_{p_1}  \cap Q_{p_2}=[(10,60),$ $(30,85)]$, which gives $Q=[Q_k=\{\neg natural,\neg fictive\}\wedge Q_t=[60,800]\wedge Q_p = [(10,60),$ $(30,85)]]$.

Finally, we note that the Global Filter algorithm can use the Greedy Keyword Filter with the naive Greedy Time and Spatial Filters, which we refer to experimentally as {\bf Greedy}, or the Binary Partition Search variants, which we refer to experimentally as {\bf BPS}.
%time and position naive greedy algorithms to which we refer as Greedy Algorithm, or the keywords greedy algorithm with the time and position Binary Partition Search algorithms to which we refer as BPS Algorithm.

\subsection{Optimal MILP Solutions for Benchmarking}

Next, we propose an exact Mixed Integer Linear Progamming (MILP) optimization-based formulation to maximize EF1 and provide a benchmark for evaluating the two previous Global Filter algorithms (Greedy and BPS).  %Given the trivial solution of optimizing the expected precision (singleton) and expected recall (the whole collection), we consider in the following only the optimization of EF1.

\subsubsection{Fractional MILP Formulation} \hfill \\
We begin by reformulating the EF1 objective to prepare for further optimization steps by replacing the global sum of scores of all information elements with a constant $C = \sum_{j=1}^m S(j)$:
\begin{equation}
\begin{aligned}
    \emph{$EF1$} &= \dfrac{2\times \sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + \sum_{j=1}^m S(j)} = \dfrac{2 \times \sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + C}
\end{aligned}
\end{equation}

In order to obtain the EF1-optimal Global Filter, we let binary variables $\emph{I\textsubscript{filter}}($j$) \in \{0, 1\}$ indicate whether an information element $j$ is selected in each sub-filter and constrain that to be selected in the Global Filter (i.e., $I(j)=1$), $j$ must be selected in all filters (i.e., a conjunction).  This leads to the following fractional MILP formulation with sub-filter constraints to be defined later:
%intend to directly optimize the EF1 metric in terms of decision indication variables  for filter setting as follows:
% Don't use I(i)... confusing!
%\begin{align}
%\max_{\textit{filter vars}} \;\;
%& \dfrac{\sum_{j=1}^m S(i)I(j)}{\sum_{j=1}^m I(j) + C} \nonumber \\
%& \textrm{subject to constraints between {\it filter vars} and $I(j)$}
%\end{align}
\begin{equation}
\begin{aligned}
& \underset{I_{\mathit{filter}}(j)}{\text{maximize}}
& & \dfrac{\sum_{j=1}^m S(j)I(j)}{\sum_{j=1}^m I(j) + C} \\
& s.t
& & I(j) = \bigwedge I_{\mathit{filter}}(j) \\
\end{aligned} \label{eq:frac_milp}
\end{equation}

% Not necessary.  -Scott
%Note that our goal is to optimize the element set in terms of \emph{filters settings}. This means elements in the interface sharing the same property needs to be simultaneously added to the selected set. This is a unique property of our filter-based UI problem, which is different from the independent retrieval of each document in standard IR system.

\subsubsection{Transformation to a MILP} \hfill \\
While there are no direct solvers for fractional MILPs, we can transform~\eqref{eq:frac_milp} into a pure MILP form for which we have efficient and optimal solvers.  To do this, we use the Charnes-Cooper method \cite{Charnes1962} and Glover linearization method \cite{Glover1975} with big-M constraints, where auxiliary variables \emph{w(j)} and \emph{u} are  introduced\footnote{https://optimization.mccormick.northwestern.edu/index.php/Mixed-integer\_linear\_fractional\_programming\_(MILFP)}. Here, $w(j)$ is defined as $w(j)=I(j)\times u$ with $u$ defined as follows:
\begin{equation}
u = \dfrac{1}{\sum_{j=1}^m I(j) + C}
\end{equation}

Then, the EF1 optimization problem is able to be transformed into the following MILP problem:
\begin{equation}
\begin{aligned}
& \underset{w,u}{\text{maximize}}
& & \sum_{j=1}^m S(j)w(j) \\
& s.t
& & \sum_{j=1}^m w(j) + uC = 1 \\
& & & w(j) \leqslant u, \quad w(j) \leqslant M\times I(j)  \\
& & & w(j) \geqslant u - M\times [1-I(j)] \\
& & & u > 0,  \quad I(j) \in \{0, 1\}, \quad w(j) \geqslant 0 \label{eq:milp}
\end{aligned}
\end{equation}

\subsubsection{Constraints} \hfill \\
As our goal is to select elements through three sub-filters, we add three constraints to the above optimization.
\begin{enumerate}
\item {\bf Time Filter Constraint:} a two-element tuple ($t_{start}$, $t_{end}$) indicating respectively the start and the end of the time window.
\begin{equation}
\begin{aligned}
  I_{\mathit{time}}(j) &=
   \begin{cases}
     1, & \text{if $(t_{start} \leqslant t(j)) \land (t(j) \leqslant t_{end})$}  \\
     0, & \text{otherwise}
  \end{cases} \label{eq:cons1}
\end{aligned}
\end{equation}

\item {\bf Spatial Filter Constraint:} a four-element tuple ($x_{min}$, $y_{min}$, $x_{max}$, $y_{max}$) to create a bounding box filter in visualization interface.
\begin{equation}
\begin{aligned}
I_{\mathit{pos}}(j) & =\begin{cases}
1, & \text{if \ensuremath{(x_{min}\leqslant x(j))\land(x(j)\leqslant x_{max})\land}}\\
 & (y_{min}\leqslant y(j))\land(y(j)\leqslant y_{max})\\
0, & \text{otherwise}
\end{cases} \label{eq:cons2}
\end{aligned}
\end{equation}

\item {\bf Keyword Filter Constraint:} a boolean vector of terms $t^*_k$ with size $m$ - the size of the dictionary of the global collection.
\begin{equation}
  I_{\mathit{term}}(j) = \bigwedge_{t^*_k \in j} t^*_k \qquad \textnormal{for s = 1, 2, $\cdots$, m} \label{eq:cons3}
\end{equation}
All terms with $I_{\mathit{term}}=0$ are included in the negation query.

\item {\bf Global Filter Constraint:} for information element $j$ to be selected globally, it must be simultaneously selected by the three sub-filters.
%, all filter constraints have to be satisfied in this element. In others words, an AND operator is required between all the sub-filter constraints.
%{\bf TODO: mention how to apply to all filters... need to say an email j is selected if \emph{all} filters say it is selected, so an AND constraint.  \textcolor{red}{[PLEASE COMPLETE YIHAO]}.
%}
\begin{equation}
  I(j) = I_{\mathit{time}}(j) \land I_{\mathit{pos}}(j) \land I_{\mathit{keyword}}(j) \label{eq:cons4}
\end{equation}

\end{enumerate}
We refer to the above MILP formulation in~\eqref{eq:milp} with all filter constraints~\eqref{eq:cons1}--\eqref{eq:cons4} as the {\bf Optimal} Global Filter.

\subsection{Multiple Filter Selection Wrapper}

%The Global Filter selects a single best filter setting.  However, what if we want to display multiple possible Global Filters.
In practice a single Global Filter chosen by the previously described algorithms will narrow the user in on a single \textquotedblleft event\textquotedblright{}. However, there will likely be multiple anomalous events and so the user should have a choice of multiple filters.
Consider Figure~\ref{Fig:UseCase}: this actually shows three different spatial bounding boxes corresponding to three different events provided by Global Filters.
In this work, we provide an initial greedy approach for providing a ranked list of multiple filters (that is a wrapper approach working with any of the previously defined filtering algorithms -- Greedy, BPS, or Optimal); we leave it to future work to develop improved filtering and ranking methods for multiple filters.

The algorithm itself is quite simple.  After the first filter is produced, all selected elements by the filter have their scores $S(j)$ zeroed out.  The filtering algorithm is then run again, where it will inherently focus on a different content set.  This procedure is repeated until the desired number of filters is reached, or a metric / coverage score for high scoring content is reached.  The user should then be able to choose among the multiple filters in the VID.


% !TEX root = ../main.tex
% chktex-file 21
\section{Conclusion}%
\label{sec:conclusion}

In this paper we presented two approaches to speed up hyperparameter optimization: Fabolas and learning curve extrapolation.
Both use probabilistic models to estimate values of the error function.
Then four approaches to speed up training were presented: Bag of Little Bootstraps, subsample size selection for gradient descent, subsampling for logistic regression and \(k\)-means clustering for SVMs.\
All presented approaches are able to speedup training significantly on big datasets.
Since those approaches optimize different aspects of the training process, various combinations of them are possible to get further speedups.
We will now take a brief look at promising combinations.
\begin{itemize}
	\item Since Fabolas is a hyperparameter optimizer that does not make assumptions about the learner, it can be combined trivially with any of the training optimization approaches presented in Section~\ref{sec:params}.
	\item The learning curve extrapolation method described in Section~\ref{sec:hyperparams:earlyterm} requires a learner using gradient descent.
		It can thus be combined with the subsample size selection method described in Section~\ref{sec:params:samplesize}.
	\item Learning curve extrapolation could additionally be combined with Fabolas to guide the hyperparameter search.
		This would however require changing the covariance kernel of the cost model \(c\) since probes at suboptimal positions are likely to be terminated early, making them less costly and thus more attractive for the aquisition function \(a_F\).
		One possible approach to adapt the cost model is to add a dependence on the loss model \(f\).
		This would essentially merge the two Gaussian processes \(c\) and \(f\) into a single cost-loss model over the parameter space \(\Theta = \Lambda \times [0, 1] \times \{\mathrm{cost}, \mathrm{loss}\}\) where information about the loss of a configuration is indicative about its cost.
		Finding a suitable kernel function for this joint GP model could be a subject of further research.
	\item In principle Bag of Little Bootstraps (Section~\ref{sec:params:blb}) could be combined with the other three training optimization methods since it is a general purpose bagging method.
		However the combination of BLB with subsample size selection for gradient descent is problematic since the latter relies on dynamically adapting the sample size during training to reduce the expected variance.
		This approach does not work well if the training data is a resampled BLB bootstrap that does not allow for significant variance reduction since it only contains a small fraction of datapoints.
		The combinations of BLB with OSMAC and of BLB with WKM-SVM are more promising.
		In the case of OSMAC the sampling weights \(\pi_i\) could be used to sample the small bootstraps \(\check{X}\) (compare~\citet{Norazan2009}).
	\item Another promising combination is that of subsample size optimization (Section~\ref{sec:params:samplesize}) with OSMAC (Section~\ref{sec:params:osmac}).
		Since logistic regression is often implemented using gradient descent, both methods can be combined by simply using OSMAC to get the subsample for each SGD step.
\end{itemize}
Evaluating those combinations could be a subject of future work.


\chapter{datareset} \label{datareset}

\section{Introduction}

The \texttt{datareset} command assumes that certain parameters of the LPE determine which summands can be followed by which summands.
These parameters determine the control-flow of the LPE, and are therefore called \emph{control-flow parameters}.

\emph{Data parameters}, on the other hand, are used to store values, and often temporarily, which means that their values may not be used anymore after a particular state.
Data parameters can typically have different values after such a state, which causes them to add states to the state space for each of those values -- \emph{without} adding any new behavior!

The \texttt{datareset} command attempts to detect LPE control-flows and determine the parts of those control-flows where data parameters are no longer used.
At these locations, data parameters are set to a default value (`reset').
This removes states from the state space, potentially improving performance of subsequent computations.

\section{Formal background}

The control-flow analysis is based on work done in \cite{van2009state}.

\subsection{Parameter properties}

Each parameter $p$ of a summand $s$ has the following properties:
\begin{itemize}

\item Is the value of $p$ potentially altered by $s$?
If so, $p$ receives the label `changed'.

\item Does $p$ occur in the guard of $s$?
If so, $p$ receives the label `directly used'.

\item Has $p$ received the label `directly used' in $s$?
Or does $p$ occur in the assignment by $s$ to a parameter that has received the label as `changed'?
In either case, $p$ also receives the label `used'.

\item Must the value of $p$ have a specific (unique) value $v$ in order for $s$ to be enabled?
If so, $p$ receives the label `having a source' for $s$, with the source being $v$.

\item Does the value of $p$ have a specific (unique) value $v$ immediately after $s$ has been applied?
If so, $p$ receives the label `having a destination' for $s$, with the source being $v$.

\item Does $p$ have both a source and a destination for $s$?
Then $p$ is called a \emph{ruling parameter}, and $p$ is said to `rule' summand $s$.

\end{itemize}

\subsection{Control-flow graphs}

A parameter of the LPE is a \emph{control-flow parameter} of the LPE if for all summands it is either a ruling parameter or not \labeledas{changed} (or both).
A parameter that is not a control-flow parameter is defined as a \emph{data parameter}.

\vspace{1mm}

For each control-flow parameter $f$ of the LPE, a control-flow graph is constructed.
There is a state in the control-flow graph for each source and destination of $f$ (across all summands).
Two states $s_1$ and $s_2$ are connected by an edge $(s_1, i, s_2)$ if there is a summand $i$ where the source of $f$ is represented by one of the states and where the destination of $f$ is represented by the other state.
The direction of such an edge is from source state to destination state.

\subsection{Belongs-to function}

Here, the \emph{belongs-to} function is introduced.
The belongs-to function maps each data parameter $d$ to some set of control-flow parameters $b(d)$ as follows

\begin{align*}
b(d) = F \cap \bigcap\limits_{s \in S}^{} \text{ruling}(s)
\end{align*}

where

\begin{itemize}
\item $F$ is the set of all control-flow parameters;
\item $S$ is the set of all summands in which $d$ is \labeledaseither{changed}{used};
\item $\text{ruling}(s)$ is the set of all parameters that rule summand $s$.
\end{itemize}

\section{Algorithm}

The algorithm executes 3 phases: the preparatory phase, the iterative phase, and the deletion phase.

\subsection{Preparatory phase}

In this phase, the parameter properties, control-flow graphs and belongs-to function are computed.

\subsection{Iterative phase}

In the iterative phase, the so-called \emph{relevance relation} is computed.
This relation, with symbol $R$, relates a data parameter $d$, a control-flow parameter $f$, and a value $v$ if $d$ is `relevant' after a state in which $f$ has value $v$.
This is denoted $R(d, f, v)$.
Intuitively, this is a situation in which $d$ should \emph{not} be reset because its value may be needed in the future.

The computation of $R$ is a fixpoint algorithm: modifications are applied iteratively until $R$ no longer changes.
The initial value of $R$ is set to
\begin{align*}
R_0 = \bigcup\limits_{\substack{i \in S \\ d_k \in \text{directlyUsed}(i)}}^{} \;\{\; (d_k, d_j, \text{source}(i, d_j)) \;|\; d_j \in b(d_k) \;\}
\end{align*}

where

\begin{itemize}
\item $S$ is the set of all summands;
\item $\text{directlyUsed}(i)$ is a function that gives the set of all parameters that are \labeledas{directly used} in a summand $i$;
\item $\text{source}(i, f)$ is a function that gives the source value of a control-flow parameter $f$ for a summand $i$.
\end{itemize}

Each iteration can be split into two steps.
The first step checks for control-flow graphs in which a data parameter $d$ has already been \labeledas{relevant} whether this implies that $d$ is also relevant in preceding states (of the same control-flow graph).
If so, the appropriate triples are added to $R$:
\begin{align*}
R_{n}{'} = R_{n-1} \cup \bigcup\limits_{\substack{i \in S}}^{} \;\left\{\; (d_k, d_j, s) \;\middle|\; \substack{(d_l, d_j, t) \in R_{n-1} \\ d_j \in b(d_k) \\ d_k \in \text{vars}(v_i(d_l)) \\ (s, i, t) \in E_{d_j}} \;\right\}
\end{align*}

where

\begin{itemize}
\item $E_{f}$ is the set of edges that are part of the control-flow graph of control-flow parameter $f$.
\end{itemize}

The second step is similar, but data parameters that are found to be relevant are added as triples to $R$ in relation to \emph{another} control-flow parameter:
\begin{align*}
R_{n} = R_{n}{'} \cup \bigcup\limits_{\substack{i \in S}}^{} \;\left\{\; (d_k, d_j, \text{source}(i, d_j)) \;\middle|\; \substack{(d_l, d_p, t) \in R_{n}{'} \\ d_j \in b(d_k),\; d_j \notin b(d_l) \\ d_k \in \text{vars}(v_i(d_l)) \\ (r, i, t) \in E_{d_p}} \;\right\}
\end{align*}

\subsection{Deletion phase}

Finally, each summand of an LPE is subject to modification.
Modifications are made by making use of the belongs-to function $b$ and the relevance relation $R$.
Intuitively, we check whether the value of a parameter is `relevant' after a specific summand $i$ has been applied.
If so, the parameter should not be reset.
Otherwise, the parameter can be reset, for example to its initialization value:

\begin{align*}
v_{i}{'}(d_k) = \begin{cases}
v_{i}(d_k) & \text{if } \bigwedge\limits_{\substack{d_j \in \text{ruling}(i) \\ d_j \in b(d_k)}}^{} R(d_k, d_j, \text{dest}(i, d_j)) \\
v_{I}(d_k) & \text{otherwise}
\end{cases}
\end{align*}

where

\begin{itemize}
\item $\text{ruling}(s)$ is the set of all parameters that rule summand $s$;
\item $\text{source}(i, f)$ is a function that gives the destination value of a control-flow parameter $f$ for a summand $i$.
\end{itemize}

\section{Benchmark results}

The following durations were measured with a benchmark for several models:
\begin{itemize}
\item The average duration of \txs{} to make 500 steps in a model after it has been converted to LPE form;
\item The average duration of \txs{} to make 500 steps in a model after it has been converted to LPE form and after the \texttt{datareset} operation has been applied.
\end{itemize}

When plotting the second series of measurements against the first (see Figure~\ref{datareset-vs-lpe-only:fig}), it is easy to see that the impact is insignificant in most cases.

\begin{figure}[!ht]
\begin{center}
\includegraphics[width=0.7\linewidth]{charts/datareset-vs-lpe-only}
\caption{Benchmark results: datareset vs LPE transformation}
\label{datareset-vs-lpe-only:fig}
\end{center}
\end{figure}


% Copyright 2019 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\section{Three Dimensional Drawing Library}

\begin{tikzlibrary}{3d}
    This package provides some styles and options for drawing three dimensional
    shapes.
\end{tikzlibrary}

\subsection{Coordinate Systems}

\begin{coordinatesystem}{xyz cylindrical}
    The |xyz cylindrical| coordinate system allows to you specify a point in
    terms of cylindrical coordinates, sometimes also referred to as cylindrical
    polar coordinates or polar cylindrical coordinates. It is very similar to
    the |canvas polar| and |xy polar| coordinate systems with the difference
    that you provide an elevation over the $xy$-plane using the |z| key.
    %
    \begin{key}{/tikz/cs/angle=\meta{degrees} (initially 0)}
        The angle of the coordinate interpreted in the ellipse whose axes are
        the $x$-vector and the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/radius=\meta{number} (initially 0)}
        A factor by which the $x$-vector and $y$-vector are multiplied prior to
        forming the ellipse.
    \end{key}
    %
    \begin{key}{/tikz/cs/z=\meta{number} (initially 0)}
        Factor by which the $z$-vector is multiplied.
    \end{key}
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[->]
  \draw (0,0,0) -- (xyz cylindrical cs:radius=1);
  \draw (0,0,0) -- (xyz cylindrical cs:radius=1,angle=90);
  \draw (0,0,0) -- (xyz cylindrical cs:z=1);
\end{tikzpicture}
\end{codeexample}
    %
\end{coordinatesystem}

\begin{coordinatesystem}{xyz spherical}
    The |xyz spherical| coordinate system allows you to specify a point in
    terms of spherical coordinates.
    %
    \begin{key}{/tikz/cs/radius=\meta{number} (initially 0)}
        Factor by which the $x$-, $y$-, and $z$-vector are multiplied.
    \end{key}
    %
    \begin{key}{/tikz/cs/latitude=\meta{degrees} (initially 0)}
        Angle of the coordinate between the $y$- and $z$-vector, measured from
        the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/longitude=\meta{degrees} (initially 0)}
        Angle of the coordinate between the $x$- and $y$-vector, measured from
        the $y$-vector.
    \end{key}
    %
    \begin{key}{/tikz/cs/angle=\meta{degrees} (initially 0)}
        Same as |longitude|.
    \end{key}
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[->]
  \draw (0,0,0) -- (xyz spherical cs:radius=1);
  \draw (0,0,0) -- (xyz spherical cs:radius=1,latitude=90);
  \draw (0,0,0) -- (xyz spherical cs:radius=1,longitude=90);
\end{tikzpicture}
\end{codeexample}
    %
\end{coordinatesystem}

\subsection{Coordinate Planes}

Sometimes drawing with full three dimensional coordinates is not necessary and
it suffices to draw in two dimensions but in a different coordinate plane.  The
following options help you to switch to a different plane.

\subsubsection{Switching to an arbitrary plane}

\begin{key}{/tikz/plane origin=\meta{point} (initially {(0,0)})}
    Origin of the plane.
\end{key}

\begin{key}{/tikz/plane x=\meta{point} (initially {(1,0)})}
    Unit vector of the $x$-direction in the new plane.
\end{key}

\begin{key}{/tikz/plane y=\meta{point} (initially {(0,1)})}
    Unit vector of the $y$-direction in the new plane.
\end{key}

\begin{key}{/tikz/canvas is plane}
    Perform the transformation into the new canvas plane using the units above.
    Note that you have to set the units \emph{before} calling
    |canvas is plane|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[
    ->,
    plane x={(0.707,-0.707)},
    plane y={(0.707,0.707)},
    canvas is plane,
]
    \draw (0,0) -- (1,0);
    \draw (0,0) -- (0,1);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\subsubsection{Predefined planes}

\begin{key}{/tikz/canvas is xy plane at z=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,0,|\meta{dimension}|)}|,
        \item |plane x={(1,0,|\meta{dimension}|)}|, and
        \item |plane y={(0,1,|\meta{dimension}|)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is yx plane at z=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,0,|\meta{dimension}|)}|,
        \item |plane x={(0,1,|\meta{dimension}|)}|, and
        \item |plane y={(1,0,|\meta{dimension}|)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is xz plane at y=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,|\meta{dimension}|,0)}|,
        \item |plane x={(1,|\meta{dimension}|,0)}|, and
        \item |plane y={(0,|\meta{dimension}|,1)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is zx plane at y=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(0,|\meta{dimension}|,0)}|,
        \item |plane x={(0,|\meta{dimension}|,1)}|, and
        \item |plane y={(1,|\meta{dimension}|,0)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is yz plane at x=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(|\meta{dimension}|,0,0)}|,
        \item |plane x={(|\meta{dimension}|,1,0)}|, and
        \item |plane y={(|\meta{dimension}|,0,1)}|.
    \end{itemize}
\end{key}

\begin{key}{/tikz/canvas is zy plane at x=\meta{dimension}}
    A plane with
    %
    \begin{itemize}
        \item |plane origin={(|\meta{dimension}|,0,0)}|,
        \item |plane x={(|\meta{dimension}|,0,1)}|, and
        \item |plane y={(|\meta{dimension}|,1,0)}|.
    \end{itemize}
\end{key}

\subsection{Examples}

\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}[z={(10:10mm)},x={(-45:5mm)}]
  \def\wave{
    \draw[fill,thick,fill opacity=.2]
     (0,0) sin (1,1) cos (2,0) sin (3,-1) cos (4,0)
           sin (5,1) cos (6,0) sin (7,-1) cos (8,0)
           sin (9,1) cos (10,0)sin (11,-1)cos (12,0);
    \foreach \shift in {0,4,8}
    {
      \begin{scope}[xshift=\shift cm,thin]
        \draw (.5,0)  -- (0.5,0 |- 45:1cm);
        \draw (1,0)   -- (1,1);
        \draw (1.5,0) -- (1.5,0 |- 45:1cm);
        \draw (2.5,0) -- (2.5,0 |- -45:1cm);
        \draw (3,0)   -- (3,-1);
        \draw (3.5,0) -- (3.5,0 |- -45:1cm);
      \end{scope}
    }
  }
  \begin{scope}[canvas is zy plane at x=0,fill=blue]
    \wave
    \node at (6,-1.5) [transform shape] {magnetic field};
  \end{scope}
  \begin{scope}[canvas is zx plane at y=0,fill=red]
    \draw[help lines] (0,-2) grid (12,2);
    \wave
    \node at (6,1.5) [rotate=180,xscale=-1,transform shape] {electric field};
  \end{scope}
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[preamble={\usetikzlibrary{3d}}]
\begin{tikzpicture}
  \begin{scope}[canvas is zy plane at x=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}

  \begin{scope}[canvas is zx plane at y=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}

  \begin{scope}[canvas is xy plane at z=0]
    \draw (0,0) circle (1cm);
    \draw (-1,0) -- (1,0) (0,-1) -- (0,1);
  \end{scope}
\end{tikzpicture}
\end{codeexample}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "pgfmanual-pdftex-version"
%%% End:


\section{Syntax of Modules}
\label{syn-mod-sec}
For Modules there are further reserved words, identifier classes and derived
forms. There are no further special constants;
comments and lexical analysis are as for the Core.
\replacement{\thenostrsharing}{The derived forms for modules concern functors and  appear in
Appendix~\ref{derived-forms-app}.}{The derived forms for modules  appear in
Appendix~\ref{derived-forms-app}.}

%For Modules there are further keywords and identifier classes, but no
%further special constants and at present no further derived forms.
%Comments and lexical analysis are as for the Core.

\subsection{Reserved Words}
The\index{14.1} following are the additional reserved words used in Modules.\note{\thenostrsharing}{Inserted new keywords  {\tt where} and {\tt :>}}
% \begin{verbatim}
%              eqtype  functor    include   sharing
%              sig     signature  struct  structure
% \end{verbatim}
\begin{verbatim}
             eqtype    functor   include   sharing   sig
             signature   struct   structure   where   :>
\end{verbatim}
\subsection{Identifiers}
\label{syn-mod-identifiers-sec}
The additional \replacement{\thefixtypos}{syntax}{identifier} classes for Modules are SigId (signature identifiers)
and FunId (functor identifiers)\replacement{\theidstatus}{; they may be either alphanumeric -- not
starting with a prime -- or symbolic}{. Functor and signature identifiers
must be alphanumeric, not starting with a prime.}  The class of each identifier occurrence
is determined by the grammatical rules which follow.
Henceforth, therefore,
we consider all identifier classes to be disjoint.

\subsection{Infixed operators}
\label{infixopmod.sec}
In addition to the scope rules for fixity directives given for the Core syntax,
there is a further scope limitation:
if $\dir$ occurs in a structure-level declaration $\strdec$ in any of the
phrases
\[ \LET\ \strdec\ \IN\ \cdots\ \END \]
\[ \LOCAL\ \strdec\ \IN\ \cdots\ \END \]
\[ \STRUCT\ \strdec\ \END \]
then the scope of $\dir$ does not extend beyond the phrase.

One effect of this limitation is that fixity is local to a \replacement{\thenostrsharing}{generative}{basic}
structure expression --- in particular, to such an expression occurring
as a functor body.
\deletion{\thenoinfixinspec}{A more liberal scheme (which is under consideration)
would allow fixity directives to appear also as specifications, so that
fixity may be dictated by a signature expression; furthermore, it would allow an ~$\OPEN$~
or ~$\INCLUDE$~ construction to restore the fixity which prevailed
in the structures being opened, or in the signatures being included.
This scheme is not adopted at present.}

\subsection{Grammar for Modules}
\label{mod-gram-sec}
The\index{14.2} phrase classes for Modules are shown in Figure~\ref{mod-phr}.
We use the variable $\strexp$ to range over StrExp, etc.
The conventions adopted in presenting the grammatical
%\pagebreak
\begin{figure}[t]
\vspace{4pt}
\makeatletter{}
\tabskip\@centering
\halign to\textwidth
{#\hfil\tabskip1em&#\hfil\tabskip\@centering\cr
StrExp & structure expressions \cr
StrDec & structure-level declarations \cr
StrBind & structure bindings \cr
\cr
SigExp & signature expressions \cr
SigDec & signature declarations \cr
SigBind & signature bindings \cr
\cr
Spec & specifications \cr
ValDesc & value descriptions\cr
TypDesc & type descriptions\cr
DatDesc & datatype descriptions\cr
ConDesc & constructor descriptions\cr
ExDesc & exception descriptions\cr
StrDesc & structure descriptions\cr
%\ adhocinsertion{\thetypabbr}{2mm}{WhereBind & where bindings\cr}
\adhocdeletion{\thetypabbr}{2mm}{SharEq & sharing equations\cr}
\cr
FunDec & functor declarations\cr
FunBind & functor bindings\cr\adhocdeletion{\thenofuncspec}{3mm}{FunSigExp & functor signature expressions\cr
FunSpec & functor specifications\cr
FunDesc & functor descriptions\cr}TopDec  & top-level declarations\cr
}
\makeatother
\caption{Modules Phrase Classes\index{15.1}}
\label{mod-phr}
\end{figure}
rules for Modules
are the same as for the Core.
The grammatical rules are shown in Figures~\ref{str-syn},
\ref{spec-syn} and \ref{prog-syn}.

\deletion{\thenofuncspec}{It should be noted that functor specifications (FunSpec) cannot
occur in programs;
neither can the associated functor descriptions (FunDesc)
and functor signature expressions (FunSigExp).  The purpose of a $\funspec$
is to specify the static attributes (i.e. functor signature) of one
or more functors. This will be useful, in fact essential, for
separate compilation of functors. If, for example, a functor $g$
refers to another functor $f$ then --- in order to compile $g$ in
the absence of the declaration of $f$ --- at least the specification
of $f$ (i.e. its functor signature) must be available. At present there is no
special grammatical form for  a separately compilable ``chunk'' of text
-- which we may like to call call a {\sl module} -- containing a $\fundec$
together with a $\funspec$ specifying its global references. However, below in
the semantics for Modules it is defined when a
declared functor matches a functor signature specified for it. This determines
exactly those functor environments (containing declared functors
such as $f$) into which the separately compiled ``chunk''
containing the declaration of $g$ may be loaded.}
%\newpage
\begin{figure}[h]
\vspace{4pt}
\makeatletter{}
\tabskip\@centering
\halign to\textwidth
{#\hfil\tabskip1em&\hfil$#$\hfil&#\hfil&#\hfil\tabskip\@centering\cr
\strexp & ::=   & \encstrexp    & \adhocreplacementl{\thenostrsharing}{-4cm}{generative}{basic}\cr
        &       & \longstrid    & structure identifier\cr
\adhocinsertion{\thenostrsharing}{0mm}{ && \transpconstraint & transparent constraint\cr}
\adhocinsertion{\thenostrsharing}{0mm}{ && \opaqueconstraint & opaque constraint\cr}
        &       & \funappstr    & functor application\cr
        &       & \letstrexp    & local declaration\cr
\noalign{\vspace{6pt}}
%
\strdec & ::=   & \dec                          & declaration \cr
        &       & \singstrdec                   & structure \cr
        &       & \localstrdec                  & local \cr
        &       & \emptystrdec                  & empty \cr
        &       & \seqstrdec                    & sequential\cr
\noalign{\vspace{6pt}}
\strbind & ::=   & \adhocreplacementl{\thenostrsharing}{3cm}{\strbindera}{\barestrbindera} \cr
\noalign{\vspace{6pt}}
\sigexp & ::=   & \encsigexp            & \adhocreplacementl{\thenostrsharing}{-4cm}{generative}{basic}\cr
        &       & \sigid                & signature identifier\cr
\adhocinsertion{\thenostrsharing}{0mm}{&&\rlap{$\sigexp$}\hskip13mm \boxml{where type} & type realisation\cr
        &       & \hskip13mm \boxml{$\tyvarseq$ $\longtycon$ =  $\ty$}\cr}
\noalign{\vspace{6pt}}
\sigdec & ::=   & \singsigdec           & \adhocdeletion{\thenostrsharing}{0mm}{single}\cr\noalign{\vspace{6pt}}\adhocdeletion{\thenostrsharing}{0mm}{        &       & \emptysigdec          & empty\cr
        &       & \seqsigdec            & sequential\cr}%\noalign{\vspace{6pt}}
\sigbind & ::=   & \sigbinder \cr
\noalign{\vspace{6pt}}
}
\makeatother
\vspace{6pt}
\caption{Grammar: Structure and Signature Expressions\index{16.1}}
\label{str-syn}
\end{figure}

\subsection{Syntactic Restrictions}
\label{synresmod.sec}
\begin{itemize}
\item No\index{16.2} binding $\strbind$, $\sigbind$, or $\funbind$ may bind the
      same identifier twice.
\item No description $\valdesc$, $\typdesc$, $\datdesc$,
      $\exndesc$
      \replacement{\thenofuncspec}{, $\strdesc$ or $\fundesc$}{or $\strdesc$}
      may describe the same identifier
      twice; this applies also to value \replacement{\theidstatus}{constructors}{identifiers} within a $\datdesc$.
\insertion{\thecommentary}{
\item No ${\it tyvarseq}$ may contain the same ${\it tyvar}$ twice.}
\insertion{\thenostrsharing}{
\item
      Any $\tyvar$ occurring on the right side of a $\datdesc$ of the form
 $\tyvarseq \;\tycon\;\boxml{=}$ $\cdots$ must occur
      in the $\tyvarseq$; similarly, in signature expressions of the form\hfill\break $\sigexp\ \boxml{where type}\, \tyvarseq\,\longtycon\,$
$\boxml{=}\,\ty$, any $\tyvar$ occurring in $\ty$ must occur in $\tyvarseq$.}
\insertion{\thecommentary}{\item No $\datdesc$, $\valdesc$
or $\exndesc$ may describe
$\TRUE$, $\FALSE$, $\NIL$, \boxml{::} or $\REF$.
No $\datdesc$ or $\exndesc$ may describe {\tt it}.
}
\end{itemize}
%\clearpage %containing figure 'Grammar: Specifications'
\begin{figure}[m]
\vspace{4pt}
\makeatletter{}
\tabskip\@centering
\halign to\textwidth
{#\hfil\tabskip1em&\hfil$#$\hfil&#\hfil&#\hfil\tabskip\@centering\cr
\spec	& ::=	& \valspec		& value\cr
	&	& \typespec		& type\cr
	&	& \eqtypespec		& eqtype\cr
	&	& \datatypespec		& datatype\cr
\adhocinsertion{\thedatatyperepl}{2cm}{&	& \datatypereplspec		& replication\cr}
	&       & \exceptionspec        & exception\cr
        &	& \structurespec	& structure\cr\adhocdeletion{\thetypabbr}{15mm}{&	& \sharingspec	        & sharing
	&	& \localspec    	& local\cr
        &       & \openspec             & open ($n\geq 1$)\cr }\adhocreplacementtexl{\thesingleincludespec}{0mm}{        &       & \inclspec
  & include ($n\geq 1$)\cr}{        &       & \singleinclspec             & include \cr}
        &       & \emptyspec            & empty \cr
        &       & \seqspec              & sequential\cr
\adhocinsertion{\thetypabbr}{0mm}{&     & \boxml{$\spec$ sharing type}      &  sharing\cr &&\boxml{\qquad$\longtycon_1$ = $\cdots$ = $\longtycon_n$}& $(n\geq 2)$\cr}
\noalign{\vspace{6pt}}
\valdesc & ::=   & \adhocreplacementl{\theidstatus}{3cm}{\valdescription}{\valviddescription} \cr
\noalign{\vspace{6pt}}
\typdesc & ::=   & \typdescription \cr
\noalign{\vspace{6pt}}
\datdesc & ::=   & \datdescription \cr
\noalign{\vspace{6pt}}
\condesc & ::=   & \adhocreplacementl{\theidstatus}{3cm}{\condescription}{\conviddescription} \cr
\noalign{\vspace{6pt}}
\exndesc & ::=   & \adhocreplacementl{\theidstatus}{3cm}{\exndescription}{\exnviddescription} \cr
\noalign{\vspace{6pt}}
\strdesc & ::=   & \strdescription \cr
\noalign{\vspace{6pt}}
\adhocdeletion{\thenostrsharing}{0mm}{\shareq & ::=   & \strshareq            & structure sharing\cr
        &       &                       & \qquad ($n\geq 2$) \cr
        &       & \typshareq            & type sharing \cr
        &       &                       & \qquad ($n\geq 2$) \cr
        &       & \multshareq           & multiple\cr\noalign{\vspace{6pt}}
\noalign{\vspace{6pt}}}
\cr}
\makeatother
\vspace{3pt}
\caption{Grammar: Specifications\index{17}}
\label{spec-syn}
\end{figure}
%\clearpage %starting with Figure 'Grammar: Functors and Top-level declarations'

\note{\thenofuncspec}{Figure 8: Functor signature expressions and
functor specifications have been removed. Sequential and empty
functor declarations and signature declarations have been removed.
The grammar for $\topdec$ now allows sequencing (without semicolon)
of structure-level declarations, signature declarations and functor
declarations.}
%
%   1990 Definition:
%
%\begin{figure}[h]
%\vspace{4pt}
%\makeatletter{}
%\tabskip\@centering
%\halign to\textwidth
%{#\hfil\tabskip1em&\hfil$#$\hfil&#\hfil&#\hfil\tabskip\@centering\cr
%\fundec & ::=  & \singfundec           & single\cr
%        &      & \emptyfundec          & empty\cr
%        &      & \seqfundec            & sequence\cr\cr
%\noalign{\vspace{6pt}}
%\funbind & ::= & \funstrbinder        & functor binding \cr
%         &     & \qquad\qquad\qquad\qquad\optfunbinda \cr}
%\vspace*{6pt}
%\halign to\textwidth
%{#\hfil\tabskip1em&\hfil$#$\hfil&#\hfil&#\hfil\tabskip\@centering\cr
%\funsigexp & ::= & \longfunsigexpa       & functor signature expression\cr
%\noalign{\vspace{6pt}}
%\funspec & ::= & \singfunspec          & functor specification\cr
%        &     & \emptyfunspec           & empty\cr
%        &     & \seqfunspec             & sequence\cr
%\noalign{\vspace{6pt}}
%\fundesc & ::= & \longfundesc\cr
%\noalign{\vspace{6pt}}
%\topdec  & ::= & \strdec               & structure-level declaration\cr
%         &     & \sigdec               & signature declaration\cr
%         &     & \fundec               & functor declaration\cr}
%\noalign{\vspace{12pt}\hskip1cm\parbox{14cm}{{\sl Note:}\/  No $\topdec$ may
%contain, as an initial segment, a shorter top-level declaration followed by a semicolon.}
%}
%}
%\makeatother
%\vspace{6pt}
%\caption{Grammar: Functors and Top-level Declarations\index{18.1}}
%\label{prog-syn}
%\end{figure}

\begin{figure}[h]
\vspace{4pt}
\makeatletter{}
\tabskip\@centering
\halign to\textwidth
{#\hfil\tabskip1em&\hfil$#$\hfil&#\hfil&\quad#\hfil\tabskip\@centering\cr
\fundec & ::=  & \singfundec           & \cr
\noalign{\vspace{6pt}}
\funbind & ::= & \barefunstrbinder     & functor binding \cr
         &     & \qquad\qquad\qquad\qquad\optfunbinda \cr
\noalign{\vspace*{6pt}}
\topdec  & ::= & \strdecintopdec               & structure-level declaration\cr
         &     & \sigdecintopdec               & signature declaration\cr
         &     & \fundecintopdec               & functor declaration\cr
\noalign{\vspace{12pt}\hskip1cm\parbox{14cm}{{\sl Restriction:}\/  No $\topdec$ may
contain, as an initial segment, a $\strdec$ followed by a semicolon.}
}
}
\makeatother
\vspace{6pt}
\caption{Grammar: Functors and Top-level Declarations\index{18.1}}
\label{prog-syn}
\end{figure}

\deletion{\thenoclosurerestriction}{
\subsection{Closure Restrictions}
\label{closure-restr-sec}
The\index{18.2} semantics presented in later sections requires no restriction on
reference to non-local identifiers. For example, it allows a signature
expression to refer to external signature identifiers and
(via ~$\SHARING$~ or ~$\OPEN$~) to external structure identifiers; it also
allows a functor to refer to external identifiers of any kind.

However, implementers who want to provide a simple facility for
separate compilation may want to impose the following restrictions
(ignoring references to identifiers bound in the initial basis
$\B_0$, which may occur anywhere):

%However, in the present version of the language,
%apart from references to identifiers bound in the initial basis $B_0$
%(which may occur anywhere), it is required that signatures only refer
%non-locally to signature identifiers and that functors only
%refer non-locally to functor and signature identifiers.
%These restrictions ease separate
%compilation; however, they may be relaxed in a future version of the language.
%
%More precisely, the restrictions are as follows (ignoring reference to
%identifiers bound in $B_0$):
\begin{enumerate}
\item In any signature binding ~$\sigid\ \mbox{{\tt =}}\ \sigexp$~,
the only non-local
references in $\sigexp$ are to signature identifiers.
\item In any functor description ~$\funid\ \longfunsigexpa$~,
the only non-local
references in $\sigexp$ and $\sigexp'$ are to signature identifiers,
except that $\sigexp'$ may refer to $\strid$ and its components.
\item In any functor binding ~$\funstrbinder$~, the only non-local
references in $\sigexp$, $\sigexp'$ and $\strexp$ are to functor and signature
identifiers,
except that both $\sigexp'$ and $\strexp$ may refer to $\strid$ and
its components.
\end{enumerate}
In the last two cases the final qualification allows, for example, sharing
constraints to be specified between functor argument and result.
(For a completely precise definition of these closure restrictions,
see the comments to rules \ref{single-sigdec-rule}
(page~\pageref{single-sigdec-rule}),
\ref{singfunspec-rule} (page~\pageref{singfunspec-rule})
and \ref{singfundec-rule} (page~\pageref{singfundec-rule})
in the static semantics of modules, Section~\ref{statmod-sec}.)

The\index{19.1}
significance of these restrictions is that they may ease separate
compilation; this may be seen as follows. If one takes a {\sl module}
to be a sequence of signature declarations, functor specifications
and functor declarations satisfying the above restrictions then the
elaboration of a module can be made to depend on the initial
static basis alone (in particular, it will not rely on
structures outside the module). Moreover, the elaboration
of a module cannot create new free structure or type names, so
name consistency (as defined in Section~\ref{consistency-sec},
page \pageref{consistency-sec}) is automatically preserved
across separately compiled modules. On the other hand,
imposing these restrictions may force the programmer to write
many more sharing equations than is needed if functors
and signature expressions can refer to free structures.
}


\documentclass[main.tex]{subfiles}
\begin{document}

\section{Dynamics of inflation}

\marginpar{Monday\\ 2020-10-19, \\ compiled \\ \today}

Our action will be in the form
%
\begin{align}
S = S_{EH} + S_\varphi + S _{\text{matter}}
\,,
\end{align}
%
where \(S_{EH}\) is the Einstein-Hilbert action for the metric, \(S_\varphi \) is the action for the field \(\varphi \), while ``matter'' encompasses all the other fields:
%
\begin{align}
S = \frac{1}{16 \pi G} \int \dd[4]{x} \sqrt{-g} R
+ \int \dd[4]{x} \sqrt{-g} \mathscr{L}_\varphi [\varphi , g_{\mu \nu }]
+ S _{\text{matter}}
\,.
\end{align}

We are using the invariant volume element \(\dd[4]{x} \sqrt{-g}\), which represents the physical 4-volume regardless of the coordinates.

The simplest possibility for the Lagrangian of a scalar field which is able to drive inflation reads:
%
\begin{align}
\mathscr{L}_{\varphi } = -\frac{1}{2} g^{\mu \nu } \nabla_{\mu } \varphi \nabla_{\nu } \varphi  - V(\varphi )
\,,
\end{align}
%
for a \emph{real} scalar field \(\varphi \).
Also, note that we did not consider any explicit coupling of \(\varphi \) with gravity or other fields: these kinds of terms, which go by the name ``nonminimal coupling'', might look like \(\xi \varphi^2 R\).

These kinds of nonminimal theories represent one of the simplest extensions of GR: they are \emph{scalar-tensor} theories, and in them the field \(\varphi \) as well as \(g_{\mu \nu }\) can mediate gravity.
One of the theories which currently fits the cosmological data best is of this kind.

What could we put in the potential? The mass is given by \(m_\varphi^2 = \pdv*[2]{V}{\varphi }\), so a simple mass term would look like \(m^2 \varphi^2 / 2\), but we could also have quartic terms like \(\lambda \varphi^{4} / 4\): these are self-interaction terms.

The other fields will typically be negligible during inflation since their energy density is quickly ``redshifted away''.
Sometimes some of them are non-negligible: this happens if they are coupled to the scalar field, and we must consider them; we typically do so in an ``effective'' way, by inserting them into the potential \(V(\varphi )\).

We can associate an energy-momentum tensor to the scalar field: in general, it is defined by
%
\begin{align}
T_{\mu \nu }^{(\varphi )} = - \frac{2}{\sqrt{-g}} \fdv{S_{\varphi }}{g^{\mu \nu }}
\,.
\end{align}

This comes from the way we write the Einstein equations from a variational principle.
For our scalar field, integrating by parts inside the action, we find
%
\begin{align}
T_{\mu \nu } = \frac{2}{\sqrt{-g}} \qty[
    -\pdv{(\sqrt{-g} \mathscr{L}_\varphi )}{g^{\mu \nu }}
    +
    \partial_{\alpha }
    \pdv{(\sqrt{-g} \mathscr{L}_\varphi )}{\tensor{g}{^{\mu \nu }_{,\alpha }}}
    + \text{higher order terms}
]
\,.
\end{align}

% \todo[inline]{Is the derivative covariant?}

The reason for the alternating signs is that we must integrate by parts in order to get the expression in this form.
We then get
%
\begin{align}
T^{(\varphi )}_{\mu \nu } =
- 2 \frac{\partial\mathscr{L}_\varphi }{\partial g^{\mu \nu }} + \frac{2}{\sqrt{-g}} \mathscr{L}_\varphi \pdv{\sqrt{-g}}{g^{\mu \nu }}
\,,
\end{align}
%
since there is no dependence on the derivative(s) of the metric in our case.
\begin{claim}
The following expression holds:
% We need the explicit expression
%
\begin{align}
\pdv{\sqrt{-g}}{g^{\mu \nu }} = - \frac{1}{2} \sqrt{-g} g_{\mu \nu }
\,.
\end{align}
\end{claim}

\begin{proof}
The expression can be derived from the general matrix relation  \(\Tr \log M = \log \det M\), applied taking \(M = g^{\mu \nu }\): abusing the notation a bit with the indices
%
\begin{align}
\Tr \log g^{\mu \nu } = \log \det g^{\mu \nu }
\,,
\end{align}
%
which we then differentiate, using \(g = \det g_{\mu \nu }\), the fact that the trace is linear, and the matrix logarithmic derivative expression \(\delta \log M = M^{-1} \delta M\):\footnote{Which holds as long as \(M^{-1}\) and \(\delta M\) commute. }
%
\begin{align}
\Tr \qty(M^{-1} \delta M) &= \frac{ \delta \det g^{\mu \nu }}{\det g^{\mu \nu }} = \frac{ \delta (1/g)}{ 1/g}  \\
\Tr \qty(g_{\mu \nu } \delta g^{\nu \rho }) &= - \frac{ \delta g}{g}  \\
g_{\mu \nu } \delta g^{\mu \nu } &= - \frac{ \delta g}{g}
\marginnote{Symmetry of the metric's indices.}
\,.
\end{align}

Let us now consider a variation of \(\sqrt{-g}\):
%
\begin{align}
\delta \sqrt{-g} &= - \frac{1}{2} \frac{1}{\sqrt{-g}} \delta g = + \frac{1}{2} \underbrace{\frac{1}{\sqrt{-g}} g}_{-\sqrt{-g}} g_{\mu \nu } \delta g^{\mu \nu }   \\
\fdv{\sqrt{-g}}{g^{\mu \nu }} &= - \frac{\sqrt{-g}}{2} g_{\mu \nu }
\,.
\end{align}

This is the final result. We used the fact that \(g / \sqrt{-g} = - \sqrt{-g}\), which is the correct solution of \((g / \sqrt{-g})^2 = -g\) because we know  \(g\) to be negative.
\end{proof}

This yields
%
\begin{align}
T^{(\varphi )}_{\mu \nu }  &=
- 2 \pdv{\mathscr{L}_\varphi }{g^{\mu \nu }} + \mathscr{L}_\varphi g_{\mu \nu }
\\
&= \partial_{\mu } \varphi \partial_{\nu } \varphi
+ g_{\mu \nu } \qty[- \frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } - V(\varphi )]
\,.
\end{align}

\begin{claim}
In general, this is a perfect fluid stress energy tensor, with
%
\begin{align}
P &= -\frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } - V(\varphi )  \\
\rho &= - \frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta }  + V(\varphi ) \\
u_\mu &= \frac{\partial_{\mu}\varphi}{\abs{\partial \varphi }}  \\
\abs{\partial \varphi} &= \sqrt{- g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta }}
\,.
\end{align}
\end{claim}

\begin{proof}
As the parameters are given, we only need to verify that the expressions are equivalent:
%
\begin{align}
T_{\mu \nu } &= (\rho + P ) u_\mu u_\nu + P g_{\mu \nu }   \\
&= \underbrace{(- g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } )}_{\abs{\partial \varphi}^2} u_\mu u_\nu + \qty( -\frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } - V(\varphi ) ) g_{\mu \nu }  \\
&= \varphi_{, \mu } \varphi_{, \nu } - \qty(\frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } + V(\varphi )) g_{\mu \nu }
\,.
\end{align}
\end{proof}

We start by considering a homogeneous and isotropic case, and then perturb it. This is done by splitting the field into the average classical background motion of \(\varphi \), called \(\varphi_0 \)
%
\begin{align}
\varphi = \varphi (\vec{x}, t) = \varphi_0 (t) + \delta \varphi (\vec{x}, t)
\,,
\end{align}
%
where \(\varphi_0 \) will be the VEV of the field: \(\varphi_0 = \bra{0} \varphi (\vec{x}, t) \ket{0}\), while \(\delta \varphi \) encompasses the quantum fluctuations.

Are we allowed to do this kind of split?
Formally yes, but we need to guarantee that the perturbations are indeed small compared to the classical trajectory: \(\expval{ \delta \varphi^2} \ll \varphi_0^2 (t)\).
We consider the variance since it is the first nonzero moment, as \(\expval{ \delta \varphi }= 0\).

This will not always be the case, sometimes the fluctuation will be dominating; however usually for an inflationary model to work we expect that the condition is satisfied.
The fluctuations are what generates the density fluctuations which create the anisotropies in the CMB photons: we know that the size of these anisotropies is of the order of one part in \num{e5}, so we can give a qualitative argument for the perturbations of the scalar field to be relatively small compared to the mean value.

If we do the explicit computation for the energy-momentum tensor of the classical background \(\varphi_0\) only we find:
%
\begin{align} \label{eq:energy-momentum-scalar-field}
T^{0}_{0} &= - \qty( \frac{1}{2} \dot{\varphi}_0(t)^2 + V(\varphi_0 )) = - \rho_\varphi (t)   \\
T^{i}_{j} &= \qty( \frac{1}{2} \dot{\varphi}_0^2 (t) - V(\varphi_0 )) \delta^{i}_{j} = P_\varphi \delta^{i}_{j}
\,.
\end{align}

This is a perfect-fluid energy-momentum tensor.
If we are in a regime for which
%
\begin{align}
\frac{1}{2} \dot{\varphi}_0^2 (t) \ll V(\varphi_0 )
\,
\end{align}
%
when we have \(P_\varphi \approx - \rho _\varphi \): this is a \emph{quasi De Sitter} expansion, with \(w_\varphi \approx -1\).

This is achieved if the potential for the scalar field is ``flat enough'': then, we reach a \emph{friction-domination} regime, which is commonly called \emph{slow-roll} inflation.

\todo[inline]{Insert picture of flat potential}

If \(V(\varphi )\) is approximately a constant, then it mimics a cosmological constant.
Inflation is driven by the vacuum energy density associated with the scalar field.

Let us look at the slow-roll dynamics in more detail.
What is the equation of motion for this (quantum!) scalar field?

\begin{claim}

It is just the Klein-Gordon equation, which can be derived by functional differentiation of the action with respect to \(\varphi \):
%
\begin{align}
\square \varphi = \pdv{V}{\varphi }
\,.
\end{align}
\end{claim}

The Dalambertian operator here reads
%
\begin{align}
\square \varphi = \frac{1}{\sqrt{-g}} \qty(g^{\mu \nu } \sqrt{-g} \varphi_{, \mu })_{, \nu }
\,.
\end{align}

\begin{proof}
Functionally differentiating the action yields
%
\begin{align}
\fdv{S}{\varphi } &= \fdv{}{\varphi } \int \dd[4]{x} \sqrt{-g} \qty(- \frac{1}{2} g^{\alpha \beta } \varphi_{, \alpha } \varphi_{, \beta } - V(\varphi ))  \\
&= \fdv{}{\varphi } \int \dd[4]{x} \qty(\qty( \frac{\sqrt{-g}}{2} g^{\alpha \beta } \varphi_{, \alpha })_{,\beta } \varphi  - V(\varphi ) \sqrt{-g})  \\
&= \sqrt{-g} \qty( \frac{1}{\sqrt{-g}} \partial_{\beta } \qty( \sqrt{-g} g^{\alpha \beta } \partial_{\alpha } \varphi ) - \pdv{V}{\varphi }) = 0
\,.
\end{align}

\end{proof}

Let us see what this reduces to in a \textbf{flat FLRW} metric: then \(\sqrt{-g} = a^3\), so
%
\begin{align}
\square \varphi = \frac{1}{a^3} \qty(g^{00} a^3\varphi_{, 0})_{, 0}
+ \frac{1}{a^3} \qty(g^{ii} a^3\varphi_{, i})_{, i} &= \pdv{V}{\varphi }  \\
- \ddot{\varphi} - \dot{\varphi} 3\frac{\dot{a}}{a}
+ \frac{\nabla^2}{a^2} \varphi
&= \pdv{V}{ \varphi }  \\
\ddot{\varphi} + 3 H \dot{\varphi} - \frac{\nabla^2 \varphi }{a^2} &= - \pdv{V}{\varphi }
\,.
\end{align}

The term \(3 H \dot{\varphi}\) is a kind of \emph{friction} term: the propagation of the field is ``held back'' by the expansion.
If we consider the background field, it will be constant in space, so it will evolve as
%
\begin{align} \label{eq:klein-gordon-scalar-field-flat-FLRW}
\ddot{\varphi}_0 + 3 H \dot{\varphi}_0 = - \pdv{V}{\varphi_0 }
\,.
\end{align}

We then must solve this equation combined with the Friedmann equation
%
\begin{align}
H^2 = \frac{8 \pi G}{3} \qty(\rho _\varphi + \rho _m + \rho _r) - \frac{k}{a^2}
\,.
\end{align}

The matter and radiation densities scale like \(a^{-3}\) for \(\rho _m\), \(a^{-4}\) for \(\rho _r\); in this early phase the scalar field will dominate the dynamics, so the equation will simplify to
%
\begin{align} \label{eq:hubble-rate-scalar-field-flat-FLRW}
H^2 \approx \frac{8 \pi G}{3} V(\varphi_0 )
\,.
\end{align}

Under these slow-roll conditions, we also have \(\ddot{\varphi}_0 \ll 3 H \dot{\varphi}_0\): therefore equation \eqref{eq:klein-gordon-scalar-field-flat-FLRW} simplifies to
%
\begin{align} \label{eq:approx-EOM-scalar-field-flat-FLRW}
3 H \dot{\varphi}_{0} \approx - \pdv{V}{\varphi_0 }
\,.
\end{align}

We also expect that \(V\) and all of its derivatives change very slowly with \(\varphi \).
This means that in this equation we have \(\pdv{V}{\varphi_0 } \approx \const\), as well as \(H \approx \const\): this is the same equation which is obeyed by a particle under a constant force and friction: it will then reach the asymptotic ``\textbf{terminal velocity}'' and move with a constant \(\dot{\varphi}_0 \). This solution is an attractor.

We typically write this as \(\dot{\varphi} = - V' (\varphi_0 ) / 3H\), and \(H^2 = \frac{8 \pi G}{3} V(\varphi )\).

\subsection{Slow-roll parameters}

These are parameters we need to quantify how much the potential indeed looks like we expected.

The first parameter we define is
%
\begin{align} \label{eq:slow-roll-epsilon-alternative}
\epsilon
= - \frac{\dot{H}}{H^2} =
+ 4 \pi G \frac{\dot{\varphi}^2}{H^2} \approx \frac{3}{2} \frac{\dot{\varphi}^2}{V} = \frac{1}{16 \pi G} \qty(\frac{V'}{V})^2
\,,
\end{align}
%
where the manipulations follow from equations \eqref{eq:approx-EOM-scalar-field-flat-FLRW} and \eqref{eq:hubble-rate-scalar-field-flat-FLRW}.

\begin{proof}
Let us show that the manipulations work. We start by differentiating equation \eqref{eq:hubble-rate-scalar-field-flat-FLRW} with respect to time:
%
\begin{align}
2 H \dot{H} &= \frac{8 \pi G}{3} \pdv{V}{\varphi } \dot{\varphi}  \\
\dot{H} &= 4 \pi G \underbrace{\frac{V'}{3 H}}_{- \dot{\varphi}} \dot{\varphi} = - 4 \pi G \dot{\varphi}^2 \label{eq:hdot-phidot-relation}
\,,
\end{align}
%
which, divided by \(H^2\), yields the first equality.

The second equality comes from the substitution of the expression for \(H^2\) from equation \eqref{eq:hubble-rate-scalar-field-flat-FLRW}.
Then, we can square the relation \(\dot{\varphi} = - V' / 3 H\) to get
%
\begin{align}
\dot{\varphi}^2 = \qty(\frac{V'}{3H})^2
\,,
\end{align}
%
which allows us to manipulate
%
\begin{align}
\frac{3}{2} \frac{\dot{\varphi}^2}{V} &= \frac{3}{2V} \qty(\frac{V'}{3H})^2  \\
&= \frac{3 (V')^2}{2V \frac{8 \pi G}{3} V}  \\
&= \frac{1}{16 \pi G} \qty( \frac{V'}{V})^2
\,.
\end{align}
\end{proof}

\begin{claim}
The relation \(\dot{H} = - 4 \pi G \dot{\varphi}^2\) holds exactly in the case of a single scalar field with the action we have been using so far.
\end{claim}

\begin{proof}

We start from the Friedmann equation for flat spacetime:
%
\begin{align}
H^2 &= \frac{8 \pi G}{3} \rho = \frac{8 \pi G}{3} \qty(V(\varphi ) + \frac{1}{2} \dot{\varphi}^2
% - \frac{1}{2} \frac{\abs{\nabla\varphi}^2}{a^2}
)
\,.
\end{align}

Now, we proceed as we did previously, by differentiating everything with respect to time:
%
\begin{align}
2 H \dot{H} &= \frac{8 \pi G}{3} \qty( V' \dot{\varphi} + \ddot{\varphi} \dot{\varphi})  \\
% - \frac{1}{a^2} \underbrace{\partial_{i} \varphi \partial_{i} \dot{\varphi}}_{= -\dot{\varphi} \nabla^2 \varphi }
&= \frac{8 \pi G}{3} \dot{\varphi} \qty(- 3 H \dot{\varphi}) \marginnote{Equation of motion.}  \\
\dot{H} &= - 4 \pi G \dot{\varphi}^2
\,,
\end{align}
%
where we assumed homogeneity. The derivation seems not to work if we do not assume it.
\end{proof}

Requiring \(\epsilon \ll 1\) can also be stated as asking that\footnote{A note on the dimensionality: the potential \(V\) has the dimensions of the Lagrangian: in natural units, \SI{}{m^{-4}}. The field \(\varphi \) has the dimensions of an inverse length (a mass), just like the Hubble rate.}
%
\begin{align}
\frac{(V')^2}{16 \pi G V^2} &\ll 1  \\
\frac{(V')^2}{V} &\ll 16 \pi G V = \frac{2}{3} H^2 \\
\frac{1}{V} \qty(\pdv{V}{\varphi })^2 &\ll H^2
\,.
\end{align}

So, \(\epsilon \) gives a bound on the first derivative of the potential; the second derivative is controlled by the parameter
%
\begin{align}
\eta = - \frac{\ddot{\varphi}}{H \dot{\varphi}
}
\,,
\end{align}
%
and we can also define
%
\begin{align}
\eta _V = \frac{1}{3} \frac{V''}{H^2} = \frac{1}{8 \pi G} \frac{V''}{V}
\,.
\end{align}

Asking that \(\eta _V \ll 1 \) is equivalent to \(V'' \ll H^2\).

\begin{claim}
These three parameters are related by \(\eta = \eta _V - \epsilon \).
\end{claim}
% We also can ask that \(\abs{\eta _V} < 1\).

\begin{proof}
We start from the relation \(\dot{\varphi} \approx - V' / (3H)\). Differentiating it with respect to time will yield the desired relation:
%
\begin{align}
\ddot{\varphi} &\approx - \dv{}{t} \qty( \frac{V'}{3H})  \\
&= - \frac{1}{3H} V'' \dot{\varphi} - \frac{V'}{3} \underbrace{\qty(- \frac{\dot{H}}{H^2})}_{\epsilon }  \\
&= - \dot{\varphi} H \frac{V''}{3H^2} - \frac{V'}{3} \epsilon  \\
&= - \dot{\varphi} H \eta _V - \frac{V'}{3} \epsilon  \\
\underbrace{-\frac{\ddot{\varphi}}{H \ddot{\varphi}}}_{\eta } &= \eta _V - \epsilon
\,.
\end{align}
\end{proof}

\todo[inline]{Is it ok to differentiate this, even though we \emph{know }that \(\ddot{\varphi} = - V' - 3 H \dot{\varphi}\) from the EOM?}

\end{document}