\section{ Machine Learning}

According to Arthur Samuel (qtd. in Geron \cite{geron}) Machine Learning is a field of study that gives computers the ability to learn without being programmed explicitly.
This ability to learn is the property of various Machine Learning algorithms.
We will be using the terms "Machine Learning" and "learning" interchangeably.
To learn, these learning algorithms need to be trained on a collection of examples of some phenomenon\cite{burkovml}.
These collections are called \textbf{datasets}, and can be generated artificially or collected in nature.

To understand how the ML approach can solve problems better, we can examine an example application.
Let us say that we would like to build a system that can predict a type of animal movement based on accelerometer data.
To train its learning algorithm, also known as a \textbf{model}, we need to train it on a dataset that contains accelerometer measurements of different types of movement, such as walking, running, jumping and standing still.
Input to the system could be either raw measurements from all three axes, or components extracted from raw measurements such as RMS, spectral power, peak frequency and/or peak amplitude.
These inputs are also known as \textbf{features}, they are values that describe the phenomenon being observed\cite{burkovml}.
The output of the system would be a predicted type of movement.
Although we would mark each example of measurement data with what type of movement it represents, we would not define the relationship between the two directly.
Instead, we would let the model figure out the connection by itself, through the process of training.
The trained model should be general enough so that it can predict the type of movement on unseen accelerometer data correctly.
Performance of the model is measured in \textbf{accuracy}.
It tells us what percentage of input data has been predicted correctly.

There exists a large variety of different learning algorithms.
We can categorise them  broadly in several ways, and one of them depends on how much supervision the learning algorithm needs in the training process.
Algorithms like K-nearest neighbours, linear and logistic regression, Support Vector Machines fall into the category of supervised learning algorithms.
Training data that is fed into them includes solutions, also known as \textbf{labels}\cite{geron}.
The above described example is an example of a supervised learning problem.

Algorithms like k-Means and Expectation Maximization, fall into the category of unsupervised learning algorithms.
Here, training data is unlabelled, algorithms are trying to find similarities in data by themselves\cite{geron}.
Other categories exist, such as semi-supervised learning which is a combination of the previous two and reinforcement learning, where the model acts inside the environment according to learned policies\cite{geron}.

Neural Networks, algorithms inspired by neurons in human brains\cite{geron}\cite{cs231n}, can fall into either of the categories.
They are appropriate for solving complex problems like image classification, speech recognition, and autonomous driving, but they require a large amount of data and computing power for training.
They fall into the field of Deep Learning, which is a sub-field of Machine Learning.

Training of ML models is computationally demanding, and is usually done on powerful servers or computers with dedicated Graphic Processing Units to speed up training time.
After a model has been trained, data can be fed in and a prediction is computed.
This process is also known as \textbf{inference}.
The inference is computationally less intensive compared to the training process, so, with properly optimised models, we can run inference on personal computers, smartphones, tablets, and even directly in internet browsers.

\subsection{ General Machine Learning workflow}

There are several steps in ML workflow that need to happen to get from an idea to a working ML based system, and this is represented in Figure \ref{ml_workflow}.

\begin{figure}[ht]
        \centering
        \includegraphics[width=1.0\linewidth]{ml_workflow.pdf}
        \caption[Workflow diagram of solving a generic Machine Learning problem.] {Workflow diagram of solving a generic Machine Learning problem. Icons source:\cite{icons}}
        \label{ml_workflow}
\end{figure}

First, the problem has to be studied, it has to be understood what are the objectives, what are current solutions and which approach should be used.
Here, we decide on the rough type of ML model that we will use, based on the problem.
In the second step we collect and clean up data.
We should always strive to collect a large amount of quality and diverse data that represent a real world phenomenon.
Collecting that kind of data can be hard and expensive, but we can use various tools, such as data augmentation or data synthesis, thus increasing data size and variety.
Sometimes data are not collected by us, in that case we should examine them and extract information that we need.
Third, we train the ML model.
We might create something from scratch or use an existing model.
We can train several different types of models and choose the one that performs the best.
To achieve the desired accuracy, steps two and three can be repeated many times.
In step four we deploy our model and monitor its accuracy.
If accuracy drops we can always collect new data and retrain the model.

\subsection{ TinyML} \label{ml_on_embedded}

Machine Learning on embedded devices, also known as TinyML is an emerging field, which coincides nicely with the Internet of Things.
The field is experiencing a large boom in popularity, when we started working on this thesis in May, 2020, there was considerable fewer resources about it, then they are now, in December, 2020.
Nevertheless the resources about it are limited, when compared to the vast number of resources connected with Machine Learning on computers or servers.
Most of the information about it can be found in the form of scientific papers, blog posts and Machine Learning framework documentation\cite{hello_edge}\cite{tflite_risc-v}\cite{pete_tiny}\cite{daniel_edgeimpulse}\cite{eon}.

Running machine learning algorithms directly on embedded devices comes with many benefits.
\textbf{Reduced power consumption} is one of them.
In most IoT applications devices send raw sensor data over a wireless network to the server, which processes it either for visualisation or for making informed decisions about the system as a whole.
Wireless communication is one of the more power hungry operations that embedded devices can do, while computation is one of more energy efficient\cite{pete_tiny}.
For example, a Bluetooth communication might use up to 100 milliwatts, while a MobileNetV2 image classification network running 1 inference per second would use up to 110 microwatts\cite{pete_tiny}.
As deployed devices are usually battery powered, it is important to keep any wireless communication to a minimum, so minimising the amount of data that we send is paramount.
Instead of sending everything we capture, is much more efficient to process raw data on the devices and only send the results.

Another benefit of using ML on embedded devices is \textbf{decreased latency time}.
If the devices can extract high-level information from raw data, they can act on it immediately, instead of sending it to the cloud and waiting for a response.
Getting a result now takes milliseconds, instead of seconds.

Such benefits do come with some drawbacks.
Embedded devices are a more resource constrained environment when compared to personal computers or servers.
Because of limited processing power, it is not feasible to train ML models directly on microcontrollers.
Also it is not feasible to do online learning with microcontrollers, meaning that they would learn while being deployed.
Models also need to be small enough to fit on a device.
Most general purpose microcontrollers only offer several hundred kilobytes of flash, up to 2 megabytes.
For comparison, the MobileNet v1 image classification model, optimised for mobile phones, is 16.9 MB in size\cite{daniel_edgeimpulse}.
To make it fit on a microcontroller and still have space for our application, it would have to be simplified.

The usual workflow while developing Machine Learning models for microcontrollers, is to train a model on training data on a computer.
When we are satisfied with the accuracy of the model we quantize it, and convert it into a format understandable by our microcontroller.
This is  described further in Section \ref{tflite_quant}.

\section{ Neural Networks}\label{neural_networks_section}

Although the first models of Neural Networks (NN) were presented in 1943 (by McCulloch and Pitts)\cite{geron} and hailed as the starting markers of the Artificial Intelligence era, several decades of research and technological progress had to pass before they could be applied to practical, everyday problems.
Early models of NNs, such as the one proposed by McCulloch and Pitts, were inspired by how real biological neural systems work.
They proved that a very simple model of an artificial neuron, with one or more binary inputs and one binary output, is capable of computing any logical proposition when used as a part of a larger network\cite{geron}.

To learn how NNs work we can refer to Figure \ref{neuron_model}, which shows a generic version of an artificial neuron.

\begin{figure}[ht]
    \begin{subfigure}[b]{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth]{neuron_model.jpeg}
        \caption{Artificial neuron}
        \label{neuron_model}
    \end{subfigure}
    \unskip\ \vrule\
    \begin{subfigure}[b]{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth]{neural_net.jpeg}
        \caption{ 3-layer neural network}
        \label{neural_net}
    \end{subfigure}

    \caption[Mathematical model of artificial neuron and fully connected 3-layer neural network.]{(a) Mathematical model of an artificial neuron, similarities with biological neurons can be seen. (b) Fully connected 3-layer neural network. Image source: \cite{cs231n}}
    \label{neural}
\end{figure}

A Neuron takes several inputs, multiplies each input with its \textbf{weight} and sums them up.
A \textbf{bias} parameter is added to the sum, which is then passed to an activation function.

NNs consist of many neurons, which are organised into \textbf{layers}.
Neurons inside the same layer do not share any connections, but they connect to the layers before and after them.
The first layer is known as the \textbf{input} layer and last one is known as the \textbf{output} layer.
Any layers between are said to be \textbf{hidden}.
In Figure \ref{neural_net} we can see a neural network with an input layer with three inputs, two hidden layers with four neurons each, and an output layer with just one neuron.
If all inputs of neurons in one layer are connected to all outputs from the previous layer, we say that a layer is \textbf{fully connected} or \textbf{dense}, Figure \ref{neural_net} is an example of one.
NNs with many hidden layers fall into the category of Deep Neural Networks (DNN).

\subsection{ Activation functions}

Activation functions introduce non-linearity to a chain of otherwise linear transformations, which enables ANNs to approximate any continuous function\cite{geron}.
There are many different kinds of activation functions, as seen on Figure \ref{activation_functions}, such as sigmoid function and rectified linear activation function (ReLu).
A sigmoid function was used commonly in the past, as it was seen as a good model for a firing rate of a biological neuron: 0 when not firing at all, and 1 when fully saturated and firing at maximal frequency\cite{cs231n}.
It takes a real number and squeezes it into a range between 0 and 1.
It was later shown that training NNs with sigmoid activation function often hinders the training process, as saturated outputs cut off parts of networks, thus preventing the training algorithm from reaching all neurons and configuring the weights correctly\cite{cs231n}.
It has since fallen out of practice, and is nowadays replaced by ReLu or some other activation function.

\begin{figure}[ht!]
        \centering
        \includegraphics[width=1.0\linewidth]{activation_func.pdf}
        \caption{Different activation functions and their equations.}
        \label{activation_functions}
\end{figure}

