Skip to content

Instantly share code, notes, and snippets.

@empet
Last active August 29, 2015 14:01
Show Gist options
  • Save empet/960a0ea4040a50091987 to your computer and use it in GitHub Desktop.
Save empet/960a0ea4040a50091987 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:3ef7145c7aa9b79a3d9fc1565673db017213f2d09b0e1a3d849f80a0fd20242e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Binary images generated by a mixture of multivariate Bernoulli distributions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import scipy.stats as st\n",
"from IPython.html.widgets import interact\n",
"from IPython.display import display"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" $X=(X_1, X_2, \\ldots, X_d)\\sim Bern(p_1, p_2, \\ldots, p_d)$ is a multivariate Bernoulli random variable, whose components, $X_i$, are independent Bernoulli random variables of parameters $p_i$, $i=\\overline{1,d}$.\n",
"\n",
"\n",
"$X$ is a model for a binary image of $d$ pixels.\n",
"We take $d=r^2$, and define a mixture of three multivariate Bernoulli random variables:\n",
"\n",
"$$Y=\\pi_1X^{(1)}+\\pi_2X^{(2)}+\\pi_3 X^{(3)}, \\quad \\pi_i\\in(0,1)\\,\\,\n",
"\\sum_{k=1}^3\\pi_i=1,$$ \n",
"\n",
"\n",
"\n",
"$$X^{(k)}=(X^{(k)}_1,X^{(k)}_2,\\ldots, X^{(k)}_{d})\\sim \\mbox{Bern}(p_{k1},p_{k2}, \\ldots, p_{k d})$$\n",
"The parameters $p_{ki}$ of the r.v. $X^{(k)}_i$, $k=\\overline{1,3}$, $i=\\overline{1,d}$, are uniformly distributed \n",
"on a subinterval of $(0,1)$.\n",
"\n",
"In our example, the parameters associated to the random vectors $X^{(k)}$, $k=1,2,3$, are uniformly distributed respectively\n",
" in the intervals, $(0.2, 0.8)$, $(0.35, 0.75)$, $(0.3, 0.9)$.\n",
"\n",
"Simulating this mixture, we generate n binary images, and display them progressively with `interact`.\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def simDiscrete(pr):\n",
" k=0\n",
" F=pr[0]\n",
" u=np.random.random()\n",
" while(u>F):\n",
" k+=1\n",
" F=F+pr[k]\n",
" return k"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plt.rcParams['figure.figsize'] = 4, 4\n",
"\n",
"Pi=[0.37, 0.41, 0.22]\n",
"\n",
"d=2500\n",
"r=50\n",
"p=np.zeros((3,d))\n",
"sint=[(0.2, 0.8), (0.35, 0.75), (0.3, 0.9)]# subintervals for parameters\n",
"for k in range(3):\n",
" p[k]=st.uniform.rvs(size=d, loc=sint[k][0], scale=sint[k][1]-sint[k][0] )\n",
"\n",
"\n",
"def RandomImage(n): \n",
" fig, ax=plt.subplots()\n",
" k=simDiscrete(Pi)\n",
" pixels=map(lambda (pr): st.bernoulli.rvs(pr), p[k]) \n",
" img=np.array(pixels).reshape((r,r)) \n",
" ax.imshow(img, origin='upper', cmap='gray', interpolation='nearest') "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"interact(RandomImage, n=(1,20))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<function __main__.RandomImage>"
]
},
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAP0AAAD+CAYAAADxoQNSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFFFJREFUeJzt3U1sE0cbB/C/I5A4FBG1VZaKUKUfxBBCcVRabiXBcnIA\nQtRIERwiBFx7gEM/6IWoBzDlUAW1x7ayOEBzogi1qai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kM/nyz/n83k0Nzebqk4rz/NQLBYBAIVCAU1NTSFH9LiHDx+iv78fg4OD6OvrA1Af\nca9YsQLbtm3DlStXIh3v5cuXcfbsWTz33HPYvXs3Lly4gMHBwUjHLMtY0m/atAm//fYbbt68iX//\n/RefffYZent7TVWnVW9vLzKZDAAgk8mUkyoqhBDYv38/2tracODAgfLvoxr37du3y73c9+/fx/nz\n59HR0RHZeAHgyJEjyOfzmJiYwOnTp7F161acPHky0jFLM9lh8MUXX4jW1lbxwgsviCNHjpisStmu\nXbvEM888I5YuXSqam5vFJ598Iv7880+RTCbFmjVrRCqVEnfu3Ak7zHm+/fZbEYvFxMaNG0UikRCJ\nREJ8+eWXkY376tWroqOjQ2zcuFFs2LBBvP/++0IIEdl4K+VyObFjxw4hRP3EXA2H4RI5hiPyiBzD\npCdyDJOeyDFMeiLHMOmJHMOkJ3IMk57IMUx6Isf8B3+vKIjgD6JgAAAAAElFTkSuQmCC\n",
"text": [
"<matplotlib.figure.Figure at 0xa8d3860>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"def css_styling():\n",
" styles = open(\"./styles/custom.css\", \"r\").read()\n",
" return HTML(styles)\n",
"css_styling()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<style>\n",
" @font-face {\n",
" font-family: \"Computer Modern\";\n",
" src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');\n",
" }\n",
" div.cell{\n",
" width:800px;\n",
" margin-left:16% !important;\n",
" margin-right:auto;\n",
" }\n",
" h1 {\n",
" font-family: Helvetica, serif;\n",
" }\n",
" h4{\n",
" margin-top:12px;\n",
" margin-bottom: 3px;\n",
" }\n",
" div.text_cell_render{\n",
" font-family: Computer Modern, \"Helvetica Neue\", Arial, Helvetica, Geneva, sans-serif;\n",
" line-height: 145%;\n",
" font-size: 130%;\n",
" width:800px;\n",
" margin-left:auto;\n",
" margin-right:auto;\n",
" }\n",
" .CodeMirror{\n",
" font-family: \"Source Code Pro\", source-code-pro,Consolas, monospace;\n",
" }\n",
" .prompt{\n",
" display: None;\n",
" }\n",
" .text_cell_render h5 {\n",
" font-weight: 300;\n",
" font-size: 22pt;\n",
" color: #4057A1;\n",
" font-style: italic;\n",
" margin-bottom: .5em;\n",
" margin-top: 0.5em;\n",
" display: block;\n",
" }\n",
" \n",
" .warning{\n",
" color: rgb( 240, 20, 20 )\n",
" } \n",
"</style>\n",
"<script>\n",
" MathJax.Hub.Config({\n",
" TeX: {\n",
" extensions: [\"AMSmath.js\"]\n",
" },\n",
" tex2jax: {\n",
" inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n",
" displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n",
" },\n",
" displayAlign: 'center', // Change this to 'center' to center equations.\n",
" \"HTML-CSS\": {\n",
" styles: {'.MathJax_Display': {\"margin\": 4}}\n",
" }\n",
" });\n",
"</script>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"<IPython.core.display.HTML at 0xaa4ba58>"
]
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment