{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "D. Danks. 2003. \"Equilibria of the Rescorla-Wagner model.\" *Journal of Mathematical Psychology* 47:109-121." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "import pandas.rpy.common as com\n", "import numpy as np\n", "\n", "%load_ext autoreload\n", "%autoreload 2\n", "\n", "%load_ext rmagic" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load data into pandas" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%R\n", "library(ndl)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "This is ndl version 0.2.16. \n", "For an overview of the package, type 'help(\"ndl.package\")'.\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "data = com.load_data('danks')\n", "data" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CuesOutcomesFrequency
1 pots_red_blue y 5
2 pots_red_blue n 0
3 pots_red y 10
4 pots_red n 5
5 pots_blue y 5
6 pots_blue n 10
7 pots y 0
8 pots n 5
\n", "

8 rows \u00d7 3 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ " Cues Outcomes Frequency\n", "1 pots_red_blue y 5\n", "2 pots_red_blue n 0\n", "3 pots_red y 10\n", "4 pots_red n 5\n", "5 pots_blue y 5\n", "6 pots_blue n 10\n", "7 pots y 0\n", "8 pots n 5\n", "\n", "[8 rows x 3 columns]" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "marginals = data.groupby('Cues',as_index=False).Frequency.sum()\n", "marginals = marginals.rename(columns={'Frequency':'Total'})\n", "marginals" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CuesTotal
0 pots 5
1 pots_blue 15
2 pots_red 15
3 pots_red_blue 5
\n", "

4 rows \u00d7 2 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ " Cues Total\n", "0 pots 5\n", "1 pots_blue 15\n", "2 pots_red 15\n", "3 pots_red_blue 5\n", "\n", "[4 rows x 2 columns]" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "data = pd.merge(data,marginals,on='Cues')\n", "data" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CuesOutcomesFrequencyTotal
0 pots_red_blue y 5 5
1 pots_red_blue n 0 5
2 pots_red y 10 15
3 pots_red n 5 15
4 pots_blue y 5 15
5 pots_blue n 10 15
6 pots y 0 5
7 pots n 5 5
\n", "

8 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ " Cues Outcomes Frequency Total\n", "0 pots_red_blue y 5 5\n", "1 pots_red_blue n 0 5\n", "2 pots_red y 10 15\n", "3 pots_red n 5 15\n", "4 pots_blue y 5 15\n", "5 pots_blue n 10 15\n", "6 pots y 0 5\n", "7 pots n 5 5\n", "\n", "[8 rows x 4 columns]" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create cue matrix" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from sklearn.feature_extraction import DictVectorizer" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "def explode(cues):\n", " return {}.fromkeys(cues.split('_'),True)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "yes = data[data.Outcomes=='y']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Left-hand side of equation (15) on pg. 114" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vec = DictVectorizer(dtype=int,sparse=False)\n", "M = vec.fit_transform([explode(c) for c in yes.Cues])\n", "M" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "array([[1, 1, 1],\n", " [0, 1, 1],\n", " [1, 1, 0],\n", " [0, 1, 0]])" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "P = np.diag(yes.Total/sum(yes.Total))\n", "P" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "array([[ 0.125, 0. , 0. , 0. ],\n", " [ 0. , 0.375, 0. , 0. ],\n", " [ 0. , 0. , 0.375, 0. ],\n", " [ 0. , 0. , 0. , 0.125]])" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "left = M.T.dot(P).dot(M)\n", "left" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "array([[ 0.5 , 0.5 , 0.125],\n", " [ 0.5 , 1. , 0.5 ],\n", " [ 0.125, 0.5 , 0.5 ]])" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "O = yes.Frequency / yes.Total\n", "O" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "0 1.000000\n", "2 0.666667\n", "4 0.333333\n", "6 0.000000\n", "dtype: float64" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "right = M.T.dot(P).dot(O)\n", "right" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "array([ 0.25 , 0.5 , 0.375])" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "np.linalg.solve(left,right)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "array([ 0.33333333, 0. , 0.66666667])" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "vec.get_feature_names()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "['blue', 'pots', 'red']" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "A = pd.DataFrame({'Cue':vec.get_feature_names(),'Weight':np.linalg.solve(left,right)})\n", "A" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CueWeight
0 blue 0.333333
1 pots 0.000000
2 red 0.666667
\n", "

3 rows \u00d7 2 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ " Cue Weight\n", "0 blue 0.333333\n", "1 pots 0.000000\n", "2 red 0.666667\n", "\n", "[3 rows x 2 columns]" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ndl import *" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "data = com.load_data('danks')\n", "danks(data)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yn
blue 0.333333-0.333333
pots 0.000000 1.000000
red 0.666667-0.666667
\n", "

3 rows \u00d7 2 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ " y n\n", "blue 0.333333 -0.333333\n", "pots 0.000000 1.000000\n", "red 0.666667 -0.666667\n", "\n", "[3 rows x 2 columns]" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "lexample = com.load_data('lexample')\n", "lexample['Cues'] = orthoCoding(lexample['Word'],grams=1,sep='_')\n", "lexample" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
WordFrequencyOutcomesCues
1 hand 10 hand h_a_n_d
2 hands 20 hand_plural h_a_n_d_s
3 land 8 land l_a_n_d
4 lands 3 land_plural l_a_n_d_s
5 and 35 and a_n_d
6 sad 18 sad s_a_d
7 as 35 as a_s
8 lad 102 lad l_a_d
9 lad 54 lad_plural l_a_d
10 lass 134 lass l_a_s_s
\n", "

10 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ " Word Frequency Outcomes Cues\n", "1 hand 10 hand h_a_n_d\n", "2 hands 20 hand_plural h_a_n_d_s\n", "3 land 8 land l_a_n_d\n", "4 lands 3 land_plural l_a_n_d_s\n", "5 and 35 and a_n_d\n", "6 sad 18 sad s_a_d\n", "7 as 35 as a_s\n", "8 lad 102 lad l_a_d\n", "9 lad 54 lad_plural l_a_d\n", "10 lass 134 lass l_a_s_s\n", "\n", "[10 rows x 4 columns]" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "danks(lexample)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "LinAlgError", "evalue": "Singular matrix", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mLinAlgError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdanks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlexample\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/malouf/Dropbox/learning/ndl.pyc\u001b[0m in \u001b[0;36mdanks\u001b[0;34m(data)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0mright\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMTP\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mO\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m \u001b[0mV\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinalg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mleft\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mright\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 64\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0moutcome\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mV\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/malouf/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.pyc\u001b[0m in \u001b[0;36msolve\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 379\u001b[0m \u001b[0msignature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'DD->D'\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misComplexType\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;34m'dd->d'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[0mextobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_linalg_error_extobj\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_raise_linalgerror_singular\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 381\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgufunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msignature\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mextobj\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mextobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 382\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 383\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mwrap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult_t\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/malouf/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.pyc\u001b[0m in \u001b[0;36m_raise_linalgerror_singular\u001b[0;34m(err, flag)\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 89\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_raise_linalgerror_singular\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflag\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 90\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mLinAlgError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Singular matrix\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 91\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_raise_linalgerror_nonposdef\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflag\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mLinAlgError\u001b[0m: Singular matrix" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "ndl(lexample)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
andashandladlandlasspluralsad
a 0.375349 1.034097-4.884981e-15 0.409446-0.375349-0.034097-0.448823-0.409446
d-0.161751-0.443564 2.442491e-15 0.394685 0.161751-0.556436 0.531307 0.605315
h-0.688749 0.053487 1.000000e+00 0.364738-0.311251-0.053487 0.490994-0.364738
l-0.214565-0.619968 2.803313e-15 0.165467 0.214565 0.619968 0.222744-0.165467
n 0.611964-0.420404 7.771561e-16-0.808440 0.388036 0.420404-0.088284-0.191560
s-0.205220-0.335424 2.997602e-15-0.540644 0.205220 0.335424 0.272209 0.540644
\n", "

6 rows \u00d7 8 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ " and as hand lad land lass plural \\\n", "a 0.375349 1.034097 -4.884981e-15 0.409446 -0.375349 -0.034097 -0.448823 \n", "d -0.161751 -0.443564 2.442491e-15 0.394685 0.161751 -0.556436 0.531307 \n", "h -0.688749 0.053487 1.000000e+00 0.364738 -0.311251 -0.053487 0.490994 \n", "l -0.214565 -0.619968 2.803313e-15 0.165467 0.214565 0.619968 0.222744 \n", "n 0.611964 -0.420404 7.771561e-16 -0.808440 0.388036 0.420404 -0.088284 \n", "s -0.205220 -0.335424 2.997602e-15 -0.540644 0.205220 0.335424 0.272209 \n", "\n", " sad \n", "a -0.409446 \n", "d 0.605315 \n", "h -0.364738 \n", "l -0.165467 \n", "n -0.191560 \n", "s 0.540644 \n", "\n", "[6 rows x 8 columns]" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }