Þ¤pathÙI/home/runner/work/course/course/lectures/Discriminative Classification.jl¬cell_resultsÞNÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61Чrunning§runtimeÎ|“¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢u6·has_pluto_hook_features¤bodyÚI

Prior

In Bayesian logistic regression, we often add a Gaussian prior on the weights:

$$\begin{align*} p(w) = \mathcal{N}(w \,|\, m_0, S_0) \tag{B-4.140} \end{align*}$$

°persist_js_stateÂÙ$25f1ab08-d294-11ef-32ed-493792e121b7Чrunning§runtimeÎyP¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f1ab08-d294-11ef-32ed-493792e121b7¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢"{t·has_pluto_hook_features¤bodyÚj

Application: the predictive distribution

For a new data point $x_\bullet$, the predictive distribution for $y_\bullet=1$ is given by

$$\begin{align*} p(y_\bullet = 1 | x_\bullet, D) &= \int p(y_\bullet = 1 \,|\, x_\bullet, w) \, p(w| D) \,\mathrm{d}w \\ &= \int \sigma(w^T x_\bullet) \, p(w| D) \,\mathrm{d}w \tag{B-4.145} \end{align*}$$

°persist_js_stateÂÙ$557c4545-2208-43c9-9302-991e78202734Чrunning§runtimeÍ3}¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$557c4545-2208-43c9-9302-991e78202734¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æä÷s·has_pluto_hook_features¤bodyÚK
°persist_js_stateÂÙ$25f41118-d294-11ef-13a8-3fa6587c1bf3Чrunning§runtimeÎAý®¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f41118-d294-11ef-13a8-3fa6587c1bf3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÆäZŠ·has_pluto_hook_features¤bodyÚž

Let us recapitulate the differences between the generative and discriminative approaches to classification in a table:

Generative Discriminative (ML)
1Like density estimation, model joint prob.
\[p(\mathcal{C}_k) p(x|\mathcal{C}_k) = \pi_k \mathcal{N}(\mu_k,\Sigma)\]
Like (linear) regression, model conditional
\[p(\mathcal{C}_k|x,\theta)\]
2Leads to softmax posterior class probability
\[ p(\mathcal{C}_k|x,\theta ) = e^{\theta_k^T x}/Z\]

with structured \(\theta\)

Choose also softmax posterior class probability

\[ p(\mathcal{C}_k|x,\theta ) = e^{\theta_k^T x}/Z\]

but now with ‘free’ \(\theta\)

3

For Gaussian \(p(x|\mathcal{C}_k)\) and multinomial priors,

\[\hat \theta_k = \left[ {\begin{array}{c} { - \frac{1}{2} \mu_k^T \sigma^{-1} \mu_k + \log \pi_k} \\ {\sigma^{-1} \mu_k } \\ \end{array}} \right]\]

in one shot.

Find \(\hat\theta_k\) through gradient-based adaptation

