\section{Evaluation}
\label{sec:evaluation}

The probability density of the state evaluation in \eqref{equ:bayesInt} is given by 
%
\begin{equation}
	%\begin{split}
		p(\vec{o}_t \mid \vec{q}_t) = 
		p(\vec{o}_t \mid \vec{q}_t)_\text{wifi} 
		\,p(\vec{o}_t \mid \vec{q}_t)_\text{act}
		\enspace ,
	%\end{split}
	\label{eq:evalBayes}
\end{equation}
%
where every component refers to a probabilistic sensor model which are statistical independent.
The barometer and accelerometer readings are used to determine the current activity $\mObsActivity$, which is then evaluated using $p(\vec{o}_t \mid \vec{q}_t)_\text{act}$. 
Absolute positioning information is given by $p(\vec{o}_t \mid \vec{q}_t)_\text{wifi}$ for \docWIFI{}. 

\subsection{\docWIFI{}}
\label{sec:wifi}

As stated in section \ref{sec:relatedWork}, we use the smartphone's \docWIFI{} component to provide an absolute location estimation based on a comparison between recent RSSI measurements of nearby AP's and signal strength predictions. The probability given those measurements $\mRssiVec_\text{wifi}$ and a prediction, corresponding to a well-known location $\mPosVec = (x,y,z)^T$ provided by $\vec{q}_t$, can thus be written as

\begin{equation}
	p(\vec{o}_t \mid \vec{q}_t)_\text{wifi} = 
	p(\mRssiVec_\text{wifi} \mid \mPosVec) =
	\prod_{\mRssi_{i} \in \mRssiVec_\text{wifi}} p(\mRssi_{i} \mid \mPosVec),\enskip
	%\mPos = (x,y,z)^T
	\mPosVec \in \R^3
	\enskip .
	\label{eq:wifiObs}
\end{equation}

\noindent We assume a statistical independence between the respective AP's. 
The comparison between a single RSSI measurement $\mRssi_i$ and the reference is given by

\begin{equation}
	p(\mRssi_i \mid \mPosVec) = 
	\mathcal{N}(\mRssi_i \mid \mu_{i,\mPosVec}, \sigma_{\text{wifi}}^2)
	\enskip ,
	\label{eq:wifiProb}
\end{equation}

%\commentByFrank{ich wuerde einfach $\sigma_\text{wifi}$ nehmen. es haengt nicht von der pos $\mPosVec$ ab, und wir hatten immer fuer jeden AP das gleiche}

\noindent where $\mu_{i,\mPosVec}$ denotes the (predicted) signal strength for the \docAPshort{} identified by $i$, regarding the location $\mPosVec$. 
A certain noise is allowed by the corresponding standard deviation $\sigma_{\text{wifi}}$.
Within this work $\mu_{\mPosVec}$ is calculated by a modified version of the wall-attenuation-factor model as presented in \cite{Ebner-17}. 
Here, the prediction depends on the 3D distance $d$ between the \docAPshort{} in question and the location $\mPosVec$ as well as the number of floors $\Delta f$ between them:
\begin{equation}
	\mu_{\mPosVec} = \mTXP - 10 \mPLE \log_{10}{\frac{\mMdlDist}{\mMdlDist_0}} + \Delta{f} \mWAF 
	\label{eq:wallAtt}
\end{equation}
%\commentByFrank{
%	hier sollte das $i$, das du vorher hattest, wohl wieder mit rein?
%	was genau $d$ bzw $d_i$ oder $d_{i,\mPosVec}$ ist, muessten wir vermutlich auch kurz erklären.
%	Ich hatte auch immer unterschieden zwischen der fraglichen position (z.B. $\vec{\rho}$)
%	und der position des access points (z.B. $\mPosVec_i$). also, zwei verschiedene zeichen, dass das klar wird.
%	ich weis aber nicht, ob $\vec{\rho}$ noch frei ist, bzw was auf den folgenden seiten nocht kommt.
%	eigentlich gehoert das $i$ dann auch noch ans $P_0$ und $\gamma$ und $d_0$.. aber der einfachheit halber, reicht das ja im text.
%	vorschlag wäre etwas wie:
%}
%\begin{equation}
%	\mu(i,\vec{\rho}) = \mTXP - 10 \mPLE \log_{10}{\frac{\mMdlDist}{\mMdlDist_0}} + \Delta{f} \mWAF
%	,\enskip
%	d = \| \vec{\rho} - \mPosVec_i \|
%	\label{eq:wallAtt}
%\end{equation}

