{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Setting the beta hyper-parameters\n", "\n", "Suppose $\\theta \\sim \\beta(\\alpha_1,\\alpha_2)$ and we believe that $E[\\theta] = m$ and $P(l < \\theta < u) = 0.95$. Write a program that can solve for $\\alpha_1$ and $\\alpha_2$ in terms of $m$, $l$, and $u$.\n", "\n", "The mean for the beta distribution is well-known, so we have that\n", "\n", "\\begin{equation}\n", "E[\\theta] = m = \\frac{\\alpha_1}{\\alpha_1 + \\alpha_2}.\n", "\\end{equation}\n", "\n", "Thus, this implies that \n", "\n", "\\begin{equation}\n", "\\alpha_2 = \\alpha_1\\frac{1-m}{m}.\n", "\\end{equation}\n", "\n", "Now, as $\\alpha_1$ increases the strength of our prior increases, so $P(l < \\theta < u)$ is a monotonic function of $\\alpha_1$. And so, we can solve for $\\alpha_1$, and thereby, $\\alpha_2$ with a binary search." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.506062413888118\n", "25.534353681276208\n" ] } ], "source": [ "from scipy import stats\n", "\n", "m = 0.15\n", "l = 0.05\n", "u = 0.3\n", "\n", "alpha_1_lower = 0.01\n", "alpha_1_upper = 1000\n", "alpha_1 = (alpha_1_lower + alpha_1_upper)/2\n", "tol = 1e-15\n", "while alpha_1_upper - alpha_1_lower >= 1e-9:\n", " alpha_2 = alpha_1*(1-m)/m\n", " density = stats.beta.cdf(u, a = alpha_1, b = alpha_2) - stats.beta.cdf(l, a = alpha_1, b = alpha_2)\n", " if density > 0.95:\n", " alpha_1_upper = alpha_1\n", " else:\n", " alpha_1_lower = alpha_1\n", " alpha_1 = (alpha_1_lower + alpha_1_upper)/2\n", "\n", "print(alpha_1)\n", "print(alpha_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The problem asks for $\\alpha_1$ and $\\alpha_2$ if $m = 0.15$, $l = 0.05$, and $u = 0.3$. We find that\n", "\n", "\\begin{align}\n", "\\alpha_1 &\\approx 4.506062413888118 \\\\\n", "\\alpha_2 &\\approx 25.534353681276208.\n", "\\end{align}\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }