Skip to content

Instantly share code, notes, and snippets.

@adsurbum
Forked from yoavram/A3-logaddexp.ipynb
Created May 25, 2020 20:06
Show Gist options
  • Save adsurbum/85b14429163917ede381121306489eea to your computer and use it in GitHub Desktop.
Save adsurbum/85b14429163917ede381121306489eea to your computer and use it in GitHub Desktop.
A3: logaddexp
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`np.logaddexp`:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\log \\mathcal{L}(\\theta \\mid x, y) = \\log \\prod_{i=1}^{n}{\\Big[g_i P(y_i \\mid a, b, x, e) + (1-g_i) P(y_i \\mid a, b, x, \\sigma)\\Big]}=\\\\\n",
"=\\sum_{i=1}^{n}{\\log{\\Big[g_i P(y_i \\mid a, b, x, e) + (1-g_i) P(y_i \\mid a, b, x, \\sigma)\\Big]}}\\\\\n",
"=\\sum_{i=1}^{n}{\\log{\\Big[L1 + L2\\Big]}}\n",
"$$\n",
"$L1,L2$ can be very small, and can exceed the range of normal floating point numbers. If we want precise calculation here, we do the trick:\n",
"\n",
"$$\n",
"\\sum_{i=1}^{n}{\\log{\\Big[L1 + L2\\Big]}}=\\sum_{i=1}^{n}{\\log{\\Big[\\exp(\\log(L1)) + exp(\\log(L2))\\Big]}}\\\\\\\\\n",
"=np.logaddexp(logL1, logL2).sum()\n",
"$$\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:scipy]",
"language": "python",
"name": "conda-env-scipy-py"
},
"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.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment