<jupyter_start><jupyter_text>Exponential-family belief updating  Reduction of Bayes' theorem to mean updating Mean updatingThe mean $\bar{x}_n$ of a set $\{x_1, x_2, \ldots, x_n\}$ is$$\bar{x}_n := \frac{1}{n} \sum_{i=1}^{n} x_i$$For example, $x_i$ could be observations made sequentially. If we now add a new element $x_{n+1}$ to our set, the new mean $\bar{x}_{n+1}$ is$$\bar{x}_{n+1} = \bar{x}_n + \frac{1}{n+1} \left( x_{n+1} - \bar{x}_n \right)$$*Proof:*$$\begin{align}\bar{x}_{n+1} &= \frac{1}{n+1} \sum_{i=1}^{n+1} x_i \\&= \frac{1}{n+1} \left( x_{n+1} + n \cdot \frac{1}{n} \sum_{i=1}^{n} x_i \right) \\&= \frac{1}{n+1} \left( x_{n+1} + n \bar{x}_n \right) \\&= \frac{1}{n+1} \left( x_{n+1} - \bar{x}_n + (n+1) \bar{x}_n \right) \\&= \bar{x}_n + \frac{1}{n+1} \left( x_{n+1} - \bar{x}_n \right)\end{align}$$  Bayes' theoremBayes' theorem is an immediate consequence of the product rule of probability and states that the posterior is the likelihood times the prior divided by the model evidence.$$p\left( \vartheta \middle| x \right) = \frac{p\left( x \middle| \vartheta \right) p\left( \vartheta \right)}{p\left( x \right)}= \frac{p\left( x \middle| \vartheta \right) p\left( \vartheta \right)}{\int_\Omega p\left( x \middle| \vartheta' \right) p\left( \vartheta' \right) \mathrm{d}\vartheta'},$$where $\Omega$ is the support of $p(\vartheta)$.  Exponential families of probability distributionsA family of probability distributions is an *exponential family* if and only if it can be written in the following form$$p\left( x \middle| \vartheta \right) = f_x(\vartheta) := h(x) \exp\left( \eta(\vartheta) \cdot T(x) - B(\vartheta) \right),$$where $x$ is a vector-valued observation, $\vartheta$ is a parameter vector, $h(x)$ is a normalization constant, $\eta(\vartheta)$ is the so-called 'natural' parameter vector, $T(x)$ is the sufficient statistic vector, and $B(\vartheta)$ is a scalar function.  Bayes' theorem applied to exponential familiesWhen the likelihood belongs to an exponential family, applying Bayes' theorem becomes extremely simple with an appropriate prior. We choose a prior with a natural interpretation, implying that we have made $\nu \in \mathbb{R}$ observations with sufficient statistics $\xi$:$$p\left( \vartheta \middle| \xi, \nu \right) = g_{\xi, \nu}(\vartheta) := z\left( \xi, \nu \right) \exp\left( \nu\left( \eta \left( \vartheta \right) \cdot \xi - B(\vartheta) \right) \right),$$where $z(\xi, \nu)$ is a normalization constant:$$z(\xi, \nu) := \left( \int_\Omega \exp\left( \nu\left( \eta \left( \vartheta \right) \cdot \xi - B(\vartheta) \right) \right) \mathrm{d}\vartheta \right)^{-1}.$$The *hyperpriors* $\xi$ and $\nu$ parameterize the prior $g_{\xi, \nu}$ of $\vartheta$. This prior is *conjugate* to the likelihood; that is, the posterior has the same form $g_{\xi, \nu}$. When passing from the prior to the posterior after a new observation $x$, all that needs to be updated are the hyperpriors $\xi$ and $\nu$:$$\begin{align}\nu &\leftarrow \nu+1 \\\xi &\leftarrow \xi + \frac{1}{\nu+1} \left( T(x) - \xi \right)\end{align}$$*Proof:*$$\begin{align}p\left( \vartheta \middle| x, \xi, \nu \right) &\propto p\left( x \middle| \vartheta \right) p\left( \vartheta \middle| \xi, \nu \right) \\\\&= f_x(\vartheta)g_{\xi, \nu}(\vartheta) \\\\&= h(x) \exp\left( \eta(\vartheta) \cdot T(x) - B(\vartheta) \right) z\left( \xi, \nu \right) \exp\left( \nu\left( \eta \left( \vartheta \right) \cdot \xi - B(\vartheta) \right) \right) \\\\&\propto \exp\left( \eta(\vartheta) \cdot \left( T(x) + \nu \xi \right) - (\nu+1) B(\vartheta) \right) \\\\&= \exp\left( (\nu+1) \left( \eta(\vartheta) \cdot \frac{1}{\nu+1} \left( T(x) + \nu \xi \right) - B(\vartheta) \right)\right) \\\\&= \exp\left( (\nu+1) \left( \eta(\vartheta) \cdot \left( \xi + \frac{1}{\nu+1} \left( T(x) - \xi \right) \right) - B(\vartheta) \right)\right) \\\\\\\Longrightarrow p\left( \vartheta \middle| x, \xi, \nu \right) &= g_{\xi', \nu'}(\vartheta) \qquad \text{with} \qquad \nu' = \nu+1 \qquad \text{and} \qquad \xi' = \xi + \frac{1}{\nu+1} \left( T(x) - \xi \right)\end{align}$$Since the prior can   Gaussian with unknown mean and known precision LikelihoodThe likelihood is$$\begin{aligned}p\left( x \middle| \vartheta \right) &= h\left( x \right) \exp\left( \eta\left( \vartheta \right) \cdot T(x) - B(\vartheta) \right) \\&= \sqrt{\frac{\tau}{2\pi}} \exp \left( -\frac{\tau}{2}\left( x -\mu \right)^2 \right) \\&= \mathcal{N} \left( x ; \mu, \tau^{-1} \right)\end{aligned}$$with$$\begin{aligned}h( x ) &: = \sqrt{\frac{\tau}{2\pi}} \exp\left(-\frac{\tau}{2} x^2\right),\\\vartheta &:= \mu,\\\eta (\vartheta) &: = \tau\mu,\\T(x) &: = x,\\B(\vartheta) &:= \frac{\tau}{2}\mu^2.\end{aligned}$$  PriorThe prior is$$p\left( \vartheta \middle| \xi, \nu \right) = z\left( \xi, \nu\right) \exp\left( \nu\left( \eta \left( \vartheta \right) \cdot \xi -B(\vartheta) \right) \right),$$with normalization factor$$z(\xi, \nu) := \left( \int_\Omega \exp\left( \nu\left( \eta \left( \vartheta \right) \cdot \xi -B(\vartheta) \right) \right) \mathrm{d}\vartheta \right)^{-1}.$$With the definitions above, this means$$\begin{aligned}z(\xi, \nu)^{-1} &= \int_{-\infty}^\infty \exp \left( \nu \left( \tau \mu \xi - \frac{1}{2}\tau \mu^2 \right) \right) \mathrm{d}\mu \\&= \int_{-\infty}^\infty \exp \left( -\frac{\nu\tau}{2} \left( \mu^2 - 2 \mu \xi \right) \right) \mathrm{d}\mu \\&= \int_{-\infty}^\infty \exp \left( -\frac{\nu\tau}{2} \left( \left( \mu - \xi \right)^2 - \xi^2 \right) \right) \mathrm{d}\mu \\&= \exp \left( \frac{\nu\tau}{2} \xi^2 \right) \int_{-\infty}^\infty \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi \right)^2 \right) \mathrm{d}\mu \\&= \sqrt{\frac{2\pi}{\nu \tau}} \exp \left( \frac{\nu\tau}{2} \xi^2 \right)\end{aligned}$$and$$\begin{aligned}p\left( \vartheta \middle| \xi, \nu \right) &= z(\xi, \nu) \exp \left( \frac{\nu\tau}{2} \xi^2 \right) \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi \right)^2 \right) \\&= \sqrt{\frac{\nu \tau}{2\pi}} \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi \right)^2 \right) \\&= \mathcal{N} \left( \mu ; \xi, (\nu\tau)^{-1} \right)\end{aligned}$$  Prior predictive distributionMarginalizing the joint over the parameter $\vartheta$ gives us the prior predictive distribution$$\begin{aligned}p\left( x | \xi, \nu \right) &= \int_\Omega p(x | \vartheta) p(\vartheta | \xi, \nu) \mathrm{d}\vartheta \\&= \int_{-\infty}^\infty \mathcal{N} \left( x ; \mu, \tau^{-1} \right) \mathcal{N} \left( \mu ; \xi, (\nu\tau)^{-1} \right) \mathrm{d}\mu \\&= \mathcal{N} \left( x ; \xi, \tau^{-1} + (\nu\tau)^{-1} \right) \\&= \mathcal{N} \left( x ; \xi, \left( \frac{\nu\tau}{\nu+1} \right)^{-1} \right).\end{aligned}$$The third equality is a consequence of the fact that compounding two Gaussians gives a Gaussian whose variance is the sum of those of the compounded Gaussians. A more tedious derivation gives the same result:$$\begin{aligned}p\left( x | \xi, \nu \right) &= \int_\Omega p(x | \vartheta) p(\vartheta | \xi, \nu) \mathrm{d}\vartheta \\&= \int_{-\infty}^\infty \mathcal{N} \left( x ; \mu, \tau^{-1} \right) \mathcal{N} \left( \mu ; \xi, (\nu\tau)^{-1} \right) \mathrm{d}\mu \\&= \int_{-\infty}^\infty \sqrt{\frac{\tau}{2\pi}} \exp \left( -\frac{\tau}{2}\left( x -\mu \right)^2 \right) \sqrt{\frac{\nu \tau}{2\pi}} \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi \right)^2 \right) \mathrm{d}\mu \\&= \frac{\sqrt{\nu} \tau}{2\pi} \int_{-\infty}^\infty \exp \left( -\frac{\tau}{2}\left( x -\mu \right)^2 \right)  \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi \right)^2 \right) \mathrm{d}\mu \\&= \frac{\sqrt{\nu} \tau}{2\pi} \int_{-\infty}^\infty \exp \left( -\frac{\tau}{2}\left( (\nu+1)\mu^2 -2(x+\nu\xi)\mu + x^2 +\nu\xi^2 \right) \right) \mathrm{d}\mu \\&= \frac{\sqrt{\nu} \tau}{2\pi} \exp \left( -\frac{\tau}{2}\left( x^2 +\nu\xi^2 \right) \right) \int_{-\infty}^\infty \exp \left( -\frac{(\nu+1) \tau}{2}\left( \left( \mu - \frac{x+\nu\xi}{\nu+1} \right)^2 - \left( \frac{x+\nu\xi}{\nu+1} \right)^2 \right) \right) \mathrm{d}\mu \\&= \frac{\sqrt{\nu} \tau}{2\pi} \exp \left( -\frac{\tau}{2}\left( x^2 +\nu\xi^2 - \frac{(x+\nu\xi)^2}{\nu+1} \right) \right) \int_{-\infty}^\infty \exp \left( -\frac{(\nu+1) \tau}{2}\left( \left( \mu - \frac{x+\nu\xi}{\nu+1} \right)^2 \right) \right) \mathrm{d}\mu \\&= \frac{\sqrt{\nu} \tau}{2\pi} \exp \left( -\frac{\tau}{2}\left( x^2 +\nu\xi^2 - \frac{(x+\nu\xi)^2}{\nu+1} \right) \right) \sqrt{\frac{2\pi}{(\nu+1)\tau}} \\&= \sqrt{\frac{\nu\tau}{2\pi(\nu+1)}} \exp \left( -\frac{\tau}{2}\left( x^2 +\nu\xi^2 - \frac{(x+\nu\xi)^2}{\nu+1} \right) \right) \\&= \sqrt{\frac{\nu\tau}{2\pi(\nu+1)}} \exp \left( -\frac{\tau}{2}\left( x^2 - \frac{x^2}{\nu+1} - \frac{2\nu\xi x}{\nu+1}- \frac{\nu^2\xi^2}{\nu+1} +\nu\xi^2 \right) \right) \\&= \sqrt{\frac{\nu\tau}{2\pi(\nu+1)}} \exp \left( -\frac{\tau}{2}\left( x^2 \left( 1  - \frac{1}{\nu+1} \right) - 2\frac{\nu}{\nu+1}\xi x - \frac{\nu}{\nu+1} \left( +\nu\xi^2 + (\nu+1)\xi^2 \right) \right) \right) \\&= \sqrt{\frac{\nu\tau}{2\pi(\nu+1)}} \exp \left( -\frac{\nu\tau}{2(\nu+1)}(x - \xi)^2 \right) \\&= \mathcal{N} \left( x ; \xi, \left( \frac{\nu\tau}{\nu+1} \right)^{-1} \right).\end{aligned}$$  Gaussian with mean and precision unknown LikelihoodWhether precision is known or unknown does not affect the likelihood. It still is$$\begin{aligned}p\left( x \middle| \vartheta \right) &= h\left( x \right) \exp\left( \eta\left( \vartheta \right) \cdot T(x) - B(\vartheta) \right) \\&= \sqrt{\frac{\tau}{2\pi}} \exp \left( -\frac{\tau}{2}\left( x -\mu \right)^2 \right) \\&= \mathcal{N} \left( x ; \mu, \tau^{-1} \right),\end{aligned}$$but this time with$$\begin{aligned}h( x ) &: = \frac{1}{\sqrt{2\pi}},\\\vartheta &:= \left( \mu, \tau \right)^\intercal \\\eta (\vartheta) &: = \left( \tau\mu, -\tau/2 \right)^\intercal \\T(x) &: = \left( x, x^2 \right)^\intercal \\B(\vartheta) &:= \frac{1}{2} \left( \tau \mu^2 - \ln \tau \right).\end{aligned}$$  PriorWith$$\begin{aligned}z(\xi, \nu)^{-1} &= \int_0^\infty \int_{-\infty}^\infty \exp \left( \nu \left( \tau \mu \xi_1 - \frac{1}{2}\tau \xi_2 - \frac{1}{2}\tau \mu^2  + \frac{1}{2} \ln \tau \right) \right) \mathrm{d}\mu \mathrm{d}\tau \\&= \int_0^\infty \tau^\frac{\nu}{2} \int_{-\infty}^\infty \exp \left( \frac{\nu\tau}{2} \left( 2\mu\xi_1 - \xi_2 - \mu^2 \right) \right) \mathrm{d}\mu \mathrm{d}\tau \\&= \int_0^\infty \tau^\frac{\nu}{2} \exp \left( -\frac{\nu\tau}{2} \xi_2 \right) \int_{-\infty}^\infty \exp \left( -\frac{\nu\tau}{2} \left( \mu^2 - 2\mu\xi_1 \right) \right) \mathrm{d}\mu \mathrm{d}\tau \\&= \int_0^\infty \tau^\frac{\nu}{2} \exp \left( -\frac{\nu\tau}{2} \left( \xi_2 - \xi_1^2 \right) \right) \int_{-\infty}^\infty \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi_1 \right)^2 \right) \mathrm{d}\mu \mathrm{d}\tau \\&= \int_0^\infty \tau^\frac{\nu}{2} \exp \left( -\frac{\nu\tau}{2} \left( \xi_2 - \xi_1^2 \right) \right) \sqrt{\frac{2\pi}{\nu\tau}} \mathrm{d}\tau \\&= \sqrt{\frac{2\pi}{\nu}} \int_0^\infty \tau^\frac{\nu-1}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \tau \right) \mathrm{d}\tau \\&= \sqrt{\frac{2\pi}{\nu}} \frac{\Gamma \left( \frac{\nu+1}{2} \right)}{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2}} \\\end{aligned}$$we get$$\begin{aligned}p\left( \vartheta \middle| \xi, \nu \right) &= z(\xi, \nu) \tau^\frac{\nu}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \tau \right) \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi_1 \right)^2 \right) \\&= \frac{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \tau^\frac{\nu-1}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \tau \right) \sqrt{\frac{\nu\tau}{2\pi}} \exp \left( -\frac{\nu\tau}{2} \left( \mu - \xi_1 \right)^2 \right) \\&= \mathcal{N} \left( \mu ; \xi_1, (\nu\tau)^{-1} \right) \text{Gamma} \left( \tau; \frac{\nu+1}{2}, \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right). \\\end{aligned}$$  Prior predictive distribution$$\begin{aligned}p\left( x | \xi, \nu \right) &= \int_\Omega p(x | \vartheta) p(\vartheta | \xi, \nu) \mathrm{d}\vartheta \\&= \int_0^\infty \int_{-\infty}^\infty \mathcal{N} \left( x ; \mu, \tau^{-1} \right) \mathcal{N} \left( \mu ; \xi_1, (\nu\tau)^{-1} \right) \text{Gamma} \left( \tau; \frac{\nu+1}{2}, \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right) \mathrm{d}\mu \mathrm{d}\tau \\&= \int_0^\infty \mathcal{N} \left( x ; \xi_1, \left( \frac{\nu\tau}{\nu+1} \right)^{-1} \right) \text{Gamma} \left( \tau; \frac{\nu+1}{2}, \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right) \mathrm{d}\tau \\&= \int_0^\infty \sqrt{\frac{\nu\tau}{2\pi(\nu+1)}} \exp \left( -\frac{\nu\tau}{2(\nu+1)}(x - \xi_1)^2 \right) \frac{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \tau^\frac{\nu-1}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \tau \right) \mathrm{d}\tau \\&= \sqrt{\frac{\nu}{2\pi(\nu+1)}} \frac{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \int_0^\infty \exp \left( -\frac{\nu\tau}{2(\nu+1)}(x - \xi_1)^2 \right) \tau^\frac{\nu}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \tau \right) \mathrm{d}\tau \\&= \sqrt{\frac{\nu}{2\pi(\nu+1)}} \frac{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \int_0^\infty \tau^\frac{\nu}{2} \exp \left( -\frac{\nu}{2} \left( \xi_2 - \xi_1^2 + \frac{(x - \xi_1)^2}{\nu+1} \right) \tau \right) \mathrm{d}\tau \\&= \sqrt{\frac{\nu}{2\pi(\nu+1)}} \frac{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 \right) \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \frac{\Gamma \left( \frac{\nu}{2} + 1\right)}{\left( \frac{\nu}{2} \left( \xi_2 - \xi_1^2 + \frac{(x - \xi_1)^2}{\nu+1} \right) \right)^{\frac{\nu}{2} + 1}} \\&= \sqrt{\frac{1}{\pi(\nu+1)}} \frac{\left( \xi_2 - \xi_1^2 \right)^\frac{\nu+1}{2} }{\Gamma \left( \frac{\nu+1}{2} \right)} \frac{\Gamma \left( \frac{\nu}{2} + 1\right)}{\left( \xi_2 - \xi_1^2 + \frac{(x - \xi_1)^2}{\nu+1} \right)^{\frac{\nu}{2} + 1}} \\&= \sqrt{\frac{1}{\pi(\nu+1)\left( \xi_2 - \xi_1^2 \right)}} \frac{\Gamma \left( \frac{\nu+2}{2} \right)}{\Gamma \left( \frac{\nu+1}{2} \right)} \left( \frac{\xi_2 - \xi_1^2}{\xi_2 - \xi_1^2 + \frac{(x - \xi_1)^2}{\nu+1}} \right)^{\frac{\nu+2}{2}} \\&= \sqrt{\frac{1}{\pi(\nu+1)\left( \xi_2 - \xi_1^2 \right)}} \frac{\Gamma \left( \frac{\nu+2}{2} \right)}{\Gamma \left( \frac{\nu+1}{2} \right)} \left( 1 + \frac{(x - \xi_1)^2}{(\nu+1) \left( \xi_2 - \xi_1^2 \right)} \right)^{-\frac{\nu+2}{2}} \\&= \text{Student's-}t \left( x; \nu+1, \xi_1, \xi_2 - \xi_1^2 \right) \\\end{aligned}$$ This motivates the definition of the *normal-predictive* distribution $\mathcal{NP}$:$$\mathcal{NP} \left( x; \xi_1, \xi_2, \nu \right) :=  \sqrt{\frac{1}{\pi(\nu+1)\left( \xi_2 - \xi_1^2 \right)}} \frac{\Gamma \left( \frac{\nu+2}{2} \right)}{\Gamma \left( \frac{\nu+1}{2} \right)} \left( 1 + \frac{(x - \xi_1)^2}{(\nu+1) \left( \xi_2 - \xi_1^2 \right)} \right)^{-\frac{\nu+2}{2}}$$  Code and examples<jupyter_code>dnp <- function(x, xi1, xi2, nu) {
    # Mean
    mu <- xi1
    # 'Standard deviation'
    sigma <- sqrt(xi2 - xi1^2)
    # Degrees of freedom
    df <- nu + 1
    # Argument
    a <- (x - mu)/sigma
    
    # Density
    d <- 1/sigma*dt(a, df)
    return(d)
}
dnp(1, 1, 2, 7)
dt(1, 3)
library(ggplot2)
options(repr.plot.width = 4, repr.plot.height = 3)
ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
    stat_function(fun = dt, args = list(df = 1)) +
    stat_function(fun = dnp, args = list(xi1 = 1, xi2 = 2, nu = 200), colour = "red")
# Define hyperprior update function
ud <- function(xi, nu, x, T = identity) {
    xi <- xi + 1/(1 + nu)*(T(x) - xi)
    return(xi)
}
# Define sufficient statistics of univariate
# Gaussian with both parameters unknown
Tnorm <- function(x) {
    T <- c(x, x^2)
    return(T)
}
# Set parameters of sampling distribution
mux <- 5
sdx <- 1/4 # ie pix == 16

# Set hyperpriors to define prior
xi <- c(0, 1/8)
nu <- 1

# Sample n observations
n <- 10001
xs <- rnorm(n, mux, sdx)

# Hyperprior update loop
xis <- xi
nus <- nu
for (i in 1:n) {
    xi <- ud(xi, nu, xs[i], Tnorm)
    xis <- rbind(xis, xi)
    nu <- nu + 1
    nus <- append(nus, nu)
}
# Set up plot
options(repr.plot.width = 9, repr.plot.height = 3)
p <- ggplot(data.frame(x = c(-4, 8)), aes(x = x))

# Plot prior predictive
p <- p + stat_function(fun = dnp, args = list(xi1 = xis[1,1], xi2 = xis[1,2], nu = nus[1]), n = 401, colour = "black")


# Plot every mth posterior predictive
m <- 200
for (i in seq(2, n + 1, m)) {
    p <- p + stat_function(fun = dnp, args = list(xi1 = xis[i,1], xi2 = xis[i,2], nu = nus[i]), n = 401, colour = "grey")
}

# Plot sampling distribution
p <- p + stat_function(fun = dnorm, args = list(mean = mux, sd = sdx), n = 401, colour = "red")

# Make the plot
p
gauss_dens <- function(x, mu, la) {
    T <- c(x, x^2)
    h <- 1/sqrt(2*pi)
    eta <- c(la*mu, -la/2)
    B <- (la*mu^2 - log(la))/2
    p <- drop(h*exp(eta %*% T - B))

    return(p)
}
gauss_dens(3,0,1)
dnorm(3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.28111700577158516, 'fraction_numerical': 0.031714568880079286, 'mean_word_length': 3.069750889679715, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '515957', 'license': 'MIT', 'n_tokens_mistral': 7385, 'n_tokens_neox': 7008, 'n_words': 1733, 'path': 'notebooks/exp-fam.ipynb', 'repo_name': 'julifurjes/methods-4-course', 'size': '17153'}