Another commonly used activation function is a softmax function (seen in \ref{softmax_equ}, which takes a vector as an input, computes the exponential of every element and divides that with the sum of exponentials of all elements\cite{geron}
The input vector of values becomes a vector of probabilities.
Softmax is usually used as an activation in the last layer of a classifier network.
\begin{equation}\label{softmax_equ}
    \sigma(y_i) = \frac{e^{y_i}}{\sum_{j=1}^{K}e^{y_j}}\;\;\;\text{for}\;\;i\;=\;1,\cdots,K\;\;\text{and}\;\;y=(y_1,\cdots,y_k)\in\mathbb{R}^K
\end{equation}
Where:\\
$y$ - Input vector\\
$K$ - Number of elements in the input vector\\
$\sigma(y_i)$ - Computed probability of the i-th element in the input vector

\subsection{ Backpropagation}

Training of Neural Networks is done with a training algorithm, known as \textbf{backpropagation}.
As mentioned before, we train the Neural Network by showing it a large amount of training data with labels.
At the start of the training phase, all weights and biases are set to randomly small values.
During each training step, a Neural Network is shown a small batch of training data.
Each instance is fed into the NN and the final output label is calculated.
This is known as \textbf{forward pass}, which is the same as making predictions, except that intermediate results are stored from each neuron from every layer.
Calculated output is compared to an expected one using a \textbf{loss} (also known as \textbf{cost}) function.
The loss function returns a single value, which tells us how badly our is NN performing: the higher it is, the worse is our NN performing.
The goal is to minimise the loss function, thus increasing the accuracy of our NN.
In the context of multivariable calculus, this means that we have to calculate the negative gradient of weights and biases, which will tell us in which direction we have to change each weight and bias so that the value of loss function decreases.

Doing this for all weights and biases at the same time would be complicated, so the backpropagation algorithm does this in steps.
After computing the loss function, the algorithm calculates analytically how much each output connection contributed to the loss function (essentially the local gradient) with the help of previously stored intermediate values.
This step is done recursively for each layer until the first input layer is reached.
At that moment the algorithm knows in which direction each weight and bias should change, so that the value of the loss function lowers.
A procedure is then performed, known as a \textbf{Gradient Descent}.
All local gradients are multiplied with a small number known as a \textbf{learning rate}, and then subtracted from all weights and biases.
This way, in each step we change weights and biases slowly in the right direction, while minimising the loss function.
Gradient Descent is not only used when training neural networks but also when training other ML algorithms.

We do not have to execute a backpropagation algorithm for each training instance, instead, we can calculate predictions for a small set of training data, calculate the average loss function and then apply backpropagation.
\clearpage

\subsection{ Convolutional Neural Networks}\label{cnn_para}

Convolutional Neural Networks (CNN) are a kind of Neural Networks that work especially well with image data.
Like NNs they have found inspiration in nature, in their case the visual cortex of the brain.\footnotemark
\footnotetext{Scientists Weisel and Hubel showed that different cells in the primary visual cortex of a cat responded differently to different visual stimuli\cite{cs231n}.
Some were activated when shown a horizontal line in a specific location, some were activated by vertical lines.
More complex cells responded to boxes, circles and so on.
CNNs also detect simpler shapes first and use them to detect more complex ones later.}

In Figure \ref{convnet} we can see an example of CNN which takes an image of a car as an input and outputs probability results in five different classes.
\newline
\begin{figure}[ht]
        \centering
        \includegraphics[width=1.0\linewidth]{convnet.jpeg}
        \caption[Structure of a Convolutional Neural Network.]{Structure of a Convolutional Neural Network. Image source:\cite{cs231n}}
        \label{convnet}
\end{figure}

Specific to CNNs are two different types of layers, \textbf{convolutional} layers and \textbf{pooling} layers.
Each convolutional layer detects some sort of shapes: the first ones detect different kinds of edges, while later ones detect more complex shapes and objects, like wheels, legs, eyes, ears.
Pooling layers downsample the data in the spatial dimension, thus decreasing the number of parameters and operations needed in CNN.
After a few alternating pairs of convolutional and pooling layers, the output of the last pooling layer is flattened out into one dimensional vector and fed into a fully connected NN which produces probability results in given classes.

It makes sense to explain how convolutional and pooling layers work in greater detail, as this will be important later when we will be designing our CNN models in Section \ref{cnn_ref}.

\subsubsection{ Convolutional layers}

Data that CNNs operate on are 3 dimensional matrices, where width and height correspond to an image resolution, and depth corresponds to the number of colour channels, 3 for colourful images (red, green, blue) and 1 for greyscale.
When speaking about these matrices we will refer to them as volumes.

Convolution layers perform dot products between input volume and several \textbf{filters} or \textbf{kernels} to produce output volume.
In these layers, filters are configured through the training phase.
We can see a concrete example in Figure \ref{conv1}.
2D filter with size 2 x 2 covers a part of the input volume, over which element-wise multiplication is computed, elements are summed and the result is written into the first element of output volume.
\newline
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.75\linewidth]{conv1.png}
\caption[Dot product operation between filter and zero-padded input matrix.] {Dot product operation between filter and zero-padded input matrix. Image sources: \cite{conv_layer_img}}
    \label{conv1}
\end{figure}

The filter then moves a fixed distance or \textbf{stride} and the process is repeated.
It is important to note that, although we can choose the width and height of the filter, the depth of the filter is always equal to the depth of the input volume.
If the depth is larger than one, then dot products are done for each 2D matrix in the depth dimension separately, and then an element-wise sum operation is performed between these matrices.
To avoid losing information from the image pixels that are on the edges (as they would be included in dot products fewer times compared to central ones) we often pad input images with zeros.

The size of output volume depends on several factors as seen in \ref{size_eq}.
\begin{equation}\label{size_eq}
V_{o} = (V_{i} - F + 2P) / S + 1
\end{equation}
Where:\\
$V_{i}$ - Input volume size, only width or height\\
$V_{o}$ - Output volume size, only width or height\\
$F$ - Filter or receptive field size\\
$P$ - Amount of zero padding used on the border\\
$S$ - Stride length

If we examine the example in Figure \ref{conv1} we can see that input with a size 3 x 3, stride 1, padding 1 and filter with a size 2 x 2, produces an output with size 4 x 4.

The depth of output volume is equal to the number of filters used in the convolutional layer as seen in Figure \ref{conv2}, it is a norm that a single convolutional layer uses a large number of filters to produce a deep output volume\cite{cs231n}.
It is also common to set padding, stride and filter size so that the width and height of the input volume are preserved.
This prevents the information at the edges from being lost too quickly\cite{cs231n}.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.5\linewidth]{conv2.jpeg}
\caption[Convolutional layer with five different filters.] {Convolutional layer with five different filters. Image sources: \cite{conv_layer_img}\cite{cs231n}}
    \label{conv2}
\end{figure}

At the end of the convolutional layer the output volume is fed into neurons similar to the one described in Section \ref{neural_networks_section}.
All elements in the same depth are affected by the same bias term and fed into the activation function.
The size of the volume is preserved in this step.

\subsubsection{ Pooling layers}

Pooling layers perform the downsampling of input volumes in both width and height dimensions.
This is done by sliding a filter of fixed size over the input and doing a MAX operation on elements that the filter covers, and only the largest value element is copied into the output (Figure \ref{pool_layer}).
Pooling is done on each depth slice separately from other slices, so depth size is preserved through the layer.
\newline
\begin{figure}[ht]
    \begin{subfigure}[b]{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth]{pool1.jpeg}
        \caption{Max pooling operation}
        \label{pool1}
    \end{subfigure}
    \unskip\ \vrule\
    \begin{subfigure}[b]{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth]{pool2.jpeg}
        \caption{ Effect of polling on input dimensions}
        \label{pool2}
    \end{subfigure}
    \caption[Polling layer.] {Pooling layer. Image source: \cite{cs231n}}
    \label{pool_layer}
\end{figure}

It is common to select pool size 2 x 2 and stride 2.
Like this, inputs are downsampled by two in height and width dimensions, discarding 75 \% of activations.
Pooling layers therefore reduce the number of activations, and prepare them to be flattened out and fed into a fully connected layer.

\section{ Transfer Learning}\label{transfer_para}

Transfer Learning is a ML technique, which transfers knowledge that was learned on one problem, to another, similar problem.

We mentioned earlier in Section \ref{cnn_para} that CNNs learn in first few layers how to detect simple shapes, like edges and lines, which are in later layers used to detect boxes, circles, faces and so on.
This behaviour is not limited only to CNNs, but to all DNNs.

The task of detecting simple shapes in first few layers of DNNs can be common between models that are trying to solve a similar problem.
Example would be a model classifying images depicting different types of objects and a model classifying images with different types of animals.
First model is trying to solve a more general problem than the seconds one, however, the learned weights in first few layers from first model can be reused in the second model.

And this is exactly what Transfer Learning is all about, it is possible to train a big, complex model on a large dataset and then reuse first few layers in a different model.
Example Transfer learning is shown in Figure \ref{transfer_learning}.
It is important to note that the weights of transferred layers are fixed, they are not being trained.
\newline
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.75\linewidth]{transfer_learning.png}
    \caption[Transfer Learning.] {Transfer Learning. Image source: \cite{geron}}
    \label{transfer_learning}
\end{figure}

Benefits of using Transfer learning are:

\begin{itemize}
    \item \textbf{Shorter training time.} Using a pre-trained model with fixed weights and adding few trainable layers on its top greatly lower training time, simply because there are less parameters to train.
    \item \textbf{No need for large datasets.} Training DNN usually requires large amount of data, which we might not have. Using a pre-trained model, that was trained on a large dataset, and applying it to a specific small dataset yielded better performance than just training a model from scratch\cite{geron}.
\end{itemize}

\section{ TensorFlow}

TensorFlow is a free and open-source framework for numerical computation.
It is particularly suited for large-scale Machine Learning applications\cite{geron}.
It started as a proprietary project developed by a Google Brain team at Google in 2011, and became open-source in late 2015.
It is used in many of Google's products such as Gmail, Google Cloud Speech and Google Search.

TensorFlow gives programmers tools for creating and training ML models, without needlessly diving into the specifics of computing Neural Networks.
Programmers can write high level code in Python API, which calls a highly efficient C++ code.
When using TensorFlow, the hardest part of an ML project is usually data preparation.
After that is done, the creation of an ML model, its training and evaluation can be done in a few lines of Python code.

TensorFlow also supports Keras high level API for building ML models.
Keras is a Python library that functions as a wrapper for TensorFlow.
When building ML models developers can use Keras Sequential API, where each layer in a model is represented as one line of code.
Users do not need to care about connections between the layers, they only need to choose the type of layer (convolutional, max pool, fully connected), its size and a few other specific parameters.
Sequential API is used most of the time, but if a finer level of control is needed TensorFlow provides low level math operations as well.

Finally, TensorFlow's trained model is portable\cite{geron}.
Models can be trained in one environment and executed in another.
This means that we can train our model by writing Python code on a Linux machine and execute it with Java on an Android device.
This last functionality is important for running ML models on microcontrollers.

