{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Use of conjugate priors\n", "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from scipy import stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std = 5 # MPa\n", "sample = [30.1, 28.7, 21.2, 28.6, 25.8, 25.1, 24.2, 27.3, 20.0, 34.8] # MPa\n", "n = len(sample)\n", "\n", "prior_mu_mean = 40 # MPa\n", "prior_mu_std = 100 # MPa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Calculate the mean of the sample\n", "mean_sample = ...\n", "print(f\"Mean of the sample: {mean_sample:.2f} MPa\")\n", "\n", "# Posterior distribution with conjugate prior\n", "posterior_mu_mean = ...\n", "posterior_mu_std = ...\n", "print(f\"Posterior mean: {posterior_mu_mean:.2f} MPa\")\n", "print(f\"Posterior standard deviation: {posterior_mu_std:.2f} MPa\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Calculate prior and posterior values for x using scipy\n", "x = np.linspace(0, 100, 1000)\n", "prior = ...\n", "posterior = ...\n", "\n", "fig, ax = plt.subplots()\n", "ax.plot(x, prior, label=\"Prior\")\n", "ax.plot(x, posterior, label=\"Posterior\")\n", "ax.set_xlim(0, 100)\n", "ax.set_ylim(0, 0.3)\n", "ax.set_xlabel(\"Mean (MPa)\")\n", "ax.set_ylabel(\"Density\")\n", "ax.set_title(\"Distribution of the Mean\")\n", "ax.legend()\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }