\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{algorithm,algpseudocode}
\usepackage{bm}
\usepackage{hyperref}
\usepackage{float}
\usepackage{subcaption}
\usepackage{enumitem}
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0}
\definecolor{aaltoBlue}{RGB}{0, 51, 102}
\definecolor{pinegreen}{cmyk}{0.92,0,0.59,0.25}
\definecolor{royalblue}{cmyk}{1,0.50,0,0}
\definecolor{lavander}{cmyk}{0,0.48,0,0}
\definecolor{violet}{cmyk}{0.79,0.88,0,0}
\definecolor{lightblue}{RGB}{173, 216, 230}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,arrows.meta,positioning,fit,shapes.geometric,shapes.misc,intersections,patterns,decorations.pathreplacing,backgrounds,matrix}
\usepgfplotslibrary{fillbetween,groupplots}
\setcounter{secnumdepth}{-\maxdimen}
% Long unbreakable tokens (the ISO/IEC author names in citations, term names
% in the title) otherwise overflow the margin by a few points; a modest
% emergency stretch lets TeX loosen the paragraph instead.
\emergencystretch=2em
\usepackage{mleftright}
% Project macros used by this entry (auto-extracted).
\newcommand{\dataset}{\mathcal{D}}
\newcommand{\defeq}{:=}
\newcommand{\featurevec}{\vx}
\newcommand{\hypothesis}{h}
\newcommand{\pair}[2]{\left( #1,#2 \right)}
\newcommand{\prob}[1]{\mathbb{P}\left(#1\right)}
\newcommand{\reals}{\mathbb{R}}
\newcommand{\sampleidx}{r}
\newcommand{\samplesize}{m}
\newcommand{\sigmoid}{\sigma}
\newcommand{\trainset}{\dataset^{(\rm train)}}
\newcommand{\truelabel}{y}
\newcommand{\vw}[0]{{\bf w}}
\newcommand{\vx}[0]{{\bf x}}
\newcommand{\weights}{\vw}

\begin{document}

\section{sigmoid function}

\subsection*{Author and editors}
Alexander Jung\textsuperscript{*}, Department of Computer Science, Aalto University, Espoo, Finland (ORCID \mbox{\href{https://orcid.org/0000-0001-7538-0990}{0000-0001-7538-0990}}) \\
Editors: Konstantina Olioumtsevits (ORCID \mbox{\href{https://orcid.org/0000-0003-2697-8659}{0000-0003-2697-8659}}), Ekkehard Schnoor (ORCID \mbox{\href{https://orcid.org/0000-0001-8252-1499}{0000-0001-8252-1499}}), Aalto University, Espoo, Finland \\
\textsuperscript{*}Corresponding author: \href{mailto:alex.jung@aalto.fi}{alex.jung@aalto.fi}

\subsection{Synonyms}
logistic function

\subsection{Abstract}
The sigmoid function maps the real line into the open
		interval $(0, 1)$. It translates the real-valued score of a
		binary classifier into an estimate for the probability
		of a specific label value. It is smooth and strictly
		increasing, with a derivative that approaches zero as the
		magnitude of the argument grows. Machine learning (ML) uses it in two roles: as an
		activation function inside a neuron, and as part of the
		log-likelihood function for learning the model parameters of a
		probabilistic model.

\subsection{Definition}
A frost warning for Krems an der Donau (Austria) has to
	say how likely frost is, not only whether it is expected.
	Fig.~\ref{fig_sigmoid_tmin_dict} shows the air temperature measured
	at $06{:}00$ (UTC)
	at that station. A classifier that warns of frost reads
		the $06{:}00$ reading of a day, its feature vector
		$\featurevec$ with this single feature, and delivers a
		real-valued score $\hypothesis(\featurevec)$. The classification is a
		threshold: a frost warning for the next day is issued if and only
		if $\hypothesis(\featurevec) > 0$, and the magnitude
		$|\hypothesis(\featurevec)|$ measures the confidence in that
		warning. In practice it is often more useful to state a
		probability. The sigmoid function translates the score
		$\hypothesis(\featurevec)$ into a number between $0$ and $1$ that
		can be read as the probability of frost, as
		Fig.~\ref{fig_sigmoid_tmin_dict} shows for its last day.
		\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\begin{axis}[
				width=0.74\linewidth, height=0.34\linewidth,
				axis lines=left,
				xlabel={date}, ylabel={$06{:}00$ (UTC) reading ($^{\circ}$C)},
				xmin=0.3, xmax=8.8, ymin=-10, ymax=4.5,
				xtick={1,2,3,4,5,6},
				xticklabels={15/01, 16/01, 17/01, 18/01, 19/01, 20/01},
				x tick label style={font=\scriptsize},
				y tick label style={font=\small},
				label style={font=\small},
				]
				\addplot[ycomb, black, thick, mark=*, mark size=1.8pt]
					table[col sep=comma, x=day, y=t06]
					{sigmoid_tmin.csv};
				\node[font=\small, anchor=north] at (axis cs:6,-8.8)
					{$\featurevec$};
				\node[font=\scriptsize, anchor=west, align=left]
					at (axis cs:6.5,-4.6)
					{$\hypothesis(\featurevec) = 2.077$\\[2pt]
					 $\sigmoid\big(\hypothesis(\featurevec)\big) \approx 0.89$};
				\draw[->, >=stealth, shorten >=2pt]
					(axis cs:6.1,-6.6) -- (axis cs:6.45,-5.0);
			\end{axis}
		\end{tikzpicture}
		\caption{Six consecutive days at Krems an der Donau, each stem the
			air temperature measured at $06{:}00$ (UTC) that day. The
			reading of
			the last of them is the feature vector $\featurevec$ a
			classifier reads, from which it computes the score
			$\hypothesis(\featurevec) = 2.077$. That score is positive, so a
			frost warning is issued, and the sigmoid function turns it into
			$\sigmoid(2.077) \approx 0.89$, an $89$ percent probability
			of frost on 21/01. Data generated by
			\texttt{pythondemos/sigmoid.py}
			\label{fig_sigmoid_tmin_dict}}
		\end{figure}
		The sigmoid function maps the real line into the open interval
		$(0, 1)$. It is the
		function $\sigmoid: \reals \rightarrow (0,1)$ given by
		\[
			\sigmoid(z) \defeq \frac{1}{1 + e^{-z}}
			\text{.}
		\]
		It rises from $\sigmoid(z) \rightarrow 0$ as $z \rightarrow -\infty$
		to $\sigmoid(z) \rightarrow 1$ as $z \rightarrow +\infty$, and passes
		through $\sigmoid(0) = 1/2$ (see Fig.~\ref{fig_sigmoid_dict}). It is
		also called the logistic function. For two classes with scores
		$z$ and $0$, the softmax function returns the pair
		$\big(\sigmoid(z), 1 - \sigmoid(z)\big)$, so the sigmoid function
		is the softmax function for two classes: it delivers the
		probability of one class, and the other class has the
		complementary probability.
		\begin{figure}[H]
		\centering
		\begin{tikzpicture}
			\begin{axis}[
				width=0.72\linewidth, height=0.38\linewidth,
				xmin=-7.2, xmax=7.2, ymin=-0.1, ymax=1.2,
				xlabel={$z$}, ylabel={$\sigmoid(z)$},
				axis lines=center,
				xtick={-6,-4,-2,0,2,4,6},
				ytick={0, 0.5, 1},
				tick label style={font=\small},
				label style={font=\small},
				% each axis label sits next to the tip of its own arrow
				every axis x label/.style={at={(ticklabel* cs:1.0)},
					anchor=west, xshift=3pt},
				every axis y label/.style={at={(ticklabel* cs:1.0)},
					anchor=south, yshift=3pt},
				domain=-6:6, samples=200,
			]
				\draw[dashed, gray] (axis cs:-7.2, 1) -- (axis cs:7.2, 1);
				\draw[dashed, gray] (axis cs:-7.2, 0) -- (axis cs:7.2, 0);
				\addplot[black, thick] {1/(1+exp(-x))};
				\addplot[only marks, mark=*, mark size=1.3pt, black]
					coordinates {(0, 0.5)};
			\end{axis}
		\end{tikzpicture}
		\caption{The sigmoid function $\sigmoid(z) = 1/(1 + e^{-z})$ maps
			the real line into the open interval $(0,1)$, with
			$\sigmoid(0) = 1/2$
			\label{fig_sigmoid_dict}}
		\end{figure}
		The sigmoid function is smooth and strictly increasing, and it
		satisfies the symmetry $\sigmoid(-z) = 1 - \sigmoid(z)$
		(Bishop, 2006, Sect.~4.2). Its derivative is available in closed
		form, and in terms of the function itself
		(Bishop, 2006, Sect.~4.3.2, Eq.~(4.88)),
		\[
			\sigmoid'(z) = \sigmoid(z)\big(1 - \sigmoid(z)\big)
			\text{.}
		\]
		These properties make the sigmoid function a popular choice for the
		activation function of an artificial neural network (ANN). One drawback of that choice is that
		the derivative decreases quickly as the argument grows in
		magnitude, which slows the progress of gradient-based
		learning methods, the vanishing gradient problem. The vanishing
		gradient problem is why sigmoidal activation functions are rarely used
		in the hidden layers of feedforward deep nets, where
		other activation functions such as the rectified linear unit (ReLU) are preferred
		(Goodfellow et al., 2016, Sect.~6.3.2).
\par
		A probabilistic model for a binary label uses the sigmoid function
		to turn a score into a probability. Let the label
		$\truelabel$ be either $1$ or $-1$, and let the score be
		$\weights^{\top} \featurevec$ with model parameters $\weights$.
		The model reads
		\[
			\prob{\truelabel = 1 \mid \featurevec}
			= \sigmoid\big(\weights^{\top} \featurevec\big)
			\text{,} \qquad
			\prob{\truelabel = -1 \mid \featurevec}
			= \sigmoid\big(-\weights^{\top} \featurevec\big)
			\text{,}
		\]
		where the second follows from the symmetry
		$\sigmoid(-z) = 1 - \sigmoid(z)$. Both cases are the single
		expression $\sigmoid(\truelabel \, \weights^{\top} \featurevec)$,
		which is where the sigmoid function enters the log-likelihood. For
		a training set
		$\trainset = \big\{ \pair{\featurevec^{(\sampleidx)}}
		{\truelabel^{(\sampleidx)}} \big\}_{\sampleidx=1}^{\samplesize}$
		of labeled data points, the log-likelihood of the
		model parameters is
		\[
			\sum_{\sampleidx=1}^{\samplesize} \ln
			\sigmoid\big( \truelabel^{(\sampleidx)} \weights^{\top}
			\featurevec^{(\sampleidx)} \big)
			\text{.}
		\]
		Maximizing it over $\weights$ is maximum likelihood estimation,
		and it is logistic regression (Bishop and Bishop, 2024, Sect.~5.4.3;
		Shalev-Shwartz and Ben-David, 2014, Sect.~9.3). Each summand is the negative of a
		logistic loss, so maximizing the log-likelihood and minimizing the
		average logistic loss are the same computation
		(Bishop, 2006, Sect.~4.3.2). For example, consider the
		classifier map $\hypothesis$ learned by logistic regression from the
		days of $2024$ at Krems. Where its score is zero the sigmoid
		returns $1/2$, so the warning is issued exactly where the
		probability passes one half. A score of $0.907$ becomes a
		probability of $0.71$ for frost tomorrow, and a score of
		$-2.534$ becomes $0.07$.

\subsection{Cross References}
activation function, ReLU, softmax function, logistic regression, linear model, classifier, probability, smooth

\subsection{References}
\begin{enumerate}
  \item Bishop (2006). Pattern Recognition and Machine Learning. Springer Science+Business Media, New York, NY, USA.
  \item Bishop and Bishop (2024). Deep Learning: Foundations and Concepts. Springer Nature, Cham, Switzerland.
  \item Goodfellow et al. (2016). Deep Learning. MIT Press, Cambridge, MA, USA.
  \item Shalev-Shwartz and Ben-David (2014). Understanding Machine Learning: From Theory to Algorithms. Cambridge Univ. Press, New York, NY, USA.
\end{enumerate}

\end{document}