\[\nabla_{\theta_k}\mathrm{L}(\theta) = \sum_n \Big( y_{nk} - \frac{e^{\theta_k^T x_n}}{\sum_{k^\prime} e^{\theta_{k^\prime}^T x_n}} \Big)\, x_n\]
°persist_js_stateÂÙ$9704ee6a-e233-49f1-8c66-d81543927342Чrunning§runtimeÎcˆ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9704ee6a-e233-49f1-8c66-d81543927342¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÆvÉ`·has_pluto_hook_features¤bodyÚ

🎯 Key concept

It is easy to write down the Bayesian equations for the weight posterior and the predictive distribution of $y_\bullet$, but in practice these do not admit closed-form solutions. A traditional workaround is the Laplace approximation, which has long been used to approximate the marginalization integrals in Bayesian inference.

°persist_js_stateÂÙ$38b4854f-be02-4696-802f-2106481e3aeaЧrunning§runtimeÎ8Á¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$38b4854f-be02-4696-802f-2106481e3aea¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢*33·has_pluto_hook_features¤bodyÚÞ

Bayesian Processing of Uncertainties

We now make an important observation: According to Eq. B-4.143, the posterior covariance matrix $S_N$ of the weight vector depends on both the prior variance $S_0$ and the distribution of the training data $\{(x_n, y_n)\}_{n=1}^N$. In regions with limited training data and/or an uninformative prior (i.e., large $S_0$), the posterior uncertainty about the weights remains high. This increased uncertainty raises $\sigma_a^2$ in Eq. B-4.150, causing the posterior class probability in Eq. B-4.152 to approach $0.5$ (since $\Phi(0) = 0.5$, see Gaussian CDF image), thereby reflecting greater uncertainty in the prediction.

In other words, if you draw a new feature $x_\bullet$ from a region with little training data, then the predictive class probability $p(y_\bullet | x_\bullet, D)$ naturally tends toward $0.5$, a built-in expression of uncertainty, courtesy of the Bayesian framework.

In contrast, if you eliminate uncertainty by representing the weights as fixed-point estimates (i.e., as mere numbers), then the model becomes overconfident. It will still produce sharp predictions, even in regions where it has seen no data, exactly when it should be most uncertain.

°persist_js_stateÂÙ$25f0adde-d294-11ef-353e-4b4773df9ff5Чrunning§runtimeÎ Ÿdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f0adde-d294-11ef-353e-4b4773df9ff5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢ ·has_pluto_hook_features¤bodyÚ'

Data-generating distribution

We will take inspiration from the generative classification approach, where we derived the class posterior

$$p(y_{nk} = 1\,|\,x_n,\beta_k,\gamma_k) = \sigma(\beta_k^T x_n + \gamma_k)$$

as a softmax function of a linear map of the input.

Here, for the 2-class problem, we choose the 2-class softmax function (which is called the logistic function) with linear discrimination bounderies for the posterior class probability:

$$p(y_n =1 \,|\, x_n, w) = \sigma(w^T x_n) \,.$$

where

$$\sigma(a) = \frac{1}{1+e^{-a}}$$

is the logistic function.

°persist_js_stateÂÙ$25ef6ece-d294-11ef-270a-999c8d457b24Чrunning§runtimeÎä¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25ef6ece-d294-11ef-270a-999c8d457b24¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢s·has_pluto_hook_features¤bodyÚj

Framework

A data set is given by $D = \{(x_1,y_1),\dotsc,(x_N,y_N)\}$ with $x_n \in \mathbb{R}^M$ and $y_n \in \mathcal{C}_k$, with $k=1,\ldots,K$.

°persist_js_stateÂÙ$8b0bb225-bdc1-45ec-bd34-68d674d6f08dЧrunning§runtimeÎ9 u¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8b0bb225-bdc1-45ec-bd34-68d674d6f08d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢&hƒ·has_pluto_hook_features¤bodyÚ ¸

The Laplace Approximation approximates a function by a Gaussian-shaped function. In this case, we will approximate the weight posterior $p(w|D)$ (Eq. B-4.142) by a Gaussian distribution

$$q(w) = \mathcal{N}\left(w\,|\, m_{N}, S_N\right) \tag{B-4.144}$$

with

$$\begin{align} m_N &= \arg\max_w \log p(w|D) \\ S_N^{-1} &= S_0^{-1} + \sum_n \sigma_n (1-\sigma_n) x_n x_n^T \tag{B-4.143} \end{align}$$

where we used short-hand $\sigma_n = \sigma\left((2y_n-1) m_N^T x_n\right)$.

If we substitute the Gaussian approximation from Eq. B-4.143 into the expression for the predictive class distribution (Eq. B-4.145), we obtain (after some additional approximations):

$$\begin{align*} p(y_\bullet = 1 \mid x_\bullet, D) &= \int p(y_\bullet = 1 \,|\, x_\bullet, w) \cdot p(w\,|\, D) \,\mathrm{d}w \\ &\approx \int p(y_\bullet = 1 \,|\, x_\bullet, w) \cdot q(w) \,\mathrm{d}w \\ &= \int \sigma(w^T x_\bullet) \cdot \mathcal{N}\left(w \,|\, m_N, S_N\right) \,\mathrm{d}w \tag{B-4.145} \\ &\approx \Phi\left( \frac{\mu_a}{\sqrt(\lambda^{-2} +\sigma_a^2)}\right) \tag{B-4.152} \end{align*}$$

where

$$\begin{align} \lambda^2 &= \pi / 8 \\ \mu_a &= m^T_{N} x_\bullet \tag{B-4.149} \\ \sigma_a^2 &= x^T_\bullet S_N x_\bullet \tag{B-4.150} \end{align}$$

and $\Phi(x)= \frac{1}{\sqrt(2\pi)}\int_{-\infty}^{x}e^{-t^2/2}\mathrm{d}t$ is the Standard Normal cumulative distribution function (CDF).

Although the intermediate equations may look intimidating, the final result for the predictive distribution Eq. B-4.152 has a simple closed-form expression.

°persist_js_stateÂÙ$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fcЧrunning§runtimeÍtï¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fc¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æv™·has_pluto_hook_features¤bodyÚ À
°persist_js_stateÂÙ$fe66a986-2f55-4417-a71d-b3b99f6369ccЧrunning§runtimeÎ1~׸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$fe66a986-2f55-4417-a71d-b3b99f6369cc¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Å 4D·has_pluto_hook_features¤bodyÚ2

Challenge: difficult class-conditional data distributions

°persist_js_stateÂÙ$e4cc517b-d3b5-4517-a28b-efb8aba24496Чrunning§runtimeÎyƸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e4cc517b-d3b5-4517-a28b-efb8aba24496¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢)Ƚ·has_pluto_hook_features¤bodyÙÑ

The numerical issues associated with the Laplace approximation and the evaluation of the predictive class distribution are discussed in detail in the following mini-lecture.

°persist_js_stateÂÙ$25f1c2a0-d294-11ef-009c-69b64e87e5fbЧrunning§runtimeÎ4ä¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f1c2a0-d294-11ef-009c-69b64e87e5fb¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢"úƒ·has_pluto_hook_features¤bodyÚ

Many methods have been developed to approximate these types of Bayesian integrals. Here, we present the Laplace approximation, which is one of the simplest methods with broad applicability to Bayesian calculations.

°persist_js_stateÂÙ$173e545a-9125-4dc4-8df4-c3aa84a4b88aЧrunning§runtimeÎ&J¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$173e545a-9125-4dc4-8df4-c3aa84a4b88a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢1ÉO·has_pluto_hook_features¤bodyÙ:

Summary

°persist_js_stateÂÙ$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3cЧrunning§runtimeÎXà¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3c¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢æ§·has_pluto_hook_features¤bodyÙd

Bayesian Logistic Regression

°persist_js_stateÂÙ$69706576-0333-43bf-8523-e7838f373529Чrunning§runtimeÎ1cŸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$69706576-0333-43bf-8523-e7838f373529¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢*ž¶·has_pluto_hook_features¤bodyÚ

Note that we get a full predictive posterior distribution over the assignment of class labels for every datapoint, so instead of assigning a point to a class, we get a measure of uncertainty over our class assignment!

°persist_js_stateÂÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60Чrunning§runtimeÎÚÅY=¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Èâ·has_pluto_hook_features¤body °persist_js_stateÂÙ$66351c02-1921-44dc-b461-84a536c40fd5Чrunning§runtimeÎ6ƒÉ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$66351c02-1921-44dc-b461-84a536c40fd5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢'!!·has_pluto_hook_features¤bodyÚ7

The logistic function $\sigma(a) = 1/(1+e^{-a})$ (red), together with the scaled Standard Normal cumulative distribution function (CDF) $\Phi(\lambda a) = \frac{1}{\sqrt(2\pi)}\int_{-\infty}^{a}e^{-t^2/2}\mathrm{d}t$, for $\lambda^2=\pi/8$ (in blue). We will use this approximation later in the Laplace approximation. Based on Bishop fig.4.9.

°persist_js_stateÂÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952Чrunning§runtimeÎŽ¡¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Ãdïзhas_pluto_hook_features¤body °persist_js_stateÂÙ$93083660-6a49-4147-b00c-d62a4453f222Чrunning§runtimeÎ ç¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$93083660-6a49-4147-b00c-d62a4453f222¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢e ·has_pluto_hook_features¤bodyÚy

That's not a good fit for the $y=1$ class!

Now, we could continue down this road and try to fit a more complex distribution to the $y = 1$ class to make it fit. But let's explore a different approach, modeling the discriminative boundary directly.

°persist_js_stateÂÙ$b94644f8-725d-49bf-9641-3dad8b647f45Чrunning§runtimeÎë�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b94644f8-725d-49bf-9641-3dad8b647f45¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢2B¯·has_pluto_hook_features¤bodyÚã

Discrimination boundaries (*)

Show that for logistic regression with $p(y_n =1 \,|\, x_n) = \sigma(w^T x_n)$, the discrimination boundary, which can be computed by

$$\frac{p(y_n =1|x_n)}{p(y_n =0|x_n)} \overset{!}{=} 1$$

is a straight line.

°persist_js_stateÂÙ$4ceede48-a4d5-446b-bb34-26cec4af357aЧrunning§runtimeÎ1�Ýʸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4ceede48-a4d5-446b-bb34-26cec4af357a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Å0Ó,·has_pluto_hook_features¤bodyÚ w°persist_js_stateÂÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5Чrunning§runtimeÎޏdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&ÇqJÇ·has_pluto_hook_features¤bodyÙ-plot_dataset (generic function with 1 method)°persist_js_stateÂÙ$25efa2fe-d294-11ef-172f-9bb09277f59eЧrunning§runtimeΞe¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25efa2fe-d294-11ef-172f-9bb09277f59e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢=â·has_pluto_hook_features¤bodyÚŸ

Here's an IDEA! Let's model the posterior

$$p(y_n\in\mathcal{C}_k|x_n)$$

directly, without any assumptions on the prior class densities $p(x)$. This approach is called Discriminative Classification.

°persist_js_stateÂÙ$6eee35ee-fd55-498f-9441-f18c2508de19Чrunning§runtimeÎÏ?¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6eee35ee-fd55-498f-9441-f18c2508de19¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢2Žó·has_pluto_hook_features¤bodyÙ4

Code

°persist_js_stateÂÙ$6b56ec96-4b9d-4281-bd63-061df324867fЧrunning§runtimeÎãwr¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6b56ec96-4b9d-4281-bd63-061df324867f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassignee¤X_c1²last_run_timestampËAÚ¹&Ç\ªÖ·has_pluto_hook_features¤bodyÚE72×2 adjoint(::Matrix{Float64}) with eltype Float64: 2.37585 3.34038 0.859544 4.75254 0.931862 4.07941 1.3216 5.8365 0.018782 3.679 0.0323811 2.75244 0.985239 2.89621 â‹® 1.12348 3.87913 0.342695 2.65799 0.666544 2.99033 0.943866 3.06345 1.86646 3.73893 0.301837 3.20804°persist_js_stateÂÙ$b8790891-1546-48e0-9f96-e09eade31c12Чrunning§runtimeÎ姸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b8790891-1546-48e0-9f96-e09eade31c12¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Æÿ\·has_pluto_hook_features¤bodyÙ&logσ (generic function with 1 method)°persist_js_stateÂÙ$4ab4bcca-da6e-4137-858d-257c04388277Чrunning§runtimeÎó�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4ab4bcca-da6e-4137-858d-257c04388277¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÆvðÜ·has_pluto_hook_features¤bodyÚ

🎯 Key concept

The Laplace approximation renders a closed-form and interpretable (approximate) solution for the predictive class distribution,

$$p(y_\bullet = 1 | x_\bullet, D) \approx \Phi\left( \frac{m^T_{N} x_\bullet}{\sqrt{(8/\pi) +x^T_\bullet S_N x_\bullet)}}\right)\,.$$

°persist_js_stateÂÙ$25f02ac6-d294-11ef-26c4-f142b8ac4b5fЧrunning§runtimeÎfæ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f02ac6-d294-11ef-26c4-f142b8ac4b5f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢à·has_pluto_hook_features¤bodyÙ¦

What model should we use for the posterior distribution $p(y_n \in \mathcal{C}_k|x_n)$?

°persist_js_stateÂÙ$3b24b142-2239-4951-9177-ff87b5da4b68Чrunning§runtimeÎ�¶¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$3b24b142-2239-4951-9177-ff87b5da4b68¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æœkô·has_pluto_hook_features¤bodyÚ ¶
Click to see proof

The Log-likelihood is

$$\mathrm{L}(w) = \log \prod_n \prod_k {\underbrace{p(y_{nk}=1|x_n,w)}_{p_{nk}}}^{y_{nk}} = \sum_{n,k} y_{nk} \log p_{nk}$$

Use the fact that the softmax $\phi_k \equiv e^{a_k} / {\sum_j e^{a_j}}$ has analytical derivative,

$$ \begin{align*} \frac{\partial \phi_k}{\partial a_j} &= \frac{(\sum_j e^{a_j})e^{a_k}\delta_{kj}-e^{a_j}e^{a_k}}{(\sum_j e^{a_j})^2} = \frac{e^{a_k}}{\sum_j e^{a_j}}\delta_{kj} - \frac{e^{a_j}}{\sum_j e^{a_j}} \frac{e^{a_k}}{\sum_j e^{a_j}}\\ &= \phi_k \cdot(\delta_{kj}-\phi_j) \,. \end{align*}$$

Take the derivative of $\mathrm{L}(w)$ (or: how to spend an hour ...)

$$\begin{align*} \nabla_{w_j} \mathrm{L}(w) &= \sum_{n,k} \frac{\partial \mathrm{L}_{nk}}{\partial p_{nk}} \cdot\frac{\partial p_{nk}}{\partial a_{nj}}\cdot\frac{\partial a_{nj}}{\partial w_j} \\ &= \sum_{n,k} \frac{y_{nk}}{p_{nk}} \cdot p_{nk} (\delta_{kj}-p_{nj}) \cdot x_n \\ &= \sum_n \Big( y_{nj} (1-p_{nj}) -\sum_{k\neq j} y_{nk} p_{nj} \Big) \cdot x_n \\ &= \sum_n \left( y_{nj} - p_{nj} \right)\cdot x_n \\ &= \sum_n \Big( \underbrace{y_{nj}}_{\text{target}} - \underbrace{\frac{e^{w_j^T x_n}}{\sum_{j^\prime} e^{w_{j^\prime}^T x_n}}}_{\text{prediction}} \Big)\cdot x_n \end{align*}$$

°persist_js_stateÂÙ$25f12528-d294-11ef-0c65-97c61935e9c2Чrunning§runtimeÎÛw¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f12528-d294-11ef-0c65-97c61935e9c2¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢‰Ê·has_pluto_hook_features¤bodyÚ5

Since $y_n \in \{0,1\}$, it follows that $p(y_n =0 \,|\, x_n, w) = 1- \sigma(w^T x_n)$.

In summary, the posterior class distribution $p(y_n \,|\, x_n, w)$ in logistic regression is specified as

$$\begin{align*} p(y_n \,|\, x_n, w) &= \mathrm{Bernoulli}\left(y_n \,|\, \sigma(w^T x_n) \right) \\ &= \sigma(w^T x_n)^{y_n} \left(1 - \sigma(w^T x_n)\right)^{(1-y_n)} \tag{B-4.89} \\ &= \sigma\left( (2y_n-1) w^T x_n\right) \end{align*}$$

Note that for the 3rd equality, we have made use of the fact that $\sigma(-a) = 1-\sigma(a)$.

Each of these three models in B-4.89 are equivalent. We mention all three notational options since they all appear in the literature.

°persist_js_stateÂÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244Чrunning§runtimeÎÿI¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢%ð ·has_pluto_hook_features¤bodyÙŠ

Working out Numerics with Laplace Approximation

°persist_js_stateÂÙ$5d91a2ae-975c-4913-b8ff-3df043cb1f03Чrunning§runtimeÎƸ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5d91a2ae-975c-4913-b8ff-3df043cb1f03¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢.㬷has_pluto_hook_features¤bodyÙT

Recap Classification

°persist_js_stateÂÙ$0e713a7c-f4fa-4b86-abb3-26520e24d989Чrunning§runtimeÍ-¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$0e713a7c-f4fa-4b86-abb3-26520e24d989¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æè¦Ê·has_pluto_hook_features¤bodyÚ�
� Previous lecture Next lecture →
°persist_js_stateÂÙ$25ef12bc-d294-11ef-1557-d98ba829a804Чrunning§runtimeÎmƸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25ef12bc-d294-11ef-1557-d98ba829a804¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢'ò·has_pluto_hook_features¤bodyÚ

Preliminaries

Goal
Materials
°persist_js_stateÂÙ$25f19230-d294-11ef-2dfd-6d4927e86f57Чrunning§runtimeÎÉZ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f19230-d294-11ef-2dfd-6d4927e86f57¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢1“Š·has_pluto_hook_features¤bodyÚÈ

Discriminative Training or Discriminative Models?

In this lecture series, we presented two approaches to classification, namely the generative and the discriminative approach.

While the discriminative approach is intuitive and effective for many tasks, it sits somewhat uncomfortably with the Bayesian modeling approach outlined in the Bayesian Machine Learning lecture. Specifically, the discriminative approach does not define a full joint model over all variables in the system. Instead, it focuses only on modeling the conditional distribution $p(y | x)$, effectively ignoring the input distribution $p(x)$ that would normally be part of a fully generative Bayesian model.

In a short paper by T. Minka (2005), the model assumptions underlying discriminative classification are reinterpreted as arising from a special case of a generative model. This effectively restores discriminative approaches as fully compatible with the Bayesian modeling framework. (Note: the Minka paper is not required reading for the exam.)

°persist_js_stateÂÙ$a00c545c-2274-4086-94ca-319d1436fa26Чrunning§runtimeÍÅødepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a00c545c-2274-4086-94ca-319d1436fa26¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æç"·has_pluto_hook_features¤bodyÚ×

Exercises

°persist_js_stateÂÙ$22121f20-6b90-4782-8bed-25486cc23ae7Чrunning§runtimeÎQ²¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$22121f20-6b90-4782-8bed-25486cc23ae7¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Å3r÷·has_pluto_hook_features¤bodyÚ �
°persist_js_stateÂÙ$e7c45ff8-9fa2-4ea3-a06f-5769d877540eЧrunning§runtimeΦ*‘¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e7c45ff8-9fa2-4ea3-a06f-5769d877540e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Äê÷Y·has_pluto_hook_features¤bodyÚP¾ °persist_js_stateÂÙ$25f1b404-d294-11ef-1c3a-a5a8142bb202Чrunning§runtimeÎþH¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f1b404-d294-11ef-1c3a-a5a8142bb202¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢"§ß·has_pluto_hook_features¤bodyÚ;

While Eq. B-4.145 gives the expression for the Bayesian predictive class distribution, the integral becomes analytically intractable when we substitute the posterior distribution over weights, $p(w | D)$ (from Eq. B-4.142), into it :(

°persist_js_stateÂÙ$7932fff4-0568-49de-b34c-711e51487ae3Чrunning§runtimeÍóá¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$7932fff4-0568-49de-b34c-711e51487ae3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÆyQ·has_pluto_hook_features¤bodyÚ$

Challenge Revisited: Bayesian Logistic Regression

°persist_js_stateÂÙ$1f2bfcf4-fef4-4612-8683-d5c86a326eefЧrunning§runtimeÎçl¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1f2bfcf4-fef4-4612-8683-d5c86a326eef¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢.fA·has_pluto_hook_features¤bodyÙL

Closing Thoughts

°persist_js_stateÂÙ$9554ed0b-69dd-443c-9538-03a4117eeb78Чrunning§runtimeÎ÷?¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9554ed0b-69dd-443c-9538-03a4117eeb78¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÆçRµ·has_pluto_hook_features¤bodyÚ3
Click to see proof

$$\begin{align} \frac{ p(y_n =1 |x_n) }{ p(y_n =0|x_n) } &= \frac{ \sigma(w^T x_n)}{1 - \sigma(w^T x_n)} \\ &= \frac{ \frac{1}{1+\exp(-w^T x_n)} }{ 1 -\frac{1}{1+\exp(-w^T x_n)} } \\ &= \frac{1}{1+\exp(-w^T x_n) - 1} \\ &= \exp(w^T x) \end{align}$$

Setting $\exp(w^T x) \overset{!}{=} 1$, leads to

$$ w^T x = 0$$

for the discrimination boundary, which is a line.

°persist_js_stateÂÙ$aeee1072-5173-4eae-8027-3fbf2e338d95Чrunning§runtimeÎeÖ˜¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$aeee1072-5173-4eae-8027-3fbf2e338d95¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢ýë·has_pluto_hook_features¤bodyÚo

Why Not Generative Classification?

Like in the previous lecture, let's try to fit a generative distribution to the data. This is what we get when fitting two Gaussians to the two classes:

°persist_js_stateÂÙ$22f4972e-0f6d-49a1-bd1f-10569b77a852Чrunning§runtimeÎ ¯ÜÞ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$22f4972e-0f6d-49a1-bd1f-10569b77a852¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÅHÿ†·has_pluto_hook_features¤bodyÚƒ

🎯 Key concept

Discriminative classification follows the approach we used for Regression, namely, a direct (conditional) model

$$\begin{align} p(y_n \,|\, x_n, w) &= \mathrm{Bernoulli}\left(y_n \,|\, \sigma(w^T x_n) \right) \\ p(w) &= \mathcal{N}(w \,|\, m_0, S_0) \end{align}$$

for outputs $y_n$, given inputs $x_n$.

°persist_js_stateÂÙ$25f3bef2-d294-11ef-1438-e9f7e469336fЧrunning§runtimeÎ|=¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f3bef2-d294-11ef-1438-e9f7e469336f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢*QH·has_pluto_hook_features¤bodyÚ2

Let us perform Bayesian inference to estimate the posterior distribution of $w$ given the data set from the introduction. To allow an offset in the discrimination boundary, we add a constant 1 to the feature vector $x$.

°persist_js_stateÂÙ$cf829697-6283-4d2f-b0dd-bbfbd689a145Чrunning§runtimeÎÓð¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$cf829697-6283-4d2f-b0dd-bbfbd689a145¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢2µ¿·has_pluto_hook_features¤bodyÙJ

Data Generation

°persist_js_stateÂÙ$7e7cab21-09ab-4d06-9716-ab7864b229abЧrunning§runtimeÎ%¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$7e7cab21-09ab-4d06-9716-ab7864b229ab¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢R‡·has_pluto_hook_features¤bodyÙ\

See data generation code.

°persist_js_stateÂÙ$75076ca1-7226-4229-8e70-06cc586507c3Чrunning§runtimeÍ&`¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$75076ca1-7226-4229-8e70-06cc586507c3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime­image/svg+xml¬rootassigneeÀ²last_run_timestampËAÚ¹&É=·has_pluto_hook_features¤bodyÉ’ë °persist_js_stateÂÙ$25f14f82-d294-11ef-02fb-2dc632b8f118Чrunning§runtimeÎȸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f14f82-d294-11ef-02fb-2dc632b8f118¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢?·has_pluto_hook_features¤bodyÚ°

