Skip to content

Instantly share code, notes, and snippets.

@psinger
Created April 28, 2016 12:08
Show Gist options
  • Save psinger/80ed54e643defc7e37e1ee8830063ebe to your computer and use it in GitHub Desktop.
Save psinger/80ed54e643defc7e37e1ee8830063ebe to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def stick_breaking(a, samples=10):\n",
" out = []\n",
" for s in range(samples):\n",
" rs = []\n",
" res = []\n",
" for i in range(len(a)-1):\n",
" r = np.random.beta(a[i],a[i+1:].sum())\n",
" rs.append(r)\n",
" rs = np.array(rs)\n",
" #print rs\n",
" lengths = np.cumprod(1 - rs)\n",
" #print lengths\n",
" #print \"--\"\n",
" \n",
" res.append(rs[0])\n",
" #print rs, lengths[0:-1]\n",
" rest = rs[1:] * lengths[0:-1]\n",
" res += list(rest)\n",
" res.append(1-np.array(res).sum())\n",
" print res"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.48974938315910654, 0.0047873386802407712, 0.005204838306359847, 0.0051531851512049004, 0.0052691997675365131, 0.48983605493555138]\n",
"[0.48901213106318192, 0.0048153332545771395, 0.0054811772556320314, 0.0050379334371551425, 0.0049136710004585929, 0.49073975398899516]\n",
"[0.48631469952575129, 0.0049147072930897494, 0.0050255471498571215, 0.005196053979963899, 0.0051518210389495823, 0.49339717101238834]\n",
"[0.49011751283043509, 0.0052621604666821488, 0.0046928325948145172, 0.0050742344773534323, 0.0048879954017979465, 0.48996526422891684]\n",
"[0.48594140936377145, 0.0053250351025033024, 0.0052557889612454703, 0.0044719037839141568, 0.0056142310332264155, 0.49339163175533918]\n",
"[0.4858212929973037, 0.0050878185322553865, 0.0052247578323147493, 0.0048549624618914418, 0.0050433684667009031, 0.49396779970953375]\n",
"[0.48527009650528635, 0.0050608760589918329, 0.0045721570611608258, 0.0049358715859239653, 0.0052879162797139943, 0.49487308250892303]\n",
"[0.49121010845204904, 0.005370754397591963, 0.0049753008017866121, 0.0047105789261552159, 0.0051607908079086074, 0.48857246661450848]\n",
"[0.49076817457606631, 0.0047815967114421933, 0.0053080716521472192, 0.0050106023986171257, 0.0045465315065500939, 0.48958502315517705]\n",
"[0.49413609968098388, 0.0047863879694022646, 0.0048914196807050437, 0.0049163813339388853, 0.0049880377873115439, 0.48628167354765839]\n"
]
}
],
"source": [
"stick_breaking(np.array([20000,200,200,200,200,20000]))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"a = [1,2]\n",
"print a[1:-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment