update() and predict() alter in place.

I made the update() and predict() functions alter the belief array
in place rather than returning the value in a new array. I find this
far more readable and less error prone.
This commit is contained in:
Roger Labbe 2014-12-20 20:56:23 -08:00
parent 17b30bc395
commit fc7c174999
2 changed files with 64 additions and 82 deletions

File diff suppressed because one or more lines are too long

View File

@ -349,25 +349,19 @@
"source": [
"This is still a little hard to compare to Bayes' equation because we are dealing with probability distributions rather than probabilities. So let's cast our minds back to the discrete Bayes chapter where we computed the probability that our dog was at any given position in the hallway. It looked like this:\n",
"\n",
" def normalize(p):\n",
" s = sum(p)\n",
" for i in range (len(p)):\n",
" p[i] = p[i] / s\n",
"\n",
" def update(pos, measure, p_hit, p_miss):\n",
" q = np.array(pos, dtype=float)\n",
" def update(pos_belief, measure, p_hit, p_miss):\n",
" for i in range(len(hallway)):\n",
" if hallway[i] == measure:\n",
" q[i] = pos[i] * p_hit\n",
" pos_belief[i] *= p_hit\n",
" else:\n",
" q[i] = pos[i] * p_miss\n",
" normalize(q)\n",
" return q\n",
" pos_belief[i] *= p_miss\n",
"\n",
" pos_belief /= sum(pos_belief)\n",
"\n",
"Let's rewrite this using our newly learned terminology.\n",
"\n",
" def update(prior_probability, measure, prob_hit, prob_miss):\n",
" posterior_probability = np.array(prior_probability, dtype=float)\n",
" posterior_probability = np.zeros(len(prior_probability))\n",
" for i in range(len(hallway)):\n",
" if hallway[i] == measure:\n",
" posterior_probability[i] = prior_probability[i] * p_hit\n",