Note that, in the discriminative approach, the regression parameters $w$ are not structured into $\{\mu,\Sigma,\pi \}$. In principle, they are "free" parameters for which we can choose any value that seems appropriate. This provides discriminative approach with more flexibility than the generative approach.

°persist_js_stateÂÙ$25eefb10-d294-11ef-0734-2daf18636e8eЧrunning§runtimeͨ”¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25eefb10-d294-11ef-0734-2daf18636e8e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&ÄÑ�%·has_pluto_hook_features¤bodyÙñDiscriminative Classification°persist_js_stateÂÙ$5e4bb719-ea9b-4a30-8800-5d753f405fd1Чrunning§runtimë́Ƹdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5e4bb719-ea9b-4a30-8800-5d753f405fd1¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Ç0)ž·has_pluto_hook_features¤body °persist_js_stateÂÙ$25f19ed8-d294-11ef-3298-efa16dda1ddeЧrunning§runtimeÎåk¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f19ed8-d294-11ef-3298-efa16dda1dde¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢ÅÒ·has_pluto_hook_features¤bodyÚÀ

Parameter Inference

Note that for generative classification, for the sake of simplicity, we used maximum likelihood estimation for the model parameters. We could have used Bayesian parameter estimation for the generative classification model but the math is not suited for an introductory lesson.

In this lesson on discriminative classification, we specify both a prior and likelihood function for the parameters $w$, which allows us to compute a Bayesian posterior for the weights.

As before, once the model is specified, everything else follows directly from the rules of probability theory.

°persist_js_stateÂÙ$b5a19a34-b210-41ec-853d-c5df13ae17ceЧrunning§runtimeÍ0ܸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b5a19a34-b210-41ec-853d-c5df13ae17ce¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Çe|é·has_pluto_hook_features¤body °persist_js_stateÂÙ$7e8804bc-b7a4-4f08-b801-fbf567a5641dЧrunning§runtimeÎ2]¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$7e8804bc-b7a4-4f08-b801-fbf567a5641d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢&¼I·has_pluto_hook_features¤bodyÙè

Note that the scaled standard normal CDF closely approximates the logistic sigmoid function, with

$$\Phi\big(\sqrt{\pi/8} a\big) \approx \sigma(a)\,.$$

°persist_js_stateÂÙ$e379cc2a-43f8-432f-84fc-a88fd4f3ad0aЧrunning§runtimeΘf¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e379cc2a-43f8-432f-84fc-a88fd4f3ad0a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&ÂTéÝ·has_pluto_hook_features¤body °persist_js_stateÂÙ$25ef2806-d294-11ef-3cb6-0f3e76b9177eЧrunning§runtimeΦX¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25ef2806-d294-11ef-3cb6-0f3e76b9177e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢R~·has_pluto_hook_features¤bodyÚ

Our task will be the same as in the preceding class on (generative) classification. But this time, the class-conditional data distributions look very non-Gaussian, yet the linear discriminative boundary looks easy enough.

°persist_js_stateÂÙ$bda07a2e-3769-4ffe-9bc5-2b8a515247f6Чrunning§runtimeÎç0¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$bda07a2e-3769-4ffe-9bc5-2b8a515247f6¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢"&)·has_pluto_hook_features¤bodyÚ¸

The posterior for the weights follows by Bayes rule,

$$\begin{align*} \underbrace{p(w \,|\, D)}_{\text{posterior}} &= \frac{p(w) p(D|w)}{\int p(w) p(D|w) \mathrm{d}w} \\ &= \frac{\overbrace{\mathcal{N}(w \,|\, m_0, S_0)}^{\text{prior}} \cdot \overbrace{\prod_{n=1}^N \sigma\left( (2y_n-1) w^T x_n\right)}^{\text{likelihood}}}{\underbrace{\int \mathcal{N}(w \,|\, m_0, S_0) \prod_{n=1}^N \sigma\left( (2y_n-1) w^T x_n\right) \mathrm{d}w}_{\text{evidence}}} \tag{B-4.142} \end{align*}$$

In principle, Bayesian learning of the parameters is done now!

Unfortunately, the posterior $p(w | D)$ is not Gaussian, and the evidence $p(D)$ is also not analytically computable. (We will deal with this later).

°persist_js_stateÂÙ$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76Чrunning§runtimeÎ�žð®¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime­image/svg+xml¬rootassigneeÀ²last_run_timestampËAÚ¹&Æv·has_pluto_hook_features¤bodyÈ–« °persist_js_stateÂÙ$fcec3c3a-8b0b-4dfd-b010-66abbf330069Чrunning§runtimeÎ6@¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$fcec3c3a-8b0b-4dfd-b010-66abbf330069¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Æõƒ®·has_pluto_hook_features¤bodyÙ1generate_dataset (generic function with 1 method)°persist_js_stateÂÙ$25f14226-d294-11ef-369f-e545d5fe2700Чrunning§runtime·C¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f14226-d294-11ef-369f-e545d5fe2700¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢öo·has_pluto_hook_features¤bodyÚ\

This choice for the class posterior is called logistic regression, in analogy to linear regression:

$$\begin{align} p(y_n|x_n,w) &= \mathcal{N}(y_n|w^T x_n,\beta^{-1}) \tag{for linear regression} \\ p(y_n|x_n,w) &= \mathrm{Bernoulli}\left(y_n \,|\, \sigma(w^T x_n) \right) \tag{for logistic regression} \end{align}$$

°persist_js_stateÂÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4eaЧrunning§runtimeÎIŸ‚�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Ã)·has_pluto_hook_features¤body °persist_js_stateÂÙ$25ef7f54-d294-11ef-3f05-0d85fe6e7a17Чrunning§runtimeι¿¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25ef7f54-d294-11ef-3f05-0d85fe6e7a17¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢A¢·has_pluto_hook_features¤bodyÚ?

Sometimes, the precise assumptions of the (Gaussian-Categorical) generative model

$$p(x_n,y_n\in\mathcal{C}_k|\theta) = \pi_k \cdot \mathcal{N}(x_n|\mu_k,\Sigma)$$

clearly do not match the data distribution.

°persist_js_stateÂÙ$fd908bf5-71a1-4ae8-8416-cc1fdf084dcbЧrunning§runtimeÎît¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Ç"¤�·has_pluto_hook_features¤bodyÙï
predictive_posterior

Computes the predictive posterior eq. B-4.152 using the given approximation to the sigmoid function.

°persist_js_stateÂÙ$25f365e2-d294-11ef-300e-9914333b1233Чrunning§runtimeÎ3šÃ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f365e2-d294-11ef-300e-9914333b1233¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢-±6·has_pluto_hook_features¤bodyÚX

Rather than the computationally involved Laplace approximation, in practice, discriminative classification is often executed through maximum likelihood estimation.

With the usual 1-of-K encoding scheme for classes,

$$y_{nk} = \begin{cases} 1 & \text{if } x_n \in \mathcal{C}_k \\ 0 & \text{otherwise} \,,\end{cases}$$

the log-likelihood for a $K$-dimensional discriminative classifier evaluates to

$$\begin{align*} \mathrm{L}(w) &= \log \prod_n \prod_k {p(\mathcal{C}_k|x_n,w)}^{y_{nk}} \\ &= \sum_n \sum_k y_{kn} \log \bigg( \underbrace{\frac{e^{w_k^T x_n}}{ \sum_j e^{w_j^T x_n}}}_{=\text{softmax}(w_k^T x_n)} \bigg) \end{align*}$$

The gradient $\nabla_{w_k} \mathrm{L}(w)$ to the weight $w_k$ can be worked out to

$$\nabla_{w_k} \mathrm{L}(w) = \sum_n \underbrace{\big( \underbrace{y_{nk}}_{\text{target}} - \underbrace{\frac{e^{w_k^T x_n}}{ \sum_j e^{w_j^T x_n}}}_{\text{prediction}} \big)}_{\text{prediction error}}\cdot x_n .$$

°persist_js_stateÂÙ$25f1390c-d294-11ef-364d-17e4c93b9a57Чrunning§runtimeÎZÿ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f1390c-d294-11ef-364d-17e4c93b9a57¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢!Âv·has_pluto_hook_features¤bodyÚª

For the data set $D = \{(x_1,y_1),\dotsc,(x_N,y_N)\}$, the likelihood function for the parameters $w$ is given by

$$p(D|w) = \prod_{n=1}^N p(y_n|x_n,w) = \prod_{n=1}^N \sigma\left( (2y_n-1) w^T x_n\right)$$

°persist_js_stateÂÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69dfЧrunning§runtimeÍ\S¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69df¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassignee¤X_c2²last_run_timestampËAÚ¹&Ç^Œ*·has_pluto_hook_features¤bodyÚ>48×2 adjoint(::Matrix{Float64}) with eltype Float64: 6.28972 0.0219173 3.07004 6.10065 6.4579 -0.252939 4.64578 6.44116 3.07336 6.34726 7.87204 6.42865 5.11242 -0.109727 â‹® 3.37472 6.09446 8.383 6.18726 5.60987 0.657917 4.68727 6.38955 7.77538 6.30464 7.62106 6.28921°persist_js_stateÂÙ$b48f8800-473d-48e4-ab78-eb07653db7a5Чrunning§runtimeÎâù¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b48f8800-473d-48e4-ab78-eb07653db7a5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Ç? ·has_pluto_hook_features¤bodyÙ/log_likelihood (generic function with 1 method)°persist_js_stateÂÙ$1bfac9c5-e5cf-4a70-b077-11bb00cb1482Чrunning§runtime΀‘?¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1bfac9c5-e5cf-4a70-b077-11bb00cb1482¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Ç"aþ·has_pluto_hook_features¤bodyÚ
bayesian_discrimination_boundary

This function computes the posterior distribution over regression weights using the Laplace Approximation. We use logσ as a numerically stable alternative to logistic, and we avoid matrix inversions by computing the precision matrix of the posterior distribution instead of the covariance.

The math in this function corresponds to eq. B-4.143

°persist_js_stateÂÙ$98ef7093-f8ed-4a44-a153-0a64ab483f65Чrunning§runtimeÎ�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$98ef7093-f8ed-4a44-a153-0a64ab483f65¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢*¶R·has_pluto_hook_features¤bodyÙH

Implementation

°persist_js_stateÂÙ$f37ac438-cd56-4a09-bc8e-a75469955a2fЧrunning§runtimeÎ,RjJ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f37ac438-cd56-4a09-bc8e-a75469955a2f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime­image/svg+xml¬rootassigneeÀ²last_run_timestampËAÚ¹&ÇöÀQ·has_pluto_hook_features¤bodyÈìÆ °persist_js_stateÂÙ$25f3ff84-d294-11ef-0031-63b23d23324dЧrunning§runtimeÎX8¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25f3ff84-d294-11ef-0031-63b23d23324d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢.È—·has_pluto_hook_features¤bodyÚY

Why be Bayesian?

Why should you embrace the Bayesian approach to logistic regression? After all, Maximum Likelihood for logistic regression seems simpler.

Still, consider the following:

  • Bayesian logistic regression with the Laplace approximation ultimately leads to simple closed-form classification rules. Moreover, modern probabilistic programming languages and packages can automate the inference derivations above. (We just do them here to gain insight into a difficult inference process.)

  • Bayesian logistic regression allows for the computation of model evidence, enabling principled comparison of model performance across alternative models.

  • Perhaps most importantly, Bayesian logistic regression processes uncertainties; e.g., in places where almost no data is observed, the posterior class probability will pull back to the prior class probability rather than predicting some arbitrary probability.

°persist_js_stateÂÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3abЧrunning§runtimeÍ2–¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&Æ{²}·has_pluto_hook_features¤bodyÚ w°persist_js_stateÂÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5Чrunning§runtimeΆ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢gÇ·has_pluto_hook_features¤bodyÚ„

Model Specification

We will work this idea out for a 2-class problem. Assume a data set is given by

$$D = \{(x_1,y_1),\dotsc,(x_N,y_N)\}$$

with $x_n \in \mathbb{R}^M$ and $y_n \in \{0,1\}$ .

°persist_js_stateÂÙ$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bcЧrunning§runtimeι�¬¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bc¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime­image/svg+xml¬rootassigneeÀ²last_run_timestampËAÚ¹&ǬÑl·has_pluto_hook_features¤bodyÈÌ@ °persist_js_stateÂÙ$0045e569-dc3c-4998-86da-9d96f599c599Чrunning§runtime΋¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$0045e569-dc3c-4998-86da-9d96f599c599¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢+y·has_pluto_hook_features¤bodyÙf

Maximum Likelihood Estimation

°persist_js_stateÂÙ$ff31d8c1-db35-4c85-a609-67fc40e9e78dЧrunning§runtimeÎr�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ff31d8c1-db35-4c85-a609-67fc40e9e78d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ¹&¢.¹·has_pluto_hook_features¤bodyÚB

The parameter vector $w$ for logistic regression can then be estimated through iterative gradient-based adaptation. For instance, start with a random weight $\hat{w} = w_0$, and iterate through

$$\hat{w}^{(i+1)} = \hat{w}^{(i)} + \eta \cdot \left. \nabla_w \mathrm{L}(w) \right|_{w = \hat{w}^{(i)}}$$

until convergence.

°persist_js_stateÂÙ$616e84d7-063d-4d9d-99e4-56aecf3c7ee4Чrunning§runtimeÎNù¶¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$616e84d7-063d-4d9d-99e4-56aecf3c7ee4¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚ¹&Äjd�·has_pluto_hook_features¤body °persist_js_state±published_objects€­julia_version§v1.13.3±cell_dependenciesÞNÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61„´precedence_heuristic §cell_idÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25f1ab08-d294-11ef-32ed-493792e121b7„´precedence_heuristic §cell_idÙ$25f1ab08-d294-11ef-32ed-493792e121b7´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$557c4545-2208-43c9-9302-991e78202734„´precedence_heuristic §cell_idÙ$557c4545-2208-43c9-9302-991e78202734´downstream_cells_map€²upstream_cells_map�±keyconceptsummary�Ù$25f41118-d294-11ef-13a8-3fa6587c1bf3„´precedence_heuristic §cell_idÙ$25f41118-d294-11ef-13a8-3fa6587c1bf3´downstream_cells_map�«#463#result�²upstream_cells_map†°HypertextLiteral�¯MarkdownLiteral�·HypertextLiteral.Result�Ù MarkdownLiteral.CMParsedRenderer�¤@mdx‘Ù$ad196ae6-c65e-4aaa-b0cc-bd72daa41952·HypertextLiteral.Bypass�Ù$9704ee6a-e233-49f1-8c66-d81543927342„´precedence_heuristic §cell_idÙ$9704ee6a-e233-49f1-8c66-d81543927342´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�ªkeyconcept�Ù$38b4854f-be02-4696-802f-2106481e3aea„´precedence_heuristic §cell_idÙ$38b4854f-be02-4696-802f-2106481e3aea´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25f0adde-d294-11ef-353e-4b4773df9ff5„´precedence_heuristic §cell_idÙ$25f0adde-d294-11ef-353e-4b4773df9ff5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25ef6ece-d294-11ef-270a-999c8d457b24„´precedence_heuristic §cell_idÙ$25ef6ece-d294-11ef-270a-999c8d457b24´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$8b0bb225-bdc1-45ec-bd34-68d674d6f08d„´precedence_heuristic §cell_idÙ$8b0bb225-bdc1-45ec-bd34-68d674d6f08d´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fc„´precedence_heuristic §cell_idÙ$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fc´downstream_cells_map€²upstream_cells_map�¬NotebookCard�Ù$fe66a986-2f55-4417-a71d-b3b99f6369cc„´precedence_heuristic §cell_idÙ$fe66a986-2f55-4417-a71d-b3b99f6369cc´downstream_cells_map€²upstream_cells_map�³challenge_statement�Ù$e4cc517b-d3b5-4517-a28b-efb8aba24496„´precedence_heuristic §cell_idÙ$e4cc517b-d3b5-4517-a28b-efb8aba24496´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25f1c2a0-d294-11ef-009c-69b64e87e5fb„´precedence_heuristic §cell_idÙ$25f1c2a0-d294-11ef-009c-69b64e87e5fb´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$173e545a-9125-4dc4-8df4-c3aa84a4b88a„´precedence_heuristic §cell_idÙ$173e545a-9125-4dc4-8df4-c3aa84a4b88a´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3c„´precedence_heuristic §cell_idÙ$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3c´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$69706576-0333-43bf-8523-e7838f373529„´precedence_heuristic §cell_idÙ$69706576-0333-43bf-8523-e7838f373529´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60„´precedence_heuristic §cell_idÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60´downstream_cells_map�­bayesian_plot‘Ù$75076ca1-7226-4229-8e70-06cc586507c3²upstream_cells_mapÞ¤mean�¡/�´predictive_posterior‘Ù$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb¡X‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1¬plot_dataset‘Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5¤vcat�¦length�¡+�¡y‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1Ù bayesian_discrimination_boundary‘Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482¥range�¥plot!�¥diagm�¨heatmap!�¥zeros�¡*�¶MvNormalMeanCovariance�¤ones�Ù$66351c02-1921-44dc-b461-84a536c40fd5„´precedence_heuristic §cell_idÙ$66351c02-1921-44dc-b461-84a536c40fd5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$ad196ae6-c65e-4aaa-b0cc-bd72daa41952„´precedence_heuristic§cell_idÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952´downstream_cells_map�¤@mdx‘Ù$25f41118-d294-11ef-13a8-3fa6587c1bf3²upstream_cells_map€Ù$93083660-6a49-4147-b00c-d62a4453f222„´precedence_heuristic §cell_idÙ$93083660-6a49-4147-b00c-d62a4453f222´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$b94644f8-725d-49bf-9641-3dad8b647f45„´precedence_heuristic §cell_idÙ$b94644f8-725d-49bf-9641-3dad8b647f45´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$4ceede48-a4d5-446b-bb34-26cec4af357a„´precedence_heuristic §cell_idÙ$4ceede48-a4d5-446b-bb34-26cec4af357a´downstream_cells_mapƒ©#438#bond�¡N‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1¦N_bond‘Ù$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab²upstream_cells_map‹°HypertextLiteral�¸HypertextLiteral.content�«PlutoRunner�»PlutoRunner.Core.applicable�©@bindname�·HypertextLiteral.Result�¡:�Ù#PlutoRunner.PlutoRunner.create_bond�·HypertextLiteral.Bypass�¦Slider�´PlutoRunner.Base.get�Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5„´precedence_heuristic §cell_idÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5´downstream_cells_map�¬plot_dataset“Ù$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bcÙ$f37ac438-cd56-4a09-bc8e-a75469955a2fÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60²upstream_cells_mapŠ¥plot!�¦X_test‘Ù$b5a19a34-b210-41ec-853d-c5df13ae17ce¦@L_str�¸LaTeXStrings.latexstring�¡:�¤X_c1‘Ù$6b56ec96-4b9d-4281-bd63-061df324867f¬LaTeXStrings‘Ù$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea§scatter�¨scatter!�¤X_c2‘Ù$f8bb4fcd-9b20-44e5-8e22-e792e74b69dfÙ$25efa2fe-d294-11ef-172f-9bb09277f59e„´precedence_heuristic §cell_idÙ$25efa2fe-d294-11ef-172f-9bb09277f59e´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6eee35ee-fd55-498f-9441-f18c2508de19„´precedence_heuristic §cell_idÙ$6eee35ee-fd55-498f-9441-f18c2508de19´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6b56ec96-4b9d-4281-bd63-061df324867f„´precedence_heuristic §cell_idÙ$6b56ec96-4b9d-4281-bd63-061df324867f´downstream_cells_map�¤X_c1’Ù$f37ac438-cd56-4a09-bc8e-a75469955a2fÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5²upstream_cells_map‡¤conj�¡:�§findall�¡X‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1¡y‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1§adjoint�¡!�Ù$b8790891-1546-48e0-9f96-e09eade31c12„´precedence_heuristic §cell_idÙ$b8790891-1546-48e0-9f96-e09eade31c12´downstream_cells_map�¥logσ‘Ù$b48f8800-473d-48e4-ab78-eb07653db7a5²upstream_cells_map‚¡-�¨softplus�Ù$4ab4bcca-da6e-4137-858d-257c04388277„´precedence_heuristic §cell_idÙ$4ab4bcca-da6e-4137-858d-257c04388277´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�ªkeyconcept�Ù$25f02ac6-d294-11ef-26c4-f142b8ac4b5f„´precedence_heuristic §cell_idÙ$25f02ac6-d294-11ef-26c4-f142b8ac4b5f´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$3b24b142-2239-4951-9177-ff87b5da4b68„´precedence_heuristic §cell_idÙ$3b24b142-2239-4951-9177-ff87b5da4b68´downstream_cells_map€²upstream_cells_mapƒ§@md_str�ªhide_proof�¨getindex�Ù$25f12528-d294-11ef-0c65-97c61935e9c2„´precedence_heuristic §cell_idÙ$25f12528-d294-11ef-0c65-97c61935e9c2´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$3422dd29-6da9-4e0f-a4ab-646f223c2244„´precedence_heuristic §cell_idÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$5d91a2ae-975c-4913-b8ff-3df043cb1f03„´precedence_heuristic §cell_idÙ$5d91a2ae-975c-4913-b8ff-3df043cb1f03´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$0e713a7c-f4fa-4b86-abb3-26520e24d989„´precedence_heuristic §cell_idÙ$0e713a7c-f4fa-4b86-abb3-26520e24d989´downstream_cells_map€²upstream_cells_map�²navigate_prev_next�Ù$25ef12bc-d294-11ef-1557-d98ba829a804„´precedence_heuristic §cell_idÙ$25ef12bc-d294-11ef-1557-d98ba829a804´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25f19230-d294-11ef-2dfd-6d4927e86f57„´precedence_heuristic §cell_idÙ$25f19230-d294-11ef-2dfd-6d4927e86f57´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$a00c545c-2274-4086-94ca-319d1436fa26„´precedence_heuristic §cell_idÙ$a00c545c-2274-4086-94ca-319d1436fa26´downstream_cells_map€²upstream_cells_map�©exercises�Ù$22121f20-6b90-4782-8bed-25486cc23ae7„´precedence_heuristic §cell_idÙ$22121f20-6b90-4782-8bed-25486cc23ae7´downstream_cells_map€²upstream_cells_map�¬NotebookCard�Ù$e7c45ff8-9fa2-4ea3-a06f-5769d877540e„´precedence_heuristic §cell_idÙ$e7c45ff8-9fa2-4ea3-a06f-5769d877540e´downstream_cells_map€²upstream_cells_map‚·PlutoUI.TableOfContents�§PlutoUI�Ù$25f1b404-d294-11ef-1c3a-a5a8142bb202„´precedence_heuristic §cell_idÙ$25f1b404-d294-11ef-1c3a-a5a8142bb202´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$7932fff4-0568-49de-b34c-711e51487ae3„´precedence_heuristic §cell_idÙ$7932fff4-0568-49de-b34c-711e51487ae3´downstream_cells_map€²upstream_cells_map�²challenge_solution�Ù$1f2bfcf4-fef4-4612-8683-d5c86a326eef„´precedence_heuristic §cell_idÙ$1f2bfcf4-fef4-4612-8683-d5c86a326eef´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$9554ed0b-69dd-443c-9538-03a4117eeb78„´precedence_heuristic §cell_idÙ$9554ed0b-69dd-443c-9538-03a4117eeb78´downstream_cells_map€²upstream_cells_mapƒ§@md_str�ªhide_proof�¨getindex�Ù$aeee1072-5173-4eae-8027-3fbf2e338d95„´precedence_heuristic §cell_idÙ$aeee1072-5173-4eae-8027-3fbf2e338d95´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$22f4972e-0f6d-49a1-bd1f-10569b77a852„´precedence_heuristic §cell_idÙ$22f4972e-0f6d-49a1-bd1f-10569b77a852´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�ªkeyconcept�Ù$25f3bef2-d294-11ef-1438-e9f7e469336f„´precedence_heuristic §cell_idÙ$25f3bef2-d294-11ef-1438-e9f7e469336f´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$cf829697-6283-4d2f-b0dd-bbfbd689a145„´precedence_heuristic §cell_idÙ$cf829697-6283-4d2f-b0dd-bbfbd689a145´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$7e7cab21-09ab-4d06-9716-ab7864b229ab„´precedence_heuristic §cell_idÙ$7e7cab21-09ab-4d06-9716-ab7864b229ab´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$75076ca1-7226-4229-8e70-06cc586507c3„´precedence_heuristic §cell_idÙ$75076ca1-7226-4229-8e70-06cc586507c3´downstream_cells_map€²upstream_cells_map�­bayesian_plot‘Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60Ù$25f14f82-d294-11ef-02fb-2dc632b8f118„´precedence_heuristic §cell_idÙ$25f14f82-d294-11ef-02fb-2dc632b8f118´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25eefb10-d294-11ef-0734-2daf18636e8e„´precedence_heuristic §cell_idÙ$25eefb10-d294-11ef-0734-2daf18636e8e´downstream_cells_map€²upstream_cells_map�¥title�Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1„´precedence_heuristic §cell_idÙ$5e4bb719-ea9b-4a30-8800-5d753f405fd1´downstream_cells_map‚¡X“Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60Ù$6b56ec96-4b9d-4281-bd63-061df324867fÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69df¡y“Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60Ù$6b56ec96-4b9d-4281-bd63-061df324867fÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69df²upstream_cells_map‚¡N‘Ù$4ceede48-a4d5-446b-bb34-26cec4af357a°generate_dataset‘Ù$fcec3c3a-8b0b-4dfd-b010-66abbf330069Ù$25f19ed8-d294-11ef-3298-efa16dda1dde„´precedence_heuristic §cell_idÙ$25f19ed8-d294-11ef-3298-efa16dda1dde´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$b5a19a34-b210-41ec-853d-c5df13ae17ce„´precedence_heuristic §cell_idÙ$b5a19a34-b210-41ec-853d-c5df13ae17ce´downstream_cells_map�¦X_test‘Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5²upstream_cells_map€Ù$7e8804bc-b7a4-4f08-b801-fbf567a5641d„´precedence_heuristic §cell_idÙ$7e8804bc-b7a4-4f08-b801-fbf567a5641d´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$e379cc2a-43f8-432f-84fc-a88fd4f3ad0a„´precedence_heuristic§cell_idÙ$e379cc2a-43f8-432f-84fc-a88fd4f3ad0a´downstream_cells_map�²BmlipTeachingTools�²upstream_cells_map€Ù$25ef2806-d294-11ef-3cb6-0f3e76b9177e„´precedence_heuristic §cell_idÙ$25ef2806-d294-11ef-3cb6-0f3e76b9177e´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$bda07a2e-3769-4ffe-9bc5-2b8a515247f6„´precedence_heuristic §cell_idÙ$bda07a2e-3769-4ffe-9bc5-2b8a515247f6´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76„´precedence_heuristic §cell_idÙ$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76´downstream_cells_map€²upstream_cells_map‹¡/�¡-�£exp�£cdf�¢Ï€�¦Normal�¡+�¥plot!�¤sqrt�¤plot�¡*�Ù$fcec3c3a-8b0b-4dfd-b010-66abbf330069„´precedence_heuristic §cell_idÙ$fcec3c3a-8b0b-4dfd-b010-66abbf330069´downstream_cells_map�°generate_dataset‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1²upstream_cells_mapÞ¦isless�¥Int64�¦Vector�¡+�¡>�§Float64�¥randn�¡:�¤Bool�¡<�¤sqrt�©StableRNG�¦Matrix�¥undef�¤rand�¡*�Ù$25f14226-d294-11ef-369f-e545d5fe2700„´precedence_heuristic §cell_idÙ$25f14226-d294-11ef-369f-e545d5fe2700´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea„´precedence_heuristic§cell_idÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea´downstream_cells_mapƒ¦Random�¬LaTeXStrings‘Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5¥Plots�²upstream_cells_map€Ù$25ef7f54-d294-11ef-3f05-0d85fe6e7a17„´precedence_heuristic §cell_idÙ$25ef7f54-d294-11ef-3f05-0d85fe6e7a17´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb„´precedence_heuristic §cell_idÙ$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb´downstream_cells_map‚´predictive_posterior‘Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60¨#437#val�²upstream_cells_mapÞ¤mean�¡/�£inv�£Any�§adjoint�¢Ï€�¡+�¤@doc�£cov�¥Union�§normcdf�º#___this_pluto_module_name�¤sqrt�¤conj�¥Tuple�¡*�Ù$25f365e2-d294-11ef-300e-9914333b1233„´precedence_heuristic §cell_idÙ$25f365e2-d294-11ef-300e-9914333b1233´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$25f1390c-d294-11ef-364d-17e4c93b9a57„´precedence_heuristic §cell_idÙ$25f1390c-d294-11ef-364d-17e4c93b9a57´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f8bb4fcd-9b20-44e5-8e22-e792e74b69df„´precedence_heuristic §cell_idÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69df´downstream_cells_map�¤X_c2’Ù$f37ac438-cd56-4a09-bc8e-a75469955a2fÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5²upstream_cells_map†¤conj�¡:�§findall�¡X‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1¡y‘Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1§adjoint�Ù$b48f8800-473d-48e4-ab78-eb07653db7a5„´precedence_heuristic §cell_idÙ$b48f8800-473d-48e4-ab78-eb07653db7a5´downstream_cells_map�®log_likelihood‘Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482²upstream_cells_map†¤conj�¥logσ‘Ù$b8790891-1546-48e0-9f96-e09eade31c12¡-�£sum�§adjoint�¡*�Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482„´precedence_heuristic §cell_idÙ$1bfac9c5-e5cf-4a70-b077-11bb00cb1482´downstream_cells_map‚¨#436#val�Ù bayesian_discrimination_boundary‘Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60²upstream_cells_mapÞ¤mean�¡-�£Any�¨logistic�§adjoint�¯Optim.minimizer�¦Vector�¦length�¦logpdf�¡+�¤@doc�¥Union�µMvNormalMeanPrecision�¡:�º#___this_pluto_module_name�¤Bool�©precision�¤conj�¥Tuple�¥zeros�¦Matrix�¥Optim‘Ù$616e84d7-063d-4d9d-99e4-56aecf3c7ee4®log_likelihood‘Ù$b48f8800-473d-48e4-ab78-eb07653db7a5¤view�¨optimize�¡*�Ù$98ef7093-f8ed-4a44-a153-0a64ab483f65„´precedence_heuristic §cell_idÙ$98ef7093-f8ed-4a44-a153-0a64ab483f65´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f37ac438-cd56-4a09-bc8e-a75469955a2f„´precedence_heuristic §cell_idÙ$f37ac438-cd56-4a09-bc8e-a75469955a2f´downstream_cells_map€²upstream_cells_map‹¤X_c1‘Ù$6b56ec96-4b9d-4281-bd63-061df324867f¬plot_dataset‘Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5§adjoint�¤X_c2‘Ù$f8bb4fcd-9b20-44e5-8e22-e792e74b69df¨MvNormal�§nothing�¥range�§fit_mle�¤conj�¨contour!�£pdf�Ù$25f3ff84-d294-11ef-0031-63b23d23324d„´precedence_heuristic §cell_idÙ$25f3ff84-d294-11ef-0031-63b23d23324d´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab„´precedence_heuristic §cell_idÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab´downstream_cells_map€²upstream_cells_map�¦N_bond‘Ù$4ceede48-a4d5-446b-bb34-26cec4af357aÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5„´precedence_heuristic §cell_idÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bc„´precedence_heuristic §cell_idÙ$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bc´downstream_cells_map€²upstream_cells_map�¬plot_dataset‘Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5Ù$0045e569-dc3c-4998-86da-9d96f599c599„´precedence_heuristic §cell_idÙ$0045e569-dc3c-4998-86da-9d96f599c599´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$ff31d8c1-db35-4c85-a609-67fc40e9e78d„´precedence_heuristic §cell_idÙ$ff31d8c1-db35-4c85-a609-67fc40e9e78d´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$616e84d7-063d-4d9d-99e4-56aecf3c7ee4„´precedence_heuristic§cell_idÙ$616e84d7-063d-4d9d-99e4-56aecf3c7ee4´downstream_cells_mapˆ­LinearAlgebra�±ExponentialFamily�¥Optim‘Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482¯LogExpFunctions�©StatsFuns�­Distributions�©BayesBase�ªStableRNGs�²upstream_cells_map€®last_save_timeËAÚ¹&¡øt «notebook_idÙ$38f12344-d465-11f1-81aa-0f95722f21ec«in_temp_dir¨metadata�«frontmatterƒ«descriptionÙVIntroduction to discriminative classification models and Bayesian logistic regression.¥imageÙMhttps://github.com/bmlip/course/blob/v2/assets/figures/Figure4.9.png?raw=true¦author‘‚¤name¥BMLIP£url¸https://github.com/bmlip¥bonds€´cell_execution_orderÜNÙ$e379cc2a-43f8-432f-84fc-a88fd4f3ad0aÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4eaÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952Ù$616e84d7-063d-4d9d-99e4-56aecf3c7ee4Ù$25eefb10-d294-11ef-0734-2daf18636e8eÙ$e7c45ff8-9fa2-4ea3-a06f-5769d877540eÙ$25ef12bc-d294-11ef-1557-d98ba829a804Ù$fe66a986-2f55-4417-a71d-b3b99f6369ccÙ$25ef2806-d294-11ef-3cb6-0f3e76b9177eÙ$4ceede48-a4d5-446b-bb34-26cec4af357aÙ$7e7cab21-09ab-4d06-9716-ab7864b229abÙ$aeee1072-5173-4eae-8027-3fbf2e338d95Ù$93083660-6a49-4147-b00c-d62a4453f222Ù$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3cÙ$25ef6ece-d294-11ef-270a-999c8d457b24Ù$25ef7f54-d294-11ef-3f05-0d85fe6e7a17Ù$25efa2fe-d294-11ef-172f-9bb09277f59eÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5Ù$25f02ac6-d294-11ef-26c4-f142b8ac4b5fÙ$25f0adde-d294-11ef-353e-4b4773df9ff5Ù$22121f20-6b90-4782-8bed-25486cc23ae7Ù$25f12528-d294-11ef-0c65-97c61935e9c2Ù$25f14226-d294-11ef-369f-e545d5fe2700Ù$25f14f82-d294-11ef-02fb-2dc632b8f118Ù$25f15e0a-d294-11ef-3737-79a68c9b3c61Ù$22f4972e-0f6d-49a1-bd1f-10569b77a852Ù$25f19ed8-d294-11ef-3298-efa16dda1ddeÙ$25f1390c-d294-11ef-364d-17e4c93b9a57Ù$bda07a2e-3769-4ffe-9bc5-2b8a515247f6Ù$25f1ab08-d294-11ef-32ed-493792e121b7Ù$25f1b404-d294-11ef-1c3a-a5a8142bb202Ù$25f1c2a0-d294-11ef-009c-69b64e87e5fbÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244Ù$8b0bb225-bdc1-45ec-bd34-68d674d6f08dÙ$7e8804bc-b7a4-4f08-b801-fbf567a5641dÙ$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76Ù$66351c02-1921-44dc-b461-84a536c40fd5Ù$e4cc517b-d3b5-4517-a28b-efb8aba24496Ù$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fcÙ$9704ee6a-e233-49f1-8c66-d81543927342Ù$4ab4bcca-da6e-4137-858d-257c04388277Ù$38b4854f-be02-4696-802f-2106481e3aeaÙ$7932fff4-0568-49de-b34c-711e51487ae3Ù$25f3bef2-d294-11ef-1438-e9f7e469336fÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3abÙ$69706576-0333-43bf-8523-e7838f373529Ù$98ef7093-f8ed-4a44-a153-0a64ab483f65Ù$0045e569-dc3c-4998-86da-9d96f599c599Ù$25f365e2-d294-11ef-300e-9914333b1233Ù$3b24b142-2239-4951-9177-ff87b5da4b68Ù$ff31d8c1-db35-4c85-a609-67fc40e9e78dÙ$1f2bfcf4-fef4-4612-8683-d5c86a326eefÙ$25f3ff84-d294-11ef-0031-63b23d23324dÙ$5d91a2ae-975c-4913-b8ff-3df043cb1f03Ù$25f41118-d294-11ef-13a8-3fa6587c1bf3Ù$25f19230-d294-11ef-2dfd-6d4927e86f57Ù$173e545a-9125-4dc4-8df4-c3aa84a4b88aÙ$557c4545-2208-43c9-9302-991e78202734Ù$a00c545c-2274-4086-94ca-319d1436fa26Ù$b94644f8-725d-49bf-9641-3dad8b647f45Ù$9554ed0b-69dd-443c-9538-03a4117eeb78Ù$0e713a7c-f4fa-4b86-abb3-26520e24d989Ù$6eee35ee-fd55-498f-9441-f18c2508de19Ù$fcec3c3a-8b0b-4dfd-b010-66abbf330069Ù$b8790891-1546-48e0-9f96-e09eade31c12Ù$b48f8800-473d-48e4-ab78-eb07653db7a5Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482Ù$fd908bf5-71a1-4ae8-8416-cc1fdf084dcbÙ$cf829697-6283-4d2f-b0dd-bbfbd689a145Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1Ù$6b56ec96-4b9d-4281-bd63-061df324867fÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69dfÙ$b5a19a34-b210-41ec-853d-c5df13ae17ceÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5Ù$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bcÙ$f37ac438-cd56-4a09-bc8e-a75469955a2fÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60Ù$75076ca1-7226-4229-8e70-06cc586507c3®process_status¥readyªcell_orderÜNÙ$25eefb10-d294-11ef-0734-2daf18636e8eÙ$e7c45ff8-9fa2-4ea3-a06f-5769d877540eÙ$25ef12bc-d294-11ef-1557-d98ba829a804Ù$fe66a986-2f55-4417-a71d-b3b99f6369ccÙ$25ef2806-d294-11ef-3cb6-0f3e76b9177eÙ$4ceede48-a4d5-446b-bb34-26cec4af357aÙ$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bcÙ$7e7cab21-09ab-4d06-9716-ab7864b229abÙ$aeee1072-5173-4eae-8027-3fbf2e338d95Ù$f37ac438-cd56-4a09-bc8e-a75469955a2fÙ$93083660-6a49-4147-b00c-d62a4453f222Ù$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3cÙ$25ef6ece-d294-11ef-270a-999c8d457b24Ù$25ef7f54-d294-11ef-3f05-0d85fe6e7a17Ù$25efa2fe-d294-11ef-172f-9bb09277f59eÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5Ù$25f02ac6-d294-11ef-26c4-f142b8ac4b5fÙ$25f0adde-d294-11ef-353e-4b4773df9ff5Ù$22121f20-6b90-4782-8bed-25486cc23ae7Ù$25f12528-d294-11ef-0c65-97c61935e9c2Ù$25f14226-d294-11ef-369f-e545d5fe2700Ù$25f14f82-d294-11ef-02fb-2dc632b8f118Ù$25f15e0a-d294-11ef-3737-79a68c9b3c61Ù$22f4972e-0f6d-49a1-bd1f-10569b77a852Ù$25f19ed8-d294-11ef-3298-efa16dda1ddeÙ$25f1390c-d294-11ef-364d-17e4c93b9a57Ù$bda07a2e-3769-4ffe-9bc5-2b8a515247f6Ù$25f1ab08-d294-11ef-32ed-493792e121b7Ù$25f1b404-d294-11ef-1c3a-a5a8142bb202Ù$25f1c2a0-d294-11ef-009c-69b64e87e5fbÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244Ù$8b0bb225-bdc1-45ec-bd34-68d674d6f08dÙ$7e8804bc-b7a4-4f08-b801-fbf567a5641dÙ$ffe6e2b9-8b0b-4ee4-a103-6b7a8a299d76Ù$66351c02-1921-44dc-b461-84a536c40fd5Ù$e4cc517b-d3b5-4517-a28b-efb8aba24496Ù$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fcÙ$9704ee6a-e233-49f1-8c66-d81543927342Ù$4ab4bcca-da6e-4137-858d-257c04388277Ù$38b4854f-be02-4696-802f-2106481e3aeaÙ$7932fff4-0568-49de-b34c-711e51487ae3Ù$25f3bef2-d294-11ef-1438-e9f7e469336fÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3abÙ$75076ca1-7226-4229-8e70-06cc586507c3Ù$69706576-0333-43bf-8523-e7838f373529Ù$98ef7093-f8ed-4a44-a153-0a64ab483f65Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60Ù$0045e569-dc3c-4998-86da-9d96f599c599Ù$25f365e2-d294-11ef-300e-9914333b1233Ù$3b24b142-2239-4951-9177-ff87b5da4b68Ù$ff31d8c1-db35-4c85-a609-67fc40e9e78dÙ$1f2bfcf4-fef4-4612-8683-d5c86a326eefÙ$25f3ff84-d294-11ef-0031-63b23d23324dÙ$5d91a2ae-975c-4913-b8ff-3df043cb1f03Ù$25f41118-d294-11ef-13a8-3fa6587c1bf3Ù$25f19230-d294-11ef-2dfd-6d4927e86f57Ù$173e545a-9125-4dc4-8df4-c3aa84a4b88aÙ$557c4545-2208-43c9-9302-991e78202734Ù$a00c545c-2274-4086-94ca-319d1436fa26Ù$b94644f8-725d-49bf-9641-3dad8b647f45Ù$9554ed0b-69dd-443c-9538-03a4117eeb78Ù$0e713a7c-f4fa-4b86-abb3-26520e24d989Ù$6eee35ee-fd55-498f-9441-f18c2508de19Ù$e379cc2a-43f8-432f-84fc-a88fd4f3ad0aÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4eaÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952Ù$616e84d7-063d-4d9d-99e4-56aecf3c7ee4Ù$fcec3c3a-8b0b-4dfd-b010-66abbf330069Ù$a65ca01a-0e9a-42cb-b1d7-648102a77eb5Ù$b8790891-1546-48e0-9f96-e09eade31c12Ù$b48f8800-473d-48e4-ab78-eb07653db7a5Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482Ù$fd908bf5-71a1-4ae8-8416-cc1fdf084dcbÙ$cf829697-6283-4d2f-b0dd-bbfbd689a145Ù$5e4bb719-ea9b-4a30-8800-5d753f405fd1Ù$6b56ec96-4b9d-4281-bd63-061df324867fÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69dfÙ$b5a19a34-b210-41ec-853d-c5df13ae17ce«cell_inputsÞNÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f15e0a-d294-11ef-3737-79a68c9b3c61«code_foldedäcodeÙÇmd""" #### Prior In *Bayesian* logistic regression, we often add a **Gaussian prior on the weights**: ```math \begin{align*} p(w) = \mathcal{N}(w \,|\, m_0, S_0) \tag{B-4.140} \end{align*} ``` """Ù$25f1ab08-d294-11ef-32ed-493792e121b7„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f1ab08-d294-11ef-32ed-493792e121b7«code_foldedäcodeÚpmd""" ## Application: the predictive distribution For a new data point ``x_\bullet``, the predictive distribution for ``y_\bullet=1`` is given by ```math \begin{align*} p(y_\bullet = 1 | x_\bullet, D) &= \int p(y_\bullet = 1 \,|\, x_\bullet, w) \, p(w| D) \,\mathrm{d}w \\ &= \int \sigma(w^T x_\bullet) \, p(w| D) \,\mathrm{d}w \tag{B-4.145} \end{align*} ``` """Ù$557c4545-2208-43c9-9302-991e78202734„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$557c4545-2208-43c9-9302-991e78202734«code_foldedäcode³keyconceptsummary()Ù$25f41118-d294-11ef-13a8-3fa6587c1bf3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f41118-d294-11ef-13a8-3fa6587c1bf3«code_foldedäcodeÚª@mdx """ Let us recapitulate the differences between the generative and discriminative approaches to classification in a table:
Generative Discriminative (ML)
1Like density estimation, model joint prob. ```math p(\\mathcal{C}_k) p(x|\\mathcal{C}_k) = \\pi_k \\mathcal{N}(\\mu_k,\\Sigma) ``` Like (linear) regression, model conditional ```math p(\\mathcal{C}_k|x,\\theta) ```
2Leads to softmax posterior class probability ```math p(\\mathcal{C}_k|x,\\theta ) = e^{\\theta_k^T x}/Z ``` with structured ``\\theta`` Choose also softmax posterior class probability ```math p(\\mathcal{C}_k|x,\\theta ) = e^{\\theta_k^T x}/Z ``` but now with 'free' ``\\theta``
3 For Gaussian ``p(x|\\mathcal{C}_k)`` and multinomial priors, ```math \\hat \\theta_k = \\left[ {\\begin{array}{c} { - \\frac{1}{2} \\mu_k^T \\sigma^{-1} \\mu_k + \\log \\pi_k} \\\\ {\\sigma^{-1} \\mu_k } \\\\ \\end{array}} \\right] ``` in one shot. Find ``\\hat\\theta_k`` through gradient-based adaptation ```math \\nabla_{\\theta_k}\\mathrm{L}(\\theta) = \\sum_n \\Big( y_{nk} - \\frac{e^{\\theta_k^T x_n}}{\\sum_{k^\\prime} e^{\\theta_{k^\\prime}^T x_n}} \\Big)\\, x_n ```
"""Ù$9704ee6a-e233-49f1-8c66-d81543927342„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$9704ee6a-e233-49f1-8c66-d81543927342«code_foldedäcodeÚfkeyconcept("", md""" It is easy to write down the Bayesian equations for the weight posterior and the predictive distribution of ``y_\bullet``, but in practice these do not admit closed-form solutions. A traditional workaround is the **Laplace approximation**, which has long been used to approximate the marginalization integrals in Bayesian inference. """)Ù$38b4854f-be02-4696-802f-2106481e3aea„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$38b4854f-be02-4696-802f-2106481e3aea«code_foldedäcodeÚ@md""" ## Bayesian Processing of Uncertainties We now make an important observation: According to Eq. B-4.143, the posterior covariance matrix ``S_N`` of the weight vector depends on both the prior variance ``S_0`` and the distribution of the training data ``\{(x_n, y_n)\}_{n=1}^N``. In regions with limited training data and/or an uninformative prior (i.e., large ``S_0``), the posterior uncertainty about the weights remains high. This increased uncertainty raises ``\sigma_a^2`` in Eq. B-4.150, **causing the posterior class probability in Eq. B-4.152 to approach ``0.5``** (since ``\Phi(0) = 0.5``, see [Gaussian CDF image](https://en.wikipedia.org/wiki/Normal_distribution#/media/File:Normal_Distribution_CDF.svg)), thereby reflecting greater uncertainty in the prediction. In other words, if you draw a new feature ``x_\bullet`` from a region with little training data, then the predictive class probability ``p(y_\bullet | x_\bullet, D)`` naturally tends toward ``0.5``, a built-in expression of uncertainty, courtesy of the Bayesian framework. In contrast, if you eliminate uncertainty by representing the weights as fixed-point estimates (i.e., as mere numbers), then the model becomes overconfident. It will still produce sharp predictions, even in regions where it has seen no data, exactly when it should be most uncertain. """Ù$25f0adde-d294-11ef-353e-4b4773df9ff5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f0adde-d294-11ef-353e-4b4773df9ff5«code_foldedäcodeÚþmd""" #### Data-generating distribution We will take inspiration from the [generative classification](https://bmlip.github.io/course/lectures/Generative%20Classification.html#softmax) approach, where we derived the class posterior ```math p(y_{nk} = 1\,|\,x_n,\beta_k,\gamma_k) = \sigma(\beta_k^T x_n + \gamma_k) ``` as a **softmax** function of a linear map of the input. Here, for the 2-class problem, we *choose* the 2-class softmax function (which is called the [**logistic** function](https://en.wikipedia.org/wiki/Logistic_function)) with linear discrimination bounderies for the posterior class probability: ```math p(y_n =1 \,|\, x_n, w) = \sigma(w^T x_n) \,. ``` where ```math \sigma(a) = \frac{1}{1+e^{-a}} ``` is the *logistic* function. """Ù$25ef6ece-d294-11ef-270a-999c8d457b24„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25ef6ece-d294-11ef-270a-999c8d457b24«code_foldedäcodeÙ«md""" ## Framework A data set is given by ``D = \{(x_1,y_1),\dotsc,(x_N,y_N)\}`` with ``x_n \in \mathbb{R}^M`` and ``y_n \in \mathcal{C}_k``, with ``k=1,\ldots,K``. """Ù$8b0bb225-bdc1-45ec-bd34-68d674d6f08d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8b0bb225-bdc1-45ec-bd34-68d674d6f08d«code_foldedäcodeÚ¶md""" The [Laplace Approximation](https://en.wikipedia.org/wiki/Laplace%27s_approximation) approximates a function by a Gaussian-shaped function. In this case, we will approximate the weight posterior ``p(w|D)`` (Eq. B-4.142) by a Gaussian distribution ```math q(w) = \mathcal{N}\left(w\,|\, m_{N}, S_N\right) \tag{B-4.144} ``` with ```math \begin{align} m_N &= \arg\max_w \log p(w|D) \\ S_N^{-1} &= S_0^{-1} + \sum_n \sigma_n (1-\sigma_n) x_n x_n^T \tag{B-4.143} \end{align} ``` where we used short-hand ``\sigma_n = \sigma\left((2y_n-1) m_N^T x_n\right)``. If we substitute the Gaussian approximation from Eq. B-4.143 into the expression for the predictive class distribution (Eq. B-4.145), we obtain (after some additional approximations): ```math \begin{align*} p(y_\bullet = 1 \mid x_\bullet, D) &= \int p(y_\bullet = 1 \,|\, x_\bullet, w) \cdot p(w\,|\, D) \,\mathrm{d}w \\ &\approx \int p(y_\bullet = 1 \,|\, x_\bullet, w) \cdot q(w) \,\mathrm{d}w \\ &= \int \sigma(w^T x_\bullet) \cdot \mathcal{N}\left(w \,|\, m_N, S_N\right) \,\mathrm{d}w \tag{B-4.145} \\ &\approx \Phi\left( \frac{\mu_a}{\sqrt(\lambda^{-2} +\sigma_a^2)}\right) \tag{B-4.152} \end{align*} ``` where ```math \begin{align} \lambda^2 &= \pi / 8 \\ \mu_a &= m^T_{N} x_\bullet \tag{B-4.149} \\ \sigma_a^2 &= x^T_\bullet S_N x_\bullet \tag{B-4.150} \end{align} ``` and ``\Phi(x)= \frac{1}{\sqrt(2\pi)}\int_{-\infty}^{x}e^{-t^2/2}\mathrm{d}t`` is the Standard Normal [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) (CDF). Although the intermediate equations may look intimidating, the final result for the predictive distribution Eq. B-4.152 has a simple closed-form expression. """Ù$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fc„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$33b859f2-9ea8-4f8b-b0f8-08a19c6a96fc«code_foldedäcodeÙQNotebookCard("https://bmlip.github.io/course/minis/Laplace%20Approximation.html")Ù$fe66a986-2f55-4417-a71d-b3b99f6369cc„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$fe66a986-2f55-4417-a71d-b3b99f6369cc«code_foldedäcodeÙechallenge_statement("difficult class-conditional data distributions" , header_level=1, color= "red" )Ù$e4cc517b-d3b5-4517-a28b-efb8aba24496„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$e4cc517b-d3b5-4517-a28b-efb8aba24496«code_foldedäcodeÙ·md""" The numerical issues associated with the Laplace approximation and the evaluation of the predictive class distribution are discussed in detail in the following mini-lecture. """Ù$25f1c2a0-d294-11ef-009c-69b64e87e5fb„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f1c2a0-d294-11ef-009c-69b64e87e5fb«code_foldedäcodeÙåmd""" Many methods have been developed to approximate these types of Bayesian integrals. Here, we present the **Laplace approximation**, which is one of the simplest methods with broad applicability to Bayesian calculations. """Ù$173e545a-9125-4dc4-8df4-c3aa84a4b88a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$173e545a-9125-4dc4-8df4-c3aa84a4b88a«code_foldedäcode³md""" # Summary """Ù$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3c„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d1bbdc6a-e5ff-4cd6-9175-860b5ec04f3c«code_foldedäcodeÙ(md""" # Bayesian Logistic Regression """Ù$69706576-0333-43bf-8523-e7838f373529„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$69706576-0333-43bf-8523-e7838f373529«code_foldedäcodeÙämd""" Note that we get a full predictive posterior distribution over the assignment of class labels for every datapoint, so instead of assigning a point to a class, we get a measure of uncertainty over our class assignment! """Ù$fce5d561-ea76-4bd8-9cce-6f707f72fc60„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$fce5d561-ea76-4bd8-9cce-6f707f72fc60«code_folded¤codeÚ™bayesian_plot = let X_ext = vcat(X, ones(1, length(y))) # Define a prior distribution over parameters, play with this to see the result change! prior = MvNormalMeanCovariance(zeros(3), 100 .* diagm(ones(3))) posterior = bayesian_discrimination_boundary(prior, X_ext, y) # Plot 50% boundary θ = mean(posterior) disc_boundary(x1) = -1 / θ[2] * (θ[1]*x1 + θ[3]) plot_dataset() plot!([-2., 10.], disc_boundary; label="Discr. boundary", linewidth=2) # Plot heatmap xrange = range(-1.6, 9; length=50) yrange = range(-2, 7; length=30) heatmap!( xrange, yrange, (x,y) -> predictive_posterior([x, y, 1], posterior); alpha=0.5, color=:redblue, ) end;Ù$66351c02-1921-44dc-b461-84a536c40fd5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$66351c02-1921-44dc-b461-84a536c40fd5«code_foldedäcodeÚmd""" _The logistic function_ ``\sigma(a) = 1/(1+e^{-a})`` _(red), together with the scaled Standard Normal_ [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) _(CDF)_ ``\Phi(\lambda a) = \frac{1}{\sqrt(2\pi)}\int_{-\infty}^{a}e^{-t^2/2}\mathrm{d}t``, _for_ ``\lambda^2=\pi/8`` _(in blue). We will use this approximation later in the_ [Laplace approximation](https://bmlip.github.io/course/minis/Laplace%20Approximation.html#gaussian-cdf). _Based on Bishop fig.4.9._ """Ù$ad196ae6-c65e-4aaa-b0cc-bd72daa41952„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$ad196ae6-c65e-4aaa-b0cc-bd72daa41952«code_folded¤code»using MarkdownLiteral: @mdxÙ$93083660-6a49-4147-b00c-d62a4453f222„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$93083660-6a49-4147-b00c-d62a4453f222«code_foldedäcodeÚmd""" That's not a good fit for the $y=1$ class! Now, we could continue down this road and try to fit a more complex distribution to the ``y = 1`` class to make it fit. **But let's explore a different approach**, modeling the discriminative boundary directly. """Ù$b94644f8-725d-49bf-9641-3dad8b647f45„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b94644f8-725d-49bf-9641-3dad8b647f45«code_foldedäcodeÚ md""" #### Discrimination boundaries (*) Show that for logistic regression with ``p(y_n =1 \,|\, x_n) = \sigma(w^T x_n)``, the discrimination boundary, which can be computed by ```math \frac{p(y_n =1|x_n)}{p(y_n =0|x_n)} \overset{!}{=} 1 ``` is a straight line. """Ù$4ceede48-a4d5-446b-bb34-26cec4af357a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4ceede48-a4d5-446b-bb34-26cec4af357a«code_foldedäcodeÙKbegin N_bond = @bindname N Slider(8:200; default=120, show_value=true) endÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a65ca01a-0e9a-42cb-b1d7-648102a77eb5«code_foldedäcodeÚ_function plot_dataset() result = scatter(X_c1[:,1], X_c1[:,2],markersize=4, label=L"y=0", xlabel=L"x_1", ylabel=L"x_2", xlims=(-1.6, 9), ylims=(-2, 7)) scatter!(X_c2[:,1], X_c2[:,2],markersize=4, label=L"y=1") scatter!([X_test[1]], [X_test[2]], markersize=7, marker=:star, label=L"y=?") plot!(legend=:bottomright) return result endÙ$25efa2fe-d294-11ef-172f-9bb09277f59e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25efa2fe-d294-11ef-172f-9bb09277f59e«code_foldedäcodeÚmd""" Here's an **IDEA**! Let's model the posterior ```math p(y_n\in\mathcal{C}_k|x_n) ``` *directly*, without any assumptions on the prior class densities ``p(x)``. This approach is called [Discriminative Classification](https://en.wikipedia.org/wiki/Discriminative_model). """Ù$6eee35ee-fd55-498f-9441-f18c2508de19„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6eee35ee-fd55-498f-9441-f18c2508de19«code_foldedäcode°md""" # Code """Ù$6b56ec96-4b9d-4281-bd63-061df324867f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6b56ec96-4b9d-4281-bd63-061df324867f«code_folded¤codeÙ8X_c1 = X[:,findall(.!y)]' # Split X based on class labelÙ$b8790891-1546-48e0-9f96-e09eade31c12„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b8790891-1546-48e0-9f96-e09eade31c12«code_foldedäcode·logσ(x) = -softplus(x)Ù$4ab4bcca-da6e-4137-858d-257c04388277„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4ab4bcca-da6e-4137-858d-257c04388277«code_foldedäcodeÚ)keyconcept("", md""" The Laplace approximation renders a closed-form and interpretable (approximate) solution for the predictive class distribution, ```math p(y_\bullet = 1 | x_\bullet, D) \approx \Phi\left( \frac{m^T_{N} x_\bullet}{\sqrt{(8/\pi) +x^T_\bullet S_N x_\bullet)}}\right)\,. ``` """)Ù$25f02ac6-d294-11ef-26c4-f142b8ac4b5f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f02ac6-d294-11ef-26c4-f142b8ac4b5f«code_foldedäcodeÙdmd""" What model should we use for the posterior distribution ``p(y_n \in \mathcal{C}_k|x_n)``? """Ù$3b24b142-2239-4951-9177-ff87b5da4b68„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$3b24b142-2239-4951-9177-ff87b5da4b68«code_foldedäcodeÚ hide_proof( md""" The Log-likelihood is ```math \mathrm{L}(w) = \log \prod_n \prod_k {\underbrace{p(y_{nk}=1|x_n,w)}_{p_{nk}}}^{y_{nk}} = \sum_{n,k} y_{nk} \log p_{nk} ``` Use the fact that the softmax ``\phi_k \equiv e^{a_k} / {\sum_j e^{a_j}}`` has analytical derivative, ```math \begin{align*} \frac{\partial \phi_k}{\partial a_j} &= \frac{(\sum_j e^{a_j})e^{a_k}\delta_{kj}-e^{a_j}e^{a_k}}{(\sum_j e^{a_j})^2} = \frac{e^{a_k}}{\sum_j e^{a_j}}\delta_{kj} - \frac{e^{a_j}}{\sum_j e^{a_j}} \frac{e^{a_k}}{\sum_j e^{a_j}}\\ &= \phi_k \cdot(\delta_{kj}-\phi_j) \,. \end{align*} ``` Take the derivative of ``\mathrm{L}(w)`` (or: how to spend an hour ...) ```math \begin{align*} \nabla_{w_j} \mathrm{L}(w) &= \sum_{n,k} \frac{\partial \mathrm{L}_{nk}}{\partial p_{nk}} \cdot\frac{\partial p_{nk}}{\partial a_{nj}}\cdot\frac{\partial a_{nj}}{\partial w_j} \\ &= \sum_{n,k} \frac{y_{nk}}{p_{nk}} \cdot p_{nk} (\delta_{kj}-p_{nj}) \cdot x_n \\ &= \sum_n \Big( y_{nj} (1-p_{nj}) -\sum_{k\neq j} y_{nk} p_{nj} \Big) \cdot x_n \\ &= \sum_n \left( y_{nj} - p_{nj} \right)\cdot x_n \\ &= \sum_n \Big( \underbrace{y_{nj}}_{\text{target}} - \underbrace{\frac{e^{w_j^T x_n}}{\sum_{j^\prime} e^{w_{j^\prime}^T x_n}}}_{\text{prediction}} \Big)\cdot x_n \end{align*} ``` """)Ù$25f12528-d294-11ef-0c65-97c61935e9c2„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f12528-d294-11ef-0c65-97c61935e9c2«code_foldedäcodeÚ¿md""" Since ``y_n \in \{0,1\}``, it follows that ``p(y_n =0 \,|\, x_n, w) = 1- \sigma(w^T x_n)``. In summary, the posterior class distribution ``p(y_n \,|\, x_n, w)`` in logistic regression is specified as ```math \begin{align*} p(y_n \,|\, x_n, w) &= \mathrm{Bernoulli}\left(y_n \,|\, \sigma(w^T x_n) \right) \\ &= \sigma(w^T x_n)^{y_n} \left(1 - \sigma(w^T x_n)\right)^{(1-y_n)} \tag{B-4.89} \\ &= \sigma\left( (2y_n-1) w^T x_n\right) \end{align*} ``` Note that for the 3rd equality, we have made use of the fact that ``\sigma(-a) = 1-\sigma(a)``. Each of these three models in B-4.89 are **equivalent**. We mention all three notational options since they all appear in the literature. """Ù$3422dd29-6da9-4e0f-a4ab-646f223c2244„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$3422dd29-6da9-4e0f-a4ab-646f223c2244«code_foldedäcodeÙ0.6)) # p(y=true) = 0.6 # Sample class 1 conditional distribution if rand(rng)<0.5 X[:,n] = [6.0; 0.5] .* rand(rng, 2) .+ [3.0; 6.0] else X[:,n] = sqrt(0.5) * randn(rng, 2) .+ [5.5, 0.0] end else # Sample class 2 conditional distribution X[:,n] = randn(rng, 2) .+ [1., 4.] end end return (X, y) endÙ$25f14226-d294-11ef-369f-e545d5fe2700„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f14226-d294-11ef-369f-e545d5fe2700«code_foldedäcodeÚ£md""" This choice for the class posterior is called **logistic regression**, in analogy to [linear regression](https://bmlip.github.io/course/lectures/Regression.html#likelihood-function): ```math \begin{align} p(y_n|x_n,w) &= \mathcal{N}(y_n|w^T x_n,\beta^{-1}) \tag{for linear regression} \\ p(y_n|x_n,w) &= \mathrm{Bernoulli}\left(y_n \,|\, \sigma(w^T x_n) \right) \tag{for logistic regression} \end{align} ``` """Ù$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a759653c-0da4-40b7-9e9e-1e3d2e4df4ea«code_folded¤codeÙ!using Random, Plots, LaTeXStringsÙ$25ef7f54-d294-11ef-3f05-0d85fe6e7a17„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25ef7f54-d294-11ef-3f05-0d85fe6e7a17«code_foldedäcodeÙæmd""" Sometimes, the precise assumptions of the (Gaussian-Categorical) generative model ```math p(x_n,y_n\in\mathcal{C}_k|\theta) = \pi_k \cdot \mathcal{N}(x_n|\mu_k,\Sigma) ``` clearly do not match the data distribution. """Ù$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$fd908bf5-71a1-4ae8-8416-cc1fdf084dcb«code_foldedäcodeÚQ""" Computes the predictive posterior eq. B-4.152 using the given approximation to the sigmoid function. """ function predictive_posterior(x, weight_posterior) λsq = Ï€ / 8 wN = mean(weight_posterior) μ = wN' * x σ = x' * cov(weight_posterior) * x query_point = μ / (sqrt(inv(λsq) + σ )) return normcdf(0, 1, query_point) endÙ$25f365e2-d294-11ef-300e-9914333b1233„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f365e2-d294-11ef-300e-9914333b1233«code_foldedäcodeÚémd""" Rather than the computationally involved Laplace approximation, in practice, discriminative classification is often executed through maximum likelihood estimation. With the usual 1-of-K encoding scheme for classes, ```math y_{nk} = \begin{cases} 1 & \text{if } x_n \in \mathcal{C}_k \\ 0 & \text{otherwise} \,,\end{cases} ``` the log-likelihood for a ``K``-dimensional discriminative classifier evaluates to ```math \begin{align*} \mathrm{L}(w) &= \log \prod_n \prod_k {p(\mathcal{C}_k|x_n,w)}^{y_{nk}} \\ &= \sum_n \sum_k y_{kn} \log \bigg( \underbrace{\frac{e^{w_k^T x_n}}{ \sum_j e^{w_j^T x_n}}}_{=\text{softmax}(w_k^T x_n)} \bigg) \end{align*} ``` The gradient ``\nabla_{w_k} \mathrm{L}(w)`` to the weight ``w_k`` can be worked out to ```math \nabla_{w_k} \mathrm{L}(w) = \sum_n \underbrace{\big( \underbrace{y_{nk}}_{\text{target}} - \underbrace{\frac{e^{w_k^T x_n}}{ \sum_j e^{w_j^T x_n}}}_{\text{prediction}} \big)}_{\text{prediction error}}\cdot x_n . ``` """Ù$25f1390c-d294-11ef-364d-17e4c93b9a57„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f1390c-d294-11ef-364d-17e4c93b9a57«code_foldedäcodeÙêmd""" For the data set ``D = \{(x_1,y_1),\dotsc,(x_N,y_N)\}``, the **likelihood function** for the parameters ``w`` is given by ```math p(D|w) = \prod_{n=1}^N p(y_n|x_n,w) = \prod_{n=1}^N \sigma\left( (2y_n-1) w^T x_n\right) ``` """Ù$f8bb4fcd-9b20-44e5-8e22-e792e74b69df„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f8bb4fcd-9b20-44e5-8e22-e792e74b69df«code_folded¤code·X_c2 = X[:,findall(y)]'Ù$b48f8800-473d-48e4-ab78-eb07653db7a5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b48f8800-473d-48e4-ab78-eb07653db7a5«code_foldedäcodeÙRfunction log_likelihood(w, X, y) return sum(logσ.((2*y .- 1) .* (X' * w))) end Ù$1bfac9c5-e5cf-4a70-b077-11bb00cb1482„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$1bfac9c5-e5cf-4a70-b077-11bb00cb1482«code_foldedäcodeÚd""" This function computes the posterior distribution over regression weights using the Laplace Approximation. We use `logσ` as a numerically stable alternative to `logistic`, and we avoid matrix inversions by computing the precision matrix of the posterior distribution instead of the covariance. The math in this function corresponds to eq. B-4.143 """ function bayesian_discrimination_boundary(prior_w, X::Matrix, y::Vector{Bool}) m_0 = mean(prior_w) p_0 = precision(prior_w) negative_unnormalized_posterior = w -> -log_likelihood(w, X, y) - logpdf(prior_w, w) MAP_w = Optim.minimizer(optimize(negative_unnormalized_posterior, zeros(3))) σ_n = logistic.((2y .- 1) .* (X' * MAP_w)) inv_Σ = p_0 for i in 1:length(y) slice = view(X, :, i) inv_Σ .+= σ_n[i] * (1.0 - σ_n[i]) .* (slice * slice') end return MvNormalMeanPrecision(MAP_w, inv_Σ) endÙ$98ef7093-f8ed-4a44-a153-0a64ab483f65„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$98ef7093-f8ed-4a44-a153-0a64ab483f65«code_foldedäcode»md""" ## Implementation """Ù$f37ac438-cd56-4a09-bc8e-a75469955a2f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f37ac438-cd56-4a09-bc8e-a75469955a2f«code_foldedäcodeÚglet d1 = fit_mle(MvNormal, X_c1') d2 = fit_mle(MvNormal, X_c2') plot_dataset() xrange = range(-1.6, 9; length=20) yrange = range(-2, 7; length=15) contour!( xrange, yrange, (x,y) -> pdf(d1, [x,y]); opacity=.4, color=:blues, ) contour!( xrange, yrange, (x,y) -> pdf(d2, [x,y]); opacity=.4, color=:red, colorbar=nothing, ) endÙ$25f3ff84-d294-11ef-0031-63b23d23324d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25f3ff84-d294-11ef-0031-63b23d23324d«code_foldedäcodeÚÝmd""" ## Why be Bayesian? Why should you embrace the Bayesian approach to logistic regression? After all, Maximum Likelihood for logistic regression seems simpler. Still, consider the following: * Bayesian logistic regression with the Laplace approximation ultimately leads to [simple closed-form classification rules](#Working-out-Numerics-with-Laplace-Approximation). Moreover, modern probabilistic programming languages and packages can automate the inference derivations above. (We just do them here to gain insight into a difficult inference process.) * Bayesian logistic regression allows for the computation of model evidence, enabling principled comparison of model performance across alternative models. * Perhaps most importantly, Bayesian logistic regression processes uncertainties; e.g., in places where almost no data is observed, the posterior class probability will pull back to the prior class probability rather than predicting some arbitrary probability. """Ù$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$aaf764da-cf1b-4bc7-83ea-6d25a80ca3ab«code_foldedäcode¦N_bondÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25efd6b6-d294-11ef-3b21-6363ef531eb5«code_foldedäcodeÙÝmd""" ## Model Specification We will work this idea out for a 2-class problem. Assume a data set is given by ```math D = \{(x_1,y_1),\dotsc,(x_N,y_N)\} ``` with ``x_n \in \mathbb{R}^M`` and ``y_n \in \{0,1\}`` . """Ù$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bc„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d29ccc9e-d4a6-46ae-b907-2bc68c8d99bc«code_foldedäcode®plot_dataset()Ù$0045e569-dc3c-4998-86da-9d96f599c599„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$0045e569-dc3c-4998-86da-9d96f599c599«code_foldedäcodeÙ*md""" # Maximum Likelihood Estimation """Ù$ff31d8c1-db35-4c85-a609-67fc40e9e78d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$ff31d8c1-db35-4c85-a609-67fc40e9e78d«code_foldedäcodeÚ]md""" The parameter vector ``w`` for logistic regression can then be estimated through iterative gradient-based adaptation. For instance, start with a random weight ``\hat{w} = w_0``, and iterate through ```math \hat{w}^{(i+1)} = \hat{w}^{(i)} + \eta \cdot \left. \nabla_w \mathrm{L}(w) \right|_{w = \hat{w}^{(i)}} ``` until convergence. """Ù$616e84d7-063d-4d9d-99e4-56aecf3c7ee4„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$616e84d7-063d-4d9d-99e4-56aecf3c7ee4«code_folded¤codeÙousing Distributions, ExponentialFamily, LinearAlgebra, LogExpFunctions, StatsFuns, BayesBase, Optim, StableRNGs´last_hot_reload_timeË©shortpathÙ Discriminative Classification.jl¥nbpkgŠ­busy_packages�Ù,waiting_for_permission_but_probably_disabled§enabledðterminal_outputsެLaTeXStringsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===±ExponentialFamilyÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¦RandomÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===ªnbpkg_syncÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===©StatsFunsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¯MarkdownLiteralÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===²BmlipTeachingToolsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===­DistributionsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¥PlotsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===ªStableRNGsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===­LinearAlgebraÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¥OptimÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¯LogExpFunctionsÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===©BayesBaseÚ& Waiting for other notebooks to finish Pkg operations... === Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_vhhcelnwnx/Manifest.toml` Instantiating... === Precompiling... ===¶waiting_for_permission·restart_recommended_msgÀ´restart_required_msgÀ²installed_versions�¬LaTeXStrings¥1.4.1Ù!__internal_julia_manifest_version¦1.13.3±ExponentialFamily¥2.9.0¦Random¦stdlib©StatsFuns¥2.2.1¯MarkdownLiteral¥0.1.5²BmlipTeachingTools¥1.4.1­Distributions¨0.25.132¥Plots¦1.41.7ªStableRNGs¥1.1.0­LinearAlgebra¦stdlib¸__internal_julia_version¦1.13.3¥Optim¥2.3.2¯LogExpFunctions¥1.0.3©BayesBase¦1.5.12¯install_time_nsÎ*#T¬instantiatedípluto_version¦v1.1.0