\noindent Here, $\mTXP$ is the \docAPshort{}'s signal strength measurable at a known distance $\mMdlDist_0$ (usually \SI{1}{\meter}) and $\mPLE$ denotes the signals depletion over distance, which depends on the \docAPshort{}'s surroundings like walls and other obstacles.
The attenuation per floor is given by $\mWAF$. 
For example, a viable choice for steel enforced concrete floors is $\mWAF \approx \SI{-8.0}{dB}$ \cite{Ebner-15}.
Of course, eq. \eqref{eq:wallAtt} needs to be calculated separately for every $i$ and thus available \docAPshort{}.
It should be noted, that we omitted the index $i$ in eq. \eqref{eq:wallAtt} for the sake of clarity and consistency with other literature.

The environmental parameters $\mTXP$, $\mPLE$ and $\mWAF$ need to be known beforehand and often vary greatly between single \docAPshort{}'s. 
Nevertheless, for simplicity's sake it is common practice to use some fixed empirically chosen values, the same for every \docAPshort{}.
This might already provide enough accuracy for some use-cases and buildings, but fails in complex scenarios, as discussed in section \ref{sec:intro}. 
Therefore, instead of using a pure empiric model, we deploy an optimization scheme to find a well-suited set of parameters ($\mPosAPVec{}, \mTXP{}, \mPLE{}, \mWAF{}$) per \docAPshort{}, where $\mPosAPVec{} = (x,y,z)^T$ denotes the \docAPshort{}'s estimated position. 
The optimization is based on a few reference measurements $\vec{s_{\text{opt}}}$ throughout the building, e.g. every \SI{3}{} to \SI{5}{\meter} centred within a corridor and between \SI{1}{} and \SI{4}{} references per room, depending on the room's size. 
Compared to classical fingerprinting, where reference measurements are recorded on small grids between \SI{1}{} to \SI{2}{\meter}, this highly reduces their required number and thus the overall setup-time.

The target function to optimize the $6$ model parameters for one \docAPshort{} is given by

\begin{equation}
		(\mPosAPVec, \mTXP, \mPLE, \mWAF) =
		\argmin_{\mPosAPVec, \mTXP, \mPLE, \mWAF}
		\sum_{s_{i} \in \vec{s_{\text{mac}}}}
			(s_{i} - \mu_{\mPosVec})^2
		\enskip,\enskip\enskip
		\mu_{\mPosVec} = 
		\mTXP{} - 10 \mPLE{} \log_{10} \| \mPosVec-\mPosAPVec \| + \Delta f \mWAF{} 
\enspace .
	\label{eq:optTarget}
