Files
zmVault/decrease-in-sigma.md
T

2.4 KiB

id, aliases, title, tags, dg-publish
id aliases title tags dg-publish
Decrease in Sigma
destiny/uncertain
topic/math/statistics
type/encyclopedia-entry
true

Decrease in Sigma

Normal PDF

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
      width=13cm,
      height=7cm,
      axis lines=middle,
      xlabel={$x$},
      ylabel={$\varphi(x;\mu,\sigma)$},
      xmin=-6, xmax=6,
      ymin=0, ymax=0.85,
      samples=400,
      domain=-6:6,
      legend style={draw=none, fill=none, at={(0.98,0.98)}, anchor=north east},
      legend cell align=left,
      ytick=\empty,
    ]

    % Normal PDF: (1/(sigma*sqrt(2*pi))) * exp(-(x-mu)^2/(2*sigma^2))

    \addplot[thick]
      { (1/(1.8*sqrt(2*pi))) * exp(-((x-0.8)^2)/(2*1.8^2)) };
    \addlegendentry{$\mu=0,\ \sigma=1.8$}

    \addplot[thick, dashed]
      { (1/(0.8*sqrt(2*pi))) * exp(-((x-0.8)^2)/(2*0.8^2)) };
    \addlegendentry{$\mu=0,\ \sigma=0.8$}

    \end{axis}
\end{tikzpicture}
\end{document}

Lognormal PDF

!lognormal-pdf.gif

%%

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm
import imageio.v2 as imageio
import os

mean_target = 10.0
sigmas = np.linspace(1.0, 0.1, 25)
x = np.linspace(0.001, 40, 2000)

# Precompute global y-limit
pdf_max = 0.0
for sigma in sigmas:
    mu = np.log(mean_target) - 0.5 * sigma**2
    pdf = lognorm.pdf(x, s=sigma, scale=np.exp(mu))
    pdf_max = max(pdf_max, pdf.max())

frames = []
tmp_dir = "frames"
os.makedirs(tmp_dir, exist_ok=True)

for i, sigma in enumerate(sigmas):
    mu = np.log(mean_target) - 0.5 * sigma**2
    pdf = lognorm.pdf(x, s=sigma, scale=np.exp(mu))

    plt.figure(figsize=(6, 4))
    plt.plot(x, pdf)
    plt.axvline(mean_target, linestyle="--", linewidth=1)
    plt.text(
        mean_target, pdf_max * 0.95,
        "mean",
        rotation=90,
        verticalalignment="top",
        horizontalalignment="right"
    )

    plt.title(f"Lognormal PDF\nmean = {mean_target}, sigma = {sigma:f}")
    plt.xlabel("x")
    plt.ylabel("density")
    plt.ylim(0, pdf_max * 1.05)
    plt.tight_layout()

    frame_path = f"{tmp_dir}/frame_{i:02d}.png"
    plt.savefig(frame_path, dpi=120)
    plt.close()

    frames.append(imageio.imread(frame_path))

os.makedirs("out", exist_ok=True)
gif_path = "out/lognormal-pdf.gif"
imageio.mimsave(gif_path, frames, duration=0.15)

%%