\subsection{ TensorFlow Lite for Microcontrollers} \label{tflite_quant}

TensorFlow Lite (TFLite) is a set of tools and libraries that enable running ML inferences on constrained devices\cite{tensorflow_github}.
It provides support for Android and iOS devices, and embedded Linux.
TensorFlow Lite for Microcontrollers (TFLite Micro) is a recent port of TFLite (as of mid 2019), dedicated to running ML models on microcontrollers.
TFLite itself provides API in different languages, such as Java, Swift, Python and C++.
TFLite Micro uses C++ API, specifically C++11, which reuses a large part of the general TensorFlow codebase.

TFLite Micro library does not require any specific hardware peripherals, which means that the same C++ code can be compiled to run on a microcontroller or a personal computer with minimal changes.
Users are only expected to implement their version of the \verb|printf()| function.
As microcontroller binaries are usually quite big, flashing firmware to a microcontroller is a time consuming process.
It makes sense to first test and debug a program that includes only ML inference specific code on a personal computer, before moving on to a microcontroller, to save time.
Implementation of the test setup is described in \ref{build_system_label}.

The TFLite Micro library is available publicly as a part of a much larger TensorFlow project on GitHub\cite{tensorflow_github}.
To use the library for embedded development the whole project has to be cloned from the GitHub.
The TensorFlow team provides users with several example projects that have been ported to several different platforms, such as Mbed, Arduino, OpenMV and ESP32.
Example projects show how to use TFLite API, while showcasing different ML applications: Motion detection, wake word detection and person detection.

Is important to know that TFLite is just an extension of the existing TensorFlow project.
General steps for creating a trained ML model are still the same as seen in Figure \ref{ml_workflow}, although we have to be aware of some details.
Figure \ref{micro_workflow} shows all steps that are needed to prepare an ML model for running on a microcontroller.

\begin{figure}[ht]
    \centering
    \includegraphics[width=1.0\linewidth]{micro_workflow.pdf}
    \caption[Workflow of preparing an ML model for an inference on a microcontroller.]{Workflow of preparing a ML model for an inference on a microcontroller. Icons source:\cite{icons}}
    \label{micro_workflow}
\end{figure}

We start with a small but inaccurate model that can still accomplish the basic criteria that our objective demands.
When the end of this workflow process is reached and we made sure that the model can fit into a flash memory area of our target microcontroller, we can start training more complex models to increase accuracy.
We are allowed to use only operations that have supported implementations on microcontrollers.
This is usually not a restriction, as many of them are supported.

The model that is created is usually quite big, and needs to be converted with the TensorFlow Lite Converter tool.
This tool provides a non-optimised conversion and several different optimised conversions.

To import and use the optimised model, we need to convert it into binary format, which is done with the command line tool xxd.
The model is then ready to be executed on a microcontroller, we can run it and process the results.
Accuracy will be the same as compared to running the same .tflite model on a personal computer, but execution time will naturally be different.
If needed, we can tweak the model parameters, train a new model and repeat the described workflow again.

\subsubsection{ Post-training quantization}

By using quantization optimisation we approximate floating-point numbers in a different format, usually with 8-bit integers.
When computing Neural Networks we can quantize weights, biases and intermediate values output by separate neurons.
Quantization has a dramatic effect on the size of the model and its execution speed.
By changing 32-bit floating-point numbers with 8-bit integers size decreases by a factor of 4.
Floating-point math is by nature slow to compute, many microcontrollers do not even have a floating-point unit.
In comparison integer math is faster to compute, therefore quantized models are executed faster.
Model accuracy decreases after using quantization, but usually by less than a percent.

\section{ IoT and wireless technologies}

The Internet of Things, or IoT, is a system of uniquely identifiable devices, which communicate with each other or other systems over wireless networks\cite{IoT}.
A device or a thing is a battery powered embedded system such as smart watch, heart monitor, or animal tracker which would transmit collected sensor data to an IoT gateway, which would relay the data over to the cloud.
These data can then be analysed and displayed in such a fashion which would provide businesses or users with valuable information.
Examples of this would be tracking the energy consumption of machines in factories, monitoring conditions of crops in agriculture, or monitoring locations of endangered species in African conservation parks.

An important part of the IoT system is a wireless network that is used to transport data from edge devices to gateways, or directly to the Internet.
The choice of a wireless network is highly dependent on a type of problem an IoT solution is trying to solve.
Factors such as required battery life, amount of data being sent, the distance that data have to travel and environment conditions of the edge device itself are important.

Because our early warning system demands a decent battery life of several months and needs to send a small amount of data over one or two kilometres, we will focus on wireless technologies such as NB-IoT, Sigfox and LoRa.

Narrowband IoT or NB-IoT is a radio technology standard developed by the 3GPP standard organisation\cite{lora_nbiot}.
NB-IoT was made specifically with embedded devices in mind, it has a range of up to 15 \si{\kilo\meter} and it has deep indoor penetration\cite{lora_nbiot}.
Compared to Sigfox and LoRa it has better latency and a higher data rate, but also higher power consumption\cite{lora_nbiot_sigfox}.
However it is unsuitable for our use case as it operates on the network provided by the cellular base towers, which is inconvenient as the mobile connection in Assam, India can be inconsistent\cite{wildlabs-elephants}.

Sigfox is a radio technology developed by the company of the same name that operates on an unlicensed Industrial, Scientific and Medical (ISM) radio band.
In many views it is similar to LoRa, as it has the comparative range and power consumption\cite{lora_nbiot_sigfox}.
However, there are a few important differences.
Although Sigfox modules are a bit cheaper when compared to Lora modules, each message is paid, devices are limited to 12 bytes per uplink, 140 uplinks per day and only 4 downlinks are available per day.
Sigfox devices can also only communicate with base stations, installed by the Sigfox company\cite{lora_nbiot_sigfox}.
This means that users can not build their own network and are dependent on the coverage provided by Sigfox.

This leaves us with the Lora protocol, which covers our use case from points view of long range, low power consumption and the ability to set up our own network.

\subsection{ LoRa and LoRaWAN}

LoRa (Long Range) is a physical layer protocol that defines how information is modulated and transmitted over the air\cite{lora_article}\cite{lora_nbiot}.
The protocol is proprietary and owned by a semiconductor company, Semtech, who is the sole designer and manufacturer of Lora radio chips in the world.
The LoRa protocol uses a modulation similar to chirp spread spectrum modulation\cite{lora_article}.
As the protocol is proprietary, exact details of it are not known, although it was reverse engineered by a radio frequency specialist\cite{lora_github}.
An example of a LoRa signal that was captured with a software defined radio can be seen in Figure \ref{lora1}.
Each symbol is modulated into a radio signal whose frequency is either increasing or decreasing with a constant rate inside of a specified bandwidth.
When the bandwidth boundary is reached, the signal "wraps around" and appears at the other boundary.
Although the frequency is always changing with a constant rate, it is not continuous inside the bandwidth window, but it can change to a different frequency immediately and continue from there.
\newline
\begin{figure}[ht]
    \begin{subfigure}{0.3\textwidth}
        \centering
        \includegraphics[width=0.8\textwidth, height=6cm]{lora_signal.png}
        \caption{ LoRa signal}
        \label{lora1}
    \end{subfigure}
    \hspace{0.5cm}%
    \begin{subfigure}{0.7\textwidth}
        \includegraphics[width=\textwidth, height=6cm]{lora_properties.png}
        \caption{ Properties of LoRa signal}
        \label{lora2}
    \end{subfigure}
\caption[Properties of Lora signal.] {Lora signal (left) and different properties of LoRa with their effects on range, bit rate, receiver sensitivity, time on air and consumption (right). Image sources:\cite{lora_github}\cite{lora_philly}}
    \label{lora}
\end{figure}

This kind of modulation gives LoRa extreme resiliency against the interference of other radio frequency signals that might be using the same frequency band\cite{lora_article}\cite{lora_philly}.
For example, on a lower part of Figure \ref{lora1} we can see a signal with constant frequency transmitting inside the bandwidth window that the LoRa signal is using.
This kind of interference is filtered out easily by a LoRa receiver.

The size of a bandwidth window, rate of frequency change (also known as a spreading factor) and transmitting power further define the LoRa signal.
With these factors, we can influence the range, power consumption and bit rate of a LoRa signal.
For example, as seen in Figure \ref{lora2}, by increasing the spreading factor we increase the time on air, thus giving the receiver more time to sample the signal, which leads to better sensitivity, but increases power consumption.

While LoRa defines the physical layer, LoRaWAN defines the media access control protocol for wide area networks, which are built on top of LoRa\cite{lora_article}.
Its specification is open, so anyone can implement it.
LoRaWAN takes care of communication between end-devices and gateways and manages communication frequency bands, data rates and transmitting power.

LoRaWAN has a star of stars topology\cite{lora_article}.
Devices deployed in the field transmit messages on frequency bands that differ from region to region.
Messages are received by gateways which relay them to the network server.
The network server displays relayed messages, decodes them and sends them to various applications.
If the same message is heard by several gateways, the server drops all duplicates.
The server also decides which gateway will send a downlink message to a specific device.

Because LoRaWAN operates on an unlicensed ISM band, anyone can setup up their network without any licensing fees.
For some use cases, a single gateway with an internet connection is enough to provide coverage to a large number of devices.

\section{ Thermal cameras} \label{thermal_cameras}

Thermal cameras are transducers that convert infrared (IR) radiation into electrical signals, which can be used to form a thermal image.
A comparison between a normal and a thermal image can be seen in Figure \ref{thermal_comparison}.
\newline

\begin{figure}[ht]
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{thermal_sample_b.png}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{thermal_sample_a.png}
    \end{subfigure}
    \caption[Comparison between a normal image and thermal image]{Comparison between a normal image and thermal image. Image source: Arribada Initiative\cite{thermal_comparison}}
    \label{thermal_comparison}
\end{figure}

IR is an electromagnetic (EM) radiation, and covers part of the EM spectrum that is invisible to the human eye.
The IR spectrum covers wavelengths from 780 \si{\micro\meter} to 1 \si{\milli\meter}, but only a small part of that spectrum is used for IR imaging (from 0.9 \si{\micro\meter} to 14 \si{\micro\meter})\cite{thermal_book}.
We can classify IR cameras broadly into two categories: photon detectors or thermal detectors\cite{thermal_book}.
Photon detectors convert absorbed EM radiation directly into electric signals by the change of concentration of free charge carriers\cite{thermal_book}.
Thermal detectors covert absorbed EM radiation into thermal energy, raising the detector temperature\cite{thermal_book}.
The change of the detector's temperature is then converted into an electrical signal.
Since photon detectors are expensive, large and therefore unsuitable for our use case, we will not describe them in greater detail.