\end{equation}
%\commentByFrank{hier muesste dann auch das $i$ rein, bzw die funktion $\mu()$. vorschlag waere dann:}
%\begin{equation}
%		(\mPosAPVec, \mTXP, \mPLE, \mWAF)_i =
%		\argmin_{\mPosVec, \mTXP, \mPLE, \mWAF}
%		\sum_{s_{i,\vec{\rho}} \in \vec{s}_i}
%			\big(s_{i,\vec{\rho}} - \mu(i,\mPosVec) \big)^2
%\enspace .
%	\label{eq:optTarget}
%\end{equation}
%\commentByFrank{argmin liefert die argumente, nicht den fehler. da muesste nur min stehen}
%\commentByFrank{
%	hier braucht es drigend eine unterscheidung zwischen den beiden positionen. der vom fingerprint und der vom ap
%	$\mPosAPVec$ ist, wegen dem $\hat{ }$ einfach nur die \emph{beste}. aber sie ist halt generell anders als der fingerprint.
%	deshalb brauchen wir da zwei formel zeichen.
%	und wir muessen einheitlich machen, ob wir das $i$ jetzt mitnehmen, oder nicht. sonst wirkt es verwirrend
%}
\noindent Here, one reduces the squared error between reference measurements $s_{i} \in  \vec{s_{\text{mac}}}$ with well-known location $\mPosVec$ and corresponding model predictions $\mu_{\mPosVec}$ (cf. eq. \eqref{eq:wallAtt}). 
Whereas $\vec{s_{\text{mac}}}$ is the subset of $\vec{s_{\text{opt}}}$ for the \docAPshort{} in question, identified by its MAC-adress.
The number of floors between $\mPosVec$ and $\mPosAPVec$ is again given by $\Delta f$.
As discussed by \cite{Ebner-17}, optimizing all 6 parameters, especially the unknown \docAPshort{} position $\mPosAPVec$, usually results in optimizing a non-convex, discontinuous function.
A promising way to deal with non-convex functions is using a genetic algorithm, which is inspired by the process of natural selection \cite{goldberg89}. 

The here deployed algorithm starts with a initial population, that is uniformly sampled within predefined limits of the to-be-optimized parameters. 
The \docAPshort{}'s location $\mPosAPVec$ must be within the building and is therefore limited by its size. 
\mTXP{}, \mPLE{} and \mWAF{} are set within a sane interval around empirically chosen values.
During each iteration, the best \SI{25}{\percent} of the population are kept.
The remaining entries are then re-created by modifying the best entries with uniform random values within $\pm$\SI{10}{\percent} of the known limits.
Inspired by {\em cooling} known from simulated annealing \cite{Kirkpatrick83optimizationby},		 the result is stabilized by narrowing the allowed modification limits over time and thus decrease in the probability of accepting worse solutions.

%\commentByToni{Wollen wir das mal genauer beschreiben? Also wie genau funktioniert das cooling. Das ist ja alles sehr wischi waschi gehalten}
%\commentByFrank{ich wuerde es so lassen. da gibts genug in der literatur ueber ideen und potentielle ansaetze}

To further improve the results, we optimize a model for each floor of the building instead of a single global one, using only the reference measurements that belong to the corresponding floor. 
The reason for this comes from the assumptions made in eq. \eqref{eq:wallAtt}. 
Here, no walls are considered and thus we expect erroneous RSSI measurements for regions that are heavily shrouded, e.g. by steel-reenforced concrete or metallized glass.
During evaluation, the $z$-value from $\mPosVec$ in eq. \eqref{eq:wifiProb} is used to select the correct model for this location, what then provides the signal strength prediction.
For example, if a pedestrian walks on a staircase and thus is in-between multiple storeys, the average prediction of all corresponding models is calculated instead.

%man muss zwar messungen machen, dafür muss man aber die position der ap's nicht mehr kennen. daher kostet das jetzt nicht viel mehr zeit.

Basically, any kind of wireless network which allows to measure RSSI can be used for the above. 
%\commentByMarkus{Provieded der AP die RSSI? Misst nicht das Smartphone an seiner Antenne?}
However, most buildings do not provide a satisfying and well covered \docWIFI{} infrastructure, e.g. staircases or hallways are often neglected for office spaces. 
This applies in particular to historical buildings, as discussed in section \ref{sec:intro}.
To improve $\docWIFI$ coverage we are able to distribute a small number of simple and cheap \docWIFI{} beacons.
As beacons we use a WEMOS D1 mini, which is based on the ESP-8266EX \docWIFI{} chip \cite{Wemos}. 
The building considered in this work has no \docWIFI{} infrastructure at all, not even a single \docAPshort{}.
Nevertheless, our method allows to distribute beacons in the whole building by simply plugging them into available power outlets.


\subsection{Activity Recognition}
\label{sec:activity}

To enable continuous floor changes we use a simple activity recognition based on the smartphone's barometer and accelerometer. 
The method distinguishes between the following: standing, walking, walking up or walking down.
For each sensor we define two fixed-sized windows: $\vec{\omega}_\text{s}$ (short) and $\vec{\omega}_\text{l}$ (long).
As their naming suggests, the windows differ in size and thus in the number of raw sensor measurements they hold. 
Both windows are implemented as real-time queues.
Therefore, if a new sensor measurement is added, the oldest entry will be removed.

To recognize the respective activities, we suggest a very simple threshold-based process. 
Defining a threshold $t_\text{acc}$ for acceleration (m/s$^2$) and $t_\text{baro}$ for altitude (hPa).  
A corresponding activity is chosen by 

\begin{equation}
	\mObsActivity = 
		\begin{cases}
			\text{standing} & \Delta \bar\omega_\text{acc} < t_\text{acc} \\
			\text{walking} & |\Delta \bar\omega_\text{baro}| < t_\text{baro} \\
			\text{walking down} & \Delta \bar\omega_\text{baro} > 0 \\
			\text{walking up} & \text{otherwise} 
		\end{cases}
\end{equation}

\noindent where

\begin{equation}
	\Delta \bar\omega = \bar\omega_\text{l} - \bar\omega_\text{s}
\end{equation}
\noindent and $\bar\omega$ provides the arithmetic mean of the respective windows and thus represents a moving average. 
We set $t_\text{acc} = $ \SI{0.015}{\meter/\square\second} and $t_\text{baro} = $ \SI{0.042}{\meter/\square\second}.
For both involved sensors we suggest to set the size of $\vec{\omega}_\text{s}$ between \SI{0.3}{\second} and \SI{0.6}{\second}. 
Recognizing if the pedestrian is standing or walking requires less prior data, then climbing stairs.
Therefore, $\vec{\omega}_\text{l, acc}$ is recommended between \SI{1}{\second} and \SI{2}{\second}, while $\vec{\omega}_\text{l, baro}$ between \SI{3}{\second} and \SI{5}{\second}. 
It should be noted, that the window size is a classic trade-off between flexibility and robustness.
The larger the window, the slower changes become noticeable and vice versa.
Of course, the above suggested values are dependent upon the particular requirements and used sensors. 
However, they should be valid for many modern commercially available smartphones.

%\commentByFrank{hier hast du quotes um die activitites. in der intro noch nicht. vlt einheitlich machen ueber macros?}
The activity is now evaluated using $p(\vec{o}_t \mid \vec{q}_t)_\text{act}$ by providing a probability based on whether the 3D location $\mPosVec$ of the state-in-question is on a staircase, in an elevator or on the floor. 
If the current activity $\mObsActivity$ is recognized as "standing", a $\mPosVec$ located on the floor results in a probability given by $\kappa$, otherwise $1 - \kappa$.
The same applies to "walking up" and "walking down", here a $\mPosVec$ located on one of the possible staircases or elevators provides $\kappa$ and those who remain on the floor $1 - \kappa$.
The likelihood for $\kappa$ is chosen empirically.
It is useful to find a reasonable value that is not too restrictive.
In most cases, $\kappa = 0.75$ provides good results by remaining enough room for erroneous classifications.
A significant higher value like $\kappa = 0.99$ could cause the system to be stuck on a staircase or to be unable to change floors.


%\commentByToni{Wir haben im related work schon von particeln gesprochen. hier in der eval nehm ich aber wieder viel state und state-in-question. wie wollen wir es machen?}

%\commentByToni{warum wir die große treeppe so schwer ist: wlan model zieht JEDE decke ab, nicht nur die sichtbaren, weil das model einfach so gebaut wurde. }