Common examples of thermal detectors are thermopiles and microbolometers.
Thermopiles are composed of several thermocouples.
Thermocouples consists of two different metals joined at one end, which is known as the hot junction.
The other two ends of the metals are known as cold junctions.
When there is a temperature difference between the hot and cold junctions, a voltage proportional to that difference is generated on the open ends of the metals.
To increase voltage responsivity, several thermocouples are connected in series to form a thermopile\cite{thermal_book}.
Thermopiles have lower responsivity when compared to microbolometers, but they do not require temperature stabilisation\cite{thermal_book}.

Microbolometers can be found in most IR cameras today\cite{thermal_book}.
They are sensitive to IR wavelengths of 8 to 14 \si{\micro\meter}, which is a part of the long wave infrared region (LWIR)\cite{thermal_book}.
Measuring part of a microbolometer is known as Focal Point Array (FPA) (Figure \ref{FPA}).
FPA consists of IR thermal detectors, bolometers (Figure \ref{FPA_pixel}), that convert IR radiation into an electric signal.
Each bolometer consists of an absorber material connected to a Readout Integrated Circuit (ROIC) over thermally insulated, but electrically conductive legs\cite{thermal_article}.
\begin{figure}[h]
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{FPA.png}
        \caption{Focal point array}
        \label{FPA}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{FPA_pixel.png}
        \caption{Bolometer}
        \label{FPA_pixel}
    \end{subfigure}
    \caption[Focal point array and bolometer.] {(a) Focal point array under electronic microscope. (b) Bolometer with $\lambda /4$ resonant cavity. Image source: Vollmer, Möllmann\cite{thermal_book}}
    \label{FPA_microbolo}
\end{figure}

Absorber material is either made out of metals such as gold, platinum, titanium, or, more commonly, out of semiconductors such as vanadium-oxide (VOx)\cite{thermal_article}.
The important property of absorber materials is that electrical resistance changes proportionally with the material's temperature\cite{thermal_book}.
When IR radiation hits absorber material, it is converted into thermal energy, which raises the absorber's temperature, thus changing its resistance.
To detect the change in resistance, ROIC applies a steady-state bias current to the absorber material, while measuring voltage over the conductive legs\cite{thermal_book}.

When deciding between different types of thermal cameras we are often comparing them in terms of the cost, size and image resolution.
One important property that also has to be taken into account is temperature sensitivity, also known as Noise Equivalent Temperature Difference (NETD).
Comparison between images taken with two cameras with different NETD values can be seen in Figure \ref{NETD}.
\newline
\begin{figure}[ht]
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{NETD_60mk.png}
        \caption{NETD is 60 \si{\milli\kelvin}}
        \label{NETD_60mk}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=5cm]{NETD_80mk.png}
        \caption{NETD is 80 \si{\milli\kelvin}}
        \label{NETD_80mk}
    \end{subfigure}
    \caption[Comparison of images of the same object taken with cameras with different NETD values.]{Comparison of images of the same object taken with cameras with different NETD values. Low NETD values are more appropriate for object recognition. Image source: MoviTherm \cite{NETD}}
    \label{NETD}
\end{figure}

NETD is measured in \si{\milli\kelvin}, and tells us the minimum temperature difference that can still be detected by a thermal camera.
In microbolometers, NETD is proportional to the thermal conductance of the absorber material, among other factors\cite{thermal_book}.
The thermal conductance of bolometers is minimised by enclosing FPA into the vacuum chamber, thus excluding thermal convection and conduction due to the surrounding gases.
The only means of heat transfer that remain are radiant heat exchange (highly reflective material below the absorber is minimising its radiative losses), and conductive heat exchange through the supportive legs.
NETD also depends on the temperature inside the camera, as higher ambient temperatures can raise the internal temperature, thus increasing the NETD and noise present in the thermal image.
Today's thermopiles can achieve NETD of 100 \si{\milli\kelvin}, microbolometers 45 \si{\milli\kelvin}, while photon detectors can have NETD of 10 \si{\milli\kelvin}.
Although tens of \si{\milli\kelvin} does not seem a lot, we can see in Figure \ref{NETD} what a difference of 20 \si{\milli\kelvin} means for image resolution and noise.

\subsection{ Choosing the thermal camera} \label{choosing_thermal}

The choice of thermal camera was made by the Arribada Initiative\cite{thermal_comparison}.
They tested several different thermopiles and microbolometers while searching for the desired properties.
The camera had to be relatively inexpensive, and small enough so that it could be integrated into a relatively small housing.
The main property that they searched for was that elephants could be recognised easily from thermal images.
That meant that the camera needed to have decent resolution and low NETD.
Cameras were tested in Whipsnade Zoo and the Yorkshire Wildlife Park where images of elephants and polar bears could be made.

They tested two thermopile cameras (Heimann 80x64, MELEXIS MLX90640) and two microbolometer cameras (ULIS Micro80 Gen2, FLIR Lepton 2.5).
Although thermopile cameras were cheaper than microbolometer cameras, the quality of images they produced was inferior, as can be seen in Figure \ref{thermal_comparison_images}.
\clearpage
\begin{figure}[ht]
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=4.5cm]{thermal_comparison_a.png}
        \label{thermal_comparison_a}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=4.5cm]{thermal_comparison_b.png}
        \label{thermal_comparison_b}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=4.5cm]{thermal_comparison_c.png}
        \label{thermal_comparison_c}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \centering
        \includegraphics[width=1.0\linewidth, height=4.5cm]{thermal_comparison_d.png}
        \label{thermal_comparison_d}
    \end{subfigure}
\caption[Comparison of image quality made by different thermal cameras.]{Comparison of image quality made by different thermal cameras, MELEXIS MLX90640 (top left), Heimann 80x64 (top right), ULIS Micro80 Gen2 (bottom left) and FLIR Lepton 2.5 (bottom right). Image source: Arribada Initiative \cite{thermal_comparison}}
    \label{thermal_comparison_images}
\end{figure}

The MELEXIS MLX90640 camera had a resolution of 32 x 24 pixels and NETD of 100 \si{\milli\kelvin}, while the Heimann camera had a resolution of 80 x 64 pixels and NETD of 400 \si{\milli\kelvin}.
It was concluded that images taken by either one of the thermopile cameras could not be used for object recognition, merely only if the object was present or not\cite{thermal_comparison}.

Microbolometers produced better results.
Both the Ulis Micro80 and FLIR Lepton had a similar resolution, 80 x 80 and 80 x 60 respectively, but the Ulis Micro80 had two times bigger NETD compared to the FLIR Lepton camera, 100 \si{\milli\kelvin} and 50 \si{\milli\kelvin}, respectively.
Images produced by the FLIR Lepton were much cleaner, so it was chosen as an appropriate camera for the task.

It is important to note that the FLIR Lepton, like all microbolometers, requires frequent calibration to function properly.
In temperature non-stabilised cameras small temperature drifts can have a major impact on image quality\cite{thermal_book}.
Calibration is done either by internal algorithms of the camera or by exposing the camera to a uniform thermal scene.
The FLIR Lepton camera comes with a shutter, which acts as a uniform thermal signal and enables regular calibration.
Calibration in the FLIR Lepton is by default automatic, triggering at startup, and every 3 minutes afterwards or if camera temperature drifts by more than 1.5 \si{\celsius}.

The FLIR Lepton camera comes in two versions, 2.5 and 3.5.
Both cameras function the same and have exactly the specifications, they only differ in resolution: the 3.5 has a resolution of 120 x 160, while the 2.5 has 60 x 80.
Both were used in the process of image collection.


\documentclass[../../main.tex]{subfiles}
\begin{document}
% START SKRIV HER

\section{Model Evaluation}

\subsection{Classification}
A commonly used method of evaluation classification models is via a confusion matrix. It compares predictions against actual observations. In our case, predicting out-of-stock is defined as the positive class. From the confusion matrix, we get the false positives (the item was predicted to go out-of-stock, but it was in-stock), true positives (the item was predicted to go out-of-stock and actually did), false negatives (the item was predicted to be in-stock, but it went out-of-stock) and true negatives (it was predicted to be in-stock and did not go out-of-stock). This method is highly intuitive and valuable in a business setting. One can attribute a cost to each of the cases, and calculate a model's profit. The overall ccuracy is the most basic performance measure and is defined as

Accuracy = TP + TN / TP + TN + FP + FN.

Confusion matrix with positive class on the right side.

%%%%%%

\documentclass[11pt]{report}
\usepackage{graphicx}

%%%% Confusion Matrix Body %%%%
\newcommand\MyBox[1]{%
  \fbox{\parbox[c][1.7cm][c]{1.7cm}{\centering #1}}%
  % Size of boxes
}
\newcommand\MyVBox[1]{%
  \parbox[c][1cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr1.7cm+2\fboxsep\relax]{%
  \parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\newcommand\MyTBox[4]{%
  \MyVBox{#1}
  \MyBox{#2}\hspace*{-\fboxrule}%
  \MyBox{#3}\par\vspace{-\fboxrule}%
}
%%%%
\newcommand*\rot{\rotatebox{90}}

\begin{document}

\begin{figure}
\begin{center}
{

\offinterlineskip

\raisebox{-5cm}[0pt][0pt]{
\parbox[c][0pt][c]{0cm}{\hspace{-3.5cm}\rot{\textbf{Actual}}\\[20pt]}}\par

\hspace*{1cm}\MyHBox[\dimexpr3.4cm+6\fboxsep\relax]{Predicted}\par

\hspace*{1cm}\MyHBox{30}\MyHBox{90}\par

\MyTBox{30}{9418}{1471}

\MyTBox{90}{144}{317}

}
\end{center}
\end{figure}
\end{document}

%%%%%%

\subsubsection{Precision and Recall}
Precision and recall measures different types of errors, and are useful when the two represent different costs (James et al, 2013). Precision is the accuracy of predicting the positive class. In our case, when we predict a stockout, how accurate are we? Formally, it is written as

Precision = TP / TP + FP

Recall measures how effective the model is at detecting the positive class:

Recall = TP / TP + FN.

When predicting stockouts, recall measures how skilled the model is at detecting a stockout. Combined, they represent a trade-off, where precision is defined as one minus recall. This is especially relevant if the dataset is imbalanced. For instance, since stockouts occur in 30\% of the products, a model that always predicts that a product will go out-of-stock will achieve an overall accuracy of 70\%, even though the model does not represent any skill. It is as good as guessing.

\subsubsection{Receiver Operating  Characteristic (ROC) Curve}
A Receiver Operating Curve (ROC) is often recommended in a classification setting because it controls for class imbalances (Bradley, 1997). It is represented by a quadratic square, where the false positive rate is measured on the $X$-axis, while the true positive rate is measured on the $Y$-axis. A diagonal represents a model with no skill, ie. this accuracy would be achieved by predicting the most common class. The two dimensions represents the trade-off between the false positive rate and the true positive rate. The trade-offs are represented by the ROC curve, and are achieved by using a different threshold for converting probabilities to classifications. It follows that an optimal threshold can be achieved along any point on the ROC curve, depending on the cost associated by the different errors. A model has more skill if the ROC curve moves closer to the corner such that the area under the ROC curve increases. Hence, the area under the ROC curve is considered a good measure for a classification model (James et al., 2013).

Bradley 1997: https://www.sciencedirect.com/science/article/pii/S0031320396001422

\subsection{Regression}
Machine learning models use a loss function that penalizes errors when training. It follows that the choice of loss function will affect how the model learns. For a regression problem, the most common evaluation methods are root mean squared error (RMSE) and mean absolute error (MAE). Mean absolute error is defined as

MAE = 1 / N sum(abs(yhat - y)),

while RMSE is defined as

$RMSE = sqrt(1 / N(sum(yhat - y)^2)).$

For our problem, we choose RMSE as the loss function. The reason is that RMSE penalized large deviations more than small deviations, which has substantial impact on our business problem. In our dataset, products with a larger stockout quantity is intuitively products that sell more, ie. more popular products. It could be argued that these products have a higher impact, as they sell more and the company can delight more customers with these products. In comparison, a book about flowers written in Old English that sells one unit a year is not that important to the company. As a consequence, we want to avoid large deviations, as this means a higher error for the most popular products.

% SLUTT SKRIV HER
\end{document}


\chapter{Semantics for Quantified Logic}
\label{chap:semantics_for_ql}
\markright{Chap \ref{chap:semantics_for_ql}: Semantics for QL}
\setlength{\parindent}{1em}

% *****************************
% *		Creating Models in QL      *
% ****************************

\section{Creating models in Quantified Logic}

In Chapter 3 we developed the truth table system, a semantic method for evaluating sentences and arguments in SL. The method was called semantic, because it involved establishing meanings for all the non-logical parts of a sentence, in this case, the sentence letters. These were called non-logical because their meaning was not fixed by the rules of SL. The logical part of the language was the system of connectives and parentheses. As it turned out, the logical parts of our language were truth functional: the meaning of the larger sentences built up with the connectives and parentheses was simply a function of the truth of the smaller parts. Because the system of sentential connectives and parentheses is truth functional, our semantic system only needed to look at one aspect of the meaning of the sentence letters, their truth value. Thus an interpretation of a sentence in SL simply turned out to be a truth assignment on the sentence letters. That's why you spent Chapter 3 merrily writing Ts and Fs under sentence letters. Each little row of Ts and Fs represented one way of assigning meaning to a sentence or argument, and all the little rows in the truth table represented all the possible ways of assigning meaning to the sentence or argument (at least as far as truth was concerned.)

In this chapter we are going to develop similar methods for QL. Because QL is a more complicated system than SL developing semantics for QL will be much more complicated, and there will be greater limitations for what we can do with these methods.

\subsection{Basic Models}

The first thing we need to do is to find an equivalent in QL of a little row of Ts and Fs beneath a sentence in SL. We called this little row of Ts and Fs an interpretation, because it gave one aspect of the meaning of the parts of the sentence, namely their truth values. To come up with an interpretation of a sentence in QL, we will need to look at more than the truth or falsity of sentences, though. We have, after all, broken open the atomic sentence to look at its subatomic parts, and now we will need to assign meaning to these parts. The way to start is to look at the symbolization keys we created when we were translating sentences in and out of QL. The symbolization key contained a universe of discourse (UD), a meaning for each predicate, and an object picked out by each term. For example:
\begin{ekey}
\item[UD:] comic book characters
\item[Fx:] $x$ fights crime.
\item[b:] Batman
\item[w:] Bruce Wayne
\end{ekey}

Given this symbolization key, we can translate sentences like this

\begin{earg}
\item[] $Fb$: Batman fights crime.
\item[] $Fw$: Bruce Wayne fights crime.
\item[] $Fb \eif Fw$: If Batman fights crime, then Bruce Wayne fights crime.
\end{earg}
This symbolization key, along with some basic knowledge of superhero stories, gives us enough information to figure out that the sentence $Fb$ is true. (Notice that the sentence $Fb$ is not true \emph{just because} of the interpretation. The way we interpret the sentence, plus the way the world is, makes the sentence true.)

This is the information we need to develop an interpretation of sentences in QL. We are not just assigning truth values anymore. We need to get deeper into meaning here, and look at the \emph{reference} of the parts of the sentence. \define{Reference} is an aspect of meaning that deals with the way bits of language pick out or identify objects in the real world. For our purposes the real world is the universe of discourse, the set of objects we are talking about. To explain how a singular term like $b$ refers, we need only point out the member of the UD it refers to, in this case, Batman. In more technical terms, Batman is the \emph{referent} of B. For our purposes it is enough to define a referent like this: the \define{referent} of a term in QL is the unique object picked out by that term.

(Notice that here to explain what $b$ refers to, I have been forced to simply use another singular term in another language, the name ``Batman.'' To really pick out the object referred to, I would need to draw your attention to something in the actual world, for instance by holding up a comic book and pointing at the character on the cover.)

We want to use similar methods to talk about the meaning of predicates. To do this we will talk about the \emph{extension} of a predicate. The \define{extension} of a predicate is the set of objects in the UD that the predicate applies to. So if $b$ is in the extension of the predicate $F$ then the sentence $Fb$ is true.

(Identifying the extension of a predicate like $F$ again forces us to rely on another language, English. We can say that the extension of $F$ includes Batman, Superman, Green Lantern, etc. The situation is worse because $F$ has an indefinitely large extension, so we have relied on the English predicate ``fights crime'' to describe it.)

All this means that we are able to talk about the meaning of sentences in QL entirely in terms of sets. We use curly brackets \{ and \} to denote sets. The members of the set can be listed in any order, separated by commas. The fact that sets can be in any order is important, because it means that \{foo, bar\} and \{bar, foo\} are the same set. It is possible to have a set with no members in it. This is called the \define{empty set}. The empty set is sometimes written as \{\}, but usually it is written as the single symbol $\emptyset$.

We are now able to give the equivalent of a line in a truth table for QL. An interpretation in QL will be called a \emph{model}. A \define{model} of sentences or arguments in QL consists of a set that is the universe of discourse, individual members of that set that are the referents of the singular terms in the sentences or arguments, and subsets of the universe of discourse which are the extensions of the predicates used in the sentences or arguments.

To see how this works imagine I have a bunch of sentences in QL, which include the predicate $H$ and the singular term $f$. Now consider this symbolization key:
\begin{ekey}
\item[UD:]People who played as part of the Three Stooges
\item[Hx:]$x$ had head hair.
\item[f:] Mister Fine
\end{ekey}

What is the model that corresponds to this interpretation? There were six people who played as part of the Three Stooges over the years, so the UD will have six members: Larry Fine, Moe Howard, Curly Howard, Shemp Howard, Joe Besser, and Curly Joe DeRita. Curly, Joe, and Curly Joe were the only completely bald stooges. The result is this model:
\begin{partialmodel}
	UD & \{Larry, Curly, Moe, Shemp, Joe, Curly Joe\}\\
	\extension{H} & \{Larry, Moe, Shemp\}\\
	\referent{f} & Larry
\end{partialmodel}

You do not need to know anything about the Three Stooges in order to evaluate whether sentences are true or false in this \emph{model}. $Hf$ is true, since the referent of $f$ (Larry) is in the extension of $H$. Both $\exists x Hx$ and $\exists x \enot Hx$ are true, since there is at least one member of the UD that is in the extension of $H$ and at least one member that is not in the extension of $H$. In this way, the model captures all of the formal significance of the interpretation.

\subsection{Models for multiplace predicates}

Now consider this interpretation:
\begin{ekey}
\item{UD:} whole numbers less than 10
\item{Ex:} $x$ is even.
\item{Nx:} $x$ is negative.
\item{Lxy:} $x$ is less than $y$.
\item{Txyz:} $x$ times $y$ equals $z$.
\end{ekey}
What is the model that goes with this interpretation?
The UD is the set $\{1,2,3,4,5,6,7,8,9\}$.

The extension of a one-place predicate like $E$ or $N$ is just the subset of the UD of which the predicate is true. The extension of $E$ is the subset $\{2,4,6,8\}$. There are many even numbers besides these four, but these are the only members of the UD that are even. There are no negative numbers in the UD, so $N$ has an empty extension; i.e. $\extension{N}=\emptyset$.

The extension of a two-place predicate like $L$ is somewhat vexing. It seems as if the extension of $L$ ought to contain 1, since 1 is less than all the other numbers; it ought to contain 2, since 2 is less than all of the other numbers besides 1; and so on. Every member of the UD besides 9 is less than some member of the UD. What would happen if we just wrote $\extension{L}=\{1,2,3,4,5,6,7,8\}$?

The problem is that sets can be written in any order, so this would be the same as writing $\extension{L}=\{8,7,6,5,4,3,2,1\}$. This does not tell us which of the members of the set are less than which other members.

We need some way of showing that 1 is less than 8 but that 8 is not less than 1. The solution is to have the extension of $L$ consist of pairs of numbers. An \define{ordered pair} is like a set with two members, except that the order \emph{does} matter. We write ordered pairs with angle brackets $<$ and $>$. The ordered pair \mbox{$<$foo, bar$>$} is different than the ordered pair \mbox{$<$bar, foo$>$}. The extension of $L$ is a collection of ordered pairs, all of the pairs of numbers in the UD such that the first number is less than the second. Writing this out completely:
\begin{quote}
$\extension{L}=$ \{$<$1,2$>$, $<$1,3$>$, $<$1,4$>$, $<$1,5$>$, $<$1,6$>$, $<$1,7$>$, $<$1,8$>$, $<$1,9$>$,
$<$2,3$>$, $<$2,4$>$, $<$2,5$>$, $<$2,6$>$, $<$2,7$>$, $<$2,8$>$, $<$2,9$>$,
$<$3,4$>$, $<$3,5$>$, $<$3,6$>$, $<$3,7$>$, $<$3,8$>$, $<$3,9$>$,
$<$4,5$>$, $<$4,6$>$, $<$4,7$>$, $<$4,8$>$, $<$4,9$>$,
$<$5,6$>$, $<$5,7$>$, $<$5,8$>$, $<$5,9$>$,
$<$6,7$>$, $<$6,8$>$, $<$6,9$>$,
$<$7,8$>$, $<$7,9$>$,
$<$8,9$>$%
\}
\end{quote}

Three-place predicates will work similarly; the extension of a three-place predicate is a set of ordered triples where the predicate is true of those three things \emph{in that order}. So the extension of $T$ in this model will contain ordered triples like $<$2,4,8$>$, because $2\times 4 = 8$.

Generally, the extension of an n-place predicate is a set of all ordered n-tuples ${<}a_1, a_2,\ldots, a_n{>}$ such that $a_1$--$a_n$ are members of the UD and the predicate is true of $a_1$--$a_n$ in that order.

\subsection{Models for identity}
Identity is a special predicate of QL. We write it a bit differently than other two-place predicates: $x=y$ instead of $Ixy$. We also do not need to include it in a symbolization key. The sentence $x=y$ always means ``$x$ is identical to $y$,''  and it cannot be interpreted to mean anything else. In the same way, when you construct a model, you do not get to pick and choose which ordered pairs go into the extension of the identity predicate. It always contains just the ordered pair of each object in the UD with itself.

The sentence $\forall x Ixx$, which contains an ordinary two-place predicate, is contingent. Whether it is true for an interpretation depends on how you interpret $I$, and whether it is true in a model depends on the extension of $I$.

The sentence $\forall x\ x=x$ is a tautology. The extension of identity will always make it true.

Notice that although identity always has the same interpretation, it does not always have the same extension. The extension of identity depends on the UD. If the UD in a model is the set \{Doug\}, then $\extension{=}$ in that model is \{$<$Doug, Doug$>$\}. If the UD is the set \{Doug, Omar\}, then $\extension{=}$ in that model is \{$<$Doug, Doug$>$, $<$Omar, Omar$>$\}. And so on.

If the referent of two constants is the same, then anything which is true of one is true of the other. For example, if $\referent{a}=\referent{b}$, then $Aa\eiff Ab$, $Ba\eiff Bb$, $Ca\eiff Cb$, $Rca\eiff Rcb$, $\forall x Rxa\eiff \forall x Rxb$, and so on for any two sentences containing $a$ and $b$. In metaphysics, this is called principle of the indiscernibility of identicals

In our system, the reverse of this principle is not true.\label{model.nonidentity} It is possible that anything which is true of $a$ is also true of $b$, yet for $a$ and $b$ still to have different referents. This may seem puzzling, but it is easy to construct a model that shows this. Consider this model:
\begin{partialmodel}
UD & \{Rosencrantz, Guildenstern\}\\
\referent{a} & Rosencrantz\\
\referent{b} & Guildenstern\\
for all predicates \script{P}, \extension{\script{P}} & $\emptyset$\\
\extension{=} & \{$<$Rosencrantz, Rosencrantz$>$,\\
\multicolumn{2}{r}{$<$Guildenstern, Guildenstern$>$\}}
\end{partialmodel}
This specifies an extension for every predicate of QL: All the infinitely-many predicates are empty. This means that both $Aa$ and $Ab$ are false, and they are equivalent; both $Ba$ and $Bb$ are false; and so on for any two sentences that contain $a$ and $b$. Yet $a$ and $b$ refer to different things. We have written out the extension of identity to make this clear: The ordered pair $<\referent{a},\referent{b}>$ is not in it. In this model, $a=b$ is false and $a\neq b$ is true.

\practiceproblems

\problempart
\label{pr.TorF1}
Determine whether each sentence is true or false in the model given.
\begin{partialmodel}
UD & \{Corwin, Benedict\}\\
\extension{A} & \{Corwin, Benedict\}\\
\extension{B} & \{Benedict\}\\
\extension{N} & $\emptyset$\\
\referent{c} & Corwin
\end{partialmodel}
\begin{earg}
\item $Bc$
\item $Ac \eiff \enot Nc$
\item $Nc \eif (Ac \eor Bc)$
\item $\forall x Ax$
\item $\forall x \enot Bx$
\item $\exists x(Ax \eand Bx)$
\item $\exists x(Ax \eif Nx)$
\item $\forall x(Nx \eor \enot Nx)$
\item $\exists x Bx \eif \forall x Ax$
\end{earg}

\problempart
\label{pr.TorF2}
Determine whether each sentence is true or false in the model given.
\begin{partialmodel}
UD & \{Waylan, Willy, Johnny\}\\
\extension{H} & \{Waylan, Willy, Johnny\}\\
\extension{W} & \{Waylan, Willy\}\\
\extension{R} & \{$<$Waylan, Willy$>$,$<$Willy, Johnny$>$,$<$Johnny, Waylan$>$\}\\
\referent{m} & Johnny
\end{partialmodel}
\begin{earg}
\item $\exists x(Rxm \eand Rmx)$
\item $\forall x(Rxm \eor Rmx)$
\item $\forall x(Hx \eiff Wx)$
\item $\forall x(Rxm \eif Wx)$
\item $\forall x\bigl[Wx \eif(Hx \eand Wx)\bigr]$
\item $\exists x Rxx$
\item $\exists x\exists y Rxy$
\item $\forall x \forall y Rxy$
\item $\forall x \forall y (Rxy \eor Ryx)$
\item $\forall x \forall y \forall z\bigl[(Rxy \eand Ryz) \eif Rxz\bigr]$
\end{earg}

\problempart
\label{pr.TorF3}
Determine whether each sentence is true or false in the model given.
\begin{partialmodel}
	UD			& \{Lemmy, Courtney, Eddy\}\\
	\extension{G}	& \{Lemmy, Courtney, Eddy\}\\
	\extension{H}	& \{Courtney\}\\
	\extension{M}	& \{Lemmy, Eddy\}\\
	\referent{c}	& Courtney\\
	\referent{e}	& Eddy
\end{partialmodel}
\begin{earg}
\item $Hc$
\item $He$
\item $Mc \eor Me$
\item $Gc \eor \enot Gc$
\item $Mc \eif Gc$
\item $\exists x Hx$
\item $\forall x Hx$
\item $\exists x \enot Mx$
\item $\exists x(Hx \eand Gx)$
\item $\exists x(Mx \eand Gx)$
\item $\forall x(Hx \eor Mx)$
\item $\exists x Hx \eand \exists x Mx$
\item $\forall x(Hx \eiff \enot Mx)$
\item $\exists x Gx \eand \exists x \enot Gx$
\item $\forall x\exists y(Gx \eand Hy)$
\end{earg}

\problempart
\label{pr.InterpretationToModel}
Write out the model that corresponds to the interpretation given.
\begin{ekey}
\item{UD:} natural numbers from 10 to 13
\item{Ox:} $x$ is odd.
\item{Sx:} $x$ is less than 7.
\item{Tx:} $x$ is a two-digit number.
\item{Ux:} $x$ is thought to be unlucky.
\item{Nxy:} $x$ is the next number after $y$.
\end{ekey}

% *****************************
% *		Working with Models.       *
% ****************************

\section{Working with Models}
\label{sec.UsingModels}

Working with models is in some ways like working with truth tables and in some ways not. With truth tables, we could conclusively show that a sentence was a tautology or a contradiction, because the truth table would always have a finite number of lines. We cannot, however, use models to show that a sentence is a tautology or a contradiction, because there are infinitely many ways to model a sentence, and and no single way to talk about all of them, the way we talked about all the lines in a truth table. One thing we can do is show conclusively that a sentence is neither a tautology nor a contradiction, and is instead contingent. A contingent sentence will have at least one model where it is false and one model where it is true.

As we shall see, this pattern plays itself out with the other logical properties we have covered. Because there are infinitely many ways to model a sentence, we cannot construct models to prove two sentences equivalent, but we can use them to show that two sentences are not equivalent. We cannot use models to show that a set of sentences is inconsistent, but we can use models to show that a set of sentences is consistent. Finally, we cannot construct a finite number of models to show that an argument is valid, but we can use one to show that an argument is invalid.

We will use the double turnstile symbol for QL much as we did for SL. ``$\script{A}\sdtstile{}{} \script{B}$''' means that an argument from \script{A} to \script{B} is semantically valid.  $\sdtstile{}{} \script{A}$ means that \script{A} is a semantic tautology. $\script{A} \ndststile{}{} \hspace{.5em} \sdtstile{}{} \script{B}$ means \script{A} and \script{B} are semantically equivalent.

In \fullref{semantic_definitions_in_SL} we stipulated semantic definitions for various logical concepts in SL that matched our truth table method for determining these concepts. So a sentence was said to be a tautology in SL if the column under its main connective contained only Ts. This was an alternative to saying the the truth table method was an imprecise way of getting at the ordinary language versions of these concepts. We will do something similar for the semantic definitions of logical notions in QL.

\begin{enumerate}
\item A \define{semantic tautology in QL} is a sentence \script{A} that is true in every model; i.e.,  $\sdtstile{}{} \script{A}$.

\item A \define{semantic contradiction in QL} is a sentence \script{A} that is false in every model; i.e., $\sdtstile{}{} \enot\script{A}$.

\item A sentence is \define{semantically contingent in QL} if and only if it is neither a tautology nor a contradiction.

\item Two sentences \script{A} and \script{B} are \define{semantically equivalent in QL} if and only if they have the same truth value in every model.

\item The set $\{\script{A}_1,\script{A}_2,\script{A}_3,\cdots\}$ is \define{semantically consistent in QL} if and only if there is at least one model in which all of the sentences are true. The set is \define{semantically inconsistent in QL} if and if only there is no such model.

\item An argument `` $\script{P}_1, \script{P}_2, \cdots$, \therefore\ \script{C} '' is \define{semantically valid in QL} if and only if there is no model in which all of the premises are true and the conclusion is false; i.e., $\{\script{P}_1,\script{P}_2,\cdots\}\sdtstile{}{}\script{C}$. It is \define{semantically invalid in QL} otherwise.
\end{enumerate}

\subsection{Models to show contingency}

Suppose we want to show that $\forall xAxx \eif Bd$ is \emph{not} a tautology. This requires showing that the sentence is not true in every model; i.e., that it is false in some model. If we can provide just one model in which the sentence false, then we will have shown that the sentence is not a tautology.

What would such a model look like? In order for $\forall xAxx \eif Bd$ to be false, the antecedent ($\forall x Axx$) must be true, and the consequent ($Bd$) must be false.

To construct such a model, we start with a UD. It will be easier to specify extensions for predicates if we have a small UD, so start with a UD that has just one member. Formally, this single member might be anything, so let's just call it $\alpha.$

We want $\forall x Axx$ to be true, so we want all members of the UD to be paired with themselves in the extension of $A$; this means that the extension of $A$ must be \{$<\alpha,\alpha>$\}.

We want $Bd$ to be false, so the referent of $d$ must not be in the extension of $B$. We give $B$ an empty extension.

Since $\alpha$ is the only member of the UD, it must be the referent of $d$. The model we have constructed looks like this:
\begin{partialmodel}
	UD			& \{$\alpha$\}\\
	\extension{A} 	& \{$<\alpha, \alpha>$\}\\
	\extension{B}	& $\emptyset$\\
	\referent{d}	& $\alpha$
\end{partialmodel}

Strictly speaking, a model specifies an extension for \emph{every} predicate of QL and a referent for \emph{every} constant. As such, it is generally impossible to write down a complete model. That would require writing down infinitely many extensions and infinitely many referents. However, we do not need to consider every predicate in order to show that there are models in which $\forall xAxx \eif Bd$ is false. Predicates like $H$ and constants like $f_{13}$ make no difference to the truth or falsity of this sentence. It is enough to specify extensions for $A$ and $B$ and a referent for $d$, as we have done. This provides a \emph{partial model} in which the sentence is false.

Perhaps you are wondering: What is $\alpha$? What does the predicate $A$ mean in English? The partial model could correspond to an interpretation like this one:
\begin{ekey}
\item[UD:] Paris
\item[$Axy$:] $x$ is in the same country as $y$.
\item[$Bx$:] $x$ was founded in the 20th century.
\item[$d$:] the City of Lights
\end{ekey}

However, we don't have to say that this model corresponds to any particular interpretation of the sentence in English in order to know that the sentence $\forall xAxx \eif Bd$  is not a tautology. We could have made our one-object universe of discourse contain only Mahatma Gandhi, or a pebble on a beach in Africa, or the number 2. As long as the predicate and terms were given the right reference, the sentence would come out false. Thus in the future we can evalaute sentences and arguments using UDs with arbitrarily named elements, like $\alpha$, $\beta$, $\gamma$, etc.

We use the same method to show that $\forall xAxx \eif Bd$ is not a contradiction. We need only specify a model in which $\forall xAxx \eif Bd$ is true; i.e., a model in which either $\forall x Axx$ is false or $Bd$ is true. Here is one such partial model:
\begin{partialmodel}
	UD			& \{$\alpha$\}\\
	\extension{A} 	& \{$<\alpha,\alpha>$\}\\
	\extension{B}	& \{$\alpha$\}\\
	\referent{d}	& $\alpha$
\end{partialmodel}

We have now shown that $\forall xAxx \eif Bd$ is neither a tautology nor a contradiction. By the definition of ``contingent in QL,'' this means that $\forall xAxx \eif Bd$ is contingent. In general, showing that a sentence is contingent will require two models: one in which the sentence is true and another in which the sentence is false.

Notice, however, that we cannot show that a sentence actually is a tautology or a contradiction using one or two models this way. For a sentence to be a tautology, it must be true in every possible model. Similarly, if a sentence is a contradiction, it is false in all possible models. But there are infinitely many possible models for any sentence, and we don't have any tools in this text that will let us reason about all of them at once.

\subsection{Models to show non-equivalence}

Suppose we want to show that $\forall x Sx$ and $\exists x Sx$ are \emph{not} logically equivalent. We need to construct a model in which the two sentences have different truth values; we want one of them to be true and the other to be false. We start by specifying a UD. Again, we make the UD small so that we can specify extensions easily. We will need at least two members. Let the UD be \{$\alpha$, $\beta$\}. (If we chose a UD with only one member, the two sentences would end up with the same truth value. In order to see why, try constructing some partial models with one-member UDs.)

We can make $\exists x Sx$ true by including something in the extension of $S$, and we can make $\forall x Sx$ false by leaving something out of the extension of $S$. It does not matter which one we include and which one we leave out. Making $\alpha$ the only $S$, we get a partial model that looks like this:
\begin{partialmodel}
	UD			& \{$\alpha$, $\beta$\}\\
	\extension{S}	& \{$\alpha$\}
\end{partialmodel}
This partial model shows that the two sentences are \emph{not} logically equivalent.

Notice, though, that we cannot show that two sentences \emph{are} logically equivalent by simply producing a model. If we claim that two sentences are logically equivalent, we are once again making a claim about every possible model.

\subsection{Models to show consistency}

Suppose I wanted to show that the set of sentences $\{\exists x Fx, \exists x \enot Fx, \forall x Gx\}$ is consistent. For this to be the case, we need at least one model where all three sentences are true. In this case that means having one object in our UD that is $F$ and one that is not $F$, and we need both of them to be $G$. This would do the trick.

\begin{partialmodel}
	UD				 & \{$\alpha$, $\beta$\}\\
	\extension{F}		& \{$\alpha$\} \\
	\extension{G}		& \{$\alpha$, $\beta$\}
\end{partialmodel}

We cannot show a set of sentences to be inconsistent this way, because as before, that would mean creating an infinite number of models.

\subsection{Models to show invalidity}

Back on p.~\pageref{surgeon3correct}, we said that this argument would be invalid in QL:
\begin{earg}
\item[] $(Rc \eand K_1c) \eand Tc$
\item[\therefore] $Tc \eand K_2c$
\end{earg}
In order to show that it is invalid, we need to show that there is some model in which the premises are true and the conclusion is false. We can construct such a model deliberately. Here is one way to do it:
\begin{partialmodel}
	UD			& \{$\alpha$\}\\
	\extension{T}	& \{$\alpha$\}\\
	\extension{K_1}	& \{$\alpha$\}\\
	\extension{K_2}	& $\emptyset$\\
	\extension{R}	& \{$\alpha$\}\\
	\referent{c}	& $\alpha$
\end{partialmodel}

As you have probably guessed, we cannot show an argument valid with models so simply.

%\begin{table}[t]
%\caption{It is relatively easy to answer a question if you can do it by constructing a model or two. It is much harder if you need to reason about all possible models. This table shows when constructing models is enough.}
%\label{table.ModelOrArgument}
%\begin{center}
%\begin{tabular*}{\textwidth}[t]{p{10em}p{10em}p{10em}}
%& {\centerline{YES}} & {\centerline{NO}}\\
%\cline{3-3}
%
%Is \script{A} a tautology? & {show that \script{A} must be true in any model} & \tablefbox{\emph{construct a model} in which \script{A} is false}\\
%\cline{3-3}
%
%Is \script{A} a contradiction? &  {show that \script{A} must be false in any model} & \tablefbox{\emph{construct a model} in which \script{A} is true}\\
%\cline{2-3}
%
%Is \script{A} contingent? & \tablefbox{\emph{construct two models}, one in which \script{A} is true and another in which \script{A} is false}\vline & {either show that \script{A} is a tautology or show that \script{A} is a contradiction}\\
%\cline{2-3}
%
%Are \script{A} and \script{B} equivalent? & {show that \script{A} and \script{B} must have the same truth value in any model} & \tablefbox{\emph{construct a model} in which \script{A} and \script{B} have different truth values}\\
%\cline{2-3}
%
%Is the set \model{A} consistent? & \tablefbox{\emph{construct a model} in which all the sentences in \model{A} are true} & {show that the sentences could not all be true in any model}\\
%\cline{2-3}
%
%Is the argument \mbox{`\script{P}, \therefore\ \script{C}'} valid? & {show that any model in which \script{P} is true must be a model in which \script{C} is true} & \tablefbox{\emph{construct a model} in which \script{P} is true and \script{C} is false}\\
%\cline{3-3}
%\end{tabular*}
%\end{center}
%\end{table}
%
%
%
%
%
%
%
%\subsection{Satisfaction}
%
%The formula $Px$ says, roughly, that $x$ is one of the $P$s. This cannot be quite right, however, because $x$ is a variable and not a constant. It does not name any particular member of the UD. Instead, its meaning in a sentence is determined by the quantifier that binds it. The variable $x$ must stand-in for every member of the UD in the sentence $\forall xPx$, but it only needs to stand-in for one member in $\exists xPx$. Since we want the definition of satisfaction to cover $Px$ without any quantifier whatsoever, we will start by saying how to interpret a free variable like the $x$ in $Px$.
%
%We do this by introducing a \emph{variable assignment}. Formally, this is a function that matches up each variable with a member of the UD. Call this function `a.' (The `a' is for `assignment', but this is not the same as the truth value assignment that we used in defining truth for SL.)
%
%The formula $Px$ is satisfied in a model \model{M} by a variable assignment $a$ if and only if $a(x)$, the object that $a$ assigns to $x$, is in the the extension of P in \model{M}.
%
%When is $\forall x Px$ satisfied? It is not enough if $Px$ is satisfied in \model{M} by $a$, because that just means that $a(x)$ is in \extension{P}. $\forall x Px$ requires that every other member of the UD be in \extension{P} as well.
%
%So we need another bit of technical notation: For any member $\Omega$ of the UD and any variable \script{x}, let $a[\Omega|\script{x}]$ be the variable assignment that assigns $\Omega$ to \script{x} but agrees with $a$ in all other respects. We have used $\Omega$, the Greek letter Omega, to underscore the fact that it is some member of the UD and not some symbol of QL. Suppose, for example, that the UD is presidents of the United States. The function $a[\mbox{Grover Cleveland}|x]$ assigns Grover Cleveland to the variable $x$, regardless of what $a$ assigns to $x$; for any other variable, $a[\mbox{Grover Cleveland}|x]$ agrees with $a$.
%
%We can now say concisely that $\forall x Px$ is satisfied in a model \model{M} by a variable assignment $a$ if and only if, for every object $\Omega$ in the UD of \model{M}, $Px$ is satisfied in \model{M} by $a[\Omega|x]$.
%
%You may worry that this is circular, because it gives the satisfaction conditions for the sentence $\forall x Px$ using the phrase `for every object.' However, it is important to remember the difference between a logical symbol like `$\forall$' and an English language word like `every.' The word is part of the metalanguage that we use in defining satisfaction conditions for object language sentences that contain the symbol.
%
%We can now give a general definition of satisfaction, extending from the cases we have already discussed. We define a function $s$ (for `satisfaction') in a model \model{M} such that for any wff \script{A} and variable assignment $a$, $s(\script{A}, a)=1$ if \script{A} is satisfied in \model{M} by $a$; otherwise $s(\script{A}, a)=0$.
%
%\begin{enumerate}
%\item If \script{A} is an atomic wff of the form $\script{P}\script{t}_1\ldots\script{t}_n$ and $\Omega_i$ is the object picked out by $t_i$, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if ${<}\Omega_1\ldots\Omega_n{>}$ is in \extension{\script{P}} in \model{M}},\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%For each term $t_i$: If $t_i$ is a constant, then $\Omega_i = \referent{t_i}$. If $t_i$ is a variable, then $\Omega_i = a(t_i)$.
%
%\item If \script{A} is ${\enot}\script{B}$ for some wff \script{B}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if $s(\script{B}, a) = 0$},\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $(\script{B}\eand\script{C})$ for some wffs \script{B,C}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if $s(\script{B}, a) = 1$ and $s(\script{C}, a) = 1$,}\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $(\script{B}\eor\script{C})$ for some wffs \script{B,C}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	0 & \mbox{if $s(\script{B}, a) = 0$  and $s(\script{C}, a) = 0$,}\\
%	1 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $(\script{B}\eif\script{C})$ for some wffs \script{B,C}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	0 & \mbox{if $s(\script{B}, a) = 1$ and $s(\script{C}, a) = 0$,}\\
%	1 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $(\script{B}\eiff\script{C})$ for some sentences \script{B,C}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if $s(\script{B}, a) = s(\script{C}, a)$},\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $\forall\script{x} \script{B}$ for some wff \script{B} and some variable \script{x}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if $s(\script{B}, a[\Omega|\script{x}])=1$ for every member $\Omega$ of the UD},\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%
%\item If \script{A} is $\exists\script{x} \script{B}$ for some wff \script{B} and some variable \script{x}, then
%\begin{displaymath}s(\script{A}, a) =
%	\left\{\begin{array}{ll}
%	1 & \mbox{if $s(\script{B}, a[\Omega|\script{x}])=1$ for at least one member $\Omega$ of the UD},\\
%	0 & \mbox{otherwise.}
%	\end{array}\right.
%\end{displaymath}
%\end{enumerate}
%
%This definition follows the same structure as the definition of a wff for QL, so we know that every wff of QL will be covered by this definition. For a model \model{M} and a variable assignment $a$, any wff will either be satisfied or not. No wffs are left out or assigned conflicting values.

%\section*{Summary of definitions}
%\begin{itemize}
%\item A \define{tautology in QL} is a sentence \script{A} that is true in every model; i.e.,  $\models\script{A}$.

%\item A \define{contradiction in QL} is a sentence \script{A} that is false in every model; i.e., $\models\enot\script{A}$.

%\item A sentence is \define{contingent in QL} if and only if it is neither a tautology nor a contradiction.

%\item An argument `` $\script{P}_1, \script{P}_2, \cdots$, \therefore\ \script{C} '' is \define{valid in QL} if and only if there is no model in which all of the premises are true and the conclusion is false; i.e., $\{\script{P}_1,\script{P}_2,\cdots\}\models\script{C}$. It is \define{invalid in QL} otherwise.

%\item Two sentences \script{A} and \script{B} are \define{logically equivalent in SL} if and only if both $\script{A}\models\script{B}$ and $\script{B}\models\script{A}$.

%\item The set $\{\script{A}_1,\script{A}_2,\script{A}_3,\cdots\}$ is \define{consistent in QL} if and only if there is at least one model in which all of the sentences are true. The set is \define{inconsistent in QL} if and if only there is no such model.
%\end{itemize}
\practiceproblems

\problempart
\label{pr.Contingent}
Show that each of the following is contingent.
\begin{earg}
\item \leftsolutions\ $Da \eand Db$
\item \leftsolutions\ $\exists x Txh$
\item \leftsolutions\ $Pm \eand \enot\forall x Px$
\item $\forall z Jz \eiff \exists y Jy$
\item $\forall x (Wxmn \eor \exists yLxy)$
\item $\exists x (Gx \eif \forall y My)$
\end{earg}

\problempart
\label{pr.NotEquiv}
Show that the following pairs of sentences are not logically equivalent.
\begin{earg}
\item $Ja$, $Ka$
\item $\exists x Jx$, $Jm$
\item $\forall x Rxx$, $\exists x Rxx$
\item $\exists x Px \eif Qc$, $\exists x (Px \eif Qc)$
\item $\forall x(Px \eif \enot Qx)$, $\exists x(Px \eand \enot Qx)$
\item $\exists x(Px \eand Qx)$, $\exists x(Px \eif Qx)$
\item $\forall x(Px\eif Qx)$, $\forall x(Px \eand Qx)$
\item $\forall x\exists y Rxy$, $\exists x\forall y Rxy$
\item $\forall x\exists y Rxy$, $\forall x\exists y Ryx$
\end{earg}

\problempart
Show that the following sets of sentences are consistent.
\begin{earg}
\item \{Ma, \enot Na, Pa, \enot Qa\}
\item \{$Lee$, $Lef$, $\enot Lfe$, $\enot Lff$\}
\item \{$\enot (Ma \eand \exists x Ax)$, $Ma \eor Fa$, $\forall x(Fx \eif Ax)$\}
\item \{$Ma \eor Mb$, $Ma \eif \forall x \enot Mx$\}
\item \{$\forall y Gy$, $\forall x (Gx \eif Hx)$, $\exists y \enot Iy$\}
\item \{$\exists x(Bx \eor Ax)$, $\forall x \enot Cx$, $\forall x\bigl[(Ax \eand Bx) \eif Cx\bigr]$\}
\item \{$\exists x Xx$, $\exists x Yx$, $\forall x(Xx \eiff \enot Yx)$\}
\item \{$\forall x(Px \eor Qx)$, $\exists x\enot(Qx \eand Px)$\}
\item \{$\exists z(Nz \eand Ozz)$, $\forall x\forall y(Oxy \eif Oyx)$\}
\item \{$\enot \exists x \forall y Rxy$, $\forall x \exists y Rxy$\}
\end{earg}

\problempart
Construct models to show that the following arguments are invalid.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\forall x(Ax \eif Bx) \sdtstile{}{} \exists x Bx$
\item $\{\forall x(Rx \eif Dx)$, $\forall x(Rx \eif Fx)\} \sdtstile{}{} \exists x(Dx \eand Fx)$
\item $\exists x(Px\eif Qx) \sdtstile{}{} \exists x Px$
\item $Na \eand Nb \eand Nc \sdtstile{}{}  \forall x Nx$
\item $\{Rde$, $\exists x Rxd\} \sdtstile{}{} Red$
\item $\{\exists x(Ex \eand Fx)$, $\exists x Fx \eif \exists x Gx\} \sdtstile{}{} \exists x(Ex \eand Gx)$
\item $\{\forall x Oxc$, $\forall x Ocx\} \sdtstile{}{} \forall x Oxx$
\item $\{\exists x(Jx \eand Kx)$, $\exists x \enot Kx$, $\exists x \enot Jx\} \sdtstile{}{} \exists x(\enot Jx \eand \enot Kx)$
\item $\{Lab \eif \forall x Lxb$, $\exists x Lxb\} \sdtstile{}{} Lbb$
\end{enumerate}

\problempart
%problem using identity, with solutions
\label{pr.IdentityModels}
\begin{earg}
\item\leftsolutions\ Show that $\{{\enot}Raa, \forall x (x=a \eor Rxa)\}$
is consistent.
%There are many possible answers. Here is one:
%\begin{partialmodel}
%UD & \{Harry, Sally\}\\
%\extension{R} &\{$<$Sally, Harry$>$\}\\
%\referent{a} & Harry
%\end{partialmodel}
\item\leftsolutions\ Show that $\{\forall x\forall y\forall z(x=y \eor y=z \eor x=z),
\exists x\exists y\ x\neq y\}$ is consistent.
%There are no predicates or constants, so we only need to give a UD.
%Any UD with 2 members will do.
\item\leftsolutions\ Show that $\{\forall x\forall y\ x=y, \exists x\ x \neq a\}$ is inconsistent.
%We need to show that it is impossible to construct a model in which these are both true. Suppose $\exists x\ x \neq a\$ is true in a model. There is something in the universe of discourse that is \emph{not} the referent of $a$. So there are at least two things in the universe of discourse: \referent{a} and this other thing. Call this other thing \script{b}---we know $a \neq \script{b}$. But if $a \neq \script{b}$, then $\forall x\forall y\ x=y$ is false. So the first sentence must be false if the second sentence is true is true. As such, there is no model in which they are both true. Therefore, they are inconsistent.
\item Show that $\exists x (x = h \eand x = i)$ is contingent.
\item Show that \{$\exists x\exists y(Zx \eand Zy \eand x=y)$, $\enot Zd$, $d=s$\} is consistent.
\item Show that $\forall x(Dx \eif \exists y Tyx)$ \therefore\ $\exists y \exists z\ y\neq z$ is invalid.
\end{earg}

%\problempart
%\label{pr.SemanticsEssay}
%\begin{earg}
%\item Many logic books define consistency and inconsistency in this way:
%`` A set $\{\script{A}_1,\script{A}_2,\script{A}_3,\cdots\}$ is inconsistent if and only if $\{\script{A}_1,\script{A}_2,\script{A}_3,\cdots\}\models(\script{B}\eand\enot\script{B})$ for some sentence \script{B}. A set is consistent if it is not inconsistent.''
%
%Does this definition lead to any different sets being consistent than the definition on  p.~\pageref{def.consistencySL}? Explain your answer.
%
%\item\leftsolutions\ Our definition of truth says that a sentence \script{A} is \define{true in} \model{M} if and only if some variable assignment satisfies \script{A} in $M$. Would it make any difference if we said instead that \script{A} is \define{true in} \model{M} if and only if \emph{every} variable assignment satisfies \script{A} in $M$? Explain your answer.
%\end{earg}